Diffstat (limited to 'noncore/settings/networksettings2/networksettings2/resources.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/settings/networksettings2/networksettings2/resources.cpp | 137 |
1 files changed, 136 insertions, 1 deletions
diff --git a/noncore/settings/networksettings2/networksettings2/resources.cpp b/noncore/settings/networksettings2/networksettings2/resources.cpp index 0301361..e6ce2b7 100644 --- a/noncore/settings/networksettings2/networksettings2/resources.cpp +++ b/noncore/settings/networksettings2/networksettings2/resources.cpp | |||
@@ -1,7 +1,11 @@ | |||
1 | #include <unistd.h> | ||
2 | #include <errno.h> | ||
3 | #include <fcntl.h> | ||
4 | #include <pwd.h> | ||
1 | #include <qpixmap.h> | 5 | #include <qpixmap.h> |
6 | #include <qdir.h> | ||
2 | #include <qpe/qlibrary.h> | 7 | #include <qpe/qlibrary.h> |
3 | #include <qpe/qpeapplication.h> | 8 | #include <qpe/qpeapplication.h> |
4 | #include <qdir.h> | ||
5 | #include <opie2/odebug.h> | 9 | #include <opie2/odebug.h> |
6 | #include <qtopia/resource.h> | 10 | #include <qtopia/resource.h> |
7 | 11 | ||
@@ -75,6 +79,8 @@ TheNSResources::TheNSResources( void ) : NodeTypeNameMap(), | |||
75 | 79 | ||
76 | // get access to the system | 80 | // get access to the system |
77 | TheSystem = new System(); | 81 | TheSystem = new System(); |
82 | |||
83 | detectCurrentUser(); | ||
78 | } | 84 | } |
79 | 85 | ||
80 | TheNSResources::~TheNSResources( void ) { | 86 | TheNSResources::~TheNSResources( void ) { |
@@ -232,3 +238,132 @@ void TheNSResources::renumberConnections( void ) { | |||
232 | NC->setModified( 1 ); | 238 | NC->setModified( 1 ); |
233 | } | 239 | } |
234 | } | 240 | } |
241 | |||
242 | typedef struct EnvVars { | ||
243 | char * Name; | ||
244 | int Len; | ||
245 | } EnvVar_t; | ||
246 | |||
247 | #define AnEV(x) x, sizeof(x)-1 | ||
248 | |||
249 | static EnvVar_t EV[] = { | ||
250 | // AnEV( "HOME=" ), -> SPECIAL | ||
251 | // AnEV( "LOGNAME=" ), -> SPECIAL | ||
252 | AnEV( "USER=" ), | ||
253 | AnEV( "LD_LIBRARY_PATH=" ), | ||
254 | AnEV( "PATH=" ), | ||
255 | AnEV( "QTDIR=" ), | ||
256 | AnEV( "OPIEDIR=" ), | ||
257 | AnEV( "SHELL=" ), | ||
258 | { NULL, 0 } | ||
259 | }; | ||
260 | |||
261 | void TheNSResources::detectCurrentUser( void ) { | ||
262 | // find current running qpe | ||
263 | QString QPEEnvFile = ""; | ||
264 | |||
265 | // open proc dir and find all dirs in it | ||
266 | { QRegExp R("[0-9]+"); | ||
267 | QDir ProcDir( "/proc" ); | ||
268 | QString QPELoc = QPEApplication::qpeDir() + "bin/qpe"; | ||
269 | QFileInfo FI; | ||
270 | QStringList EL = ProcDir.entryList( QDir::Dirs ); | ||
271 | |||
272 | // print it out | ||
273 | for ( QStringList::Iterator it = EL.begin(); | ||
274 | it != EL.end(); | ||
275 | ++it ) { | ||
276 | if( R.match( (*it) ) >= 0 ) { | ||
277 | QString S = ProcDir.path()+"/"+ (*it); | ||
278 | S.append( "/exe" ); | ||
279 | FI.setFile( S ); | ||
280 | // get the linke | ||
281 | S = FI.readLink(); | ||
282 | if( S == QPELoc ) { | ||
283 | // found running qpe | ||
284 | QPEEnvFile.sprintf( ProcDir.path()+ "/" + (*it) + "/environ" ); | ||
285 | break; | ||
286 | } | ||
287 | } | ||
288 | } | ||
289 | } | ||
290 | |||
291 | if( QPEEnvFile.isEmpty() ) { | ||
292 | // could not find qpe | ||
293 | fprintf( stderr, "Could not find qpe\n" ); | ||
294 | return; | ||
295 | } | ||
296 | |||
297 | // FI now contains path ProcDir to the cmd dir | ||
298 | { char * Buf = 0; | ||
299 | char TB[1024]; | ||
300 | long BufSize = 0; | ||
301 | int fd; | ||
302 | int rd; | ||
303 | |||
304 | fd = open( QPEEnvFile.latin1(), O_RDONLY ); | ||
305 | if( fd < 0 ) { | ||
306 | fprintf( stderr, "Could not open %s : %d\n", | ||
307 | QPEEnvFile.latin1(), errno ); | ||
308 | return; | ||
309 | } | ||
310 | |||
311 | while( (rd = read( fd, TB, sizeof(TB) ) ) > 0 ) { | ||
312 | Buf = (char *)realloc( Buf, BufSize+rd ); | ||
313 | memcpy( Buf+BufSize, TB, rd ); | ||
314 | BufSize += rd; | ||
315 | } | ||
316 | |||
317 | char * Data = Buf; | ||
318 | char * DataEnd = Data+BufSize-1; | ||
319 | |||
320 | // get env items out of list | ||
321 | while( Data < DataEnd ) { | ||
322 | if( strncmp( Data, "LOGNAME=", 8 ) == 0 ) { | ||
323 | CurrentUser.UserName = Data+8; | ||
324 | CurrentUser.EnvList.resize( CurrentUser.EnvList.size()+1 ); | ||
325 | CurrentUser.EnvList[CurrentUser.EnvList.size()-1] = | ||
326 | strdup( Data ); | ||
327 | } else if( strncmp( Data, "HOME=", 5 ) == 0 ) { | ||
328 | CurrentUser.HomeDir = Data+5; | ||
329 | CurrentUser.EnvList.resize( CurrentUser.EnvList.size()+1 ); | ||
330 | CurrentUser.EnvList[CurrentUser.EnvList.size()-1] = | ||
331 | strdup( Data ); | ||
332 | } else { | ||
333 | EnvVar_t * Run = EV; | ||
334 | while( Run->Name ) { | ||
335 | if( strncmp( Data, Run->Name, Run->Len ) == 0 ) { | ||
336 | CurrentUser.EnvList.resize( CurrentUser.EnvList.size()+1 ); | ||
337 | CurrentUser.EnvList[CurrentUser.EnvList.size()-1] = | ||
338 | strdup( Data ); | ||
339 | break; | ||
340 | } | ||
341 | Run ++; | ||
342 | } | ||
343 | } | ||
344 | |||
345 | Data += strlen( Data )+1; | ||
346 | } | ||
347 | |||
348 | free( Buf ); | ||
349 | |||
350 | if( ! CurrentUser.UserName.isEmpty() ) { | ||
351 | // find user info | ||
352 | struct passwd pwd; | ||
353 | struct passwd * pwdres; | ||
354 | |||
355 | if( getpwnam_r( CurrentUser.UserName.latin1(), | ||
356 | &pwd, TB, sizeof(TB), &pwdres ) || | ||
357 | pwdres == 0 ) { | ||
358 | fprintf( stderr, "Could not determine user %s : %d\n", | ||
359 | CurrentUser.UserName.latin1(), errno ); | ||
360 | return; | ||
361 | } | ||
362 | CurrentUser.Uid = pwd.pw_uid; | ||
363 | CurrentUser.Gid = pwd.pw_gid; | ||
364 | } else{ | ||
365 | CurrentUser.Uid = | ||
366 | CurrentUser.Gid = -1; | ||
367 | } | ||
368 | } | ||
369 | } | ||