1 #define _GNU_SOURCE 2 #include <pwd.h> 3 #include <stdio.h> 4 #include <unistd.h> 5 #include <string.h> 6 cuserid(char * buf)7char *cuserid(char *buf) 8 { 9 static char usridbuf[L_cuserid]; 10 struct passwd pw, *ppw; 11 long pwb[256]; 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); 21 return buf; 22 } 23