user.cxx

Go to the documentation of this file.
00001 //
00002 // Copyright 2002, Lowell Boggs Jr.
00003 //
00004 // This file or directory, containing source code for a computer program,
00005 // is Copyrighted by Lowell Boggs, Jr.  987 Regency Drive, Lewisville
00006 // TX (USA), 75067.  You may use, copy, modify, and distribute this
00007 // source file without charge or obligation so long as you agree to
00008 // the following:
00009 //
00010 //  1.  You must indemnify Lowell Boggs against any and all financial
00011 //      obligations caused by its use, misuse, function, or malfunction.
00012 //      Further, you acknowledge that there is no warranty of any kind,
00013 //      whatsoever.
00014 //
00015 //  2.  You agree not to attempt to patent any portion of this original
00016 //      work -- though you may attempt to patent your own extensions to
00017 //      it if you so choose.
00018 //
00019 //  3.  You keep this copyright notice with the file and all copies
00020 //      of the file and do not change it anyway except language translation.
00021 //
00022 // You are responsible for enforcing your own compliance with these
00023 // conditions and may not use this source file if you cannot agree to the
00024 // above terms and conditions.
00025 
00028 
00029 #include <cxxtls/user.h>
00030 #include <cxxtls/options.h>
00031 
00032 namespace cxxtls
00033 {
00034 
00035 
00036 #ifdef _MSC_VER
00037 # define getuid()  0  /* stubbed out on nt for the moment */
00038 # include <stdlib.h>
00039 static char const *get_environment_value(char const *name)
00040 {
00041   if(ProgramOptions::global_options_)
00042     return ProgramOptions::global_options_->getenv(name).c_str();
00043 
00044   return getenv(name);
00045 }
00046 #else
00047 # include <unistd.h>
00048 #include <pwd.h>
00049 #endif
00050 
00051 
00052 
00053 UserInfo
00054 UserInfo::
00055 get(long uid)
00056 {
00057   UserInfo rv;
00058 
00059 #ifdef _MSC_VER
00060   char const *username = get_environment_value("USERNAME");
00061 
00062   if(username == 0)
00063     username="USER";
00064 
00065   char const *shell = get_environment_value("COMSPEC");
00066 
00067 
00068   if(shell == 0)
00069     shell = "C:\\WINNT\\system32\\cmd.exe";
00070 
00071 
00072   char const *home = get_environment_value("HOME");
00073 
00074   if(home == 0)
00075     home = "C:/";
00076 
00077   rv.name_     = username;
00078   rv.home_dir_ = home;
00079   rv.shell_    = shell;
00080   rv.uid_      = 0;
00081   rv.gid_      = 0;
00082 
00083 #else
00084 
00085   struct passwd &pwent = *getpwuid(getuid());
00086 
00087   if( &pwent == 0 )
00088    return rv;
00089   else
00090   {
00091     rv.name_      = pwent.pw_name;
00092     rv.password_  = pwent.pw_passwd;
00093     rv.home_dir_  = pwent.pw_dir;
00094     rv.shell_     = pwent.pw_shell;
00095     rv.uid_       = pwent.pw_uid;
00096     rv.gid_       = pwent.pw_gid;
00097 
00098   }
00099 
00100 #endif
00101 
00102   return rv;
00103 
00104 }
00105 
00106 UserInfo
00107 UserInfo::
00108 get(char const *user)
00109 {
00110   UserInfo rv;
00111 
00112 #ifdef _MSC_VER
00113 
00114   // just return the current user's info
00115 
00116   char const *username = get_environment_value("USERNAME");
00117 
00118   if(username == 0)
00119     username="USER";
00120 
00121   char const *shell = get_environment_value("COMSPEC");
00122 
00123 
00124   if(shell == 0)
00125     shell = "C:\\WINNT\\system32\\cmd.exe";
00126 
00127 
00128   char const *home = get_environment_value("HOME");
00129 
00130   if(home == 0)
00131     home = "C:/";
00132 
00133   rv.name_     = username;
00134   rv.home_dir_ = home;
00135   rv.shell_    = shell;
00136   rv.uid_      = 0;
00137   rv.gid_      = 0;
00138 
00139 #else
00140 
00141   struct passwd &pwent = * getpwnam(user);
00142 
00143 
00144   if( &pwent == 0 )
00145    return rv;
00146   else
00147   {
00148     rv.name_      = pwent.pw_name;
00149     rv.password_  = pwent.pw_passwd;
00150     rv.home_dir_  = pwent.pw_dir;
00151     rv.shell_     = pwent.pw_shell;
00152     rv.uid_       = pwent.pw_uid;
00153     rv.gid_       = pwent.pw_gid;
00154 
00155   }
00156 
00157 #endif
00158 
00159   return rv;
00160 
00161 }
00162 
00163 
00164 UserInfo
00165 UserInfo::
00166 get()
00167 {
00168   return get(long(getuid()));
00169 }
00170 
00171 } // namespace cxxtls
Generated on Wed Feb 29 22:50:05 2012 for CXXUtilities by  doxygen 1.6.3