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 static char *lookup_env (char **); 26 27 /* where people might scribble their name into the environment ... */ 28 29 static char *login_strings[] = 30 { 31 "LOGIN", "USER", "MAILNAME", (char *) 0 32 }; 33 34 static char *group_strings[] = 35 { 36 "GROUP", (char *) 0 37 }; 38 39 40 static char *anonymous = "anonymous"; /* if all else fails ... */ 41 42 static char *home_dir = "."; /* we feel (no|every)where at home */ 43 static char *login_shell = "not cmd.exe!"; 44 45 static char *login = (char *) 0;/* cache the names here */ 46 static char *group = (char *) 0; 47 48 static struct passwd pw; /* should we return a malloc()'d structure */ 49 static struct group gr; /* instead of pointers to static structures? */ 50 51 /* return something like a username in a (butchered!) passwd structure. */ 52 struct passwd * 53 getpwuid (int uid) 54 { 55 pw.pw_name = getlogin (); 56 pw.pw_dir = home_dir; 57 pw.pw_shell = login_shell; 58 pw.pw_uid = 0; 59 60 return &pw; 61 } 62 63 struct passwd * 64 getpwnam (char *name) 65 { 66 return (struct passwd *) 0; 67 } 68 69 /* return something like a groupname in a (butchered!) group structure. */ 70 struct group * 71 getgrgid (int uid) 72 { 73 gr.gr_name = getgr_name (); 74 gr.gr_gid = 0; 75 76 return &gr; 77 } 78 79 struct group * 80 getgrnam (char *name) 81 { 82 return (struct group *) 0; 83 } 84 85 /* return something like a username. */ 86 char * 87 getlogin () 88 { 89 if (!login) /* have we been called before? */ 90 login = lookup_env (login_strings); 91 92 if (!login) /* have we been successful? */ 93 login = anonymous; 94 95 return login; 96 } 97 98 /* return something like a group. */ 99 char * 100 getgr_name () 101 { 102 if (!group) /* have we been called before? */ 103 group = lookup_env (group_strings); 104 105 if (!group) /* have we been successful? */ 106 group = anonymous; 107 108 return group; 109 } 110 111 /* return something like a uid. */ 112 int 113 getuid () 114 { 115 return 0; /* every user is a super user ... */ 116 } 117 118 int 119 getgid () 120 { 121 return 0; 122 } 123 124 int 125 geteuid () 126 { 127 return 0; 128 } 129 130 int 131 getegid () 132 { 133 return 0; 134 } 135 136 struct passwd * 137 getpwent () 138 { 139 return (struct passwd *) 0; 140 } 141 142 void 143 setpwent () 144 { 145 } 146 147 void 148 endpwent () 149 { 150 } 151 152 void 153 endgrent () 154 { 155 } 156 157 /* return groups. */ 158 int 159 getgroups (int ngroups, int *groups) 160 { 161 *groups = 0; 162 return 1; 163 } 164 165 /* lookup environment. */ 166 static char * 167 lookup_env (char *table[]) 168 { 169 char *ptr; 170 char *entry; 171 size_t len; 172 173 while (*table && !(ptr = getenv (*table++))) ; /* scan table */ 174 175 if (!ptr) 176 return (char *) 0; 177 178 len = strcspn (ptr, " \n\t\n\r"); /* any WS? */ 179 if (!(entry = malloc (len + 1))) 180 { 181 fprintf (stderr, "Out of memory.\nStop."); 182 exit (-1); 183 } 184 185 strncpy (entry, ptr, len); 186 entry[len] = '\0'; 187 188 return entry; 189 190 } 191 192 /* 193 * Local Variables: 194 * mode:C 195 * ChangeLog:ChangeLog 196 * compile-command:make 197 * End: 198 */ 199