home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fonts 1 / freshfonts1.bin / bbs / programs / amiga / pastex13.lha / DVIPS / dvips5519.lha / dvips / search.c.bak < prev    next >
Text File  |  1993-09-04  |  11KB  |  383 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 "dvips.h" /* The copyright notice in that file is included too! */
  9. #include <ctype.h>
  10.  
  11. #if defined(SYSV) || defined(VMS) || defined(__THINK__) || defined(MSDOS) || defined(AMIGA)
  12. #define MAXPATHLEN (256)
  13. #else
  14. #include <sys/param.h>          /* for MAXPATHLEN */
  15. #endif
  16. #ifndef MSDOS
  17. #ifndef VMS
  18. #ifndef MVSXA
  19. #ifndef VMCMS /* IBM: VM/CMS */
  20. #ifndef __THINK__
  21. #ifndef AMIGA
  22. #include <pwd.h>
  23. #endif
  24. #endif
  25. #endif
  26. #endif  /* IBM: VM/CMS */
  27. #endif
  28. #endif
  29. /*
  30.  *
  31.  *   We hope MAXPATHLEN is enough -- only rudimentary checking is done!
  32.  */
  33.  
  34. #ifdef DEBUG
  35. extern integer debug_flag;
  36. #endif  /* DEBUG */
  37. extern char *mfmode ;
  38. extern int actualdpi ;
  39. char realnameoffile[MAXPATHLEN] ;
  40. FILE *
  41. search(path, file, mode)
  42.         char *path, *file, *mode ;
  43. {
  44.    extern char *getenv(), *newstring() ;
  45.    register char *nam ;                 /* index into fname */
  46.    register FILE *fd ;                  /* file desc of file */
  47.    char fname[MAXPATHLEN] ;             /* to store file name */
  48.    static char *home = 0 ;              /* home is where the heart is */
  49. #ifdef MVSXA
  50. char fname_safe[256];
  51. register int i, firstext, lastext, lastchar;
  52. #endif
  53. #ifdef VMCMS /* IBM: VM/CMS - we don't have paths or dirsep's but we strip off
  54.                              filename if there is a Unix path dirsep    */
  55.    register char *lastdirsep ;
  56.    lastdirsep = strrchr(file, '/') ;
  57.    if ( NULL != lastdirsep ) file = lastdirsep + 1 ;
  58.    if ((fd=fopen(file,mode)) != NULL) {
  59.       return(fd) ;
  60.    } else {
  61.       return(NULL) ;
  62.    }
  63. #else
  64.    if (*file == DIRSEP) {               /* if full path name */
  65.       if ((fd=fopen(file,mode)) != NULL) {
  66.          strcpy(realnameoffile, file) ;
  67.          return(fd) ;
  68.       } else
  69.          return(NULL) ;
  70.    }
  71. #endif   /* IBM: VM/CMS */
  72.  
  73. #if defined MSDOS || defined OS2
  74.    if ( isalpha(file[0]) && file[1]==':' ) {   /* if full path name */
  75.       if ((fd=fopen(file,mode)) != NULL)
  76.          return(fd) ;
  77.       else
  78.          return(NULL) ;
  79.    }
  80. #endif
  81.  
  82. #if defined AMIGA
  83.    if (strchr(file, ':')) {            /* if full path name */
  84.       if ((fd=fopen(file,mode)) != NULL)
  85.          return(fd) ;
  86.       else
  87.          return(NULL) ;
  88.    }
  89. #endif
  90.  
  91.    do {
  92.       /* copy the current directory into fname */
  93.       nam = fname;
  94.       /* copy till PATHSEP */
  95.       if (*path == '~') {
  96.          char *p = nam ;
  97.          path++ ;
  98.          while (*path && *path != PATHSEP && *path != DIRSEP)
  99.             *p++ = *path++ ;
  100.          *p = 0 ;
  101.          if (*nam == 0) {
  102.             if (home == 0) {
  103.                if (0 != (home = getenv("HOME")))
  104.                   home = newstring(home) ;
  105.                else
  106.                   home = "." ;
  107.             }
  108.             strcpy(fname, home) ;
  109.          } else {
  110. #if defined MSDOS || defined OS2
  111.             error("! ~username in path???") ;
  112. #else
  113. #ifdef VMS
  114.             error("! ~username in path???") ;
  115. #else
  116. #ifdef VMCMS  /* IBM: VM/CMS */
  117.             error("! ~username in path???") ;
  118. #else
  119. #ifdef MVSXA  /* IBM: MVS/XA */
  120.             error("! ~username in path???") ;
  121. #else
  122. #ifdef __THINK__
  123.             error("! ~username in path???") ;
  124. #else
  125. #ifdef AMIGA
  126.             error("! ~username in path???") ;
  127. #else
  128.             struct passwd *pw = getpwnam(fname) ;
  129.             if (pw)
  130.                strcpy(fname, pw->pw_dir) ;
  131.             else
  132.                error("no such user") ;
  133. #endif
  134. #endif
  135. #endif  /* IBM: VM/CMS */
  136. #endif
  137. #endif
  138. #endif
  139.          }
  140.          nam = fname + strlen(fname) ;
  141.       }
  142.       while (*path != PATHSEP && *path) *nam++ = *path++;
  143.       *nam = 0 ;
  144. #ifndef VMS
  145. #ifndef __THINK__
  146.       if (nam == fname) *nam++ = '.';   /* null component is current dir */
  147.  
  148.       if (*file != '\0') {
  149.          if ((nam != fname) && *(nam-1) != DIRSEP) /* GNW 1992.07.09 */
  150.             *nam++ = DIRSEP;                  /* add separator */
  151.          (void)strcpy(nam,file);                   /* tack the file on */
  152.       }
  153.       else
  154.          *nam = '\0' ;
  155. #else
  156.       (void)strcpy(nam,file);                   /* tack the file on */
  157. #endif
  158. #else
  159.       (void)strcpy(nam,file);                   /* tack the file on */
  160. #endif
  161. #ifdef MVSXA
  162. nam = fname;
  163. if (strchr(nam,'=') != NULL) {
  164.    (void) strcpy(fname_safe,fname);  /* save fname */
  165.    firstext = strchr(nam, '=') - nam + 2;
  166.    lastext = strrchr(nam, '.') - nam + 1;
  167.    lastchar  = strlen(nam) - 1;
  168.  
  169.    (void) strcpy(fname,"dd:");  /* initialize fname */
  170.    nam=&fname[3];
  171.    for (i=lastext; i<=lastchar; i++) *nam++ = fname_safe[i] ;
  172.            *nam++  = '(' ;
  173.    for (i=firstext; i<lastext-1; i++) *nam++ = fname_safe[i] ;
  174.            *nam++  = ')' ;
  175.            *nam++  = 0   ;
  176.    }
  177.    else {
  178.       if (fname[0] == '/') {
  179.          fname[0] = '\'';
  180.          strcat(&fname[strlen(fname)],"\'");
  181.       }
  182.       if (fname[0] == '.') fname[0] = ' ';
  183.       if (fname[1] == '.') fname[1] = ' ';
  184.    }
  185. #endif
  186.  
  187. #ifdef AMIGA
  188.       if (fname[0] == '.' && fname[1] == DIRSEP) {    /* special case "./" */
  189.         int i;
  190.         for (i=2; i<strlen(fname)+1; i++) {
  191.           fname[i-2] = fname[i];    /* copy with the '\0' */
  192.         }
  193.       }
  194. #endif
  195.  
  196.       /* belated check -- bah! */
  197.       if ((nam - fname) + strlen(file) + 1 > MAXPATHLEN)
  198.          error("! overran allocated storage in search()");
  199.  
  200. #ifdef DEBUG
  201.       if (dd(D_PATHS))
  202.          (void)fprintf(stderr,"Trying to open %s\n", fname) ;
  203. #endif
  204.       if ((fd=fopen(fname,mode)) != NULL) {
  205.          strcpy(realnameoffile, fname) ;
  206.          return(fd);
  207.       }
  208.  
  209.    /* skip over PATHSEP and try again */
  210.    } while (*(path++));
  211.  
  212.    return(NULL);
  213.  
  214. }               /* end search */
  215.  
  216. FILE *
  217. pksearch(path, file, mode, n, dpi, vdpi)
  218.         char *path, *file, *mode ;
  219.     char *n ;
  220.     halfword dpi, vdpi ;
  221. {
  222.    extern char *getenv(), *newstring() ;
  223.    register char *nam ;                 /* index into fname */
  224.    register FILE *fd ;                  /* file desc of file */
  225.    char fname[MAXPATHLEN] ;             /* to store file name */
  226.    static char *home = 0 ;              /* home is where the heart is */
  227.    int sub ;
  228.  
  229.    if (*file == DIRSEP) {               /* if full path name */
  230.       if ((fd=fopen(file,mode)) != NULL)
  231.          return(fd) ;
  232.       else
  233.          return(NULL) ;
  234.    }
  235. #if defined MSDOS || defined OS2
  236.    if ( isalpha(file[0]) && file[1]==':' ) {  /* if full path name */
  237.       if ((fd=fopen(file,mode)) != NULL)
  238.          return(fd) ;
  239.       else
  240.          return(NULL) ;
  241.    }
  242. #endif
  243.    do {
  244.       /* copy the current directory into fname */
  245.       nam = fname;
  246.       sub = 0 ;
  247.       /* copy till PATHSEP */
  248.       if (*path == '~') {
  249.          char *p = nam ;
  250.          path++ ;
  251.          while (*path && *path != PATHSEP && *path != DIRSEP)
  252.             *p++ = *path++ ;
  253.          *p = 0 ;
  254.          if (*nam == 0) {
  255.             if (home == 0) {
  256.                if (0 != (home = getenv("HOME")))
  257.                   home = newstring(home) ;
  258.                else
  259.                   home = "." ;
  260.             }
  261.             strcpy(fname, home) ;
  262.          } else {
  263. #if defined MSDOS || defined OS2
  264.             error("! ~username in path???") ;
  265. #else
  266. #ifdef VMS
  267.             error("! ~username in path???") ;
  268. #else
  269. #ifdef VMCMS  /* IBM: VM/CMS */
  270.             error("! ~username in path???") ;
  271. #else
  272. #ifdef MVSXA  /* IBM: MVS/XA */
  273.             error("! ~username in path???") ;
  274. #else
  275. #ifdef __THINK__
  276.             error("! ~username in path???") ;
  277. #else
  278.             struct passwd *pw = getpwnam(fname) ;
  279.             if (pw)
  280.                strcpy(fname, pw->pw_dir) ;
  281.             else
  282.                error("no such user") ;
  283. #endif
  284. #endif /* IBM: VM/CMS */
  285. #endif
  286. #endif
  287. #endif
  288.          }
  289.          nam = fname + strlen(fname) ;
  290.       }
  291.       /* copy till PATHSEP */
  292.       while (*path != PATHSEP && *path) {
  293.          if (*path == '%') {
  294.             sub = 1 ;
  295.             path++ ;
  296.             switch(*path) {
  297.                case 'b': sprintf(nam, "%d", actualdpi) ; break ;
  298.                case 'd