libc: re-implement getuser() by stating /proc/$pid/status
The idea is to avoid the magic files that contain per process information in devcons when possible. It will make it easier to deprecate them in the future.
This commit is contained in:
parent
2e6a5e7046
commit
dced7255ec
5 changed files with 24 additions and 27 deletions
17
sys/src/libc/9sys/getuser.c
Normal file
17
sys/src/libc/9sys/getuser.c
Normal file
|
@ -0,0 +1,17 @@
|
|||
#include <u.h>
|
||||
#include <libc.h>
|
||||
|
||||
char *
|
||||
getuser(void)
|
||||
{
|
||||
static char user[64];
|
||||
char name[32];
|
||||
Dir *dir;
|
||||
|
||||
snprint(name, sizeof(name), "/proc/%lud/status", (ulong)getpid());
|
||||
if((dir = dirstat(name)) == nil)
|
||||
return "none";
|
||||
snprint(user, sizeof(user), "%s", dir->uid);
|
||||
free(dir);
|
||||
return user;
|
||||
}
|
|
@ -24,6 +24,7 @@ OFILES=\
|
|||
getenv.$O\
|
||||
getpid.$O\
|
||||
getppid.$O\
|
||||
getuser.$O\
|
||||
getwd.$O\
|
||||
idn.$O\
|
||||
iounit.$O\
|
||||
|
|
|
@ -1,21 +0,0 @@
|
|||
#include <u.h>
|
||||
#include <libc.h>
|
||||
|
||||
char *
|
||||
getuser(void)
|
||||
{
|
||||
static char user[64];
|
||||
int fd;
|
||||
int n;
|
||||
|
||||
fd = open("/dev/user", OREAD|OCEXEC);
|
||||
if(fd < 0)
|
||||
return "none";
|
||||
n = read(fd, user, (sizeof user)-1);
|
||||
close(fd);
|
||||
if(n <= 0)
|
||||
strcpy(user, "none");
|
||||
else
|
||||
user[n] = 0;
|
||||
return user;
|
||||
}
|
|
@ -32,7 +32,6 @@ CFILES=\
|
|||
frexp.c\
|
||||
getcallerpc.c\
|
||||
getfields.c\
|
||||
getuser.c\
|
||||
hangup.c\
|
||||
hypot.c\
|
||||
lnrand.c\
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue