home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / PRINTING / DVIPS386.ZIP / RESIDENT.C < prev    next >
C/C++ Source or Header  |  1991-11-03  |  20KB  |  737 lines

  1. /*
  2.  *   This code reads in and handles the defaults for the program from the
  3.  *   file config.sw.  This entire file is a bit kludgy, sorry.
  4.  */
  5. #include "structures.h" /* The copyright notice in that file is included too! */
  6. #include "paths.h"
  7. /*
  8.  *   This is the structure definition for resident fonts.  We use
  9.  *   a small and simple hash table to handle these.  We don't need
  10.  *   a big hash table.
  11.  */
  12. struct resfont *reshash[RESHASHPRIME] ;
  13. /*
  14.  *   These are the external routines we use.
  15.  */
  16. extern void error() ;
  17. extern integer scalewidth() ;
  18. extern void tfmload() ;
  19. extern FILE *search() ;
  20. extern shalfword pkbyte() ;
  21. extern integer pkquad() ;
  22. extern integer pktrio() ;
  23. extern Boolean pkopen() ;
  24. extern char *strcpy() ;
  25. extern char *getenv() ;
  26. extern char *newstring() ;
  27. extern int add_header() ;
  28. extern int add_name() ;
  29. extern char *get_name() ;
  30. extern int system() ;
  31. /*
  32.  *   These are the external variables we use.
  33.  */
  34. #ifdef DEBUG
  35. extern integer debug_flag;
  36. #endif  /* DEBUG */
  37. extern long bytesleft ;
  38. extern quarterword *raster ;
  39. extern FILE *pkfile ;
  40. extern char *oname ;
  41. extern integer swmem, fontmem ;
  42. extern char *tfmpath ;
  43. extern char *pkpath ;
  44. extern char *vfpath ;
  45. extern char *figpath ;
  46. extern char *configpath ;
  47. #ifdef SEARCH_SUBDIRECTORIES
  48. extern char *fontsubdirpath ;
  49. #endif
  50. #ifdef FONTLIB
  51. extern char *flipath, *fliname ;
  52. #endif
  53. extern char *headerpath ;
  54. extern char *paperfmt ; 
  55. extern char *nextstring ;
  56. extern char *maxstring ;
  57. extern char *warningmsg ;
  58. extern Boolean disablecomments ;
  59. extern Boolean compressed ;
  60. extern int quiet ;
  61. extern int filter ;
  62. extern Boolean reverse ;
  63. extern Boolean usesPSfonts ;
  64. extern Boolean nosmallchars ;
  65. extern Boolean removecomments ;
  66. extern Boolean safetyenclose ;
  67. extern int actualdpi ;
  68. extern int vactualdpi ;
  69. extern int maxdrift ;
  70. extern int vmaxdrift ;
  71. extern char *printer ;
  72. extern char *mfmode ;
  73. extern int lastresortsizes[] ;
  74. /*
  75.  *   To maintain a list of document fonts, we use the following
  76.  *   pointer.
  77.  */
  78. struct header_list *ps_fonts_used ;
  79. /*
  80.  *   We use malloc here.
  81.  */
  82. /* char *malloc() ; */
  83. void* malloc(unsigned) ;
  84. /*
  85.  *   Our hash routine.
  86.  */
  87. int
  88. hash(s)
  89.    char *s ;
  90. {
  91.    int h = 12 ;
  92.  
  93.    while (*s != 0)
  94.       h = (h + h + *s++) % RESHASHPRIME ;
  95.    return(h) ;
  96. }
  97. /*
  98.  *   cleanres() marks all resident fonts as not being yet sent.
  99.  */
  100. void
  101. cleanres() {
  102.    register int i ;
  103.    register struct resfont *p ;
  104.    for (i=0; i<RESHASHPRIME; i++)
  105.       for (p=reshash[i]; p; p=p->next)
  106.          p->sent = 0 ;
  107. }
  108. /*
  109.  *   The routine that looks up a font name.
  110.  */
  111. struct resfont *
  112. lookup(name)
  113.    char *name ;
  114. {
  115.    struct resfont *p ;
  116.  
  117.    for (p=reshash[hash(name)]; p!=NULL; p=p->next)
  118.       if (strcmp(p->Keyname, name)==0)
  119.          return(p) ;
  120.    return(NULL) ;
  121. }
  122. /*
  123.  *   This routine adds an entry.
  124.  */
  125. void
  126. add_entry(Keyname, TeXname, PSname, specinfo, downloadinfo)
  127.    char *Keyname, *TeXname, *PSname, *specinfo, *downloadinfo ;
  128. {
  129.    struct resfont *p ;
  130.    int h ;
  131.  
  132.    if (PSname == NULL)
  133.       PSname = TeXname ;
  134.    else if (strcmp(PSname, TeXname) && Keyname != PSname)
  135.       add_entry(PSname, TeXname, PSname, specinfo, downloadinfo) ;
  136.    p = (struct resfont *)malloc((unsigned int)sizeof(struct resfont)) ;
  137.    if (p==NULL)
  138.       error("! out of memory") ;
  139.    p->Keyname = Keyname ;
  140.    p->PSname = PSname ;
  141.    p->TeXname = TeXname ;
  142.    p->specialinstructions = specinfo ;
  143.    p->downloadheader = downloadinfo ;
  144.    h = hash(Keyname) ;
  145.    p->next = reshash[h] ;
  146.    p->sent = 0 ;
  147.    reshash[h] = p ;
  148. }
  149. /*
  150.  *   Now our residentfont routine.
  151.  */
  152. Boolean
  153. residentfont(curfnt)
  154.         register fontdesctype *curfnt ;
  155. {
  156.    register shalfword i ;
  157.    struct resfont *p ;
  158.  
  159. /*
  160.  *   First we determine if we can find this font in the resident list.
  161.  */
  162.    if (*curfnt->area)
  163.       return 0 ; /* resident fonts never have a nonstandard font area */
  164.    if ((p=lookup(curfnt->name))==NULL)
  165.       return 0 ;
  166. /*
  167.  *   We clear out some pointers:
  168.  */
  169. #ifdef DEBUG
  170.    if (dd(D_FONTS))
  171.         (void)fprintf(stderr,"Font %s <%s> is resident.\n",
  172.                                      curfnt->name, p->PSname) ;
  173. #endif  /* DEBUG */
  174.    curfnt->resfont = p ;
  175.    curfnt->name = p->TeXname ;
  176.    for (i=0; i<256; i++) {
  177.       curfnt->chardesc[i].TFMwidth = 0 ;
  178.       curfnt->chardesc[i].packptr = NULL ;
  179.       curfnt->chardesc[i].pixelwidth = 0 ;
  180.       curfnt->chardesc[i].flags = 0 ;
  181.    }
  182.    add_name(p->PSname, &ps_fonts_used) ;
  183. /*
  184.  *   We include the font here.  But we only should need to include the
  185.  *   font if we have a stupid spooler; smart spoolers should be able
  186.  *   to supply it automatically.
  187.  */
  188.    if (p->downloadheader)
  189.       if (add_header(p->downloadheader)) {
  190.          swmem -= DNFONTCOST ;
  191.          fontmem -= DNFONTCOST ;
  192.       }
  193.    tfmload(curfnt) ;
  194.    usesPSfonts = 1 ;
  195.    return(1) ;
  196. }
  197. #define INLINE_SIZE (500)
  198. static char was_inline[INLINE_SIZE] ;
  199. void
  200. bad_config() {
  201.    extern void exit() ;
  202.  
  203.    error("Error in config file:") ;
  204.    (void)fputs(was_inline, stderr) ;
  205.    exit(1) ;
  206. }
  207. /*
  208.  *   Now we have the getdefaults routine.
  209.  */
  210. void
  211. getdefaults(s)
  212. char *s ;
  213. {
  214.    FILE *deffile ;
  215.    char PSname[300] ;
  216.    register char *p ;
  217.    char *specinfo, *downloadinfo ;
  218.    int i, j ;
  219.    static int again = 0 ;
  220.    char *d = configpath ;
  221.  
  222.    if (printer == NULL) {
  223.       if (s) {
  224.          strcpy(PSname, s) ;
  225.       } else {
  226.          d = "~" ;
  227.          strcpy(PSname, DVIPSRC) ;
  228.       }
  229.    } else {
  230.       strcpy(PSname, "config.") ;
  231.       strcat(PSname, printer) ;
  232.    }
  233.    if ((deffile=search(d,PSname,READ))!=NULL) {
  234.       while (fgets(was_inline, INLINE_SIZE, deffile)!=NULL) {
  235. /*
  236.  *   We need to get rid of the newline.
  237.  */
  238.        for (p=was_inline; *p; p++) ;
  239.        if (p > was_inline) *(p-1) = 0 ;
  240.        switch (was_inline[0]) {
  241. case 'm' :
  242. #ifdef SHORTINT
  243.          if (sscanf(was_inline+1, "%ld", &swmem) != 1) bad_config() ;
  244. #else   /* ~SHORTINT */
  245.          if (sscanf(was_inline+1, "%d", &swmem) != 1) bad_config() ;
  246. #endif  /* ~SHORTINT */
  247.          break ;
  248. case 'M' :
  249.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  250.          mfmode = newstring(PSname) ;
  251.          break ;
  252. case 'o' : case 'O' :
  253.          p = was_inline + 1 ;
  254. #ifdef VMS
  255.      p[strlen(p) - 1] = '\0';
  256. #endif /* VMS */
  257.          while (*p && *p <= ' ')
  258.             p++ ;
  259.          oname = newstring(p) ;
  260.          break ;
  261. #ifdef FONTLIB
  262. case 'L' : 
  263.          {
  264.             char tempname[300] ;
  265.             extern char *fliparse() ;
  266.             if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  267.             else {
  268.                flipath = newstring(fliparse(PSname,tempname));
  269.                fliname = newstring(tempname) ;
  270.             }
  271.      }
  272.          break ;
  273. #endif
  274. case 'T' : 
  275.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  276.          else tfmpath = newstring(PSname) ;
  277.          break ;
  278. case 'P' : case 'p' :
  279.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  280.          else pkpath = newstring(PSname) ;
  281.          break ;
  282. case 'V' : case 'v' :
  283.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  284.          else vfpath = newstring(PSname) ;
  285.          break ;
  286. case 'S' :
  287.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  288.          else figpath = newstring(PSname) ;
  289.          break ;
  290. case 's':
  291.          safetyenclose = 1 ;
  292.          break ;
  293. case 'H' : 
  294.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  295.          else headerpath = newstring(PSname) ;
  296.          break ;
  297. case 't' : 
  298.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  299.          else paperfmt = newstring(PSname) ;
  300.          break ;
  301. case ' ' : case '*' : case '#' : case ';' : case '=' : case 0 : case '\n' :
  302.          break ;
  303. case 'r' :
  304.          reverse = (was_inline[1] != '0') ;
  305.          break ;
  306. /*
  307.  *   This case is for last resort font scaling; I hate this, but enough
  308.  *   people have in no uncertain terms demanded it that I'll go ahead and
  309.  *   add it.
  310.  *
  311.  *   This line must have numbers on it, resolutions, to search for the
  312.  *   font as a last resort, and then the font will be scaled.  These
  313.  *   resolutions should be in increasing order.
  314.  *
  315.  *   For most machines, just `300' is sufficient here; on the NeXT,
  316.  *   `300 400' may be more appropriate.
  317.  */
  318. case 'R':
  319.          i = 0 ;
  320.          p = was_inline + 1 ;
  321.          while (*p) {
  322.             while (*p && *p <= ' ')
  323.                p++ ;
  324.             if ('0' <= *p && *p <= '9') {
  325.                j = 0 ;
  326.                while ('0' <= *p && *p <= '9')
  327.                   j = 10 * j + (*p++ - '0') ;
  328.                if (i > 0)
  329.                   if (lastresortsizes[i-1] > j) {
  330.                      error("last resort sizes (R) must be sorted") ;
  331.                      bad_config() ;
  332.                   }
  333.                lastresortsizes[i++] = j ;
  334.             } else {
  335.                if (*p == 0)
  336.                   break ;
  337.                error("! only numbers expected on `R' line in config!") ;
  338.             }
  339.          }
  340.          lastresortsizes[i] = 32000 ;
  341.          break ;
  342. case 'D' :
  343.          if (sscanf(was_inline+1, "%d", &actualdpi) != 1) bad_config() ;
  344.          if (actualdpi < 10 || actualdpi > 10000) bad_config() ;
  345.      vactualdpi = actualdpi;
  346.          break ;
  347. /*
  348.  *   Execute a command.  This can be dangerous, but can also be very useful.
  349.  */
  350. case 'E' :
  351. #ifdef SECURE
  352.          error("dvips was compiled with SECURE, which disables E in config") ;
  353. #else
  354.          (void)system(was_inline+1) ;
  355. #endif
  356.          break ;
  357. case 'K':
  358.          removecomments = (was_inline[1] != '0') ;
  359.          break ;
  360. case 'U':
  361.          nosmallchars = (was_inline[1] != '0') ;
  362.          break ;
  363. case 'W':
  364.          for (p=was_inline+1; *p && *p <= ' '; p++) ;
  365.          if (*p)
  366.             warningmsg = newstring(p) ;
  367.          else
  368.             warningmsg = 0 ;
  369.          break ;
  370. case 'X' :
  371.          if (sscanf(was_inline+1, "%d", &actualdpi) != 1) bad_config() ;
  372.          if (actualdpi < 10 || actualdpi > 10000) bad_config() ;
  373.          break ;
  374. case 'Y' :
  375.          if (sscanf(was_inline+1, "%d", &vactualdpi) != 1) bad_config() ;
  376.          if (vactualdpi < 10 || vactualdpi > 10000) bad_config() ;
  377.          break ;
  378. case 'e' :
  379.          if (sscanf(was_inline+1, "%d", &maxdrift) != 1) bad_config() ;
  380.          if (maxdrift < 0) bad_config() ;
  381.      vmaxdrift = maxdrift;
  382.          break ;
  383. case 'q' : case 'Q' :
  384.          quiet = (was_inline[1] != '0') ;
  385.          break ;
  386. case 'f' : case 'F' :
  387.          filter = (was_inline[1] != '0') ;
  388.          break ;
  389. case 'h' : 
  390.          if (sscanf(was_inline+1, "%s", PSname) != 1) bad_config() ;
  391.          else (void)add_header(PSname) ;
  392.          break ;
  393. case 'N' :
  394.          disablecomments = (was_inline[1] != '0') ;
  395.          break ;
  396. case 'Z' :
  397.          compressed = (was_inline[1] != '0') ;
  398.          break ;
  399. default:
  400.          bad_config() ;
  401.       }
  402.      }
  403.      (void)fclose(deffile) ;
  404.    } else {
  405.       if (printer)
  406.          error("! no such printer (can't find corresponding config file)") ;
  407.    }
  408.    if (again)
  409.       return ;
  410.    else
  411.       again = 1 ;
  412.    if ((deffile=search(configpath,PSMAPFILE,READ))!=NULL) {
  413.      while (fgets(was_inline, INLINE_SIZE, deffile)!=NULL) {
  414.          char *TeXname = NULL ;
  415.          char *PSname = NULL ;
  416.          specinfo = NULL ;
  417.          downloadinfo = NULL ;
  418.          p = was_inline ;
  419.          while (*p) {
  420.             while (*p && *p <= ' ')
  421.                p++ ;
  422.             if (*p) {
  423.                if (*p == '"')
  424.                   specinfo = p + 1 ;
  425.                else if (*p == '<')
  426.                   downloadinfo = p + 1 ;
  427.                else if (TeXname)
  428.                   PSname = p ;
  429.                else
  430.                   TeXname = p ;
  431.                if (*p == '"') {
  432.                   p++ ;
  433.                   while (*p != '"' && *p)
  434.                      p++ ;
  435.                } else
  436.                   while (*p > ' ')
  437.                      p++ ;
  438.                if (*p)
  439.                   *p++ = 0 ;
  440.             }
  441.          }
  442.          if (TeXname) {
  443.             TeXname = newstring(TeXname) ;
  444.             specinfo = newstring(specinfo) ;
  445.             PSname = newstring(PSname) ;
  446.             downloadinfo = newstring(downloadinfo) ;
  447.             add_entry(TeXname, TeXname, PSname, specinfo, downloadinfo) ;
  448.      }
  449.       }
  450.       (void)fclose(deffile) ;
  451.    }
  452. }
  453. /*
  454.  *   Get environment variables! These override entries in ./config.h.
  455.  *   We substitute everything of the form ::, ^: or :$ with default,
  456.  *   so a user can easily build on to the existing paths.
  457.  */
  458. static char *getenvup(who, what)
  459. char *who, *what ;
  460. {
  461.    char *p  ;
  462.  
  463.    if (p=getenv(who)) {
  464.       register char *pp, *qq ;
  465.       int lastsep = 1 ;
  466.  
  467.       for (pp=nextstring, qq=p; *qq;) {
  468.          if (*qq == PATHSEP) {
  469.             if (lastsep) {
  470.                strcpy(pp, what) ;
  471.                pp = pp + strlen(pp) ;
  472.             }
  473.             lastsep = 1 ;
  474.          } else
  475.             lastsep = 0 ;
  476.          *pp++ = *qq++ ;
  477.       }
  478.       if (lastsep) {
  479.          strcpy(pp, what) ;
  480.          pp = pp + strlen(pp) ;
  481.       }
  482.       *pp = 0 ;
  483.       qq = nextstring ;
  484.       nextstring = pp + 1 ;
  485.       return qq ;
  486.    } else
  487.       return what ;
  488. }
  489. void checkenv(which)
  490. int which ;
  491. {
  492.    if (which) {
  493.       tfmpath = getenvup("TEXFONTS", tfmpath) ;
  494.       if (getenv("TEXPKS"))
  495.          pkpath = getenvup("TEXPKS", pkpath) ;
  496.       else if (getenv("PKFONTS"))
  497.          pkpath = getenvup("PKFONTS", pkpath) ;
  498. /* this is not a good idea on most systems.  tgr
  499.       else if (getenv("TEXFONTS"))
  500.          pkpath = getenvup("TEXFONTS", pkpath) ; */
  501.       figpath = getenvup("TEXINPUTS", figpath) ;
  502. #ifdef SEARCH_SUBDIRECTORIES
  503.       if (getenv ("TEXFONTS_SUBDIR"))
  504.          fontsubdirpath = getenvup ("TEXFONTS_SUBDIR", fontsubdirpath);
  505.       {
  506.          char *do_subdir_path();
  507.          static char *concat3();
  508.          char *dirs = do_subdir_path (fontsubdirpath);
  509.          /* If the paths were in dynamic storage before, that memory is
  510.             wasted now.  */
  511.          tfmpath = concat3 (tfmpath, ":", dirs);
  512.          pkpath = concat3 (pkpath, ":", dirs);
  513.       }
  514. #endif
  515.    } else
  516.       configpath = getenvup("TEXCONFIG", configpath) ;
  517. }
  518.  
  519. #ifdef SEARCH_SUBDIRECTORIES
  520.  
  521. #include <sys/types.h>
  522. #include <sys/stat.h>
  523. #include <errno.h>
  524.  
  525. #ifdef SYSV
  526. #include <dirent.h>
  527. typedef struct dirent *directory_entry_type;
  528. #else
  529. #include <sys/dir.h>
  530. typedef struct direct *directory_entry_type;
  531. #endif
  532.  
  533. /* Declare the routine to get the current working directory.  */
  534.  
  535. #ifndef HAVE_GETCWD
  536. extern char *getwd ();
  537. #define getcwd(b, len)  ((b) ? getwd (b) : getwd (xmalloc (len)))
  538. #else
  539. #ifdef ANSI
  540. extern char *getcwd (char *, int);
  541. #else
  542. extern char *getcwd ();
  543. #endif /* not ANSI */
  544. #endif /* not HAVE_GETWD */
  545.  
  546. #ifdef SYSV
  547. #define MAXPATHLEN (256)
  548. #else
  549. #ifdef VMS
  550. #define MAXPATHLEN (256)
  551. #else   /* ~SYSV */
  552. #include <sys/param.h>          /* for MAXPATHLEN */
  553. #endif  /* ~SYSV */
  554. #endif
  555.  
  556. extern void exit(), free() ;
  557. extern int chdir() ;
  558.  
  559. /* Memory operations: variants of malloc(3) and realloc(3) that just
  560.    give up the ghost when they fail.  */
  561.  
  562. extern char *realloc ();
  563.  
  564. char *
  565. xmalloc (size)
  566.   unsigned size;
  567. {
  568.   char *mem = malloc (size);
  569.   
  570.   if (mem == NULL)
  571.     {
  572.       fprintf (stderr, "! Cannot allocate %u bytes.\n", size);
  573.       exit (10);
  574.     }
  575.   
  576.   return mem;
  577. }
  578.  
  579.  
  580. char *
  581. xrealloc (ptr, size)
  582.   char *ptr;
  583.   unsigned size;
  584. {
  585.   char *mem = realloc (ptr, size);
  586.   
  587.   if (mem == NULL)
  588.     {
  589.       fprintf (stderr, "! Cannot reallocate %u bytes at %x.\n", size, ptr);
  590.       exit (10);
  591.     }
  592.     
  593.   return mem;
  594. }
  595.  
  596.  
  597. /* Return, in NAME, the next component of PATH, i.e., the characters up
  598.    to the next PATHSEP.  */
  599.    
  600. static void
  601. next_component (name, path)
  602.   char name[];
  603.   char **path;
  604. {
  605.   unsigned count = 0;
  606.   
  607.   while (**path != 0 && **path != PATHSEP)
  608.     {
  609.       name[count++] = **path;
  610.       (*path)++; /* Move further along, even between calls.  */
  611.     }
  612.   
  613.   name[count] = 0;
  614.   if (**path == PATHSEP)
  615.     (*path)++; /* Move past the delimiter.  */
  616. }
  617.  
  618.  
  619. #ifndef _POSIX_SOURCE
  620. #define S_ISDIR(m) ((m & S_IFMT) == S_IFDIR)
  621. #endif
  622.  
  623. /* Return true if FN is a directory or a symlink to a directory,
  624.    false if not. */
  625.  
  626. int
  627. is_dir (fn)
  628.   char *fn;
  629. {
  630.   struct stat stats;
  631.  
  632.   return stat (fn, &stats) == 0 && S_ISDIR (stats.st_mode);
  633. }
  634.  
  635.  
  636. static char *
  637. concat3 (s1, s2, s3)
  638.   char *s1, *s2, *s3;
  639. {
  640.   char *r = xmalloc (strlen (s1) + strlen (s2) + strlen (s3) + 1);
  641.   strcpy (r, s1);
  642.   strcat (r, s2);
  643.   strcat (r, s3);
  644.   return r;
  645. }
  646.  
  647.  
  648. /* DIR_LIST is the default list of directories (colon-separated) to
  649.    search.  We want to add all the subdirectories directly below each of
  650.    the directories in the path.
  651.      
  652.    We return the list of directories found.  */
  653.  
  654. char *
  655. do_subdir_path (dir_list)
  656.   char *dir_list;
  657. {
  658.   char *cwd;
  659.   unsigned len;
  660.   char *result = xmalloc (1);
  661.   char *temp = dir_list;
  662.  
  663.   /* Make a copy in writable memory.  */
  664.   dir_list = xmalloc (strlen (temp) + 1);
  665.   strcpy (dir_list, temp);
  666.   
  667.   *result = 0;
  668.  
  669.   /* Unfortunately, we can't look in the environment for the current
  670.      directory, because if we are running under a program (let's say
  671.      Emacs), the PWD variable might have been set by Emacs' parent
  672.      to the current directory at the time Emacs was invoked.  This
  673.      is not necessarily the same directory the user expects to be
  674.      in.  So, we must always call getcwd(3) or getwd(3), even though
  675.      they are slow and prone to hang in networked installations.  */
  676.   cwd = getcwd (NULL, MAXPATHLEN + 2);
  677.   if (cwd == NULL)
  678.     {
  679.       perror ("getcwd");
  680.       exit (errno);
  681.     }
  682.  
  683.   do
  684.     {
  685.       DIR *dir;
  686.       directory_entry_type e;
  687.       char dirname[MAXPATHLEN];
  688.  
  689.       next_component (dirname, &dir_list);
  690.  
  691.       /* All the `::'s should be gone by now, but we may as well make
  692.          sure `chdir' doesn't crash.  */
  693.       if (*dirname == 0) continue;
  694.  
  695.       /* By changing directories, we save a bunch of string
  696.          concatenations (and make the pathnames the kernel looks up
  697.          shorter).  */
  698.       if (chdir (dirname) != 0) continue;
  699.  
  700.       dir = opendir (".");
  701.       if (dir == NULL) continue;
  702.  
  703.       while ((e = readdir (dir)) != NULL)
  704.         {
  705.           if (is_dir (e->d_name) && strcmp (e->d_name, ".") != 0
  706.               && strcmp (e->d_name, "..") != 0)
  707.             {
  708.               char *found = concat3 (dirname, "/", e->d_name);
  709.  
  710.               result = xrealloc (result, strlen (result) + strlen (found) + 2);
  711.  
  712.               len = strlen (result);
  713.               if (len > 0)
  714.                 {
  715.                   result[len] = PATHSEP;
  716.                   result[len + 1] = 0;
  717.                 }
  718.               strcat (result, found);
  719.               free (found);
  720.             }
  721.         }
  722.       closedir (dir);
  723.  
  724.       /* Change back to the current directory, in case the path
  725.          contains relative directory names.  */
  726.       if (chdir (cwd) != 0)
  727.         {
  728.           perror (cwd);
  729.           exit (errno);
  730.         }
  731.     }
  732.   while (*dir_list != 0);
  733.   
  734.   return result;
  735. }
  736. #endif /* SEARCH_SUBDIRECTORIES */
  737.