OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [unix/] [getpwent.c] - Blame information for rev 1010

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1010 ivang
#include <stdio.h>
2
#include <sys/types.h>
3
#include <pwd.h>
4
#include <errno.h>
5
#include <unistd.h>
6
#include <stdlib.h>
7
#include <string.h>
8
#include <limits.h>
9
 
10
static struct passwd pw_passwd; /* password structure */
11
static FILE *passwd_fp;
12
 
13
static char logname[8];
14
static char password[1024];
15
static char gecos[1024];
16
static char dir[1024];
17
static char shell[1024];
18
 
19
struct passwd *
20
getpwnam (name)
21
     const char *name;
22
{
23
  FILE *fp;
24
  char buf[1024];
25
 
26
  if ((fp = fopen ("/etc/passwd", "r")) == NULL)
27
    {
28
      return NULL;
29
    }
30
 
31
  while (fgets (buf, sizeof (buf), fp))
32
    {
33
      sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n",
34
              logname, password, &pw_passwd.pw_uid,
35
             &pw_passwd.pw_gid, gecos,
36
              dir, shell);
37
      pw_passwd.pw_name = logname;
38
      pw_passwd.pw_passwd = password;
39
      pw_passwd.pw_comment = "";
40
      pw_passwd.pw_gecos = gecos;
41
      pw_passwd.pw_dir = dir;
42
      pw_passwd.pw_shell = shell;
43
 
44
      if (!strcmp (logname, name))
45
        {
46
          fclose (fp);
47
          return &pw_passwd;
48
        }
49
    }
50
  fclose (fp);
51
  return NULL;
52
}
53
 
54
struct passwd *
55
getpwuid (uid_t uid)
56
{
57
  FILE *fp;
58
  char buf[1024];
59
 
60
  if ((fp = fopen ("/etc/passwd", "r")) == NULL)
61
    {
62
      return NULL;
63
    }
64
 
65
  while (fgets (buf, sizeof (buf), fp))
66
    {
67
      sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n",
68
              logname, password, &pw_passwd.pw_uid,
69
             &pw_passwd.pw_gid, gecos,
70
              dir, shell);
71
      pw_passwd.pw_name = logname;
72
      pw_passwd.pw_passwd = password;
73
      pw_passwd.pw_comment = "";
74
      pw_passwd.pw_gecos = gecos;
75
      pw_passwd.pw_dir = dir;
76
      pw_passwd.pw_shell = shell;
77
 
78
      if (uid == pw_passwd.pw_uid)
79
        {
80
          fclose (fp);
81
          return &pw_passwd;
82
        }
83
    }
84
  fclose (fp);
85
  return NULL;
86
}
87
 
88
struct passwd *
89
getpwent ()
90
{
91
  char buf[1024];
92
 
93
  if (passwd_fp == NULL)
94
    return NULL;
95
 
96
  if (fgets (buf, sizeof (buf), passwd_fp) == NULL)
97
    return NULL;
98
 
99
  sscanf (buf, "%[^:]:%[^:]:%d:%d:%[^:]:%[^:]:%s\n",
100
          logname, password, &pw_passwd.pw_uid,
101
         &pw_passwd.pw_gid, gecos,
102
          dir, shell);
103
  pw_passwd.pw_name = logname;
104
  pw_passwd.pw_passwd = password;
105
  pw_passwd.pw_comment = "";
106
  pw_passwd.pw_gecos = gecos;
107
  pw_passwd.pw_dir = dir;
108
  pw_passwd.pw_shell = shell;
109
 
110
  return &pw_passwd;
111
}
112
 
113
void
114
setpwent ()
115
{
116
  if (passwd_fp != NULL)
117
    fclose (passwd_fp);
118
 
119
  passwd_fp = fopen ("/etc/passwd", "r");
120
}
121
 
122
void
123
endpwent ()
124
{
125
  if (passwd_fp != NULL)
126
    fclose (passwd_fp);
127
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.