From 4abab5ad6c8465a7528ccdd5f49367da05f78bbd Mon Sep 17 00:00:00 2001 From: Vladimir Azarov Date: Tue, 1 Oct 2024 15:47:05 +0200 Subject: Initial version --- src/passwd/getpwent_a.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/passwd/getpwent_a.c (limited to 'src/passwd/getpwent_a.c') diff --git a/src/passwd/getpwent_a.c b/src/passwd/getpwent_a.c new file mode 100644 index 0000000..d1b4b53 --- /dev/null +++ b/src/passwd/getpwent_a.c @@ -0,0 +1,54 @@ +#include "pwf.h" +#include + +static unsigned atou(char **s) +{ + unsigned x; + for (x=0; **s-'0'<10U; ++*s) x=10*x+(**s-'0'); + return x; +} + +int __getpwent_a(FILE *f, struct passwd *pw, char **line, size_t *size, struct passwd **res) +{ + ssize_t l; + char *s; + int rv = 0; + int cs; + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); + for (;;) { + if ((l=getline(line, size, f)) < 0) { + rv = ferror(f) ? errno : 0; + free(*line); + *line = 0; + pw = 0; + break; + } + line[0][l-1] = 0; + + s = line[0]; + pw->pw_name = s++; + if (!(s = strchr(s, ':'))) continue; + + *s++ = 0; pw->pw_passwd = s; + if (!(s = strchr(s, ':'))) continue; + + *s++ = 0; pw->pw_uid = atou(&s); + if (*s != ':') continue; + + *s++ = 0; pw->pw_gid = atou(&s); + if (*s != ':') continue; + + *s++ = 0; pw->pw_gecos = s; + if (!(s = strchr(s, ':'))) continue; + + *s++ = 0; pw->pw_dir = s; + if (!(s = strchr(s, ':'))) continue; + + *s++ = 0; pw->pw_shell = s; + break; + } + pthread_setcancelstate(cs, 0); + *res = pw; + if (rv) errno = rv; + return rv; +} -- cgit v1.2.3