user.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
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
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 }