tool_homedir.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 #include "tool_setup.h"
00023 
00024 #ifdef HAVE_PWD_H
00025 #  include <pwd.h>
00026 #endif
00027 
00028 #include "tool_homedir.h"
00029 
00030 #include "memdebug.h" /* keep this as LAST include */
00031 
00032 static char *GetEnv(const char *variable, char do_expand)
00033 {
00034   char *env = NULL;
00035 #ifdef WIN32
00036   char  buf1[1024], buf2[1024];
00037   DWORD rc;
00038 
00039   /* Don't use getenv(); it doesn't find variable added after program was
00040    * started. Don't accept truncated results (i.e. rc >= sizeof(buf1)).  */
00041 
00042   rc = GetEnvironmentVariable(variable, buf1, sizeof(buf1));
00043   if(rc > 0 && rc < sizeof(buf1)) {
00044     env = buf1;
00045     variable = buf1;
00046   }
00047   if(do_expand && strchr(variable, '%')) {
00048     /* buf2 == variable if not expanded */
00049     rc = ExpandEnvironmentStrings(variable, buf2, sizeof(buf2));
00050     if(rc > 0 && rc < sizeof(buf2) &&
00051        !strchr(buf2, '%'))    /* no vars still unexpanded */
00052       env = buf2;
00053   }
00054 #else
00055   (void)do_expand;
00056   /* no length control */
00057   env = getenv(variable);
00058 #endif
00059   return (env && env[0]) ? strdup(env) : NULL;
00060 }
00061 
00062 /* return the home directory of the current user as an allocated string */
00063 char *homedir(void)
00064 {
00065   char *home;
00066 
00067   home = GetEnv("CURL_HOME", FALSE);
00068   if(home)
00069     return home;
00070 
00071   home = GetEnv("HOME", FALSE);
00072   if(home)
00073     return home;
00074 
00075 #if defined(HAVE_GETPWUID) && defined(HAVE_GETEUID)
00076  {
00077    struct passwd *pw = getpwuid(geteuid());
00078 
00079    if(pw) {
00080      home = pw->pw_dir;
00081      if(home && home[0])
00082        home = strdup(home);
00083      else
00084        home = NULL;
00085    }
00086  }
00087 #endif /* PWD-stuff */
00088 #ifdef WIN32
00089   home = GetEnv("APPDATA", TRUE);
00090   if(!home)
00091     home = GetEnv("%USERPROFILE%\\Application Data", TRUE); /* Normally only
00092                                                                on Win-2K/XP */
00093 #endif /* WIN32 */
00094   return home;
00095 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:07