home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / unixtex-6.1b-src.tgz / tar.out / contrib / unixtex / xdvik / sfDir.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  4KB  |  160 lines

  1. /*
  2.  * Copyright 1989 Software Research Associates, Inc., Tokyo, Japan
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software and its
  5.  * documentation for any purpose and without fee is hereby granted, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Software Research Associates not be used
  9.  * in advertising or publicity pertaining to distribution of the software
  10.  * without specific, written prior permission.  Software Research Associates
  11.  * makes no representations about the suitability of this software for any
  12.  * purpose.  It is provided "as is" without express or implied warranty.
  13.  *
  14.  * SOFTWARE RESEARCH ASSOCIATES DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  15.  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
  16.  * IN NO EVENT SHALL SOFTWARE RESEARCH ASSOCIATES BE LIABLE FOR ANY SPECIAL,
  17.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  18.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  19.  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  20.  * PERFORMANCE OF THIS SOFTWARE.
  21.  *
  22.  * Author: Erik M. van der Poel
  23.  *         Software Research Associates, Inc., Tokyo, Japan
  24.  *         erik@sra.co.jp
  25.  */
  26.  
  27. #ifndef NOSELFILE /* whole file */
  28.  
  29. #include <stdio.h>
  30.  
  31. #ifdef SEL_FILE_IGNORE_CASE
  32. #include <ctype.h>
  33. #endif /* def SEL_FILE_IGNORE_CASE */
  34.  
  35. #include "sfinternal.h"
  36.  
  37. #include <kpathsea/config.h>
  38.  
  39. #include <kpathsea/c-dir.h>
  40. #include <kpathsea/c-stat.h>
  41.  
  42. #ifdef SEL_FILE_IGNORE_CASE
  43. int
  44. SFcompareEntries(p, q)
  45.     SFEntry    *p;
  46.     SFEntry    *q;
  47. {
  48.     register char    *r, *s;
  49.     register char    c1, c2;
  50.  
  51.     r = p->real;
  52.     s = q->real;
  53.  
  54.     c1 = *r++;
  55.     if (islower(c1)) {
  56.         c1 = toupper(c1);
  57.     }
  58.     c2 = *s++;
  59.     if (islower(c2)) {
  60.         c2 = toupper(c2);
  61.     }
  62.  
  63.     while (c1 == c2) {
  64.         if (!c1) {
  65.             return strcmp(p->real, q->real);
  66.         }
  67.         c1 = *r++;
  68.         if (islower(c1)) {
  69.             c1 = toupper(c1);
  70.         }
  71.         c2 = *s++;
  72.         if (islower(c2)) {
  73.             c2 = toupper(c2);
  74.         }
  75.     }
  76.  
  77.     return c1 - c2;
  78. }
  79. #else /* def SEL_FILE_IGNORE_CASE */
  80. int
  81. SFcompareEntries(p, q)
  82.     SFEntry    *p;
  83.     SFEntry    *q;
  84. {
  85.     return strcmp(p->real, q->real);
  86. }
  87. #endif /* def SEL_FILE_IGNORE_CASE */
  88.  
  89. int
  90. SFgetDir(dir)
  91.     SFDir    *dir;
  92. {
  93.     SFEntry        *result = NULL;
  94.     int        alloc = 0;
  95.     int        i;
  96.     DIR        *dirp;
  97.     struct dirent    *dp;
  98.     char        *str;
  99.     int        len;
  100.     int        maxChars;
  101.     struct stat    statBuf;
  102.  
  103.     maxChars = strlen(dir->dir) - 1;
  104.  
  105.     dir->entries = NULL;
  106.     dir->nEntries = 0;
  107.     dir->nChars = 0;
  108.  
  109.     result = NULL;
  110.     i = 0;
  111.  
  112.     dirp = opendir(".");
  113.     if (!dirp) {
  114.         return 1;
  115.     }
  116.  
  117.     (void) stat(".", &statBuf);
  118.     dir->mtime = statBuf.st_mtime;
  119.  
  120.     (void) readdir(dirp);    /* throw away "." */
  121.  
  122. #ifndef S_IFLNK
  123.     (void) readdir(dirp);    /* throw away ".." */
  124. #endif /* ndef S_IFLNK */
  125.  
  126.     while ((dp = readdir(dirp))) {
  127.         if (i >= alloc) {
  128.             alloc = 2 * (alloc + 1);
  129.             result = (SFEntry *) XtRealloc((char *) result,
  130.                 (unsigned) (alloc * sizeof(SFEntry)));
  131.         }
  132.         result[i].statDone = 0;
  133.         str = dp->d_name;
  134.         len = strlen(str);
  135.         result[i].real = XtMalloc((unsigned) (len + 2));
  136.         (void) strcat(strcpy(result[i].real, str), " ");
  137.         if (len > maxChars) {
  138.             maxChars = len;
  139.         }
  140.         result[i].shown = result[i].real;
  141.         i++;
  142.     }
  143.  
  144. #if defined(SVR4) || defined(SYSV) || defined(USG)
  145.     qsort((char *) result, (unsigned) i, sizeof(SFEntry), SFcompareEntries);
  146. #else /* defined(SVR4) || defined(SYSV) || defined(USG) */
  147.     qsort((char *) result, i, sizeof(SFEntry), SFcompareEntries);
  148. #endif /* defined(SVR4) || defined(SYSV) || defined(USG) */
  149.  
  150.     dir->entries = result;
  151.     dir->nEntries = i;
  152.     dir->nChars = maxChars + 1;
  153.  
  154.     closedir(dirp);
  155.  
  156.     return 0;
  157. }
  158.  
  159. #endif /* not NOSELFILE */
  160.