07866acf138dd232beaa5500724704977a4d5968
[musl] / src / legacy / cuserid.c
1 #define _GNU_SOURCE
2 #include <pwd.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <string.h>
6
7 char *cuserid(char *buf)
8 {
9         static char usridbuf[L_cuserid];
10         struct passwd pw, *ppw;
11         long pwb[256];
12         getpwuid_r(geteuid(), &pw, (void *)pwb, sizeof pwb, &ppw);
13         if (!ppw)
14                 return 0;
15         size_t len = strnlen(pw.pw_name, L_cuserid);
16         if (len == L_cuserid)
17                 return 0;
18         if (!buf) buf = usridbuf;
19         memcpy(buf, pw.pw_name, len+1);
20         return buf;
21 }