aboutsummaryrefslogtreecommitdiff
path: root/pwd
diff options
context:
space:
mode:
authorRoland McGrath <roland@gnu.org>1995-02-18 01:27:10 +0000
committerRoland McGrath <roland@gnu.org>1995-02-18 01:27:10 +0000
commit28f540f45bbacd939bfd07f213bcad2bf730b1bf (patch)
tree15f07c4c43d635959c6afee96bde71fb1b3614ee /pwd
downloadglibc-28f540f45bbacd939bfd07f213bcad2bf730b1bf.zip
glibc-28f540f45bbacd939bfd07f213bcad2bf730b1bf.tar.gz
glibc-28f540f45bbacd939bfd07f213bcad2bf730b1bf.tar.bz2
initial import
Diffstat (limited to 'pwd')
-rw-r--r--pwd/Makefile26
-rw-r--r--pwd/fgetpwent.c40
-rw-r--r--pwd/getpw.c48
-rw-r--r--pwd/getpwent.c67
-rw-r--r--pwd/getpwnam.c50
-rw-r--r--pwd/getpwuid.c50
-rw-r--r--pwd/putpwent.c43
-rw-r--r--pwd/pwd.h92
-rw-r--r--pwd/pwdopen.c28
-rw-r--r--pwd/pwdread.c116
10 files changed, 560 insertions, 0 deletions
diff --git a/pwd/Makefile b/pwd/Makefile
new file mode 100644
index 0000000..b33700a
--- /dev/null
+++ b/pwd/Makefile
@@ -0,0 +1,26 @@
+# Copyright (C) 1991 Free Software Foundation, Inc.
+# This file is part of the GNU C Library.
+
+# The GNU C Library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Library General Public License as
+# published by the Free Software Foundation; either version 2 of the
+# License, or (at your option) any later version.
+
+# The GNU C Library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Library General Public License for more details.
+
+# You should have received a copy of the GNU Library General Public
+# License along with the GNU C Library; see the file COPYING.LIB. If
+# not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+# Cambridge, MA 02139, USA.
+
+#
+# Sub-makefile for pwd portion of the library.
+#
+subdir := pwd
+
+routines := pwdopen pwdread fgetpwent getpw getpwent getpwnam getpwuid putpwent
+
+include ../Rules
diff --git a/pwd/fgetpwent.c b/pwd/fgetpwent.c
new file mode 100644
index 0000000..4a21cbb
--- /dev/null
+++ b/pwd/fgetpwent.c
@@ -0,0 +1,40 @@
+/* Copyright (C) 1991 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA. */
+
+#include <ansidecl.h>
+#include <errno.h>
+#include <limits.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <string.h>
+#include <pwd.h>
+
+/* Read one entry from the given stream. */
+struct passwd *
+DEFUN(fgetpwent, (stream), FILE *stream)
+{
+ static PTR info = NULL;
+ if (info == NULL)
+ {
+ info = __pwdalloc();
+ if (info == NULL)
+ return(NULL);
+ }
+
+ return(__pwdread(stream, info));
+}
diff --git a/pwd/getpw.c b/pwd/getpw.c
new file mode 100644
index 0000000..5bd5c82
--- /dev/null
+++ b/pwd/getpw.c
@@ -0,0 +1,48 @@
+/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA. */
+
+#include <ansidecl.h>
+#include <errno.h>
+#include <stdio.h>
+#include <pwd.h>
+
+
+/* Re-construct the password-file line for the given uid
+ in the given buffer. This knows the format that the caller
+ will expect, but this need not be the format of the password file. */
+int
+DEFUN(getpw, (uid, buf), __uid_t uid AND register char *buf)
+{
+ register struct passwd *p;
+
+ if (buf == NULL)
+ {
+ errno = EINVAL;
+ return -1;
+ }
+
+ p = getpwuid (uid);
+ if (p == NULL)
+ return -1;
+
+ if (sprintf (buf, "%s:%s:%u:%u:%s:%s:%s", p->pw_name, p->pw_passwd,
+ p->pw_uid, p->pw_gid, p->pw_gecos, p->pw_dir, p->pw_shell) < 0)
+ return -1;
+
+ return 0;
+}
diff --git a/pwd/getpwent.c b/pwd/getpwent.c
new file mode 100644
index 0000000..1c88950
--- /dev/null
+++ b/pwd/getpwent.c
@@ -0,0 +1,67 @@
+/* Copyright (C) 1991 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA. */
+
+#include <ansidecl.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <pwd.h>
+
+static FILE *stream = NULL;
+
+/* Rewind the stream. */
+void
+DEFUN_VOID(setpwent)
+{
+ if (stream != NULL)
+ rewind(stream);
+}
+
+
+/* Close the stream. */
+void
+DEFUN_VOID(endpwent)
+{
+ if (stream != NULL)
+ {
+ (void) fclose(stream);
+ stream = NULL;
+ }
+}
+
+
+/* Return one entry from the password file. */
+struct passwd *
+DEFUN_VOID(getpwent)
+{
+ static PTR info = NULL;
+ if (info == NULL)
+ {
+ info = __pwdalloc();
+ if (info == NULL)
+ return(NULL);
+ }
+
+ if (stream == NULL)
+ {
+ stream = __pwdopen();
+ if (stream == NULL)
+ return(NULL);
+ }
+
+ return(__pwdread(stream, info));
+}
diff --git a/pwd/getpwnam.c b/pwd/getpwnam.c
new file mode 100644
index 0000000..1e7ea5c
--- /dev/null
+++ b/pwd/getpwnam.c
@@ -0,0 +1,50 @@
+/* Copyright (C) 1991 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA. */
+
+#include <ansidecl.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <string.h>
+#include <pwd.h>
+
+/* Search for an entry with a matching name. */
+struct passwd *
+DEFUN(getpwnam, (name), register CONST char *name)
+{
+ static PTR info = NULL;
+ register FILE *stream;
+ register struct passwd *p;
+
+ if (info == NULL)
+ {
+ info = __pwdalloc();
+ if (info == NULL)
+ return(NULL);
+ }
+
+ stream = __pwdopen();
+ if (stream == NULL)
+ return(NULL);
+
+ while ((p = __pwdread(stream, info)) != NULL)
+ if (!strcmp(p->pw_name, name))
+ break;
+
+ (void) fclose(stream);
+ return(p);
+}
diff --git a/pwd/getpwuid.c b/pwd/getpwuid.c
new file mode 100644
index 0000000..30e4032
--- /dev/null
+++ b/pwd/getpwuid.c
@@ -0,0 +1,50 @@
+/* Copyright (C) 1991 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA. */
+
+#include <ansidecl.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <pwd.h>
+#include <sys/types.h>
+
+/* Search for an entry with a matching uid. */
+struct passwd *
+DEFUN(getpwuid, (uid), register uid_t uid)
+{
+ static PTR info;
+ register FILE *stream;
+ register struct passwd *p;
+
+ if (info == NULL)
+ {
+ info = __pwdalloc();
+ if (info == NULL)
+ return(NULL);
+ }
+
+ stream = __pwdopen();
+ if (stream == NULL)
+ return(NULL);
+
+ while ((p = __pwdread(stream, info)) != NULL)
+ if (p->pw_uid == uid)
+ break;
+
+ (void) fclose(stream);
+ return(p);
+}
diff --git a/pwd/putpwent.c b/pwd/putpwent.c
new file mode 100644
index 0000000..ad8a81b
--- /dev/null
+++ b/pwd/putpwent.c
@@ -0,0 +1,43 @@
+/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA. */
+
+#include <ansidecl.h>
+#include <errno.h>
+#include <stdio.h>
+#include <pwd.h>
+
+
+/* Write an entry to the given stream.
+ This must know the format of the password file. */
+int
+DEFUN(putpwent, (p, stream), register CONST struct passwd *p AND FILE *stream)
+{
+ if (p == NULL || stream == NULL)
+ {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (fprintf (stream, "%s:%s:%u:%u:%s:%s:%s\n",
+ p->pw_name, p->pw_passwd,
+ p->pw_uid, p->pw_gid,
+ p->pw_gecos, p->pw_dir, p->pw_shell) < 0)
+ return(-1);
+
+ return(0);
+}
diff --git a/pwd/pwd.h b/pwd/pwd.h
new file mode 100644
index 0000000..c72c37d
--- /dev/null
+++ b/pwd/pwd.h
@@ -0,0 +1,92 @@
+/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB. If
+not, write to the, 1992 Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA. */
+
+/*
+ * POSIX Standard: 9.2.2 User Database Access <pwd.h>
+ */
+
+#ifndef _PWD_H
+
+#define _PWD_H 1
+#include <features.h>
+
+__BEGIN_DECLS
+
+#include <gnu/types.h>
+
+
+/* The passwd structure. */
+struct passwd
+{
+ char *pw_name; /* Username. */
+ char *pw_passwd; /* Password. */
+ __uid_t pw_uid; /* User ID. */
+ __gid_t pw_gid; /* Group ID. */
+ char *pw_gecos; /* Real name. */
+ char *pw_dir; /* Home directory. */
+ char *pw_shell; /* Shell program. */
+};
+
+
+#if defined(__USE_SVID) || defined(__USE_GNU)
+#define __need_FILE
+#include <stdio.h>
+#endif
+
+#ifdef __USE_GNU
+/* Return a new stream open on the password file. */
+extern FILE *__pwdopen __P ((void));
+
+/* Read a password entry from STREAM, filling in P.
+ Return the `struct passwd' of P if successful, NULL on failure. */
+extern struct passwd *__pwdread __P ((FILE * __stream, __ptr_t __p));
+
+/* Return a chunk of memory containing pre-initialized data for __pwdread. */
+extern __ptr_t __pwdalloc __P ((void));
+#endif
+
+
+#if defined(__USE_SVID) || defined(__USE_MISC)
+/* Rewind the password-file stream. */
+extern void setpwent __P ((void));
+
+/* Close the password-file stream. */
+extern void endpwent __P ((void));
+
+/* Read an entry from the password-file stream, opening it if necessary. */
+extern struct passwd *getpwent __P ((void));
+#endif
+
+#ifdef __USE_SVID
+/* Read an entry from STREAM. */
+extern struct passwd *fgetpwent __P ((FILE * __stream));
+
+/* Write the given entry onto the given stream. */
+extern int putpwent __P ((__const struct passwd * __p, FILE * __f));
+#endif
+
+/* Search for an entry with a matching user ID. */
+extern struct passwd *getpwuid __P ((__uid_t __uid));
+
+/* Search for an entry with a matching username. */
+extern struct passwd *getpwnam __P ((__const char *__name));
+
+
+__END_DECLS
+
+#endif /* pwd.h */
diff --git a/pwd/pwdopen.c b/pwd/pwdopen.c
new file mode 100644
index 0000000..6d35ab9
--- /dev/null
+++ b/pwd/pwdopen.c
@@ -0,0 +1,28 @@
+/* Copyright (C) 1991 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA. */
+
+#include <ansidecl.h>
+#include <stdio.h>
+#include <pwd.h>
+
+/* Return a new stream open on the password file. */
+FILE *
+DEFUN_VOID(__pwdopen)
+{
+ return(fopen("/etc/passwd", "r"));
+}
diff --git a/pwd/pwdread.c b/pwd/pwdread.c
new file mode 100644
index 0000000..0ce27d7
--- /dev/null
+++ b/pwd/pwdread.c
@@ -0,0 +1,116 @@
+/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
+This file is part of the GNU C Library.
+
+The GNU C Library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Library General Public License as
+published by the Free Software Foundation; either version 2 of the
+License, or (at your option) any later version.
+
+The GNU C Library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+Library General Public License for more details.
+
+You should have received a copy of the GNU Library General Public
+License along with the GNU C Library; see the file COPYING.LIB. If
+not, write to the Free Software Foundation, Inc., 675 Mass Ave,
+Cambridge, MA 02139, USA. */
+
+#include <ansidecl.h>
+#include <errno.h>
+#include <limits.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pwd.h>
+#include <sys/types.h>
+
+
+/* This is the function that all the others are based on.
+ The format of the password file is known only here. */
+
+/* Structure containing info kept by each __pwdread caller. */
+typedef struct
+ {
+ char *buf;
+ size_t buflen;
+ struct passwd p;
+ } pwdread_info;
+
+
+/* Return a chunk of memory containing a pre-initialized `pwdread_info'. */
+PTR
+DEFUN_VOID(__pwdalloc)
+{
+ pwdread_info *info = (PTR) malloc (sizeof(pwdread_info));
+ if (info == NULL)
+ return NULL;
+ info->buf = NULL;
+ info->buflen = 0;
+ return info;
+}
+
+/* Read a password entry from STREAM, filling in P. */
+struct passwd *
+DEFUN(__pwdread, (stream, p), FILE *stream AND PTR CONST p)
+{
+ register pwdread_info *CONST info = (pwdread_info *) p;
+ char *start, *end;
+
+ /* Idiocy checks. */
+ if (stream == NULL)
+ {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ do
+ if (__getline (&info->buf, &info->buflen, stream) == -1)
+ return NULL;
+ while (info->buf[0] == '#');
+
+ start = info->buf;
+ end = strchr (start, ':');
+ if (end == NULL)
+ return NULL;
+ *end = '\0';
+ info->p.pw_name = start;
+
+ start = end + 1;
+ end = strchr (start, ':');
+ if (end == NULL)
+ return NULL;
+ *end = '\0';
+ info->p.pw_passwd = start;
+
+ info->p.pw_uid = (uid_t) strtol (end + 1, &end, 10);
+ if (*end != ':')
+ return NULL;
+ info->p.pw_gid = (gid_t) strtol (end + 1, &end, 10);
+ if (*end != ':')
+ return NULL;
+
+ start = end + 1;
+ end = strchr (start, ':');
+ if (end == NULL)
+ return NULL;
+ *end = '\0';
+ info->p.pw_gecos = start;
+
+ start = end + 1;
+ end = strchr (start, ':');
+ if (end == NULL)
+ return NULL;
+ *end = '\0';
+ info->p.pw_dir = start;
+
+ start = end + 1;
+ end = strchr (start, '\n');
+ if (end == NULL)
+ return NULL;
+ *end = '\0';
+ info->p.pw_shell = start;
+
+ return &info->p;
+}