File: passwd.c

package info (click to toggle)
ytree 1.94-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 796 kB
  • ctags: 1,085
  • sloc: ansic: 14,373; makefile: 208
file content (157 lines) | stat: -rw-r--r-- 2,460 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/***************************************************************************
 *
 * $Header: /usr/local/cvsroot/utils/ytree/passwd.c,v 1.13 2007/04/01 13:33:36 werner Exp $
 *
 * Handhabung von User-Nummern / Namen
 *
 ***************************************************************************/


#include "ytree.h"


#ifdef WIN32

#include <pwd.h>

#else

typedef struct
{
  int   uid;
  char  name[OWNER_NAME_MAX + 1];
} PasswdEntry;


extern struct passwd *getpwent(void);
#if !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined( __FreeBSD__ ) && !defined( OSF1 ) && !defined( __APPLE__ )
extern void          setpwent(void);
#endif

static PasswdEntry   *passwd_array;
static unsigned int  passwd_count;

#endif /* WIN32 */


int ReadPasswdEntries(void)
{
#ifndef WIN32

  int i;
  struct passwd *pwd_ptr;
  

  for( passwd_count=0; getpwent(); passwd_count++ )
    ;

  setpwent();

  if( passwd_array ) 
  {
    free( passwd_array );
    passwd_array = NULL;
  }

  if( passwd_count == 0 )
  {
    passwd_array = NULL;
  }
  else
  {
    if( ( passwd_array = (PasswdEntry *) calloc( passwd_count, 
					         sizeof( PasswdEntry )
					       ) ) == NULL )
    {
      ERROR_MSG( "Calloc Failed" );
      passwd_array = NULL;
      passwd_count = 0;
      return( 1 );
    }
  }

  for(i=0; i < (int)passwd_count; i++)
  {
    if( ( pwd_ptr = getpwent() ) == NULL )
    {
      ERROR_MSG( "Getpwent Failed" );
      if( passwd_array) free( passwd_array );
      passwd_array = NULL;
      passwd_count = 0;
      return( 1 );
    }
  
    passwd_array[i].uid = pwd_ptr->pw_uid;
    (void) strcpy( passwd_array[i].name, pwd_ptr->pw_name );
  }

#endif /* WIN32 */

  return( 0 );
}




char *GetPasswdName(unsigned int uid)
{

#ifdef WIN32

  struct passwd *pwd_ptr;

  pwd_ptr = getpwuid( uid );

  if( pwd_ptr ) return( pwd_ptr->pw_name );
  else          return( NULL );

#else

  int i;

  for( i=0; i < (int)passwd_count; i++ )
  {
    if( passwd_array[i].uid == (int)uid )
      return( passwd_array[i].name );
  }

  return( NULL );

#endif /* WIN32 */

}




int GetPasswdUid(char *name)
{
#ifdef WIN32

  struct passwd *pwd_ptr;

  pwd_ptr = getpwnam( name );

  if( pwd_ptr ) return( pwd_ptr->pw_uid );
  else          return( -1 );

#else

  int i;

  for( i=0; i < (int)passwd_count; i++ )
  {
    if( !strcmp( name, passwd_array[i].name ) )
      return( (int) passwd_array[i].uid );
  }

  return( -1 );

#endif /* WIN32 */

}