/* magic.c * last revised at 15, October, 1995 * * Copyright (C) 1994-5, All rights reserved. * written by Gyudong Kim(chilly@iclab.snu.ac.kr) * */ #include "findhier.h" #include #include #ifndef SYSV #define uid_t int #endif #ifdef _UNIX_ #include uid_t getuid(); #endif /* _UNIX_ */ #define ENVIRON ".magic" extern FORMAT Format; struct names *fhpath; static void add_path(src) char *src; { struct names *tmp; for(tmp=fhpath;tmp!=NULL;tmp=tmp->next) { if (!strcmp(tmp->name,src)) return; } tmp = (struct names *)myalloc(sizeof(struct names),"add_path"); tmp->name = (char *)myalloc((SIZE)(sizeof(char)*(strlen(src)+1)) ,"add_path"); (void)strcpy(tmp->name,src); tmp->next = fhpath; fhpath = tmp; } #ifdef _UNIX_ static char *getpath(env) FILE *env; { static char buf[BUFSIZE]; static char *tmp; while(fgets(buf,BUFSIZE,env)!=NULL) { if (strncmp(buf,"addpath",7)) continue; tmp = strtok(buf+8," \t\n"); if (tmp!=NULL) return(tmp); } return((char *)NULL); } /* * fhpath reversal function * This is a general singly linked list reversal routine. */ static void path_reversal() { struct names *tmp,*save,*prev; prev = (struct names *)NULL; for(tmp=fhpath;tmp!=NULL;) { save = tmp->next; tmp->next = prev; prev = tmp; tmp = save; } fhpath = prev; } static void proc(buf) char *buf; { FILE *env; char *tmp; env = fopen(buf,"r"); if (env == NULL) return; while((tmp=getpath(env))!=NULL) add_path(tmp); (void)fclose(env); } #endif /* _UNIX_ */ void myenviron() { #ifdef _UNIX_ char buf[BUFSIZE]; struct passwd *cad; uid_t uid; #endif /* _UNIX_ */ add_path("."); #ifdef _UNIX_ if (Format != MAGIC) return; if ((cad = getpwnam("cad"))!=NULL) { (void)sprintf(buf,"%s/lib/magic/sys/%s",cad->pw_dir,ENVIRON); proc(buf); } uid = getuid(); if ((cad = getpwuid(uid))!=NULL) { (void)sprintf(buf,"%s/%s",cad->pw_dir,ENVIRON); proc(buf); } proc(ENVIRON); path_reversal(); #endif /* _UNIX_ */ } /* magic.c */