home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / programm / 3949 < prev    next >
Encoding:
Text File  |  1992-07-27  |  1.5 KB  |  52 lines

  1. Path: sparky!uunet!cis.ohio-state.edu!udecc.engr.udayton.edu!blackbird.afit.af.mil!dsacg3.dsac.dla.mil!dsac.dla.mil!hq.dla.mil!fazc016
  2. From: fazc016@hq.dla.mil (Don Costello)
  3. Newsgroups: comp.unix.programmer
  4. Subject: Summary: getlogin() problem
  5. Message-ID: <833@hq.dla.mil>
  6. Date: 27 Jul 92 11:38:29 GMT
  7. Organization: HQ Defense Logistics Agency/DASC, Cameron Station, VA
  8. Lines: 41
  9. X-Newsreader: Tin 1.1 PL4
  10.  
  11.  
  12. Thanks to the following people, the getlogin() problem (which is really
  13. not a problem) is finally solved:
  14.  
  15. leo@ine.philips.nl
  16. graham@castle.edinburgh.ac.uk
  17. Matt Schroth
  18. cjc@ulysses.att.com
  19. David A. Wagner
  20. John Eric Fosler
  21. David Myers
  22. cjc@ulysses.att.com
  23.  
  24. It seems that, according to the man page, (which I obviously didn't
  25. read too thoroughly :-) getlogin reads /etc/utmp, returning the ID of
  26. the owner of whatever tty the user is on.  When doing an su to another login, 
  27. there is no utmp entry for the new login, only the original one.
  28.  
  29. The following is the solution:
  30.  
  31. #include <all-the-normal-stuff>
  32. #include <pwd.h>
  33.  
  34. char     *username;
  35. struct   passwd *userinfo;
  36.  
  37. userinfo=getpwuid(getuid());
  38. username=userinfo->pw_name;
  39.  
  40. "username" now points to the name of the current (su'd) login id
  41. (userinfo->pw_name), not the original.
  42.  
  43. Thanks again to all who have helped.
  44.  
  45.   ==- dcc-==
  46.  
  47. -- 
  48. +------------------------------------+---------------------------+
  49. | Don Costello, DASC-ZSC             |  It's hard to cram this   |
  50. | DLA Cameron Station, VA            |  signature into just 4    |
  51. | TEL: (703) 274-7215  AV: 284-7215  |  lines!!!                 | 
  52.