1570af302Sopenharmony_ci#define _GNU_SOURCE 2570af302Sopenharmony_ci#include <pwd.h> 3570af302Sopenharmony_ci#include <stdio.h> 4570af302Sopenharmony_ci#include <unistd.h> 5570af302Sopenharmony_ci#include <string.h> 6570af302Sopenharmony_ci 7570af302Sopenharmony_cichar *cuserid(char *buf) 8570af302Sopenharmony_ci{ 9570af302Sopenharmony_ci static char usridbuf[L_cuserid]; 10570af302Sopenharmony_ci struct passwd pw, *ppw; 11570af302Sopenharmony_ci long pwb[256]; 12570af302Sopenharmony_ci if (buf) *buf = 0; 13570af302Sopenharmony_ci getpwuid_r(geteuid(), &pw, (void *)pwb, sizeof pwb, &ppw); 14570af302Sopenharmony_ci if (!ppw) 15570af302Sopenharmony_ci return buf; 16570af302Sopenharmony_ci size_t len = strnlen(pw.pw_name, L_cuserid); 17570af302Sopenharmony_ci if (len == L_cuserid) 18570af302Sopenharmony_ci return buf; 19570af302Sopenharmony_ci if (!buf) buf = usridbuf; 20570af302Sopenharmony_ci memcpy(buf, pw.pw_name, len+1); 21570af302Sopenharmony_ci return buf; 22570af302Sopenharmony_ci} 23