home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d7xx / d795 / pstools.lha / PSTools / PSTools1.lha / source / search.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-14  |  7.7 KB  |  287 lines

  1. /*
  2.  *   The search routine takes a directory list, separated by PATHSEP, and
  3.  *   tries to open a file.  Null directory components indicate current
  4.  *   directory. if the file SUBDIR exists and the file is a font file,
  5.  *   it checks for the file in a subdirectory named the same as the font name.
  6.  *   Returns the open file descriptor if ok, else NULL.
  7.  */
  8. #include "structures.h" /* The copyright notice in that file is included too! */
  9. #include <ctype.h>
  10. #ifdef SYSV
  11. #define MAXPATHLEN (256)
  12. #else
  13. #ifdef VMS
  14. #define MAXPATHLEN (256)
  15. #else   /* ~SYSV */
  16. #include <sys/param.h>          /* for MAXPATHLEN */
  17. #endif  /* ~SYSV */
  18. #endif
  19. #ifndef _DCC
  20. #ifndef MSDOS
  21. #ifndef VMS
  22. #include <pwd.h>
  23. #endif
  24. #endif
  25. #endif
  26. /*
  27.  *
  28.  *   We hope MAXPATHLEN is enough -- only rudimentary checking is done!
  29.  */
  30.  
  31. #ifdef DEBUG
  32. extern integer debug_flag;
  33. #endif  /* DEBUG */
  34. extern char *mfmode ;
  35. extern int actualdpi ;
  36.  
  37. FILE *
  38. search(path, file, mode)
  39.         char *path, *file, *mode ;
  40. {
  41.    extern char *newstring() ;
  42.    register char *nam ;                 /* index into fname */
  43.    register FILE *fd ;                  /* file desc of file */
  44.    char fname[MAXPATHLEN] ;             /* to store file name */
  45.    static char *home = 0 ;              /* home is where the heart is */
  46.    if (*file == DIRSEP) {               /* if full path name */
  47.       if ((fd=fopen(file,mode)) != NULL)
  48.          return(fd) ;
  49.       else
  50.          return(NULL) ;
  51.    }
  52.  
  53. #ifdef MSDOS
  54.    if ( isalpha(file[0]) && file[1]==':' ) {   /* if full path name */
  55.       if ((fd=fopen(file,mode)) != NULL)
  56.          return(fd) ;
  57.       else
  58.          return(NULL) ;
  59.    }
  60. #endif
  61.  
  62.    do {
  63.       /* copy the current directory into fname */
  64.       nam = fname;
  65.       /* copy till PATHSEP */
  66.       if (*path == '~') {
  67.          char *p = nam ;
  68.          path++ ;
  69.          while (*path && *path != PATHSEP && *path != DIRSEP)
  70.             *p++ = *path++ ;
  71.          *p = 0 ;
  72.          if (*nam == 0) {
  73.             if (home == 0) {
  74.                if (home = getenv("HOME"))
  75.                   home = newstring(home) ;
  76.                else
  77.                   home = "." ;
  78.             }
  79.             strcpy(fname, home) ;
  80.          } else {
  81. #if defined (MSDOS) || defined (VMS)  || defined (_DCC)
  82.             error("! ~username in path???") ;
  83. #else
  84.             struct passwd *pw = getpwnam(fname) ;
  85.             if (pw)
  86.                strcpy(fname, pw->pw_dir) ;
  87.             else
  88.                error("no such user") ;
  89. #endif
  90.          }
  91.          nam = fname + strlen(fname) ;
  92.       }
  93.       while (*path != PATHSEP && *path) *nam++ = *path++;
  94.       *nam = 0 ;
  95. #if defined (VMS)
  96.       (void)strcpy(nam,file);                   /* tack the file on */
  97. #else
  98. #if defined(_DCC)         /* !! JRH !! */
  99.     if(nam != fname)                            /* go a path */
  100.     {
  101.         if(*(nam-1) != ':' && *(nam-1) != '/')  /* terminated */
  102.         {
  103.             if(strchr(fname, ':') || strchr(fname, '/'))
  104.             {
  105.                 *nam++ = '/';
  106.             }
  107.             else
  108.             {
  109.                 *nam++ = ':';
  110.             }
  111.             *nam = '\0';
  112.         }
  113.     }
  114.     strcpy(nam, file);
  115. #else
  116.       if (nam == fname) *nam++ = '.';   /* null component is current dir */
  117.  
  118.       if (*file != '\0') {
  119.          *nam++ = DIRSEP;                  /* add separator */
  120.          (void)strcpy(nam,file);                   /* tack the file on */
  121.       }
  122.       else
  123.          *nam = '\0' ;
  124. #endif
  125. #endif
  126.  
  127.       /* belated check -- bah! */
  128.       if ((nam - fname) + strlen(file) + 1 > MAXPATHLEN)
  129.          error("! overran allocated storage in search()");
  130.  
  131. #ifdef DEBUG
  132.       if (dd(D_PATHS))
  133.          (void)fprintf(stderr,"Trying to open %s\n", fname) ;
  134. #endif
  135.       if ((fd=fopen(fname,mode)) != NULL)
  136.          return(fd);
  137.  
  138.    /* skip over PATHSEP and try again */
  139.    } while (*(path++));
  140.  
  141.    return(NULL);
  142.  
  143. }               /* end search */
  144.  
  145. FILE *
  146. pksearch(path, file, mode, n, dpi)
  147.         char *path, *file, *mode ;
  148.     char *n ;
  149.     halfword dpi ;
  150. {
  151.    extern char *newstring() ;
  152.    register char *nam ;                 /* index into fname */
  153.    register FILE *fd ;                  /* file desc of file */
  154.    char fname[MAXPATHLEN] ;             /* to store file name */
  155.    static char *home = 0 ;              /* home is where the heart is */
  156.    for (nam=path; *nam; nam++)
  157.       if (*nam == '%')
  158.          break ;
  159.    if (*nam == 0)
  160.       return search(path, file, mode) ;
  161.    if (*file == DIRSEP) {               /* if full path name */
  162.       if ((fd=fopen(file,mode)) != NULL)
  163.          return(fd) ;
  164.       else
  165.          return(NULL) ;
  166.    }
  167. #ifdef MSDOS
  168.    if ( isalpha(file[0]) && file[1]==':' ) {  /* if full path name */
  169.       if ((fd=fopen(file,mode)) != NULL)
  170.          return(fd) ;
  171.       else
  172.          return(NULL) ;
  173.    }
  174. #endif
  175.    do {
  176.       /* copy the current directory into fname */
  177.       nam = fname;
  178.       /* copy till PATHSEP */
  179.       if (*path == '~') {
  180.          char *p = nam ;
  181.          path++ ;
  182.          while (*path && *path != PATHSEP && *path != DIRSEP)
  183.             *p++ = *path++ ;
  184.          *p = 0 ;
  185.          if (*nam == 0) {
  186.             if (home == 0) {
  187.                if (home = getenv("HOME"))
  188.                   home = newstring(home) ;
  189.                else
  190.                   home = "." ;
  191.             }
  192.             strcpy(fname, home) ;
  193.          } else {
  194. #ifdef MSDOS
  195.             error("! ~username in path???") ;
  196. #else
  197. #ifdef VMS
  198.             error("! ~username in path???") ;
  199. #else
  200. #ifdef _DCC
  201.             error("! ~username in path???") ;
  202. #else
  203.             struct passwd *pw = getpwnam(fname) ;
  204.             if (pw)
  205.                strcpy(fname, pw->pw_dir) ;
  206.             else
  207.                error("no such user") ;
  208. #endif
  209. #endif
  210. #endif
  211.          }
  212.          nam = fname + strlen(fname) ;
  213.       }
  214.       /* copy till PATHSEP */
  215.       while (*path != PATHSEP && *path) {
  216.          if (*path == '%') {
  217.             path++ ;
  218.             switch(*path) {
  219.                case 'd': sprintf(nam, "%d", dpi) ; break ;
  220.                case 'f': strcpy(nam, n) ; break ;
  221.                case 'm': if (mfmode == 0)
  222.                             if (actualdpi == 300) mfmode = "imagen" ;
  223.                             else if (actualdpi == 400) mfmode = "nexthi" ;
  224.                             else if (actualdpi == 635) mfmode = "linolo" ;
  225.                             else if (actualdpi == 1270) mfmode = "linohi" ;
  226.                             else if (actualdpi == 2540) mfmode = "linosuper" ;
  227.                          if (mfmode == 0)
  228.                             error("! MF mode not set, but used in pk path") ;
  229.                          strcpy(nam, mfmode) ;
  230.                          break ;
  231.                case 'p': strcpy(nam, "pk") ; break ;
  232.                case '%': strcpy(nam, "%") ; break ;
  233.                default: error("! bad format character in pk path") ;
  234.             }
  235.             nam = fname + strlen(fname) ;
  236.             if (*path)
  237.                path++ ;
  238.          } else
  239.             *nam++ = *path++;
  240.       }
  241. #ifndef VMS
  242.       if (nam == fname) *nam++ = '.';   /* null component is current dir */
  243. #endif /* VMS */
  244.  
  245.       *nam = '\0' ;
  246.  
  247.       /* belated check -- bah! */
  248.       if (strlen(fname) + 1 > MAXPATHLEN)
  249.          error("! overran allocated storage in search()");
  250.  
  251. #ifdef DEBUG
  252.       if (dd(D_PATHS))
  253.          (void)fprintf(stderr,"Trying to open %s\n", fname) ;
  254. #endif
  255.       if ((fd=fopen(fname,mode)) != NULL)
  256.          return(fd);
  257.  
  258.    /* skip over PATHSEP and try again */
  259.    } while (*(path++));
  260.  
  261.    return(NULL);
  262.  
  263. }               /* end search */
  264.  
  265. /* do we report file openings? */
  266.  
  267. #ifdef DEBUG
  268. #ifdef fopen
  269. #undef fopen
  270. #endif
  271. FILE *my_real_fopen(n, t)
  272. register char *n, *t ;
  273. {
  274.    FILE *tf ;
  275.    if (dd(D_FILES)) {
  276.       fprintf(stderr, "<%s(%s)> ", n, t) ;
  277.       tf = fopen(n, t) ;
  278.       if (tf == 0)
  279.          fprintf(stderr, "failed\n") ;
  280.       else
  281.          fprintf(stderr, "succeeded\n") ;
  282.    } else
  283.       tf = fopen(n, t) ;
  284.    return tf ;
  285. }
  286. #endif
  287.