summaryrefslogtreecommitdiff
path: root/windows-NT/pwd.c
blob: 36e6148c41d93d9a44653d45bfbb7275cfe1d4dd (plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*  pwd.c - Try to approximate UN*X's getuser...() functions under MS-DOS.
    Copyright (C) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 1, or (at your option)
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.  */

/* This 'implementation' is conjectured from the use of this functions in
   the RCS and BASH distributions.  Of course these functions don't do too
   much useful things under MS-DOS, but using them avoids many "#ifdef
   MSDOS" in ported UN*X code ...  */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "pwd.h"
#include "xalloc.h"

static char *lookup_env (char **);

/* where people might scribble their name into the environment ... */

static char *login_strings[] =
{
  "LOGIN", "USER", "MAILNAME", "USERNAME", NULL
};

static char *group_strings[] =
{
  "GROUP", NULL
};


static char *anonymous = "anonymous";	/* if all else fails ... */

static char *login = NULL;/* cache the names here	*/
static char *group = NULL;

static struct passwd pw;	/* should we return a malloc()'d structure   */
static struct group gr;		/* instead of pointers to static structures? */

/* implement limited uid behavior */
#define my_fake_uid ((const uid_t) 4545)
#define my_fake_gid my_fake_uid

/* return something like a username in a (butchered!) passwd structure. */
struct passwd *
getpwuid (int uid)
{
  pw.pw_name = getlogin ();
  pw.pw_dir = woe32_home_dir ();
  pw.pw_shell = woe32_shell ();
  pw.pw_uid = 0;

  return &pw;
}

struct passwd *
getpwnam (const char *name)
{
  return NULL;
}

/* return something like a groupname in a (butchered!) group structure. */
struct group *
getgrgid (int uid)
{
  gr.gr_name = getgr_name ();
  gr.gr_gid = 0;

  return &gr;
}

struct group *
getgrnam (const char *name)
{
  return NULL;
}

/* return something like a username. */
char *
getlogin (void)
{
  /* This is how a windows user would override their login name. */
  if (!login)
    login = lookup_env (login_strings);

  /* In the absence of user override, ask the operating system. */
  if (!login)
     login = woe32_getlogin ();

  /* If all else fails, fall back on Old Faithful. */
  if (!login)
    login = anonymous;

  return login;
}

/* return something like a group.  */
char *
getgr_name (void)
{
  if (!group)			/* have we been called before? */
    group = lookup_env (group_strings);

  if (!group)			/* have we been successful? */
    group = anonymous;

  return group;
}

/* return something like a uid.  */
uid_t
getuid (void)
{
  return my_fake_uid;
}

gid_t
getgid (void)
{
  return my_fake_gid;
}

uid_t
geteuid (void)
{
  return my_fake_uid;
}

gid_t
getegid (void)
{
  return my_fake_gid;
}

struct passwd *
getpwent (void)
{
  return NULL;
}

void
setpwent (void)
{
}

void
endpwent (void)
{
}

void
endgrent (void)
{
}

/* return groups.  */
int
getgroups (int ngroups, gid_t *groups)
{
  if (ngroups > 0)
      *groups = my_fake_gid;
  return 1;
}

/* lookup environment.  */
static char *
lookup_env (char *table[])
{
  char *ptr;
  char *entry;
  size_t len;

  while (*table && !(ptr = getenv (*table++))) ;	/* scan table */

  if (!ptr) return NULL;

  len = strcspn (ptr, " \n\t\n\r");	/* any WS? 	  */
  if (!(entry = xmalloc (len + 1)))
    {
      fprintf (stderr, "Out of memory.\nStop.");
      exit (-1);
    }

  strncpy (entry, ptr, len);
  entry[len] = '\0';

  return entry;

}

/*
 * Local Variables:
 * mode:C
 * ChangeLog:ChangeLog
 * compile-command:make
 * End:
 */