summaryrefslogtreecommitdiff
path: root/libc
diff options
context:
space:
mode:
Diffstat (limited to 'libc')
-rw-r--r--libc/pwd_grp/__getpwent_r.c12
-rw-r--r--libc/pwd_grp/__getspent_r.c10
-rw-r--r--libc/pwd_grp/__sgetspent_r.c24
-rw-r--r--libc/pwd_grp/fgetpwent.c7
-rw-r--r--libc/pwd_grp/fgetspent.c7
-rw-r--r--libc/pwd_grp/getpwnam.c23
-rw-r--r--libc/pwd_grp/getpwuid.c11
-rw-r--r--libc/pwd_grp/getspnam.c13
-rw-r--r--libc/pwd_grp/getspuid.c15
-rw-r--r--libc/pwd_grp/initgroups.c3
-rw-r--r--libc/pwd_grp/pwent.c10
-rw-r--r--libc/pwd_grp/sgetspent.c4
-rw-r--r--libc/pwd_grp/spent.c9
13 files changed, 93 insertions, 55 deletions
diff --git a/libc/pwd_grp/__getpwent_r.c b/libc/pwd_grp/__getpwent_r.c
index db49ae255..5cfdd89ba 100644
--- a/libc/pwd_grp/__getpwent_r.c
+++ b/libc/pwd_grp/__getpwent_r.c
@@ -24,6 +24,7 @@
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
+#include <errno.h>
#include "config.h"
@@ -42,12 +43,15 @@ int __getpwent_r(struct passwd * passwd, char * line_buff, size_t buflen, int pw
int line_len;
int i;
+ if (buflen<PWD_BUFFER_SIZE)
+ return ERANGE;
+
/* We use the restart label to handle malformatted lines */
- restart:
- /* Read the passwd line into the static buffer using a minimal of
+restart:
+ /* Read the passwd line into the static buffer using a minimum of
syscalls. */
if ((line_len = read(pwd_fd, line_buff, buflen)) <= 0)
- return -1;
+ return EIO;
field_begin = strchr(line_buff, '\n');
if (field_begin != NULL)
lseek(pwd_fd, (long) (1 + field_begin - (line_buff + line_len)),
@@ -56,7 +60,7 @@ int __getpwent_r(struct passwd * passwd, char * line_buff, size_t buflen, int pw
do {
if ((line_len = read(pwd_fd, line_buff, buflen)) <= 0)
- return -1;
+ return EIO;
} while (!(field_begin = strchr(line_buff, '\n')));
lseek(pwd_fd, (long) (field_begin - line_buff) - line_len + 1,
SEEK_CUR);
diff --git a/libc/pwd_grp/__getspent_r.c b/libc/pwd_grp/__getspent_r.c
index 7150520d2..73e40f031 100644
--- a/libc/pwd_grp/__getspent_r.c
+++ b/libc/pwd_grp/__getspent_r.c
@@ -20,6 +20,7 @@
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
+#include <errno.h>
#include "config.h"
@@ -28,11 +29,14 @@ int __getspent_r(struct spwd * spwd, char * line_buff, size_t buflen, int spwd_f
char *endptr;
int line_len;
+ if (buflen<PWD_BUFFER_SIZE)
+ return ERANGE;
+
/* We use the restart label to handle malformatted lines */
restart:
/* Read the shadow line into the buffer using a minimum of syscalls. */
if ((line_len = read(spwd_fd, line_buff, buflen)) <= 0)
- return -1;
+ return EIO;
endptr = strchr(line_buff, '\n');
if (endptr != NULL)
lseek(spwd_fd, (long) (1 + endptr - (line_buff + line_len)), SEEK_CUR);
@@ -40,13 +44,13 @@ restart:
/* The line is too long - skip it. :-\ */
do {
if ((line_len = read(spwd_fd, line_buff, buflen)) <= 0)
- return -1;
+ return EIO;
} while (!(endptr = strchr(line_buff, '\n')));
lseek(spwd_fd, (long) (endptr - line_buff) - line_len + 1, SEEK_CUR);
goto restart;
}
- if (__sgetspent_r(line_buff, spwd, line_buff, buflen) < 0)
+ if (__sgetspent_r(line_buff, spwd, line_buff, buflen) != 0)
goto restart;
return 0;
diff --git a/libc/pwd_grp/__sgetspent_r.c b/libc/pwd_grp/__sgetspent_r.c
index ec7c1c936..d11cfe62e 100644
--- a/libc/pwd_grp/__sgetspent_r.c
+++ b/libc/pwd_grp/__sgetspent_r.c
@@ -19,6 +19,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
+#include <errno.h>
#include "config.h"
@@ -35,15 +36,18 @@ int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, siz
char *flag_ptr=NULL;
int i;
+ if (buflen<PWD_BUFFER_SIZE)
+ return ERANGE;
+
if (string != line_buff) {
if (strlen(string) >= buflen)
- return -1;
+ return ERANGE;
strcpy(line_buff, string);
}
if (*line_buff == '#' || *line_buff == ' ' || *line_buff == '\n' ||
*line_buff == '\t')
- return -1;
+ return EINVAL;
field_begin = strchr(line_buff, '\n');
if (field_begin != NULL)
@@ -86,7 +90,7 @@ int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, siz
if (field_begin == NULL) {
if (i==4 || i==7)
break;
- return -1;
+ return EINVAL;
}
*field_begin++ = '\0';
}
@@ -97,7 +101,7 @@ int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, siz
} else {
spwd->sp_lstchg = (gid_t) strtoul(lstchg_ptr, &endptr, 10);
if (*endptr != '\0')
- return -1;
+ return EINVAL;
}
if (*min_ptr == '\0') {
@@ -105,7 +109,7 @@ int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, siz
} else {
spwd->sp_min = (gid_t) strtoul(min_ptr, &endptr, 10);
if (*endptr != '\0')
- return -1;
+ return EINVAL;
}
if (*max_ptr == '\0') {
@@ -113,7 +117,7 @@ int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, siz
} else {
spwd->sp_max = (gid_t) strtoul(max_ptr, &endptr, 10);
if (*endptr != '\0')
- return -1;
+ return EINVAL;
}
if (warn_ptr == NULL) {
@@ -129,7 +133,7 @@ int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, siz
} else {
spwd->sp_warn = (gid_t) strtoul(warn_ptr, &endptr, 10);
if (*endptr != '\0')
- return -1;
+ return EINVAL;
}
if (*inact_ptr == '\0') {
@@ -137,7 +141,7 @@ int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, siz
} else {
spwd->sp_inact = (gid_t) strtoul(inact_ptr, &endptr, 10);
if (*endptr != '\0')
- return -1;
+ return EINVAL;
}
if (*expire_ptr == '\0') {
@@ -145,7 +149,7 @@ int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, siz
} else {
spwd->sp_expire = (gid_t) strtoul(expire_ptr, &endptr, 10);
if (*endptr != '\0')
- return -1;
+ return EINVAL;
}
if (flag_ptr==NULL || *flag_ptr=='\0') {
@@ -153,7 +157,7 @@ int __sgetspent_r(const char * string, struct spwd * spwd, char * line_buff, siz
} else {
spwd->sp_flag = (gid_t) strtoul(flag_ptr, &endptr, 10);
if (*endptr != '\0')
- return -1;
+ return EINVAL;
}
}
diff --git a/libc/pwd_grp/fgetpwent.c b/libc/pwd_grp/fgetpwent.c
index 5ca08d3b0..8917c29d0 100644
--- a/libc/pwd_grp/fgetpwent.c
+++ b/libc/pwd_grp/fgetpwent.c
@@ -37,22 +37,23 @@ int fgetpwent_r (FILE *file, struct passwd *password,
char *buff, size_t buflen, struct passwd **crap)
{
if (file == NULL) {
- __set_errno(EINTR);
- return -1;
+ return EINTR;
}
return(__getpwent_r(password, buff, buflen, fileno(file)));
}
struct passwd *fgetpwent(FILE * file)
{
+ int ret;
static char line_buff[PWD_BUFFER_SIZE];
static struct passwd pwd;
LOCK;
- if (fgetpwent_r(file, &pwd, line_buff, sizeof(line_buff), NULL) != -1) {
+ if ((ret=fgetpwent_r(file, &pwd, line_buff, sizeof(line_buff), NULL)) == 0) {
UNLOCK;
return &pwd;
}
UNLOCK;
+ __set_errno(ret);
return NULL;
}
diff --git a/libc/pwd_grp/fgetspent.c b/libc/pwd_grp/fgetspent.c
index 20698da1b..79594ecea 100644
--- a/libc/pwd_grp/fgetspent.c
+++ b/libc/pwd_grp/fgetspent.c
@@ -36,22 +36,23 @@ int fgetspent_r (FILE *file, struct spwd *spwd,
char *buff, size_t buflen, struct spwd **crap)
{
if (file == NULL) {
- __set_errno(EINTR);
- return -1;
+ return EINTR;
}
return(__getspent_r(spwd, buff, buflen, fileno(file)));
}
struct spwd *fgetspent(FILE * file)
{
+ int ret;
static char line_buff[PWD_BUFFER_SIZE];
static struct spwd spwd;
LOCK;
- if (fgetspent_r(file, &spwd, line_buff, sizeof(line_buff), NULL) != -1) {
+ if ((ret=fgetspent_r(file, &spwd, line_buff, sizeof(line_buff), NULL)) == 0) {
UNLOCK;
return &spwd;
}
UNLOCK;
+ __set_errno(ret);
return NULL;
}
diff --git a/libc/pwd_grp/getpwnam.c b/libc/pwd_grp/getpwnam.c
index 00adb57ca..6f041e2d8 100644
--- a/libc/pwd_grp/getpwnam.c
+++ b/libc/pwd_grp/getpwnam.c
@@ -37,38 +37,43 @@ static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
#endif
int getpwnam_r (const char *name, struct passwd *password,
- char *buff, size_t buflen, struct passwd **crap)
+ char *buff, size_t buflen, struct passwd **result)
{
+ int ret;
int passwd_fd;
if (name == NULL) {
- __set_errno(EINVAL);
- return -1;
+ return EINVAL;
}
- if ((passwd_fd = open(_PATH_PASSWD, O_RDONLY)) < 0)
- return -1;
+ if ((passwd_fd = open(_PATH_PASSWD, O_RDONLY)) < 0) {
+ return ENOENT;
+ }
- while (__getpwent_r(password, buff, buflen, passwd_fd) != -1)
+ while ((ret=__getpwent_r(password, buff, buflen, passwd_fd)) == 0) {
if (!strcmp(password->pw_name, name)) {
+ *result=password;
close(passwd_fd);
return 0;
}
+ }
close(passwd_fd);
- return -1;
+ return ret;
}
struct passwd *getpwnam(const char *name)
{
+ int ret;
static char line_buff[PWD_BUFFER_SIZE];
- static struct passwd pwd;
+ static struct passwd pwd, *result;
LOCK;
- if (getpwnam_r(name, &pwd, line_buff, sizeof(line_buff), NULL) != -1) {
+ if ((ret=getpwnam_r(name, &pwd, line_buff, sizeof(line_buff), &result)) == 0) {
UNLOCK;
return &pwd;
}
+ __set_errno(ret);
UNLOCK;
return NULL;
}
diff --git a/libc/pwd_grp/getpwuid.c b/libc/pwd_grp/getpwuid.c
index 48ee6123d..c54df0977 100644
--- a/libc/pwd_grp/getpwuid.c
+++ b/libc/pwd_grp/getpwuid.c
@@ -23,6 +23,7 @@
#include <unistd.h>
#include <fcntl.h>
#include <paths.h>
+#include <errno.h>
#include "config.h"
#ifdef __UCLIBC_HAS_THREADS__
@@ -41,30 +42,32 @@ int getpwuid_r (uid_t uid, struct passwd *password,
int passwd_fd;
if ((passwd_fd = open(_PATH_PASSWD, O_RDONLY)) < 0)
- return -1;
+ return errno;
- while (__getpwent_r(password, buff, buflen, passwd_fd) != -1)
+ while (__getpwent_r(password, buff, buflen, passwd_fd) == 0)
if (password->pw_uid == uid) {
close(passwd_fd);
return 0;
}
close(passwd_fd);
- return -1;
+ return EINVAL;
}
struct passwd *getpwuid(uid_t uid)
{
+ int ret;
/* file descriptor for the password file currently open */
static char line_buff[PWD_BUFFER_SIZE];
static struct passwd pwd;
LOCK;
- if (getpwuid_r(uid, &pwd, line_buff, sizeof(line_buff), NULL) != -1) {
+ if ((ret=getpwuid_r(uid, &pwd, line_buff, sizeof(line_buff), NULL)) == 0) {
UNLOCK;
return &pwd;
}
UNLOCK;
+ __set_errno(ret);
return NULL;
}
diff --git a/libc/pwd_grp/getspnam.c b/libc/pwd_grp/getspnam.c
index 9d578f09a..6f3d3a128 100644
--- a/libc/pwd_grp/getspnam.c
+++ b/libc/pwd_grp/getspnam.c
@@ -40,34 +40,35 @@ int getspnam_r (const char *name, struct spwd *spwd,
int spwd_fd;
if (name == NULL) {
- __set_errno(EINVAL);
- return -1;
+ return EINVAL;
}
if ((spwd_fd = open(_PATH_SHADOW, O_RDONLY)) < 0)
- return -1;
+ return errno;
- while (__getspent_r(spwd, buff, buflen, spwd_fd) != -1)
+ while (__getspent_r(spwd, buff, buflen, spwd_fd) == 0)
if (!strcmp(spwd->sp_namp, name)) {
close(spwd_fd);
return 0;
}
close(spwd_fd);
- return -1;
+ return EINVAL;
}
struct spwd *getspnam(const char *name)
{
+ int ret;
static char line_buff[PWD_BUFFER_SIZE];
static struct spwd spwd;
LOCK;
- if (getspnam_r(name, &spwd, line_buff, sizeof(line_buff), NULL) != -1) {
+ if ((ret=getspnam_r(name, &spwd, line_buff, sizeof(line_buff), NULL)) == 0) {
UNLOCK;
return &spwd;
}
UNLOCK;
+ __set_errno(ret);
return NULL;
}
diff --git a/libc/pwd_grp/getspuid.c b/libc/pwd_grp/getspuid.c
index d57b2aa0b..3fa7fb5a1 100644
--- a/libc/pwd_grp/getspuid.c
+++ b/libc/pwd_grp/getspuid.c
@@ -21,6 +21,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
+#include <errno.h>
#include "config.h"
#ifdef __UCLIBC_HAS_THREADS__
@@ -34,28 +35,32 @@ static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
#endif
int getspuid_r (uid_t uid, struct spwd *spwd,
- char *buff, size_t buflen, struct spwd **crap)
+ char *buff, size_t buflen, struct spwd **result)
{
+ int ret;
char pwd_buff[PWD_BUFFER_SIZE];
struct passwd password;
- if (getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), NULL) < 0)
- return -1;
+ ret = getpwuid_r(uid, &password, pwd_buff, sizeof(pwd_buff), NULL);
+ if (ret != 0)
+ return ret;
- return getspnam_r(password.pw_name, spwd, buff, buflen, crap);
+ return getspnam_r(password.pw_name, spwd, buff, buflen, result);
}
struct spwd *getspuid(uid_t uid)
{
+ int ret;
static char line_buff[PWD_BUFFER_SIZE];
static struct spwd spwd;
LOCK;
- if (getspuid_r(uid, &spwd, line_buff, sizeof(line_buff), NULL) != -1) {
+ if ((ret=getspuid_r(uid, &spwd, line_buff, sizeof(line_buff), NULL)) == 0) {
UNLOCK;
return &spwd;
}
UNLOCK;
+ __set_errno(ret);
return NULL;
}
diff --git a/libc/pwd_grp/initgroups.c b/libc/pwd_grp/initgroups.c
index 9c1fbc03c..24dadc18f 100644
--- a/libc/pwd_grp/initgroups.c
+++ b/libc/pwd_grp/initgroups.c
@@ -23,6 +23,7 @@
#include <fcntl.h>
#include <paths.h>
#include <stdlib.h>
+#include <errno.h>
#include "config.h"
#ifdef __UCLIBC_HAS_THREADS__
@@ -49,7 +50,7 @@ int initgroups(__const char *user, gid_t gid)
if ((grp_fd = open(_PATH_GROUP, O_RDONLY)) < 0)
- return -1;
+ return errno;
num_groups = 0;
group_list = (gid_t *) realloc(group_list, 1);
diff --git a/libc/pwd_grp/pwent.c b/libc/pwd_grp/pwent.c
index f79be7f33..f70a6e378 100644
--- a/libc/pwd_grp/pwent.c
+++ b/libc/pwd_grp/pwent.c
@@ -67,23 +67,27 @@ void endpwent(void)
int getpwent_r (struct passwd *password, char *buff,
size_t buflen, struct passwd **crap)
{
+ int ret;
LOCK;
- if (pw_fd != -1 && __getpwent_r(password, buff, buflen, pw_fd) != -1) {
+ if (pw_fd != -1 && (ret=__getpwent_r(password, buff, buflen, pw_fd)) == 0) {
UNLOCK;
return 0;
}
UNLOCK;
- return -1;
+ __set_errno(ret);
+ return ret;
}
struct passwd *getpwent(void)
{
+ int ret;
static char line_buff[PWD_BUFFER_SIZE];
static struct passwd pwd;
- if (getpwent_r(&pwd, line_buff, sizeof(line_buff), NULL) != -1) {
+ if ((ret=getpwent_r(&pwd, line_buff, sizeof(line_buff), NULL)) == 0) {
return &pwd;
}
+ __set_errno(ret);
return NULL;
}
diff --git a/libc/pwd_grp/sgetspent.c b/libc/pwd_grp/sgetspent.c
index 1925a78ec..5f018b9f1 100644
--- a/libc/pwd_grp/sgetspent.c
+++ b/libc/pwd_grp/sgetspent.c
@@ -40,14 +40,16 @@ int sgetspent_r (const char *string, struct spwd *spwd,
struct spwd *sgetspent(const char *string)
{
+ int ret;
static char line_buff[PWD_BUFFER_SIZE];
static struct spwd spwd;
LOCK;
- if (sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), NULL) != -1) {
+ if ((ret = sgetspent_r(string, &spwd, line_buff, sizeof(line_buff), NULL)) == 0) {
UNLOCK;
return &spwd;
}
+ __set_errno(ret);
UNLOCK;
return NULL;
}
diff --git a/libc/pwd_grp/spent.c b/libc/pwd_grp/spent.c
index d1b8ca07c..b281072f4 100644
--- a/libc/pwd_grp/spent.c
+++ b/libc/pwd_grp/spent.c
@@ -64,26 +64,29 @@ void endspent(void)
int getspent_r (struct spwd *spwd, char *buff,
size_t buflen, struct spwd **crap)
{
+ int ret;
LOCK;
- if (spwd_fd != -1 && __getspent_r(spwd, buff, buflen, spwd_fd) != -1) {
+ if (spwd_fd != -1 && (ret=__getspent_r(spwd, buff, buflen, spwd_fd)) == 0) {
UNLOCK;
return 0;
}
UNLOCK;
- return -1;
+ return ret;
}
struct spwd *getspent(void)
{
+ int ret;
static char line_buff[PWD_BUFFER_SIZE];
static struct spwd spwd;
LOCK;
- if (getspent_r(&spwd, line_buff, sizeof(line_buff), NULL) != -1) {
+ if ((ret=getspent_r(&spwd, line_buff, sizeof(line_buff), NULL)) == 0) {
UNLOCK;
return &spwd;
}
UNLOCK;
+ __set_errno(ret);
return NULL;
}