doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
homedir.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * $Id: homedir.c,v 1.4 2004/01/29 13:54:08 bagder Exp $
22  ***************************************************************************/
23 
24 #include "setup.h"
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #ifdef WIN32
31 #include <windows.h>
32 #endif
33 #ifdef HAVE_PWD_H
34 #include <pwd.h>
35 #endif
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39 #ifdef VMS
40 #include <unixlib.h>
41 #endif
42 
43 #include "homedir.h"
44 
45 #ifdef CURLDEBUG
46 #include "../lib/memdebug.h"
47 #endif
48 
49 static
50 char *GetEnv(const char *variable, char do_expand)
51 {
52  char *env = NULL;
53 #ifdef WIN32
54  char buf1[1024], buf2[1024];
55  DWORD rc;
56 
57  /* Don't use getenv(); it doesn't find variable added after program was
58  * started. Don't accept truncated results (i.e. rc >= sizeof(buf1)). */
59 
60  rc = GetEnvironmentVariable(variable, buf1, sizeof(buf1));
61  if (rc > 0 && rc < sizeof(buf1)) {
62  env = buf1;
63  variable = buf1;
64  }
65  if (do_expand && strchr(variable,'%')) {
66  /* buf2 == variable if not expanded */
67  rc = ExpandEnvironmentStrings (variable, buf2, sizeof(buf2));
68  if (rc > 0 && rc < sizeof(buf2) &&
69  !strchr(buf2,'%')) /* no vars still unexpanded */
70  env = buf2;
71  }
72 #else
73  (void)do_expand;
74 #ifdef VMS
75  env = getenv(variable);
76  if (env && strcmp("HOME",variable) == 0) {
77  env = decc$translate_vms(env);
78  }
79 #else
80  /* no length control */
81  env = getenv(variable);
82 #endif
83 #endif
84  return (env && env[0])?strdup(env):NULL;
85 }
86 
87 /* return the home directory of the current user as an allocated string */
88 char *homedir(void)
89 {
90  char *home = GetEnv("HOME", FALSE);
91  if(home)
92  return home;
93 
94 #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
95  {
96  struct passwd *pw = getpwuid(geteuid());
97 
98  if (pw) {
99 #ifdef VMS
100  home = decc$translate_vms(pw->pw_dir);
101 #else
102  home = pw->pw_dir;
103 #endif
104  if (home && home[0])
105  home = strdup(home);
106  }
107  }
108 #endif /* PWD-stuff */
109 #ifdef WIN32
110  home = GetEnv("APPDATA", TRUE);
111  if(!home)
112  home = GetEnv("%USERPROFILE%\\Application Data", TRUE); /* Normally only
113  on Win-2K/XP */
114 #endif /* WIN32 */
115  return home;
116 }
#define strcmp
Definition: Str.h:41
DWORD
Definition: win_qgl.cpp:61
#define NULL
Definition: Lib.h:88
char * strdup(char *s1)
Definition: main.c:183
char * homedir(void)
Definition: homedir.c:88
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
#define FALSE
Definition: mprintf.c:70
#define TRUE
Definition: mprintf.c:69