The R Project SVN R

Rev

Rev 59039 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
15268 ripley 1
/*
2
 *  R : A Computer Language for Statistical Data Analysis
3
 *  file shext.c
4
 *  Copyright (C) 2001  Guido Masarotto and Brian Ripley
59039 ripley 5
 *                2004-6  R Core Team
15268 ripley 6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  (at your option) any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
42300 ripley 18
 *  along with this program; if not, a copy is available at
68956 ripley 19
 *  https://www.R-project.org/Licenses/
15268 ripley 20
 */
21
 
36901 ripley 22
#define WIN32_LEAN_AND_MEAN 1
23715 murdoch 23
#include <windows.h>
15268 ripley 24
#include <shlobj.h>
25
 
38793 ripley 26
static int ShellGetPersonalDirectory(char *folder)
27
  /* Folder is assumed to be at least MAX_PATH long */
23715 murdoch 28
{
29
    LPMALLOC g_pMalloc;
29159 murdoch 30
    LPITEMIDLIST pidlUser;
31
    int result;
32
 
33
    result = 0;
34
 
35
    /* Get the shell's allocator. */
36
    if (SUCCEEDED(SHGetMalloc(&g_pMalloc))) {
37
 
38
	/* Get the PIDL of the user's Directory. */
39
	if (SUCCEEDED(SHGetSpecialFolderLocation(0, CSIDL_PERSONAL, &pidlUser))) {
40
	    if (SUCCEEDED(SHGetPathFromIDList(pidlUser, folder))) result = 1;
41
	    g_pMalloc->lpVtbl->Free(g_pMalloc, pidlUser);
42
	}
43
    }
44
    return(result);
45
}
31849 ripley 46
 
47
 
48
static char RUser[MAX_PATH];
49
#include <winbase.h>
50
extern void R_Suicide(char *s);
51
 
38793 ripley 52
char *getRUser()
31849 ripley 53
{
45070 ripley 54
    /*
55
     * try R_USER then HOME then Windows homes then working directory
56
     */
31849 ripley 57
    char *p, *q;
58
 
59
    if ((p = getenv("R_USER"))) {
32890 ripley 60
	if(strlen(p) >= MAX_PATH) R_Suicide("Invalid R_USER");
31849 ripley 61
	strcpy(RUser, p);
62
    } else if ((p = getenv("HOME"))) {
32890 ripley 63
	if(strlen(p) >= MAX_PATH) R_Suicide("Invalid HOME");
31849 ripley 64
	strcpy(RUser, p);
65
    } else if (ShellGetPersonalDirectory(RUser)) {
66
	/* nothing to do */;
67
    } else if ((p = getenv("HOMEDRIVE")) && (q = getenv("HOMEPATH"))) {
32890 ripley 68
	if(strlen(p) >= MAX_PATH) R_Suicide("Invalid HOMEDRIVE");
31849 ripley 69
	strcpy(RUser, p);
70
	if(strlen(RUser) + strlen(q) >= MAX_PATH)
32890 ripley 71
	    R_Suicide("Invalid HOMEDRIVE+HOMEPATH");
31849 ripley 72
	strcat(RUser, q);
73
    } else {
74
	GetCurrentDirectory(MAX_PATH, RUser);
75
    }
76
    p = RUser + (strlen(RUser) - 1);
77
    if (*p == '/' || *p == '\\') *p = '\0';
78
    return RUser;
79
}