mirror of
git://git.musl-libc.org/musl
synced 2025-03-05 11:17:27 +00:00
refactor passwd file access code
this allows getpwnam and getpwuid to share code with the _r versions in preparation for alternate backend support.
This commit is contained in:
parent
74e334dcd1
commit
700e08993c
@ -6,5 +6,7 @@ struct passwd *fgetpwent(FILE *f)
|
||||
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;
|
||||
}
|
||||
|
31
src/passwd/getpw_a.c
Normal file
31
src/passwd/getpw_a.c
Normal file
@ -0,0 +1,31 @@
|
||||
#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;
|
||||
}
|
@ -5,7 +5,6 @@
|
||||
|
||||
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;
|
||||
@ -13,33 +12,20 @@ static int getpw_r(const char *name, uid_t uid, struct passwd *pw, char *buf, si
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
#include "pwf.h"
|
||||
|
||||
static FILE *f;
|
||||
static char *line;
|
||||
static struct passwd pw;
|
||||
static size_t size;
|
||||
|
||||
void setpwent()
|
||||
{
|
||||
@ -12,34 +15,23 @@ weak_alias(setpwent, endpwent);
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -8,14 +8,16 @@ static unsigned atou(char **s)
|
||||
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;
|
||||
@ -46,5 +48,7 @@ struct passwd *__getpwent_a(FILE *f, struct passwd *pw, char **line, size_t *siz
|
||||
break;
|
||||
}
|
||||
pthread_setcancelstate(cs, 0);
|
||||
return pw;
|
||||
*res = pw;
|
||||
if (rv) errno = rv;
|
||||
return rv;
|
||||
}
|
||||
|
@ -8,6 +8,7 @@
|
||||
#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);
|
||||
|
Loading…
Reference in New Issue
Block a user