home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3.4.17 [SPARC, PA-RISC] / nextstep33_risc.iso / NextLibrary / TeX / tex / src / texview / search.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-05  |  5.9 KB  |  193 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. #define DIRSEP '/'
  10. #define PATHSEP ':'
  11. #include <stdio.h>              /* for FILE and fopen */
  12. #ifdef SYSV
  13. #include <string.h>
  14. #define MAXPATHLEN (128)
  15. #else   /* ~SYSV */
  16. #include <sys/param.h>          /* for MAXPATHLEN */
  17. #include <strings.h>            /* for strlen */
  18. #endif  /* ~SYSV */
  19. #include <pwd.h>
  20. /*
  21.  *
  22.  *   We hope MAXPATHLEN is enough -- only rudimentary checking is done!
  23.  */
  24. extern void error() ;
  25. #ifdef DEBUG
  26. extern integer debug_flag;
  27. #endif  /* DEBUG */
  28. char *mfmode ;
  29. extern int actualdpi ;
  30. #define mode "r"
  31. FILE *
  32. search(path, file)
  33.         char *path, *file ;
  34. {
  35.    extern char *getenv(), *newstring() ;
  36.    register char *nam ;                 /* index into fname */
  37.    register FILE *fd ;                  /* file desc of file */
  38.    char fname[MAXPATHLEN] ;             /* to store file name */
  39.    static char *home = 0 ;              /* home is where the heart is */
  40.    if (*file == DIRSEP) {               /* if full path name */
  41.       if ((fd=fopen(file,mode)) != NULL)
  42.          return(fd) ;
  43.       else
  44.          return(NULL) ;
  45.    }
  46.  
  47.    do {
  48.       /* copy the current directory into fname */
  49.       nam = fname;
  50.       /* copy till PATHSEP */
  51.       if (*path == '~') {
  52.          char *p = nam ;
  53.          path++ ;
  54.          while (*path && *path != PATHSEP && *path != DIRSEP)
  55.             *p++ = *path++ ;
  56.          *p = 0 ;
  57.          if (*nam == 0) {
  58.             if (home == 0) {
  59.                if (home = getenv("HOME"))
  60.                   home = newstring(home) ;
  61.                else
  62.                   home = "." ;
  63.             }
  64.             strcpy(fname, home) ;
  65.          } else {
  66.             struct passwd *pw = getpwnam(fname) ;
  67.             if (pw)
  68.                strcpy(fname, pw->pw_dir) ;
  69.             else
  70.                error("no such user") ;
  71.          }
  72.          nam = fname + strlen(fname) ;
  73.       }
  74.       while (*path != PATHSEP && *path) *nam++ = *path++;
  75.       *nam = 0 ;
  76.       if (nam == fname) *nam++ = '.';   /* null component is current dir */
  77.  
  78.       if (*file != '\0') {
  79.          *nam++ = DIRSEP;                  /* add separator */
  80.          (void)strcpy(nam,file);                   /* tack the file on */
  81.       }
  82.       else
  83.          *nam = '\0' ;
  84.  
  85.       /* belated check -- bah! */
  86.       if ((nam - fname) + strlen(file) + 1 > MAXPATHLEN)
  87.          error("! overran allocated storage in search()");
  88.       if ((fd=fopen(fname,mode)) != NULL)
  89.          return(fd);
  90.  
  91.    /* skip over PATHSEP and try again */
  92.    } while (*(path++));
  93.  
  94.    return(NULL);
  95.  
  96. }               /* end search */
  97.  
  98. FILE *
  99. pksearch(path, file, n, dpi)
  100.         char *path, *file ;
  101.     char *n ;
  102.     halfword dpi ;
  103. {
  104.    extern char *getenv(), *newstring() ;
  105.    register char *nam ;                 /* index into fname */
  106.    register FILE *fd ;                  /* file desc of file */
  107.    char fname[MAXPATHLEN] ;             /* to store file name */
  108.    static char *home = 0 ;              /* home is where the heart is */
  109.    int sub ;
  110.  
  111.    if (*file == DIRSEP) {               /* if full path name */
  112.       if ((fd=fopen(file,mode)) != NULL)
  113.          return(fd) ;
  114.       else
  115.          return(NULL) ;
  116.    }
  117.    do {
  118.       /* copy the current directory into fname */
  119.       nam = fname;
  120.       sub = 0 ;
  121.       /* copy till PATHSEP */
  122.       if (*path == '~') {
  123.          char *p = nam ;
  124.          path++ ;
  125.          while (*path && *path != PATHSEP && *path != DIRSEP)
  126.             *p++ = *path++ ;
  127.          *p = 0 ;
  128.          if (*nam == 0) {
  129.             if (home == 0) {
  130.                if (home = getenv("HOME"))
  131.                   home = newstring(home) ;
  132.                else
  133.                   home = "." ;
  134.             }
  135.             strcpy(fname, home) ;
  136.          } else {
  137.             struct passwd *pw = getpwnam(fname) ;
  138.             if (pw)
  139.                strcpy(fname, pw->pw_dir) ;
  140.             else
  141.                error("no such user") ;
  142.          }
  143.          nam = fname + strlen(fname) ;
  144.       }
  145.       /* copy till PATHSEP */
  146.       while (*path != PATHSEP && *path) {
  147.          if (*path == '%') {
  148.             sub = 1 ;
  149.             path++ ;
  150.             switch(*path) {
  151.                case 'd': sprintf(nam, "%d", dpi) ; break ;
  152.                case 'f': strcpy(nam, n) ; break ;
  153.                case 'm': if (mfmode == 0)
  154.                             if (actualdpi == 300) mfmode = "imagen" ;
  155.                             else if (actualdpi == 400) mfmode = "nexthi" ;
  156.                             else if (actualdpi == 635) mfmode = "linolo" ;
  157.                             else if (actualdpi == 1270) mfmode = "linohi" ;
  158.                             else if (actualdpi == 2540) mfmode = "linosuper" ;
  159.                          if (mfmode == 0)
  160.                             error("! MF mode not set, but used in pk path") ;
  161.                          strcpy(nam, mfmode) ;
  162.                          break ;
  163.                case 'p': strcpy(nam, "pk") ; break ;
  164.                case '%': strcpy(nam, "%") ; break ;
  165.                default: error("! bad format character in pk path") ;
  166.             }
  167.             nam = fname + strlen(fname) ;
  168.             if (*path)
  169.                path++ ;
  170.          } else
  171.             *nam++ = *path++;
  172.       }
  173.       if (nam == fname) *nam++ = '.';   /* null component is current dir */
  174.  
  175.       if (sub == 0 && *file) {
  176.          *nam++ = DIRSEP ;
  177.          strcpy(nam, file) ;
  178.       } else
  179.          *nam = '\0' ;
  180.  
  181.       /* belated check -- bah! */
  182.       if (strlen(fname) + 1 > MAXPATHLEN)
  183.          error("! overran allocated storage in search()");
  184.       if ((fd=fopen(fname,mode)) != NULL)
  185.          return(fd);
  186.  
  187.    /* skip over PATHSEP and try again */
  188.    } while (*(path++));
  189.  
  190.    return(NULL);
  191.  
  192. }               /* end search */
  193.