Using system calls under Linux to get the current username

xiaoxiao2021-03-06  52

Directly use getEuid () is the current user's number, to get the username, you need to go to the / etc / passwd to get the username of the corresponding number

The procedure is as follows

#include #include #include #include #include #include

#define max 8192 # Define umax 128

INT SLIP (char *, char *, char *); int car2int (char *);

INT main () {int fp; file * stream; uid_t cuse; char temp [umax] [max]; char fname; int i = 0; char cname [20]; char userid [10]; cuser = geteuid (); IF ((stream = fopen ("/ etc / passwd", "r")) == null) exit (1); while ((fname = fgetc (stream))! = EOF) {TEMP [i] [0] = FNAME; IF (FGETS (& Temp [i ) == null) exit (1);} for (i = 0; i

INT SLIP (Char * STR, Char * Name, Char * UserID) {INT I = 0, J = 0, K; Char * CP; Char RetValu [10] = {}; for (k = 0; k <20; K ) Name [k] = 0; cp = str; while ((* cp)! = ':') {Name [i ] = * cp; cp ;} CP ; while (* cp)! = ':' ) {CP ;} CP ; while ((* cp)! = ':') {RetValu [J ] = * CP; CP ;} k = 0; while (k <10) {userid [k] = RetValu [K ]; K ;}; if (name == null) Return 0; Return 1;}

转载请注明原文地址:https://www.9cbs.com/read-116768.html

New Post(0)