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