home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / unixtex-6.1b-src.tgz / tar.out / contrib / unixtex / dvipsk / search.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  4KB  |  188 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.  In an environment variable, null directory components
  5.  *   indicate substitution of the default path list at that point.
  6.  */
  7. #include "dvips.h" /* The copyright notice in that file is included too! */
  8. #include <kpathsea/tex-file.h>
  9. #include <kpathsea/tex-glyph.h>
  10. extern char name[];
  11. /*
  12.  *
  13.  *   We hope MAXPATHLEN is enough -- only rudimentary checking is done!
  14.  */
  15.  
  16. #ifdef SECURE
  17. extern Boolean secure;
  18. #endif
  19. #ifdef DEBUG
  20. extern integer debug_flag;
  21. #endif  /* DEBUG */
  22. extern char *mfmode ;
  23. extern int actualdpi ;
  24. string realnameoffile ;
  25.  
  26. FILE *
  27. search(format, file, mode)
  28.     kpse_file_format_type format;
  29.     char *file, *mode ;
  30. {
  31.   FILE *ret;
  32.   string found_name;
  33.  
  34. #ifdef SECURE
  35.   /* This change suggested by maj@cl.cam.ac.uk to disallow reading of
  36.      arbitrary files.  */
  37.   if (secure && kpse_absolute_p (file)) return NULL;
  38. #endif
  39.  
  40.   /* Most file looked for through here must exist -- the exception is
  41.      VF's. Bitmap fonts go through pksearch. */
  42.   found_name = kpse_find_file (file, format, format != vfpath);
  43.  
  44.   if (found_name)
  45.     {
  46.       strcpy (name, found_name);
  47.       ret = fopen (name, mode);
  48.       if (!ret)
  49.         FATAL_PERROR (name);
  50.       realnameoffile = name;
  51.     }
  52.   else
  53.     ret = NULL;
  54.  
  55.   return ret;
  56. }               /* end search */
  57.  
  58. FILE *
  59. pksearch(path, file, mode, dpi, name_ret, dpi_ret)
  60.         char *path, *file, *mode ;
  61.     char **name_ret ;
  62.     halfword dpi;
  63.     int *dpi_ret ;
  64. {
  65.   FILE *ret;
  66.   kpse_glyph_file_type font_file;
  67.   string found_name = kpse_find_pk (file, dpi, &font_file);
  68.   
  69.   if (found_name)
  70.     {
  71.       strcpy (name, found_name);
  72.       
  73.       ret = fopen (name, mode);
  74.       if (!ret)
  75.         FATAL_PERROR (name);
  76.       realnameoffile = name;
  77.       *name_ret = font_file.name;
  78.       *dpi_ret = font_file.dpi;
  79.     }
  80.   else
  81.     ret = NULL;
  82.  
  83.   return ret;
  84. }               /* end search */
  85.  
  86. /* do we report file openings? */
  87.  
  88. #ifdef DEBUG
  89. #  ifdef fopen
  90. #    undef fopen
  91. #  endif
  92. #  ifdef VMCMS  /* IBM: VM/CMS */
  93. #    define fopen cmsfopen
  94. #  endif /* IBM: VM/CMS */
  95. FILE *my_real_fopen(n, t)
  96. register char *n, *t ;
  97. {
  98.    FILE *tf ;
  99.    if (dd(D_FILES)) {
  100.       fprintf(stderr, "<%s(%s)> ", n, t) ;
  101.       tf = fopen(n, t) ;
  102.       if (tf == 0)
  103.          fprintf(stderr, "failed\n") ;
  104.       else
  105.          fprintf(stderr, "succeeded\n") ;
  106.    } else
  107.       tf = fopen(n, t) ;
  108. #ifdef OS2
  109.    if (tf == (FILE *)NULL)
  110.      tf = fat_fopen(n, t); /* try again with filename truncated to 8.3 */
  111. #endif
  112.    return tf ;
  113. }
  114. #endif
  115.  
  116. #ifdef OS2
  117. /* truncate filename at end of fname to FAT filesystem 8.3 limit */
  118. /* if truncated, return fopen() with new name */
  119. FILE *fat_fopen(fname, t)
  120. char *fname, *t;
  121. {
  122.    char *np;    /* pointer to name within path */
  123.    char nbuf[13], *ns, *nd;
  124.    char n[MAXPATHLEN];
  125.    int ni, ne;
  126.    FILE *tf;
  127.    strcpy(n, fname);
  128.    for (ns=n; *ns; ns++) {
  129.       if (*ns=='/')
  130.          *ns=DIRSEP;
  131.    }
  132.    np = strrchr(n,DIRSEP);
  133.    if (np==(char *)NULL)
  134.       np = n;
  135.    else
  136.       np++;
  137.    /* fail if it contains more than one '.' */
  138.    ni = 0;
  139.    for (ns=np; *ns; ns++) {
  140.       if (*ns=='.')
  141.          ni++;
  142.    }
  143.    if (ni>1)
  144.       return (FILE *)NULL;
  145.    /* copy it to nbuf, truncating to 8.3 */
  146.    ns = np;
  147.    nd = nbuf;
  148.    ni = 0;
  149.    while ((*ns!='.') && (*ns) && (ni<8)) {
  150.       *nd++ = *ns++;
  151.       ni++;
  152.    }
  153.    while ((*ns!='.') && (*ns)) {
  154.       ns++;
  155.       ni++;
  156.    }
  157.    ne = 0;
  158.    if (*ns=='.') {
  159.       *nd++ = *ns++;
  160.       while ((*ns!='.') && (*ns) && (ne<3)) {
  161.          *nd++ = *ns++;
  162.          ne++;
  163.       }
  164.       while (*ns) {
  165.          ns++;
  166.          ne++;
  167.       }
  168.    }
  169.    *nd++='\0';
  170.    if ((ni>8) || (ne>3)) {
  171.       strcpy(np,nbuf);
  172.       /* now code copied from my_real_fopen() */
  173.       if (dd(D_FILES)) {
  174.          fprintf(stderr, "<%s(%s)> ", n, t) ;
  175.          tf = fopen(n, t) ;
  176.          if (tf == 0)
  177.             fprintf(stderr, "failed\n") ;
  178.          else
  179.             fprintf(stderr, "succeeded\n") ;
  180.       }
  181.       else
  182.          tf = fopen(n, t) ;
  183.       return tf;
  184.    }
  185.    return (FILE *)NULL;
  186. }
  187. #endif
  188.