~mmach/netext73/wasi-libc

« back to all changes in this revision

Viewing changes to libc-top-half/musl/src/legacy/cuserid.c

  • Committer: mmach
  • Date: 2023-09-12 15:53:36 UTC
  • Revision ID: netbit73@gmail.com-20230912155336-s1r5z5xnszz99zzo
0.0~git20230113.4362b18

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
#include <pwd.h>
3
3
#include <stdio.h>
4
4
#include <unistd.h>
 
5
#include <string.h>
5
6
 
6
7
char *cuserid(char *buf)
7
8
{
 
9
        static char usridbuf[L_cuserid];
8
10
        struct passwd pw, *ppw;
9
11
        long pwb[256];
10
 
        if (getpwuid_r(geteuid(), &pw, (void *)pwb, sizeof pwb, &ppw))
11
 
                return 0;
12
 
        snprintf(buf, L_cuserid, "%s", pw.pw_name);
 
12
        if (buf) *buf = 0;
 
13
        getpwuid_r(geteuid(), &pw, (void *)pwb, sizeof pwb, &ppw);
 
14
        if (!ppw)
 
15
                return buf;
 
16
        size_t len = strnlen(pw.pw_name, L_cuserid);
 
17
        if (len == L_cuserid)
 
18
                return buf;
 
19
        if (!buf) buf = usridbuf;
 
20
        memcpy(buf, pw.pw_name, len+1);
13
21
        return buf;
14
22
}