static char rcsid[] = "$Id: os.c,v 1.2 1992/11/08 23:22:02 mike Exp $"; /* $Log: os.c,v $ * Revision 1.2 1992/11/08 23:22:02 mike * - Fixed bug in `changed_directory'. * * Revision 1.1 1992/09/05 01:13:32 mike * Initial revision * */ /* * os.c : Misc routines that are OS dependent. * Routines: * osinit(): Initialize OS dependent stuff. Called once, very early. * blkmov(): An incrementing block mover. memmove() will work. * change_directory(dir): Change the current directory to a new one. * Returns TRUE on success. If not implemented, return FALSE. Does not * munge dir. * MMopen_code_file(name): Open a Mutt machine code file. * unique_file_name(): Create a name that can be used to open a file. * Every name must be unique. Espically important in multiprocess * systems where more than one ME may be running at the same time. * Note: These file names are ment ONLY to be used when a temporary * file is needed. They are not ment to out live the ME process that * creates them. If they do, another ME will probably generate the * same file name at some later date. * Variables: * current_directory: An array of char that contains the current * directory. Set it in osinit(). I don't like doing this but I can't * think of a better way. If not supported, init to "". * change_directory() must maintain this variable. * * C Durland Public Domain * Atari stuff from Jwahar R. Bammi (bammi@cadence.com). */ #include #include "me2.h" #include "config.h" extern char *strcpy(), *getenv(); /* ******************************************************************** */ /* **************************** Unix Stuff **************************** */ /* ******************************************************************** */ #if UX_OS #define PSEP ':' /* search path separater */ /* !!!Having a fixed size array is asking for trouble. Especially on * long file name systems. */ char current_directory[NFILEN]; static char *mutt_search_path; osinit() { static char msp[NFILEN]; /* Filled with '\0' !!!asking for trouble */ char *ptr; canonize(".", current_directory, (char *)NULL); mutt_search_path = getenv("ME2"); #if USE_DEFAULT_MUTT_SEARCH_PATH if (!mutt_search_path) { mutt_search_path = msp; if (ptr = getenv("HOME")) /* ME2=$HOME: */ { strcat(msp, ptr); strcat(msp, ":"); } strcat(msp, DEFAULT_MUTT_SEARCH_PATH); /* ME2=$HOME:/usr/local/me2 */ } #endif /* USE_DEFAULT_MUTT_SEARCH_PATH */ return TRUE; } /* Notes: * Need to arf() the directory so I can be sure that what I copy into * current_directory is a real directory name. I also need a * canonized name for Mutt programs to use. If I just * chdir(directory) then arf() and arf screws up (as is happening on * Domain/OS), its going to be a mess. */ change_directory(directory) char *directory; { char buf[NFILEN]; int s = FALSE; if (arf(strcpy(buf,directory)) && (s = (chdir(buf) == 0))) strcpy(current_directory,buf); return s; } /* block copy: my own to avoid problems with overlapping moves. * Note: memmove() can be used to replace blkmov() if you have it. */ void blkmov(to,from,n) register char *to, *from; register unsigned int n; { while (n--) *to++ = *from++; } /* Create a unique file name with format: * ME. * n is 0...19999 and then rolls over. (up to 5 characters) * pid is 1...65535. (up to 5 characters) * Notes: * Unixes with short file name file systems have a max file name * length of 14 characters (to the best of my knowledge) so this * scheme should work on those systems. */ char *unique_file_name(name) char *name; { extern char *l_to_a(); static int n = 0; char *ptr; strcpy(name,"/tmp"); if (ptr = getenv("TMP")) strcpy(name,ptr); strcat(name, "/ME"); ptr = l_to_a((long int)n); strcat(name,ptr); strcat(name,"."); ptr = l_to_a((long int)getpid()); strcat(name,ptr); n = (n+1) % 20000; return name; } #endif /* UX_OS */ /* ******************************************************************** */ /* *********************** MS-DOS Stuff ******************************* */ /* ******************************************************************** */ #if MSDOZ #define PSEP ';' /* search path separater */ char current_directory[80]; /* current directory */ static char *mutt_search_path; osinit() { canonize(".", current_directory, (char *)NULL); mutt_search_path = getenv("ME2"); return TRUE; } change_directory(directory) char *directory; { return FALSE; } /* blkmov() is assembler program in speed.asm */ #if 1 /* don't need an assembler if you have memmove() */ void blkmov(to,from,n) register char *to, *from; register unsigned int n; { memmove (to, from, n);} #endif /* Create a unique file name with format: * ME.$$$ * n is 0...19999 and then rolls over. (up to 5 characters) * Notes: * MS-DOS doesn't multiprocess so only one ME at a time. This means I * can use a pretty simple scheme. */ char *unique_file_name(name) char *name; { extern char *l_to_a(); static int n = 0; char *ptr; *name = '\0'; if (ptr = getenv("TMP")) strcpy(name,ptr); strcat(name, "ME2"); ptr = l_to_a((long int)n); strcat(name,ptr); strcat(name,".$$$"); n = (n+1) % 20000; return name; } #endif /* MSDOZ */ /* ******************************************************************** */ /* **************************** atariST Stuff ************************* */ /* ******************************************************************** */ #if ATARI #define PSEP ';' /* search path separater */ /* !!!Having a fixed size array is asking for trouble. Especially on * long file name systems. */ char current_directory[NFILEN]; static char *mutt_search_path; long _stksize = 16384; osinit() { static char msp[NFILEN]; /* Filled with '\0' !!!asking for trouble */ char *ptr; canonize(".", current_directory, (char *)NULL); mutt_search_path = getenv("ME2"); #if USE_DEFAULT_MUTT_SEARCH_PATH if (!mutt_search_path) { mutt_search_path = msp; if (ptr = getenv("HOME")) /* ME2=$HOME: */ { strcat(msp, ptr); strcat(msp, ";"); } strcat(msp, DEFAULT_MUTT_SEARCH_PATH); /* ME2=$HOME:/usr/local/me2 */ } #endif /* USE_DEFAULT_MUTT_SEARCH_PATH */ return TRUE; } /* Notes: * Need to arf() the directory so I can be sure that what I copy into * current_directory is a real directory name. I also need a * canonized name for Mutt programs to use. If I just * chdir(directory) then arf() and arf screws up (as is happening on * Domain/OS), its going to be a mess. */ int change_directory(directory) char *directory; { char buf[NFILEN]; #if 0 if (arf(strcpy(buf,directory)) && (s = (chdir(buf) == 0))) strcpy(current_directory,buf); return s; #else if ((canonize(directory,buf,NULL) != NULL) && chdir(buf) == 0) { strcpy(current_directory,buf); return TRUE; } else return FALSE; #endif } /* block copy: my own to avoid problems with overlapping moves. * Note: memmove() can be used to replace blkmov() if you have it. */ void blkmov(to,from,n) register char *to, *from; register unsigned int n; { memmove(to, from, (unsigned long)n); } /* Create a unique file name */ char *unique_file_name(name) char *name; { extern char *tmpnam(); return tmpnam(name); } #endif /* ATARI */ /* ******************************************************************** */ /* ************** Unix, MS-DOS and atariST Stuff ********************** */ /* ******************************************************************** */ #if UX_OS || MSDOZ || ATARI /* MMopen_code_file(name) : open a Mutt code (.mco) file. * Input: * name: Name of code file to open. The extension is ".mco". May have * a leading path. * Returns: * FILE * : Pointer to the opened code file. * NULL : Couldn't find the file. * Path parsing alg (same as Unix (ksh)): * If name has a slash in it, the path is not parsed. * Everything between sepraters is a path. * If path starts with ":", the current directory is searched first. * If path ends with ":", the current directory is searched last. */ /* ??? arf the name so I can handle "~?" */ FILE *MMopen_code_file(name) char *name; { extern char *strchr(); char buf[NFILEN +80], *ptr, *qtr; FILE *fptr; int n; if (strchr(name,U_SLASH) #if MSDOZ || ATARI /* MS-DOS "A:foo" */ || strchr(name,M_SLASH) || name[1] == ':' #endif ) { return fopen(name,FOPEN_BINARY); } if (!(ptr = mutt_search_path)) ptr = ""; while (TRUE) { qtr = ptr; while (*ptr != PSEP && *ptr) ptr++; n = ptr - qtr; strncpy(buf,qtr,n); if (n) buf[n++] = U_SLASH; strcpy(buf +n, name); if (fptr = fopen(buf,"rb")) return fptr; if (!*ptr++) break; } return NULL; } #endif /* UX_OS || MSDOZ || ATARI */