3ff115a1d97de0e6f84615176360582c26086744
[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         if (getpwuid_r(geteuid(), &pw, (void *)pwb, sizeof pwb, &ppw))
13                 return 0;
14         size_t len = strnlen(pw.pw_name, L_cuserid);
15         if (len == L_cuserid)
16                 return 0;
17         if (!buf) buf = usridbuf;
18         memcpy(buf, pw.pw_name, len+1);
19         return buf;
20 }