static char *line;
static struct passwd pw;
size_t size=0;
- return __getpwent_a(f, &pw, &line, &size);
+ struct passwd *res;
+ __getpwent_a(f, &pw, &line, &size, &res);
+ return res;
}
--- /dev/null
+#include "pwf.h"
+#include <pthread.h>
+
+int __getpw_a(const char *name, uid_t uid, struct passwd *pw, char **buf, size_t *size, struct passwd **res)
+{
+ FILE *f;
+ int cs;
+ int rv = 0;
+
+ *res = 0;
+
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
+
+ f = fopen("/etc/passwd", "rbe");
+ if (!f) {
+ rv = errno;
+ goto done;
+ }
+
+ while (!(rv = __getpwent_a(f, pw, buf, size, res)) && *res) {
+ if (name && !strcmp(name, (*res)->pw_name)
+ || !name && (*res)->pw_uid == uid)
+ break;
+ }
+ fclose(f);
+
+done:
+ pthread_setcancelstate(cs, 0);
+ if (rv) errno = rv;
+ return rv;
+}
static int getpw_r(const char *name, uid_t uid, struct passwd *pw, char *buf, size_t size, struct passwd **res)
{
- FILE *f;
char *line = 0;
size_t len = 0;
int rv = 0;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
- f = fopen("/etc/passwd", "rbe");
- if (!f) {
- rv = errno;
- goto done;
+ rv = __getpw_a(name, uid, pw, &line, &len, res);
+ if (!rv && size < len) {
+ *res = 0;
+ rv = ERANGE;
}
-
- *res = 0;
- while (__getpwent_a(f, pw, &line, &len)) {
- if (name && !strcmp(name, pw->pw_name)
- || !name && pw->pw_uid == uid) {
- if (size < len) {
- rv = ERANGE;
- break;
- }
- *res = pw;
- memcpy(buf, line, len);
- FIX(name);
- FIX(passwd);
- FIX(gecos);
- FIX(dir);
- FIX(shell);
- break;
- }
+ if (!rv) {
+ memcpy(buf, line, len);
+ FIX(name);
+ FIX(passwd);
+ FIX(gecos);
+ FIX(dir);
+ FIX(shell);
}
free(line);
- fclose(f);
-done:
pthread_setcancelstate(cs, 0);
return rv;
}
#include "pwf.h"
static FILE *f;
+static char *line;
+static struct passwd pw;
+static size_t size;
void setpwent()
{
struct passwd *getpwent()
{
- static char *line;
- static struct passwd pw;
- size_t size=0;
+ struct passwd *res;
if (!f) f = fopen("/etc/passwd", "rbe");
if (!f) return 0;
- return __getpwent_a(f, &pw, &line, &size);
+ __getpwent_a(f, &pw, &line, &size, &res);
+ return res;
}
struct passwd *getpwuid(uid_t uid)
{
- struct passwd *pw;
- int errno_saved;
- setpwent();
- while ((pw=getpwent()) && pw->pw_uid != uid);
- errno_saved = errno;
- endpwent();
- errno = errno_saved;
- return pw;
+ struct passwd *res;
+ __getpw_a(0, uid, &pw, &line, &size, &res);
+ return res;
}
struct passwd *getpwnam(const char *name)
{
- struct passwd *pw;
- int errno_saved;
- setpwent();
- while ((pw=getpwent()) && strcmp(pw->pw_name, name));
- errno_saved = errno;
- endpwent();
- errno = errno_saved;
- return pw;
+ struct passwd *res;
+ __getpw_a(name, 0, &pw, &line, &size, &res);
+ return res;
}
return x;
}
-struct passwd *__getpwent_a(FILE *f, struct passwd *pw, char **line, size_t *size)
+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 = errno;
free(*line);
*line = 0;
pw = 0;
break;
}
pthread_setcancelstate(cs, 0);
- return pw;
+ *res = pw;
+ if (rv) errno = rv;
+ return rv;
}
#include <limits.h>
#include "libc.h"
-struct passwd *__getpwent_a(FILE *f, struct passwd *pw, char **line, size_t *size);
+int __getpwent_a(FILE *f, struct passwd *pw, char **line, size_t *size, struct passwd **res);
+int __getpw_a(const char *name, uid_t uid, struct passwd *pw, char **buf, size_t *size, struct passwd **res);
struct group *__getgrent_a(FILE *f, struct group *gr, char **line, size_t *size, char ***mem, size_t *nmem);
int __parsespent(char *s, struct spwd *sp);