/*
- * Copyright (c) 2005,2008 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 2005, 2008, 2010 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
# include <memory.h>
# endif
# include <string.h>
-#else
-# ifdef HAVE_STRINGS_H
-# include <strings.h>
-# endif
#endif /* HAVE_STRING_H */
+#ifdef HAVE_STRINGS_H
+# include <strings.h>
+#endif /* HAVE_STRINGS_H */
+#include <fcntl.h>
#include <limits.h>
#include <pwd.h>
#include <grp.h>
static const char *grfile = "/etc/group";
static int gr_stayopen;
-void ts_setgrfile __P((const char *));
-void ts_setgrent __P((void));
-void ts_endgrent __P((void));
-struct group *ts_getgrent __P((void));
-struct group *ts_getgrnam __P((const char *));
-struct group *ts_getgrgid __P((gid_t));
+void setgrfile __P((const char *));
+void setgrent __P((void));
+void endgrent __P((void));
+struct group *getgrent __P((void));
+struct group *getgrnam __P((const char *));
+struct group *getgrgid __P((gid_t));
-void ts_setpwfile __P((const char *));
-void ts_setpwent __P((void));
-void ts_endpwent __P((void));
-struct passwd *ts_getpwent __P((void));
-struct passwd *ts_getpwnam __P((const char *));
-struct passwd *ts_getpwuid __P((uid_t));
+void setpwfile __P((const char *));
+void setpwent __P((void));
+void endpwent __P((void));
+struct passwd *getpwent __P((void));
+struct passwd *getpwnam __P((const char *));
+struct passwd *getpwuid __P((uid_t));
void
-ts_setpwfile(file)
+setpwfile(file)
const char *file;
{
pwfile = file;
if (pwf != NULL)
- ts_endpwent();
+ endpwent();
}
void
-ts_setpwent()
+setpwent()
{
- if (pwf == NULL)
+ if (pwf == NULL) {
pwf = fopen(pwfile, "r");
- else
+ if (pwf != NULL)
+ fcntl(fileno(pwf), F_SETFD, FD_CLOEXEC);
+ } else {
rewind(pwf);
+ }
pw_stayopen = 1;
}
void
-ts_endpwent()
+endpwent()
{
if (pwf != NULL) {
fclose(pwf);
}
struct passwd *
-ts_getpwent()
+getpwent()
{
static struct passwd pw;
static char pwbuf[LINE_MAX];
}
struct passwd *
-ts_getpwnam(name)
+getpwnam(name)
const char *name;
{
struct passwd *pw;
- if (pwf != NULL)
+ if (pwf == NULL) {
+ if ((pwf = fopen(pwfile, "r")) == NULL)
+ return(NULL);
+ fcntl(fileno(pwf), F_SETFD, FD_CLOEXEC);
+ } else {
rewind(pwf);
- else if ((pwf = fopen(pwfile, "r")) == NULL)
- return(NULL);
- while ((pw = ts_getpwent()) != NULL) {
+ }
+ while ((pw = getpwent()) != NULL) {
if (strcmp(pw->pw_name, name) == 0)
break;
}
}
struct passwd *
-ts_getpwuid(uid)
+getpwuid(uid)
uid_t uid;
{
struct passwd *pw;
- if (pwf != NULL)
+ if (pwf == NULL) {
+ if ((pwf = fopen(pwfile, "r")) == NULL)
+ return(NULL);
+ fcntl(fileno(pwf), F_SETFD, FD_CLOEXEC);
+ } else {
rewind(pwf);
- else if ((pwf = fopen(pwfile, "r")) == NULL)
- return(NULL);
- while ((pw = ts_getpwent()) != NULL) {
+ }
+ while ((pw = getpwent()) != NULL) {
if (pw->pw_uid == uid)
break;
}
}
void
-ts_setgrfile(file)
+setgrfile(file)
const char *file;
{
grfile = file;
if (grf != NULL)
- ts_endgrent();
+ endgrent();
}
void
-ts_setgrent()
+setgrent()
{
- if (grf == NULL)
+ if (grf == NULL) {
grf = fopen(grfile, "r");
- else
+ if (grf != NULL)
+ fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
+ } else {
rewind(grf);
+ }
gr_stayopen = 1;
}
void
-ts_endgrent()
+endgrent()
{
if (grf != NULL) {
fclose(grf);
}
struct group *
-ts_getgrent()
+getgrent()
{
static struct group gr;
static char grbuf[LINE_MAX], *gr_mem[GRMEM_MAX+1];
}
struct group *
-ts_getgrnam(name)
+getgrnam(name)
const char *name;
{
struct group *gr;
- if (grf != NULL)
+ if (grf == NULL) {
+ if ((grf = fopen(grfile, "r")) == NULL)
+ return(NULL);
+ fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
+ } else {
rewind(grf);
- else if ((grf = fopen(grfile, "r")) == NULL)
- return(NULL);
- while ((gr = ts_getgrent()) != NULL) {
+ }
+ while ((gr = getgrent()) != NULL) {
if (strcmp(gr->gr_name, name) == 0)
break;
}
}
struct group *
-ts_getgrgid(gid)
+getgrgid(gid)
gid_t gid;
{
struct group *gr;
- if (grf != NULL)
+ if (grf == NULL) {
+ if ((grf = fopen(grfile, "r")) == NULL)
+ return(NULL);
+ fcntl(fileno(grf), F_SETFD, FD_CLOEXEC);
+ } else {
rewind(grf);
- else if ((grf = fopen(grfile, "r")) == NULL)
- return(NULL);
- while ((gr = ts_getgrent()) != NULL) {
+ }
+ while ((gr = getgrent()) != NULL) {
if (gr->gr_gid == gid)
break;
}