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