static struct passwd * vnextpwent(char const *nam, uid_t uid, int doclose) { struct passwd *pw; char *line; size_t linecap; ssize_t linelen; pw = NULL; line = NULL; linecap = 0; linelen = 0; if (pwd_fp != NULL || (pwd_fp = fopen(getpwpath(_MASTERPASSWD), "r")) != NULL) { while ((linelen = getline(&line, &linecap, pwd_fp)) > 0) { /* Skip comments and empty lines */ if (*line == '\n' || *line == '#') continue; /* trim latest \n */ if (line[linelen - 1 ] == '\n') line[linelen - 1] = '\0'; pw = pw_scan(line, PWSCAN_MASTER); if (uid != (uid_t)-1) { if (uid == pw->pw_uid) break; } else if (nam != NULL) { if (strcmp(nam, pw->pw_name) == 0) break; } else break; free(pw); pw = NULL; } if (doclose) vendpwent(); } free(line); return (pw); }
static struct passwd * vnextpwent(char const * nam, uid_t uid, int doclose) { struct passwd * pw = NULL; static char pwtmp[1024]; strlcpy(pwtmp, getpwpath(_MASTERPASSWD), sizeof(pwtmp)); if (pwd_fp != NULL || (pwd_fp = fopen(pwtmp, "r")) != NULL) { int done = 0; static struct passwd pwd; while (!done && fgets(pwtmp, sizeof pwtmp, pwd_fp) != NULL) { int i, quickout = 0; char * q; char * p = strchr(pwtmp, '\n'); if (p == NULL) { while (fgets(pwtmp, sizeof pwtmp, pwd_fp) != NULL && strchr(pwtmp, '\n')==NULL) ; /* Skip long lines */ continue; } /* skip comments & empty lines */ if (*pwtmp =='\n' || *pwtmp == '#') continue; i = 0; q = p = pwtmp; bzero(&pwd, sizeof pwd); while (!quickout && (p = strsep(&q, ":\n")) != NULL) { switch (i++) { case 0: /* username */ pwd.pw_name = p; if (nam) { if (strcmp(nam, p) == 0) done = 1; else quickout = 1; } break; case 1: /* password */ pwd.pw_passwd = p; break; case 2: /* uid */ pwd.pw_uid = atoi(p); if (uid != (uid_t)-1) { if (uid == pwd.pw_uid) done = 1; else quickout = 1; } break; case 3: /* gid */ pwd.pw_gid = atoi(p); break; case 4: /* class */ if (nam == NULL && uid == (uid_t)-1) done = 1; pwd.pw_class = p; break; case 5: /* change */ pwd.pw_change = (time_t)atol(p); break; case 6: /* expire */ pwd.pw_expire = (time_t)atol(p); break; case 7: /* gecos */ pwd.pw_gecos = p; break; case 8: /* directory */ pwd.pw_dir = p; break; case 9: /* shell */ pwd.pw_shell = p; break; } } } if (doclose) vendpwent(); if (done && pwd.pw_name) { pw = &pwd; #define CKNULL(s) s = s ? s : "" CKNULL(pwd.pw_passwd); CKNULL(pwd.pw_class); CKNULL(pwd.pw_gecos); CKNULL(pwd.pw_dir); CKNULL(pwd.pw_shell); } } return pw; }
void vsetpwent(void) { vendpwent(); }