home.c

Go to the documentation of this file.
00001 /*
00002  ****************************************************************
00003  * char *
00004  * G_home ()
00005  *
00006  *   returns char pointer to home directory for user
00007  *   dies if can't determine
00008  *
00009  * char *
00010  * G__home()
00011  *
00012  *   returns char pointer to home directory for user
00013  *   NULL if can't determine
00014  *
00015  ***************************************************************/
00016 #include <stdlib.h>
00017 #include <string.h>
00018 #include <grass/gis.h>
00019 #include <grass/glocale.h>
00020 
00021 
00032 char *
00033 G_home ()
00034 {
00035     char *home;
00036     
00047 char *G_home();
00048 
00049     if ((home = G__home()))
00050         return home;
00051     
00052     G_fatal_error (_("unable to determine user's home directory"));
00053     exit(-1);
00054 }
00055 
00056 char *
00057 G__home ()
00058 {
00059     static char *home = 0;
00060     char buf[1024];
00061 
00062     if (home)
00063         return home;
00064 
00065 #ifdef __MINGW32__
00066     { 
00067         /* TODO: we should probably check if the dir exists */
00068         home = getenv ( "USERPROFILE" ) ;
00069 
00070         if ( !home ) 
00071         {
00072             sprintf ( buf, "%s%s", getenv ( "HOMEDRIVE" ), 
00073                                    getenv ( "HOMEPATH" ) );
00074 
00075             if ( strlen(buf) >= 0 )
00076                 home = G_store ( buf );
00077         }
00078 
00079         if ( !home )
00080             home = getenv ( "HOME" ) ;
00081     }
00082 #else
00083     {
00084         FILE *fd,*G_popen();
00085 
00086 /* first call must get home
00087 * execute the command "cd; pwd" and read the
00088 * output to get the home directory
00089 */
00090         if((fd = G_popen ("cd; pwd","r")))
00091         {
00092             if (fscanf (fd,"%s", buf) == 1)
00093                 home = G_store (buf);
00094             G_pclose (fd);
00095         }
00096     }
00097 #endif
00098     G_debug (2, "G__home home = %s", home );
00099     return home;
00100 }

Generated on Wed Dec 19 14:59:06 2007 for GRASS by  doxygen 1.5.4