Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!cs.utexas.edu!uunet!bfmny0!tneff From: tneff@bfmny0.UUCP (Tom Neff) Newsgroups: alt.sources Subject: euser - display the effective user name, SysV-ish Message-ID: <14448@bfmny0.UUCP> Date: 7 Jul 89 23:29:21 GMT Reply-To: tneff@bfmny0.UUCP (Tom Neff) Organization: ^ Lines: 62 Summary: Expires: References: Sender: Followup-To: Distribution: Keywords: If you need a FAST way to generate the name of your current effective user (including the effects of su(1) etc), this works. It's not fancy. Obviously it has to read /etc/passwd. Feel free to hack and pass around. #! /bin/sh # This is a shell archive, meaning: # 1. Remove everything above the #! /bin/sh line. # 2. Save the resulting text in a file. # 3. Execute the file with /bin/sh (not csh) to create: # euser.c # This archive created: Wed Jul 5 19:58:33 1989 export PATH; PATH=/bin:/usr/bin:$PATH if test -f 'euser.c' then echo shar: "will not over-write existing file 'euser.c'" else sed 's/^X//' << \SHAR_EOF > 'euser.c' X/* X euser.c rev: 05-Jul-89 Tom Neff X X Display the name of our effective user at the moment. X X System V-ish. Have a blast. X X Uses /etc/passwd. X */ X X#include X#include X Xunsigned short geteuid(), euid; X XFILE *pass; Xchar line[256], *name; Xint uid = 0; X Xmain() X{ X euid = geteuid(); X if ((pass = fopen("/etc/passwd","r")) == NULL) X exit(2); X while (fgets(line, sizeof(line), pass) != NULL) X { X name = strtok(line, ":"); X strtok(NULL, ":"); X uid = atoi(strtok(NULL, ":")); X if (euid == uid) X { X puts(name); X exit(0); X } X } X exit(1); X} SHAR_EOF fi exit 0 # End of shell archive -- "My God, Thiokol, when do you \\ Tom Neff want me to launch -- next April?" \\ uunet!bfmny0!tneff