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