xref: /openbsd/gnu/usr.bin/cvs/windows-NT/pwd.c (revision 404b540a)
1 /*  pwd.c - Try to approximate UN*X's getuser...() functions under MS-DOS.
2     Copyright (C) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet
3 
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 1, or (at your option)
7     any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.  */
13 
14 /* This 'implementation' is conjectured from the use of this functions in
15    the RCS and BASH distributions.  Of course these functions don't do too
16    much useful things under MS-DOS, but using them avoids many "#ifdef
17    MSDOS" in ported UN*X code ...  */
18 
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <pwd.h>
24 
25 char* win32getlogin();
26 static char *lookup_env (char **);
27 
28 /* where people might scribble their name into the environment ... */
29 
30 static char *login_strings[] =
31 {
32   "LOGIN", "USER", "MAILNAME", (char *) 0
33 };
34 
35 static char *group_strings[] =
36 {
37   "GROUP", (char *) 0
38 };
39 
40 
41 static char *anonymous = "anonymous";	/* if all else fails ... */
42 
43 static char *home_dir = ".";	/* we feel (no|every)where at home */
44 static char *login_shell = "not command.com!";
45 
46 static char *login = (char *) 0;/* cache the names here	*/
47 static char *group = (char *) 0;
48 
49 static struct passwd pw;	/* should we return a malloc()'d structure   */
50 static struct group gr;		/* instead of pointers to static structures? */
51 
52 /* return something like a username in a (butchered!) passwd structure. */
53 struct passwd *
54 getpwuid (int uid)
55 {
56   pw.pw_name = getlogin ();
57   pw.pw_dir = home_dir;
58   pw.pw_shell = login_shell;
59   pw.pw_uid = 0;
60 
61   return &pw;
62 }
63 
64 struct passwd *
65 getpwnam (char *name)
66 {
67   return (struct passwd *) 0;
68 }
69 
70 /* return something like a groupname in a (butchered!) group structure. */
71 struct group *
72 getgrgid (int uid)
73 {
74   gr.gr_name = getgr_name ();
75   gr.gr_gid = 0;
76 
77   return &gr;
78 }
79 
80 struct group *
81 getgrnam (char *name)
82 {
83   return (struct group *) 0;
84 }
85 
86 /* return something like a username. */
87 char *
88 getlogin ()
89 {
90   /* This is how a windows user would override their login name. */
91   if (!login)
92     login = lookup_env (login_strings);
93 
94   /* In the absence of user override, ask the operating system. */
95   if (!login)
96      login = win32getlogin();
97 
98   /* If all else fails, fall back on Old Faithful. */
99   if (!login)
100     login = anonymous;
101 
102   return login;
103 }
104 
105 /* return something like a group.  */
106 char *
107 getgr_name ()
108 {
109   if (!group)			/* have we been called before? */
110     group = lookup_env (group_strings);
111 
112   if (!group)			/* have we been successful? */
113     group = anonymous;
114 
115   return group;
116 }
117 
118 /* return something like a uid.  */
119 int
120 getuid ()
121 {
122   return 0;			/* every user is a super user ... */
123 }
124 
125 int
126 getgid ()
127 {
128   return 0;
129 }
130 
131 int
132 geteuid ()
133 {
134   return 0;
135 }
136 
137 int
138 getegid ()
139 {
140   return 0;
141 }
142 
143 struct passwd *
144 getpwent ()
145 {
146   return (struct passwd *) 0;
147 }
148 
149 void
150 setpwent ()
151 {
152 }
153 
154 void
155 endpwent ()
156 {
157 }
158 
159 void
160 endgrent ()
161 {
162 }
163 
164 /* return groups.  */
165 int
166 getgroups (int ngroups, int *groups)
167 {
168   *groups = 0;
169   return 1;
170 }
171 
172 /* lookup environment.  */
173 static char *
174 lookup_env (char *table[])
175 {
176   char *ptr;
177   char *entry;
178   size_t len;
179 
180   while (*table && !(ptr = getenv (*table++))) ;	/* scan table */
181 
182   if (!ptr)
183     return (char *) 0;
184 
185   len = strcspn (ptr, " \n\t\n\r");	/* any WS? 	  */
186   if (!(entry = malloc (len + 1)))
187     {
188       fprintf (stderr, "Out of memory.\nStop.");
189       exit (-1);
190     }
191 
192   strncpy (entry, ptr, len);
193   entry[len] = '\0';
194 
195   return entry;
196 
197 }
198 
199 /*
200  * Local Variables:
201  * mode:C
202  * ChangeLog:ChangeLog
203  * compile-command:make
204  * End:
205  */
206