home *** CD-ROM | disk | FTP | other *** search
- /* (C) Tim Graves 20th April 1994
- This code is supplied AS IS. no warrantee either expressed or implied
- is provided. This code may be freeley modified and modified as long as my
- origional authorship is acknowledged.
-
- Tim Graves
- Sun Microsystems
-
- */
- /* this file handles the textx for the existance of files etc, it also handled the searches for a file from the path */
- #include <stdio.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include "psion.h"
- static char * vsn = "@(#) psfile.c 3.1@(#)" ;
- extern int debugcall ;
- int debugpath = FALSE ;
-
- pscmdexists(path)
- char * path ;
- {
- FILE *fd ;
-
- if (debugcall >= 1)
- fprintf(stderr, "CALL: pscmdexists (path = %s)\n", path) ;
- if (debugpath)
- printf("Path, trying :%s:\n", path) ;
- /* if we can open the file use return true othersize false */
- if ((fd = fopen(path, "r")) == NULL)
- return (FALSE) ;
- /* we can open it for a read */
- fclose(fd) ;
- return(TRUE) ;
- }
-
- char * pscmdpath(pscmd, path, res)
- char * pscmd, * path, *res ;
- {
- char *ptptr, * pathptr, pt [1000] ;
- int ctr ;
- /* scan down the path appending the appropriate stiuff to pcsmd till
- we find a name that matches, once we do return a pointer to it, otherwise
- we return NULL */
-
- /* if the file begins with / its an absolute pathname so do something different */
- if (debugcall >= 1)
- fprintf(stderr, "CALL: pscmdpath (pscmd = %s, path = %s, res = %s)\n",pscmd, path,res) ;
- if (pscmd[0] == '/')
- {
- /* build the file name */
- strcpy (pt, pscmd) ;
- strcat(pt, PSCMDSUFFIX) ;
-
- /* test it */
- if (pscmdexists(pt) == TRUE)
- {
- strcpy(res, pt) ;
- return(res) ;
- }
- else
- {
- return(NULL) ;
- }
- }
-
-
- /* scann down spath one segment at a time */
- while (path != NULL)
- {
- if (debugpath)
- printf("New path = -%s-\n", path) ;
- ptptr = pt ;
- /* copy from path to pt till we find a : or \0 */
- for (ctr = 0 ; ((path[ctr] != ':') && (path[ctr] != '\0')) ; ctr ++)
- {
- if (debugpath)
- printf("scanning path, %c\n", path[ctr]) ;
-
- pt[ctr] = path[ctr] ;
- }
-
- /* if we have hit the end finish */
- if ( ctr == 0)
- break ;
- /* terminate the string */
- pt[ctr] = '\0' ;
-
- strcat(pt, "/"); /* ad a seperator */
- strcat(pt, pscmd) ; /* add the filename */
- strcat(pt, PSCMDSUFFIX) ; /* add the suffix */
-
- if (pscmdexists(pt) == TRUE)
- {
- strcpy(res, pt) ;
- return (res) ;
- }
- /* move along the string */
- path = strchr(path, ':') ;
- /* we have found the : we want the first char after it */
- if (path != NULL)
- path ++ ;
- }
- /* not found anything, return NULL */
- return(NULL) ;
- }
-
- extern char * findvar () ;
- /* load the environment from ~/.psrc.pcsmd or from ./.psrc.pscmd */
- runrc()
- {
-
- char path [1000] ;
- char rcpath[1000] ;
- char sourcecmd[1000] ;
- char * homedir ;
- /* first build a tree to see if we can locate a .psrc file */
- if (debugcall >= 1)
- fprintf(stderr, "CALL: runrc\n") ;
- strcpy(path, ".") ;
-
- /* do we have a home ? if so add it to the path */
- homedir = findvar("HOME") ;
- if (homedir != NULL)
- {
- strcat(path, ":") ;
- strcat(path, homedir) ;
- }
-
- if (debugpath)
- printf("Runrc path = -%s-\n", path) ;
- /* if the file does not exitsd return */
- if (pscmdpath(".psrc", path, rcpath) == NULL)
- return ; /* no .psrc file about */
-
- /* build the command */
- strcpy(sourcecmd, "source ") ;
- strcat(sourcecmd, rcpath) ;
- /* remove the .pscmd suffix */
- sourcecmd[strlen(sourcecmd) -6] = '\0' ;
- /* and execute it */
- cmdintr(sourcecmd) ;
- }
-