113571821Stholo /* pwd.c - Try to approximate UN*X's getuser...() functions under MS-DOS.
213571821Stholo Copyright (C) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet
313571821Stholo
413571821Stholo This program is free software; you can redistribute it and/or modify
513571821Stholo it under the terms of the GNU General Public License as published by
613571821Stholo the Free Software Foundation; either version 1, or (at your option)
713571821Stholo any later version.
813571821Stholo
913571821Stholo This program is distributed in the hope that it will be useful,
1013571821Stholo but WITHOUT ANY WARRANTY; without even the implied warranty of
1113571821Stholo MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12*2286d8edStholo GNU General Public License for more details. */
1313571821Stholo
1413571821Stholo /* This 'implementation' is conjectured from the use of this functions in
1513571821Stholo the RCS and BASH distributions. Of course these functions don't do too
1613571821Stholo much useful things under MS-DOS, but using them avoids many "#ifdef
1713571821Stholo MSDOS" in ported UN*X code ... */
1813571821Stholo
1913571821Stholo
2013571821Stholo #include <stdio.h>
2113571821Stholo #include <stdlib.h>
2213571821Stholo #include <string.h>
2313571821Stholo #include <pwd.h>
2413571821Stholo
2513571821Stholo static char *lookup_env (char **);
2613571821Stholo
2713571821Stholo /* where people might scribble their name into the environment ... */
2813571821Stholo
2913571821Stholo static char *login_strings[] =
3013571821Stholo {
3113571821Stholo "LOGIN", "USER", "MAILNAME", (char *) 0
3213571821Stholo };
3313571821Stholo
3413571821Stholo static char *group_strings[] =
3513571821Stholo {
3613571821Stholo "GROUP", (char *) 0
3713571821Stholo };
3813571821Stholo
3913571821Stholo
4013571821Stholo static char *anonymous = "anonymous"; /* if all else fails ... */
4113571821Stholo
4213571821Stholo static char *home_dir = "."; /* we feel (no|every)where at home */
4313571821Stholo static char *login_shell = "not cmd.exe!";
4413571821Stholo
4513571821Stholo static char *login = (char *) 0;/* cache the names here */
4613571821Stholo static char *group = (char *) 0;
4713571821Stholo
4813571821Stholo static struct passwd pw; /* should we return a malloc()'d structure */
4913571821Stholo static struct group gr; /* instead of pointers to static structures? */
5013571821Stholo
5113571821Stholo /* return something like a username in a (butchered!) passwd structure. */
5213571821Stholo struct passwd *
getpwuid(int uid)5313571821Stholo getpwuid (int uid)
5413571821Stholo {
5513571821Stholo pw.pw_name = getlogin ();
5613571821Stholo pw.pw_dir = home_dir;
5713571821Stholo pw.pw_shell = login_shell;
5813571821Stholo pw.pw_uid = 0;
5913571821Stholo
6013571821Stholo return &pw;
6113571821Stholo }
6213571821Stholo
6313571821Stholo struct passwd *
getpwnam(char * name)6413571821Stholo getpwnam (char *name)
6513571821Stholo {
6613571821Stholo return (struct passwd *) 0;
6713571821Stholo }
6813571821Stholo
6913571821Stholo /* return something like a groupname in a (butchered!) group structure. */
7013571821Stholo struct group *
getgrgid(int uid)7113571821Stholo getgrgid (int uid)
7213571821Stholo {
7313571821Stholo gr.gr_name = getgr_name ();
7413571821Stholo gr.gr_gid = 0;
7513571821Stholo
7613571821Stholo return &gr;
7713571821Stholo }
7813571821Stholo
7913571821Stholo struct group *
getgrnam(char * name)8013571821Stholo getgrnam (char *name)
8113571821Stholo {
8213571821Stholo return (struct group *) 0;
8313571821Stholo }
8413571821Stholo
8513571821Stholo /* return something like a username. */
8613571821Stholo char *
getlogin()8713571821Stholo getlogin ()
8813571821Stholo {
8913571821Stholo if (!login) /* have we been called before? */
9013571821Stholo login = lookup_env (login_strings);
9113571821Stholo
9213571821Stholo if (!login) /* have we been successful? */
9313571821Stholo login = anonymous;
9413571821Stholo
9513571821Stholo return login;
9613571821Stholo }
9713571821Stholo
9813571821Stholo /* return something like a group. */
9913571821Stholo char *
getgr_name()10013571821Stholo getgr_name ()
10113571821Stholo {
10213571821Stholo if (!group) /* have we been called before? */
10313571821Stholo group = lookup_env (group_strings);
10413571821Stholo
10513571821Stholo if (!group) /* have we been successful? */
10613571821Stholo group = anonymous;
10713571821Stholo
10813571821Stholo return group;
10913571821Stholo }
11013571821Stholo
11113571821Stholo /* return something like a uid. */
11213571821Stholo int
getuid()11313571821Stholo getuid ()
11413571821Stholo {
11513571821Stholo return 0; /* every user is a super user ... */
11613571821Stholo }
11713571821Stholo
11813571821Stholo int
getgid()11913571821Stholo getgid ()
12013571821Stholo {
12113571821Stholo return 0;
12213571821Stholo }
12313571821Stholo
12413571821Stholo int
geteuid()12513571821Stholo geteuid ()
12613571821Stholo {
12713571821Stholo return 0;
12813571821Stholo }
12913571821Stholo
13013571821Stholo int
getegid()13113571821Stholo getegid ()
13213571821Stholo {
13313571821Stholo return 0;
13413571821Stholo }
13513571821Stholo
13613571821Stholo struct passwd *
getpwent()13713571821Stholo getpwent ()
13813571821Stholo {
13913571821Stholo return (struct passwd *) 0;
14013571821Stholo }
14113571821Stholo
14213571821Stholo void
setpwent()14313571821Stholo setpwent ()
14413571821Stholo {
14513571821Stholo }
14613571821Stholo
14713571821Stholo void
endpwent()14813571821Stholo endpwent ()
14913571821Stholo {
15013571821Stholo }
15113571821Stholo
15213571821Stholo void
endgrent()15313571821Stholo endgrent ()
15413571821Stholo {
15513571821Stholo }
15613571821Stholo
15713571821Stholo /* return groups. */
15813571821Stholo int
getgroups(int ngroups,int * groups)15913571821Stholo getgroups (int ngroups, int *groups)
16013571821Stholo {
16113571821Stholo *groups = 0;
16213571821Stholo return 1;
16313571821Stholo }
16413571821Stholo
16513571821Stholo /* lookup environment. */
16613571821Stholo static char *
lookup_env(char * table[])16713571821Stholo lookup_env (char *table[])
16813571821Stholo {
16913571821Stholo char *ptr;
17013571821Stholo char *entry;
17113571821Stholo size_t len;
17213571821Stholo
17313571821Stholo while (*table && !(ptr = getenv (*table++))) ; /* scan table */
17413571821Stholo
17513571821Stholo if (!ptr)
17613571821Stholo return (char *) 0;
17713571821Stholo
17813571821Stholo len = strcspn (ptr, " \n\t\n\r"); /* any WS? */
17913571821Stholo if (!(entry = malloc (len + 1)))
18013571821Stholo {
18113571821Stholo fprintf (stderr, "Out of memory.\nStop.");
18213571821Stholo exit (-1);
18313571821Stholo }
18413571821Stholo
18513571821Stholo strncpy (entry, ptr, len);
18613571821Stholo entry[len] = '\0';
18713571821Stholo
18813571821Stholo return entry;
18913571821Stholo
19013571821Stholo }
19113571821Stholo
19213571821Stholo /*
19313571821Stholo * Local Variables:
19413571821Stholo * mode:C
19513571821Stholo * ChangeLog:ChangeLog
19613571821Stholo * compile-command:make
19713571821Stholo * End:
19813571821Stholo */
199