home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / pocketbk / comms / x_sun_psio / code / psfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-21  |  3.4 KB  |  144 lines

  1. /* (C) Tim Graves 20th April 1994
  2.    This code is supplied AS IS. no warrantee either expressed or implied 
  3.    is provided. This code may be freeley modified and modified as long as my
  4.    origional authorship is acknowledged. 
  5.    
  6.    Tim Graves
  7.     Sun Microsystems
  8.      
  9.      */
  10. /* this file handles the textx for the existance of files etc, it also handled the searches for a file from the path */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include "psion.h"
  16. static char * vsn = "@(#) psfile.c 3.1@(#)" ;
  17. extern int debugcall ;
  18. int debugpath = FALSE ;
  19.  
  20. pscmdexists(path)
  21. char * path ;
  22. {
  23.     FILE *fd ;
  24.  
  25.     if (debugcall >= 1)
  26.         fprintf(stderr, "CALL: pscmdexists (path = %s)\n", path) ;
  27.     if (debugpath)
  28.         printf("Path, trying :%s:\n", path) ;
  29.     /* if we can open the file use return true othersize false */
  30.     if ((fd = fopen(path, "r")) == NULL)
  31.         return (FALSE) ;
  32.     /* we can open it for a read */
  33.     fclose(fd) ;
  34.     return(TRUE) ;
  35. }
  36.  
  37. char * pscmdpath(pscmd, path, res)
  38. char * pscmd, * path, *res ;
  39. {
  40.     char *ptptr, * pathptr, pt [1000] ;
  41.     int ctr ;
  42.     /* scan down the path appending the appropriate stiuff to pcsmd till
  43.        we find a name that matches, once we do return a pointer to it, otherwise
  44.        we return NULL */
  45.  
  46.     /* if the file begins with / its an absolute pathname so do something different */
  47.     if (debugcall >= 1)
  48.         fprintf(stderr, "CALL: pscmdpath (pscmd = %s, path = %s, res = %s)\n",pscmd, path,res) ;
  49.     if (pscmd[0] == '/')
  50.     {
  51.         /* build the file name */
  52.         strcpy (pt, pscmd) ;
  53.         strcat(pt, PSCMDSUFFIX) ;
  54.  
  55.         /* test it */
  56.         if (pscmdexists(pt) == TRUE)
  57.         {
  58.             strcpy(res, pt) ;
  59.             return(res) ;
  60.         }
  61.         else
  62.         {
  63.             return(NULL) ;
  64.         }
  65.     }
  66.  
  67.  
  68.     /* scann down spath one segment at a time */
  69.     while (path != NULL)
  70.     {
  71.         if (debugpath)
  72.             printf("New path = -%s-\n", path) ;
  73.         ptptr = pt ;
  74.         /* copy from path to pt till we find a : or \0 */
  75.         for (ctr = 0 ; ((path[ctr] != ':') && (path[ctr] != '\0')) ; ctr ++)
  76.         {
  77.             if (debugpath)
  78.                 printf("scanning path, %c\n", path[ctr]) ;
  79.  
  80.             pt[ctr] = path[ctr] ;
  81.         }
  82.  
  83.         /* if we have hit the end finish */
  84.         if ( ctr == 0)
  85.             break ;
  86.         /* terminate the string */
  87.         pt[ctr] = '\0' ;
  88.  
  89.         strcat(pt, "/"); /* ad a seperator */
  90.         strcat(pt, pscmd) ; /* add the filename */
  91.         strcat(pt, PSCMDSUFFIX) ; /* add the suffix */
  92.  
  93.         if (pscmdexists(pt) == TRUE)
  94.         {
  95.             strcpy(res, pt) ;
  96.             return (res) ;
  97.         }
  98.         /* move along the string */
  99.         path = strchr(path, ':') ;
  100.         /* we have found the : we want the first char after it */
  101.         if (path != NULL)
  102.             path ++ ;
  103.     }
  104.     /* not found anything, return NULL */
  105.     return(NULL) ;
  106. }
  107.  
  108. extern char * findvar () ;
  109. /* load the environment from ~/.psrc.pcsmd or from ./.psrc.pscmd */
  110. runrc()
  111. {
  112.  
  113.     char path [1000] ;
  114.     char rcpath[1000] ;
  115.     char sourcecmd[1000] ;
  116.     char * homedir ;
  117.     /* first build a tree to see if we can locate a .psrc file */
  118.     if (debugcall >= 1)
  119.         fprintf(stderr, "CALL: runrc\n") ;
  120.     strcpy(path, ".") ;
  121.  
  122.     /* do we have a home ? if so add it to the path */
  123.     homedir = findvar("HOME") ;
  124.     if (homedir != NULL) 
  125.     {
  126.         strcat(path, ":") ;
  127.         strcat(path, homedir) ;
  128.     }
  129.  
  130.     if (debugpath)
  131.         printf("Runrc path = -%s-\n", path) ;
  132.     /* if the file does not exitsd return */
  133.     if (pscmdpath(".psrc", path, rcpath) == NULL)
  134.         return ; /* no .psrc file about */
  135.     
  136.     /* build the command */
  137.     strcpy(sourcecmd, "source ") ;
  138.     strcat(sourcecmd, rcpath) ;
  139.     /* remove the .pscmd suffix */
  140.     sourcecmd[strlen(sourcecmd) -6] = '\0' ;
  141.     /* and execute it */
  142.     cmdintr(sourcecmd) ;
  143. }
  144.