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

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [newlib/] [newlib/] [libc/] [unix/] [getpwent.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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