1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039 |
- #include "libbb.h"
- #include <assert.h>
- #define PWD_BUFFER_SIZE 256
- #define GRP_BUFFER_SIZE 256
- static int bb__pgsreader(
- int FAST_FUNC (*parserfunc)(void *d, char *line),
- void *data,
- char *__restrict line_buff,
- size_t buflen,
- FILE *f);
- static int FAST_FUNC bb__parsepwent(void *pw, char *line);
- static int FAST_FUNC bb__parsegrent(void *gr, char *line);
- #if ENABLE_USE_BB_SHADOW
- static int FAST_FUNC bb__parsespent(void *sp, char *line);
- #endif
- struct statics {
-
-
- struct passwd getpw_resultbuf;
- struct group getgr_resultbuf;
- char getpw_buffer[PWD_BUFFER_SIZE];
- char getgr_buffer[GRP_BUFFER_SIZE];
- #if 0
- struct spwd getsp_resultbuf;
- char getsp_buffer[PWD_BUFFER_SIZE];
- #endif
- };
- static struct statics *ptr_to_statics;
- static struct statics *get_S(void)
- {
- if (!ptr_to_statics)
- ptr_to_statics = xzalloc(sizeof(*ptr_to_statics));
- return ptr_to_statics;
- }
- #define RESULTBUF(name) &((S = get_S())->name##_resultbuf)
- #define BUFFER(name) (S->name##_buffer)
- int fgetpwent_r(FILE *__restrict stream, struct passwd *__restrict resultbuf,
- char *__restrict buffer, size_t buflen,
- struct passwd **__restrict result)
- {
- int rv;
- *result = NULL;
- rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, stream);
- if (!rv) {
- *result = resultbuf;
- }
- return rv;
- }
- int fgetgrent_r(FILE *__restrict stream, struct group *__restrict resultbuf,
- char *__restrict buffer, size_t buflen,
- struct group **__restrict result)
- {
- int rv;
- *result = NULL;
- rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, stream);
- if (!rv) {
- *result = resultbuf;
- }
- return rv;
- }
- #if ENABLE_USE_BB_SHADOW
- #ifdef UNUSED_FOR_NOW
- int fgetspent_r(FILE *__restrict stream, struct spwd *__restrict resultbuf,
- char *__restrict buffer, size_t buflen,
- struct spwd **__restrict result)
- {
- int rv;
- *result = NULL;
- rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, stream);
- if (!rv) {
- *result = resultbuf;
- }
- return rv;
- }
- #endif
- #endif
- #ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
- struct passwd *fgetpwent(FILE *stream)
- {
- struct statics *S;
- struct passwd *resultbuf = RESULTBUF(getpw);
- char *buffer = BUFFER(getpw);
- struct passwd *result;
- fgetpwent_r(stream, resultbuf, buffer, sizeof(BUFFER(getpw)), &result);
- return result;
- }
- struct group *fgetgrent(FILE *stream)
- {
- struct statics *S;
- struct group *resultbuf = RESULTBUF(getgr);
- char *buffer = BUFFER(getgr);
- struct group *result;
- fgetgrent_r(stream, resultbuf, buffer, sizeof(BUFFER(getgr)), &result);
- return result;
- }
- #endif
- #if ENABLE_USE_BB_SHADOW
- #ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
- struct spwd *fgetspent(FILE *stream)
- {
- struct statics *S;
- struct spwd *resultbuf = RESULTBUF(getsp);
- char *buffer = BUFFER(getsp);
- struct spwd *result;
- fgetspent_r(stream, resultbuf, buffer, sizeof(BUFFER(getsp)), &result);
- return result;
- }
- #endif
- #ifdef UNUSED_FOR_NOW
- int sgetspent_r(const char *string, struct spwd *result_buf,
- char *buffer, size_t buflen, struct spwd **result)
- {
- int rv = ERANGE;
- *result = NULL;
- if (buflen < PWD_BUFFER_SIZE) {
- DO_ERANGE:
- errno = rv;
- goto DONE;
- }
- if (string != buffer) {
- if (strlen(string) >= buflen) {
- goto DO_ERANGE;
- }
- strcpy(buffer, string);
- }
- rv = bb__parsespent(result_buf, buffer);
- if (!rv) {
- *result = result_buf;
- }
- DONE:
- return rv;
- }
- #endif
- #endif
- #define GETXXKEY_R_FUNC getpwnam_r
- #define GETXXKEY_R_PARSER bb__parsepwent
- #define GETXXKEY_R_ENTTYPE struct passwd
- #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->pw_name, key))
- #define GETXXKEY_R_KEYTYPE const char *__restrict
- #define GETXXKEY_R_PATHNAME _PATH_PASSWD
- #include "pwd_grp_internal.c"
- #define GETXXKEY_R_FUNC getgrnam_r
- #define GETXXKEY_R_PARSER bb__parsegrent
- #define GETXXKEY_R_ENTTYPE struct group
- #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->gr_name, key))
- #define GETXXKEY_R_KEYTYPE const char *__restrict
- #define GETXXKEY_R_PATHNAME _PATH_GROUP
- #include "pwd_grp_internal.c"
- #if ENABLE_USE_BB_SHADOW
- #define GETXXKEY_R_FUNC getspnam_r
- #define GETXXKEY_R_PARSER bb__parsespent
- #define GETXXKEY_R_ENTTYPE struct spwd
- #define GETXXKEY_R_TEST(ENT) (!strcmp((ENT)->sp_namp, key))
- #define GETXXKEY_R_KEYTYPE const char *__restrict
- #define GETXXKEY_R_PATHNAME _PATH_SHADOW
- #include "pwd_grp_internal.c"
- #endif
- #define GETXXKEY_R_FUNC getpwuid_r
- #define GETXXKEY_R_PARSER bb__parsepwent
- #define GETXXKEY_R_ENTTYPE struct passwd
- #define GETXXKEY_R_TEST(ENT) ((ENT)->pw_uid == key)
- #define GETXXKEY_R_KEYTYPE uid_t
- #define GETXXKEY_R_PATHNAME _PATH_PASSWD
- #include "pwd_grp_internal.c"
- #define GETXXKEY_R_FUNC getgrgid_r
- #define GETXXKEY_R_PARSER bb__parsegrent
- #define GETXXKEY_R_ENTTYPE struct group
- #define GETXXKEY_R_TEST(ENT) ((ENT)->gr_gid == key)
- #define GETXXKEY_R_KEYTYPE gid_t
- #define GETXXKEY_R_PATHNAME _PATH_GROUP
- #include "pwd_grp_internal.c"
- struct passwd *getpwuid(uid_t uid)
- {
- struct statics *S;
- struct passwd *resultbuf = RESULTBUF(getpw);
- char *buffer = BUFFER(getpw);
- struct passwd *result;
- getpwuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getpw)), &result);
- return result;
- }
- struct group *getgrgid(gid_t gid)
- {
- struct statics *S;
- struct group *resultbuf = RESULTBUF(getgr);
- char *buffer = BUFFER(getgr);
- struct group *result;
- getgrgid_r(gid, resultbuf, buffer, sizeof(BUFFER(getgr)), &result);
- return result;
- }
- #if 0
- int getspuid_r(uid_t uid, struct spwd *__restrict resultbuf,
- char *__restrict buffer, size_t buflen,
- struct spwd **__restrict result)
- {
- int rv;
- struct passwd *pp;
- struct passwd password;
- char pwd_buff[PWD_BUFFER_SIZE];
- *result = NULL;
- rv = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), &pp);
- if (!rv) {
- rv = getspnam_r(password.pw_name, resultbuf, buffer, buflen, result);
- }
- return rv;
- }
- struct spwd *getspuid(uid_t uid)
- {
- struct statics *S;
- struct spwd *resultbuf = RESULTBUF(getsp);
- char *buffer = BUFFER(getsp);
- struct spwd *result;
- getspuid_r(uid, resultbuf, buffer, sizeof(BUFFER(getsp)), &result);
- return result;
- }
- #endif
- struct passwd *getpwnam(const char *name)
- {
- struct statics *S;
- struct passwd *resultbuf = RESULTBUF(getpw);
- char *buffer = BUFFER(getpw);
- struct passwd *result;
- getpwnam_r(name, resultbuf, buffer, sizeof(BUFFER(getpw)), &result);
- return result;
- }
- struct group *getgrnam(const char *name)
- {
- struct statics *S;
- struct group *resultbuf = RESULTBUF(getgr);
- char *buffer = BUFFER(getgr);
- struct group *result;
- getgrnam_r(name, resultbuf, buffer, sizeof(BUFFER(getgr)), &result);
- return result;
- }
- #if 0
- struct spwd *getspnam(const char *name)
- {
- struct statics *S;
- struct spwd *resultbuf = RESULTBUF(getsp);
- char *buffer = BUFFER(getsp);
- struct spwd *result;
- getspnam_r(name, resultbuf, buffer, sizeof(BUFFER(getsp)), &result);
- return result;
- }
- #endif
- #if defined CONFIG_USE_BB_THREADSAFE_SHADOW && defined PTHREAD_MUTEX_INITIALIZER
- static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
- # define LOCK pthread_mutex_lock(&mylock)
- # define UNLOCK pthread_mutex_unlock(&mylock);
- #else
- # define LOCK ((void) 0)
- # define UNLOCK ((void) 0)
- #endif
- static FILE *pwf ;
- void setpwent(void)
- {
- LOCK;
- if (pwf) {
- rewind(pwf);
- }
- UNLOCK;
- }
- void endpwent(void)
- {
- LOCK;
- if (pwf) {
- fclose(pwf);
- pwf = NULL;
- }
- UNLOCK;
- }
- int getpwent_r(struct passwd *__restrict resultbuf,
- char *__restrict buffer, size_t buflen,
- struct passwd **__restrict result)
- {
- int rv;
- LOCK;
- *result = NULL;
- if (!pwf) {
- pwf = fopen_for_read(_PATH_PASSWD);
- if (!pwf) {
- rv = errno;
- goto ERR;
- }
- close_on_exec_on(fileno(pwf));
- }
- rv = bb__pgsreader(bb__parsepwent, resultbuf, buffer, buflen, pwf);
- if (!rv) {
- *result = resultbuf;
- }
- ERR:
- UNLOCK;
- return rv;
- }
- static FILE *grf ;
- void setgrent(void)
- {
- LOCK;
- if (grf) {
- rewind(grf);
- }
- UNLOCK;
- }
- void endgrent(void)
- {
- LOCK;
- if (grf) {
- fclose(grf);
- grf = NULL;
- }
- UNLOCK;
- }
- int getgrent_r(struct group *__restrict resultbuf,
- char *__restrict buffer, size_t buflen,
- struct group **__restrict result)
- {
- int rv;
- LOCK;
- *result = NULL;
- if (!grf) {
- grf = fopen_for_read(_PATH_GROUP);
- if (!grf) {
- rv = errno;
- goto ERR;
- }
- close_on_exec_on(fileno(grf));
- }
- rv = bb__pgsreader(bb__parsegrent, resultbuf, buffer, buflen, grf);
- if (!rv) {
- *result = resultbuf;
- }
- ERR:
- UNLOCK;
- return rv;
- }
- #ifdef UNUSED_FOR_NOW
- #if ENABLE_USE_BB_SHADOW
- static FILE *spf ;
- void setspent(void)
- {
- LOCK;
- if (spf) {
- rewind(spf);
- }
- UNLOCK;
- }
- void endspent(void)
- {
- LOCK;
- if (spf) {
- fclose(spf);
- spf = NULL;
- }
- UNLOCK;
- }
- int getspent_r(struct spwd *resultbuf, char *buffer,
- size_t buflen, struct spwd **result)
- {
- int rv;
- LOCK;
- *result = NULL;
- if (!spf) {
- spf = fopen_for_read(_PATH_SHADOW);
- if (!spf) {
- rv = errno;
- goto ERR;
- }
- close_on_exec_on(fileno(spf));
- }
- rv = bb__pgsreader(bb__parsespent, resultbuf, buffer, buflen, spf);
- if (!rv) {
- *result = resultbuf;
- }
- ERR:
- UNLOCK;
- return rv;
- }
- #endif
- #endif
- #ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
- struct passwd *getpwent(void)
- {
- static char line_buff[PWD_BUFFER_SIZE];
- static struct passwd pwd;
- struct passwd *result;
- getpwent_r(&pwd, line_buff, sizeof(line_buff), &result);
- return result;
- }
- struct group *getgrent(void)
- {
- static char line_buff[GRP_BUFFER_SIZE];
- static struct group gr;
- struct group *result;
- getgrent_r(&gr, line_buff, sizeof(line_buff), &result);
- return result;
- }
- #if ENABLE_USE_BB_SHADOW
- struct spwd *getspent(void)
- {
- static char line_buff[PWD_BUFFER_SIZE];
- static struct spwd spwd;
- struct spwd *result;
- getspent_r(&spwd, line_buff, sizeof(line_buff), &result);
- return result;
- }
- struct spwd *sgetspent(const char *string)
- {
- static char line_buff[PWD_BUFFER_SIZE];
- static struct spwd spwd;
- struct spwd *result;
- sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), &result);
- return result;
- }
- #endif
- #endif
- static gid_t *getgrouplist_internal(int *ngroups_ptr, const char *user, gid_t gid)
- {
- FILE *grfile;
- gid_t *group_list;
- int ngroups;
- struct group group;
- char buff[PWD_BUFFER_SIZE];
-
- group_list = xmalloc(8 * sizeof(group_list[0]));
- group_list[0] = gid;
- ngroups = 1;
- grfile = fopen_for_read(_PATH_GROUP);
- if (grfile) {
- while (!bb__pgsreader(bb__parsegrent, &group, buff, sizeof(buff), grfile)) {
- char **m;
- assert(group.gr_mem);
- if (group.gr_gid == gid)
- continue;
- for (m = group.gr_mem; *m; m++) {
- if (strcmp(*m, user) != 0)
- continue;
- group_list = xrealloc_vector(group_list, 3, ngroups);
- group_list[ngroups++] = group.gr_gid;
- break;
- }
- }
- fclose(grfile);
- }
- *ngroups_ptr = ngroups;
- return group_list;
- }
- int initgroups(const char *user, gid_t gid)
- {
- int ngroups;
- gid_t *group_list = getgrouplist_internal(&ngroups, user, gid);
- ngroups = setgroups(ngroups, group_list);
- free(group_list);
- return ngroups;
- }
- int getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups)
- {
- int ngroups_old = *ngroups;
- gid_t *group_list = getgrouplist_internal(ngroups, user, gid);
- if (*ngroups <= ngroups_old) {
- ngroups_old = *ngroups;
- memcpy(groups, group_list, ngroups_old * sizeof(groups[0]));
- } else {
- ngroups_old = -1;
- }
- free(group_list);
- return ngroups_old;
- }
- #ifdef UNUSED_SINCE_WE_AVOID_STATIC_BUFS
- int putpwent(const struct passwd *__restrict p, FILE *__restrict f)
- {
- int rv = -1;
- #if 0
-
- if (!p || !f) {
- errno = EINVAL;
- return rv;
- }
- #endif
-
- if (fprintf(f, "%s:%s:%lu:%lu:%s:%s:%s\n",
- p->pw_name, p->pw_passwd,
- (unsigned long)(p->pw_uid),
- (unsigned long)(p->pw_gid),
- p->pw_gecos, p->pw_dir, p->pw_shell) >= 0
- ) {
- rv = 0;
- }
- return rv;
- }
- int putgrent(const struct group *__restrict p, FILE *__restrict f)
- {
- int rv = -1;
- #if 0
-
- if (!p || !f) {
- errno = EINVAL;
- return rv;
- }
- #endif
- if (fprintf(f, "%s:%s:%lu:",
- p->gr_name, p->gr_passwd,
- (unsigned long)(p->gr_gid)) >= 0
- ) {
- static const char format[] ALIGN1 = ",%s";
- char **m;
- const char *fmt;
- fmt = format + 1;
- assert(p->gr_mem);
- m = p->gr_mem;
- while (1) {
- if (!*m) {
- if (fputc('\n', f) >= 0) {
- rv = 0;
- }
- break;
- }
- if (fprintf(f, fmt, *m) < 0) {
- break;
- }
- m++;
- fmt = format;
- }
- }
- return rv;
- }
- #endif
- #if ENABLE_USE_BB_SHADOW
- #ifdef UNUSED_FOR_NOW
- static const unsigned char put_sp_off[] ALIGN1 = {
- offsetof(struct spwd, sp_lstchg),
- offsetof(struct spwd, sp_min),
- offsetof(struct spwd, sp_max),
- offsetof(struct spwd, sp_warn),
- offsetof(struct spwd, sp_inact),
- offsetof(struct spwd, sp_expire)
- };
- int putspent(const struct spwd *p, FILE *stream)
- {
- const char *fmt;
- long x;
- int i;
- int rv = -1;
-
- if (fprintf(stream, "%s:%s:", p->sp_namp,
- (p->sp_pwdp ? p->sp_pwdp : "")) < 0
- ) {
- goto DO_UNLOCK;
- }
- for (i = 0; i < sizeof(put_sp_off); i++) {
- fmt = "%ld:";
- x = *(long *)((char *)p + put_sp_off[i]);
- if (x == -1) {
- fmt += 3;
- }
- if (fprintf(stream, fmt, x) < 0) {
- goto DO_UNLOCK;
- }
- }
- if ((p->sp_flag != ~0UL) && (fprintf(stream, "%lu", p->sp_flag) < 0)) {
- goto DO_UNLOCK;
- }
- if (fputc('\n', stream) > 0) {
- rv = 0;
- }
- DO_UNLOCK:
- return rv;
- }
- #endif
- #endif
- static const unsigned char pw_off[] ALIGN1 = {
- offsetof(struct passwd, pw_name),
- offsetof(struct passwd, pw_passwd),
- offsetof(struct passwd, pw_uid),
- offsetof(struct passwd, pw_gid),
- offsetof(struct passwd, pw_gecos),
- offsetof(struct passwd, pw_dir),
- offsetof(struct passwd, pw_shell)
- };
- static int FAST_FUNC bb__parsepwent(void *data, char *line)
- {
- char *endptr;
- char *p;
- int i;
- i = 0;
- while (1) {
- p = (char *) data + pw_off[i];
- if (i < 2 || i > 3) {
- *((char **) p) = line;
- if (i == 6) {
- return 0;
- }
-
- line = strchr(line, ':');
- if (!line) {
- break;
- }
- } else {
- unsigned long t = strtoul(line, &endptr, 10);
-
-
- if ((endptr == line) || (*endptr != ':')) {
- break;
- }
- line = endptr;
- if (i & 1) {
- *((gid_t *) p) = t;
- } else {
- *((uid_t *) p) = t;
- }
- }
- *line++ = '\0';
- i++;
- }
- return -1;
- }
- static const unsigned char gr_off[] ALIGN1 = {
- offsetof(struct group, gr_name),
- offsetof(struct group, gr_passwd),
- offsetof(struct group, gr_gid)
- };
- static int FAST_FUNC bb__parsegrent(void *data, char *line)
- {
- char *endptr;
- char *p;
- int i;
- char **members;
- char *end_of_buf;
- end_of_buf = ((struct group *) data)->gr_name;
- i = 0;
- while (1) {
- p = (char *) data + gr_off[i];
- if (i < 2) {
- *((char **) p) = line;
- line = strchr(line, ':');
- if (!line) {
- break;
- }
- *line++ = '\0';
- i++;
- } else {
- *((gid_t *) p) = strtoul(line, &endptr, 10);
-
-
- if ((endptr == line) || (*endptr != ':')) {
- break;
- }
- i = 1;
- p = endptr;
- if (p[1]) {
-
- *p = ',';
- do {
-
-
- if (*p == ',') {
- ++i;
- *p = 0;
- if (!*++p || (*p == ',') || isspace(*p)) {
- goto ERR;
- }
- }
- } while (*++p);
- }
-
-
- members = (char **)( (((intptr_t) p) + sizeof(char **))
- & ~((intptr_t)(sizeof(char **) - 1)) );
- if (((char *)(members + i)) > end_of_buf) {
- break;
- }
- ((struct group *) data)->gr_mem = members;
- if (--i) {
- p = endptr;
- while (1) {
- *members++ = ++p;
- if (!--i)
- break;
- while (*++p)
- continue;
- }
- }
- *members = NULL;
- return 0;
- }
- }
- ERR:
- return -1;
- }
- #if ENABLE_USE_BB_SHADOW
- static const unsigned char sp_off[] ALIGN1 = {
- offsetof(struct spwd, sp_namp),
- offsetof(struct spwd, sp_pwdp),
- offsetof(struct spwd, sp_lstchg),
- offsetof(struct spwd, sp_min),
- offsetof(struct spwd, sp_max),
- offsetof(struct spwd, sp_warn),
- offsetof(struct spwd, sp_inact),
- offsetof(struct spwd, sp_expire),
- offsetof(struct spwd, sp_flag)
- };
- static int FAST_FUNC bb__parsespent(void *data, char *line)
- {
- char *endptr;
- char *p;
- int i;
- i = 0;
- while (1) {
- p = (char *) data + sp_off[i];
- if (i < 2) {
- *((char **) p) = line;
- line = strchr(line, ':');
- if (!line) {
- break;
- }
- } else {
- *((long *) p) = strtoul(line, &endptr, 10);
- if (endptr == line) {
- *((long *) p) = -1L;
- }
- line = endptr;
- if (i == 8) {
- if (*line != '\0') {
- break;
- }
- return 0;
- }
- if (*line != ':') {
- break;
- }
- }
- *line++ = '\0';
- i++;
- }
- return EINVAL;
- }
- #endif
- static int bb__pgsreader(
- int FAST_FUNC (*parserfunc)(void *d, char *line),
- void *data,
- char *__restrict line_buff,
- size_t buflen,
- FILE *f)
- {
- int skip;
- int rv = ERANGE;
- if (buflen < PWD_BUFFER_SIZE) {
- errno = rv;
- return rv;
- }
- skip = 0;
- while (1) {
- if (!fgets(line_buff, buflen, f)) {
- if (feof(f)) {
- rv = ENOENT;
- }
- break;
- }
- {
- int line_len = strlen(line_buff) - 1;
- if (line_len >= 0 && line_buff[line_len] == '\n') {
- line_buff[line_len] = '\0';
- } else
- if (line_len + 2 == buflen) {
-
- skip = 1;
- continue;
- }
- }
- if (skip) {
-
- skip = 0;
- continue;
- }
-
-
- if (line_buff[0] != '\0' && line_buff[0] != '#' && !isspace(line_buff[0])) {
- if (parserfunc == bb__parsegrent) {
-
- ((struct group *) data)->gr_name = line_buff + buflen;
- }
- if (parserfunc(data, line_buff) == 0) {
- rv = 0;
- break;
- }
- }
- }
- return rv;
- }
|