Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / unix / getpwent.c
blob0f13a20f7b7b36903067a947aea55c8deecf056f
1 #ifndef _NO_GETPWENT
3 #include <stdio.h>
4 #include <sys/types.h>
5 #include <pwd.h>
6 #include <errno.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <limits.h>
12 static struct passwd pw_passwd; /* password structure */
13 static FILE *passwd_fp;
15 static char logname[8];
16 static char password[1024];
17 static char gecos[1024];
18 static char dir[1024];
19 static char shell[1024];
21 struct passwd *
22 getpwnam (name)
23 const char *name;
25 FILE *fp;
26 char buf[1024];
28 if ((fp = fopen ("/etc/passwd", "r")) == NULL)
30 return NULL;
33 while (fgets (buf, sizeof (buf), fp))
35 sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n",
36 logname, password, &pw_passwd.pw_uid,
37 &pw_passwd.pw_gid, gecos,
38 dir, shell);
39 pw_passwd.pw_name = logname;
40 pw_passwd.pw_passwd = password;
41 pw_passwd.pw_comment = "";
42 pw_passwd.pw_gecos = gecos;
43 pw_passwd.pw_dir = dir;
44 pw_passwd.pw_shell = shell;
46 if (!strcmp (logname, name))
48 fclose (fp);
49 return &pw_passwd;
52 fclose (fp);
53 return NULL;
56 struct passwd *
57 getpwuid (uid_t uid)
59 FILE *fp;
60 char buf[1024];
62 if ((fp = fopen ("/etc/passwd", "r")) == NULL)
64 return NULL;
67 while (fgets (buf, sizeof (buf), fp))
69 sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n",
70 logname, password, &pw_passwd.pw_uid,
71 &pw_passwd.pw_gid, gecos,
72 dir, shell);
73 pw_passwd.pw_name = logname;
74 pw_passwd.pw_passwd = password;
75 pw_passwd.pw_comment = "";
76 pw_passwd.pw_gecos = gecos;
77 pw_passwd.pw_dir = dir;
78 pw_passwd.pw_shell = shell;
80 if (uid == pw_passwd.pw_uid)
82 fclose (fp);
83 return &pw_passwd;
86 fclose (fp);
87 return NULL;
90 struct passwd *
91 getpwent ()
93 char buf[1024];
95 if (passwd_fp == NULL)
96 return NULL;
98 if (fgets (buf, sizeof (buf), passwd_fp) == NULL)
99 return NULL;
101 sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n",
102 logname, password, &pw_passwd.pw_uid,
103 &pw_passwd.pw_gid, gecos,
104 dir, shell);
105 pw_passwd.pw_name = logname;
106 pw_passwd.pw_passwd = password;
107 pw_passwd.pw_comment = "";
108 pw_passwd.pw_gecos = gecos;
109 pw_passwd.pw_dir = dir;
110 pw_passwd.pw_shell = shell;
112 return &pw_passwd;
115 void
116 setpwent ()
118 if (passwd_fp != NULL)
119 fclose (passwd_fp);
121 passwd_fp = fopen ("/etc/passwd", "r");
124 void
125 endpwent ()
127 if (passwd_fp != NULL)
128 fclose (passwd_fp);
131 #endif /* !_NO_GETPWENT */