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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uC-libc/] [getent/] [utent.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/* utent.c <ndf@linux.mit.edu> */
2
/* Let it be known that this is very possibly the worst standard ever.  HP-UX
3
   does one thing, someone else does another, linux another... If anyone
4
   actually has the standard, please send it to me.
5
 
6
   Note that because of the way this stupid stupid standard works, you
7
   have to call endutent() to close the file even if you've not called
8
   setutent -- getutid and family use the same file descriptor. */
9
 
10
#include <unistd.h>
11
#include <fcntl.h>
12
#include <paths.h>
13
#include <errno.h>
14
#include <string.h>
15
#include <utmp.h>
16
 
17
static const char * ut_name=_PATH_UTMP;
18
 
19
static int ut_fd=-1;
20
 
21
struct utmp *
22
__getutent(int utmp_fd)
23
{
24
  static struct utmp utmp;
25
  if (read(utmp_fd, (char *) &utmp, sizeof(struct utmp))!=sizeof(struct utmp))
26
    return NULL;
27
  return &utmp;
28
}
29
 
30
void
31
setutent(void)
32
{
33
  if (ut_fd!=-1)
34
    close(ut_fd);
35
  if ((ut_fd=open(ut_name, O_RDONLY))<0)
36
    {
37
      perror("setutent: Can't open utmp file");
38
      ut_fd=-1;
39
    }
40
}
41
 
42
void
43
endutent(void)
44
{
45
  if (ut_fd!=-1)
46
    close(ut_fd);
47
  ut_fd=-1;
48
}
49
 
50
struct utmp *
51
getutent(void)
52
{
53
  if (ut_fd==-1)
54
    setutent();
55
  if (ut_fd==-1)
56
    return NULL;
57
  return __getutent(ut_fd);
58
}
59
 
60
struct utmp *
61
getutid(struct utmp * utmp_entry)
62
{
63
  struct utmp * utmp;
64
 
65
  if (ut_fd==-1)
66
    setutent();
67
  if (ut_fd==-1)
68
    return NULL;
69
 
70
  while ((utmp=__getutent(ut_fd))!=NULL)
71
    {
72
      if ((utmp_entry->ut_type==RUN_LVL   ||
73
           utmp_entry->ut_type==BOOT_TIME ||
74
           utmp_entry->ut_type==NEW_TIME  ||
75
           utmp_entry->ut_type==OLD_TIME) &&
76
          utmp->ut_type==utmp_entry->ut_type)
77
        return utmp;
78
      if ((utmp_entry->ut_type==INIT_PROCESS ||
79
           utmp_entry->ut_type==DEAD_PROCESS ||
80
           utmp_entry->ut_type==LOGIN_PROCESS ||
81
           utmp_entry->ut_type==USER_PROCESS) &&
82
          !strcmp(utmp->ut_id, utmp_entry->ut_id))
83
        return utmp;
84
    }
85
 
86
  return NULL;
87
}
88
 
89
struct utmp *
90
getutline(struct utmp * utmp_entry)
91
{
92
  struct utmp * utmp;
93
 
94
  if (ut_fd==-1)
95
    setutent();
96
  if (ut_fd==-1)
97
    return NULL;
98
 
99
#if 0 /* This is driving me nuts.  It's not an implementation problem -
100
         it's a matter of how things _SHOULD_ behave.  Groan. */
101
  lseek(ut_fd, SEEK_CUR, -sizeof(struct utmp));
102
#endif
103
 
104
  while ((utmp=__getutent(ut_fd))!=NULL)
105
    {
106
      if ((utmp->ut_type==USER_PROCESS  ||
107
           utmp->ut_type==LOGIN_PROCESS) &&
108
          !strcmp(utmp->ut_line, utmp_entry->ut_line))
109
        return utmp;
110
    }
111
 
112
  return NULL;
113
}
114
 
115
struct utmp *
116
pututline(struct utmp * utmp_entry)
117
{
118
  struct utmp * ut;
119
 
120
  /* Ignore the return value.  That way, if they've already positioned
121
     the file pointer where they want it, everything will work out. */
122
  (void) lseek(ut_fd, (off_t) -sizeof(utmp_entry), SEEK_CUR);
123
 
124
  if ((ut=getutid(utmp_entry))!=NULL)
125
    {
126
      lseek(ut_fd, (off_t) -sizeof(utmp_entry), SEEK_CUR);
127
      if (write(ut_fd, (char *) utmp_entry, sizeof(utmp_entry))
128
          != sizeof(utmp_entry))
129
        return NULL;
130
    }
131
  else
132
    {
133
      lseek(ut_fd, (off_t) 0, SEEK_END);
134
      if (write(ut_fd, (char *) utmp_entry, sizeof(utmp_entry))
135
          != sizeof(utmp_entry))
136
        return NULL;
137
    }
138
 
139
  return utmp_entry;
140
}
141
 
142
void
143
utmpname(const char * new_ut_name)
144
{
145
  if (new_ut_name!=NULL)
146
    ut_name=new_ut_name;
147
 
148
  if (ut_fd!=-1)
149
    close(ut_fd);
150
}
151
 
152
 

powered by: WebSVN 2.1.0

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