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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
/* env.c
*/
/* This software is copyrighted as detailed in the LICENSE file. */
#include "EXTERN.h"
#include "common.h"
#include "init.h"
#include "final.h"
#include "util.h"
#include "util2.h"
#include "INTERN.h"
#include "env.h"
#include "env.ih"
#ifdef HAS_RES_INIT
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#endif
bool
env_init(tcbuf, lax)
char* tcbuf;
bool_int lax;
{
bool fully_successful = TRUE;
if ((homedir = getenv("HOME")) == NULL)
homedir = getenv("LOGDIR");
if ((tmpdir = getenv("TMPDIR")) == NULL)
tmpdir = getval("TMP","/tmp");
/* try to set loginName */
if (lax) {
loginName = getenv("USER");
if (!loginName)
loginName = getenv("LOGNAME");
}
#ifndef MSDOS
if (!lax || !loginName) {
loginName = getlogin();
if (loginName)
loginName = savestr(loginName);
}
#endif
/* Set realName, and maybe set loginName and homedir (if NULL). */
if (!setusername(tcbuf)) {
if (!loginName)
loginName = nullstr;
if (!realName)
realName = nullstr;
fully_successful = FALSE;
}
env_init2();
/* set phostname to the hostname of our local machine */
if (!setphostname(tcbuf))
fully_successful = FALSE;
#ifdef SUPPORT_NNTP
{
char* cp = getval("NETSPEED","5");
if (*cp == 'f')
netspeed = 10;
else if (*cp == 's')
netspeed = 1;
else {
netspeed = atoi(cp);
if (netspeed < 1)
netspeed = 1;
}
}
#endif
return fully_successful;
}
static void
env_init2()
{
if (dotdir) /* Avoid running multiple times. */
return;
if (!homedir)
homedir = "/";
dotdir = getval("DOTDIR",homedir);
trndir = savestr(filexp(getval("TRNDIR",TRNDIR)));
lib = savestr(filexp(NEWSLIB));
rnlib = savestr(filexp(PRIVLIB));
}
/* Set loginName to the user's login name and realName to the user's
** real name.
*/
bool
setusername(tmpbuf)
char* tmpbuf;
{
char* s;
char* c;
#ifdef HAS_GETPWENT
struct passwd* pwd;
if (loginName == NULL)
pwd = getpwuid(getuid());
else
pwd = getpwnam(loginName);
if (!pwd)
return 0;
if (!loginName)
loginName = savestr(pwd->pw_name);
if (!homedir)
homedir = savestr(pwd->pw_dir);
s = pwd->pw_gecos;
#else /* !HAS_GETPWENT */
int i;
if (getpw(getuid(), tmpbuf+1) != 0)
return 0;
if (!loginName) {
cpytill(buf,tmpbuf+1,':');
loginName = savestr(buf);
}
for (s = tmpbuf, i = GCOSFIELD-1; i; i--) {
if (s)
s = index(s+1,':');
}
if (!s)
return 0;
s = cpytill(tmpbuf,s+1,':');
if (!homedir) {
cpytill(buf,s+1,':');
homedir = savestr(buf);
}
s = tmpbuf;
#endif /* !HAS_GETPWENT */
#ifdef PASSNAMES
#ifdef BERKNAMES
#ifdef BERKJUNK
while (*s && !isalnum(*s) && *s != '&') s++;
#endif
if ((c = index(s, ',')) != NULL)
*c = '\0';
if ((c = index(s, ';')) != NULL)
*c = '\0';
s = cpytill(buf,s,'&');
if (*s == '&') { /* whoever thought this one up was */
c = buf + strlen(buf); /* in the middle of the night */
strcat(c,loginName); /* before the morning after */
strcat(c,s+1);
if (islower(*c))
*c = toupper(*c); /* gack and double gack */
}
realName = savestr(buf);
#else /* !BERKNAMES */
if ((c = index(s, '(')) != NULL)
*c = '\0';
if ((c = index(s, '-')) != NULL)
s = c;
realName = savestr(s);
#endif /* !BERKNAMES */
#else /* !PASSNAMES */
{
FILE* fp;
env_init2(); /* Make sure homedir/dotdir/etc. are set. */
if ((fp = fopen(filexp(FULLNAMEFILE),"r")) != NULL) {
fgets(buf,sizeof buf,fp);
fclose(fp);
buf[strlen(buf)-1] = '\0';
realName = savestr(buf);
}
else
s = "PUT_YOUR_NAME_HERE";
}
#endif /* !PASSNAMES */
#ifdef HAS_GETPWENT
endpwent();
#endif
return 1;
}
bool
setphostname(tmpbuf)
char* tmpbuf;
{
FILE* fp;
bool hostname_ok = TRUE;
/* Find the local hostname */
#ifdef HAS_GETHOSTNAME
gethostname(tmpbuf,TCBUF_SIZE);
#else
# ifdef HAS_UNAME
/* get sysname */
uname(&utsn);
strcpy(tmpbuf,utsn.nodename);
# else
# ifdef PHOSTCMD
{
FILE* popen();
FILE* pipefp = popen(PHOSTCMD,"r");
if (pipefp == NULL) {
printf("Can't find hostname\n");
finalize(1);
}
fgets(tmpbuf,TCBUF_SIZE,pipefp);
tmpbuf[strlen(tmpbuf)-1] = '\0'; /* wipe out newline */
pclose(pipefp);
}
# else
strcpy(tmpbuf, "!INVALID!");
# endif /* PHOSTCMD */
# endif /* HAS_UNAME */
#endif /* HAS_GETHOSTNAME */
localhost = savestr(tmpbuf);
/* Build the host name that goes in postings */
phostname = PHOSTNAME;
if (FILE_REF(phostname) || *phostname == '~') {
phostname = filexp(phostname);
if ((fp = fopen(phostname,"r")) == NULL)
strcpy(tmpbuf,".");
else {
fgets(tmpbuf,TCBUF_SIZE,fp);
fclose(fp);
phostname = tmpbuf + strlen(tmpbuf) - 1;
if (*phostname == '\n')
*phostname = '\0';
}
}
else
strcpy(tmpbuf,phostname);
if (*tmpbuf == '.') {
if (tmpbuf[1] != '\0')
strcpy(buf,tmpbuf);
else
*buf = '\0';
strcpy(tmpbuf,localhost);
strcat(tmpbuf,buf);
}
if (!index(tmpbuf,'.')) {
if (*tmpbuf)
strcat(tmpbuf, ".");
#ifdef HAS_RES_INIT
if (!(_res.options & RES_INIT))
res_init();
if (_res.defdname != NULL)
strcat(tmpbuf,_res.defdname);
else
#endif
#ifdef HAS_GETDOMAINNAME
if (getdomainname(buf,LBUFLEN) == 0)
strcat(tmpbuf,buf);
else
#endif
{
strcat(tmpbuf,"UNKNOWN.HOST");
hostname_ok = FALSE;
}
}
phostname = savestr(tmpbuf);
return hostname_ok;
}
char*
getval(nam,def)
char* nam;
char* def;
{
char* val;
if ((val = getenv(nam)) == NULL || !*val)
return def;
return val;
}
static bool firstexport = TRUE;
extern char** environ;
char*
export(nam,val)
char* nam;
char* val;
{
int namlen = strlen(nam);
register int i=envix(nam,namlen); /* where does it go? */
if (!environ[i]) { /* does not exist yet */
if (firstexport) { /* need we copy environment? */
int j;
#ifndef lint
char** tmpenv = (char**) /* point our wand at memory */
safemalloc((MEM_SIZE) (i+2) * sizeof(char*));
#else
char** tmpenv = NULL;
#endif /* lint */
firstexport = FALSE;
for (j = 0; j < i; j++) /* copy environment */
tmpenv[j] = environ[j];
environ = tmpenv; /* tell exec where it is now */
}
#ifndef lint
else
environ = (char**) saferealloc((char*) environ,
(MEM_SIZE) (i+2) * sizeof(char*));
/* just expand it a bit */
#endif /* lint */
environ[i+1] = NULL; /* make sure it's null terminated */
}
environ[i] = safemalloc((MEM_SIZE)(namlen + strlen(val) + 2));
/* this may or may not be in */
/* the old environ structure */
sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
return environ[i] + namlen + 1;
}
void
un_export(export_val)
char* export_val;
{
if (export_val[-1] == '=' && export_val[-2] != '_') {
export_val[0] = export_val[-2];
export_val[1] = '\0';
export_val[-2] = '_';
}
}
void
re_export(export_val, new_val, limit)
char* export_val;
char* new_val;
int limit;
{
if (export_val[-1] == '=' && export_val[-2] == '_' && !export_val[1])
export_val[-2] = export_val[0];
safecpy(export_val, new_val, limit+1);
}
static int
envix(nam, len)
char* nam;
int len;
{
register int i;
for (i = 0; environ[i]; i++) {
if (strnEQ(environ[i],nam,len) && environ[i][len] == '=')
break; /* strnEQ must come first to avoid */
} /* potential SEGV's */
return i;
}
#ifdef MSDOS
char*
GetEnv(var)
char* var;
{
#undef getenv
char* s = getenv(var);
if (s && isalpha(*s) && s[1] == ':') {
char* t = index(s,'\\');
if (t) {
char ebuf[MAXDIR+32];
strcpy(ebuf,s);
t = ebuf + (t-s);
do {
*t = '/';
} while ((t = index(t,'\\')) != NULL);
s = export(var,ebuf);
}
}
return s;
}
#endif
|