Back to index

tetex-bin  3.0
sfDir.c
Go to the documentation of this file.
00001 /*
00002  * Copyright 1989 Software Research Associates, Inc., Tokyo, Japan
00003  *
00004  * Permission to use, copy, modify, and distribute this software and its
00005  * documentation for any purpose and without fee is hereby granted, provided
00006  * that the above copyright notice appear in all copies and that both that
00007  * copyright notice and this permission notice appear in supporting
00008  * documentation, and that the name of Software Research Associates not be used
00009  * in advertising or publicity pertaining to distribution of the software
00010  * without specific, written prior permission.  Software Research Associates
00011  * makes no representations about the suitability of this software for any
00012  * purpose.  It is provided "as is" without express or implied warranty.
00013  *
00014  * SOFTWARE RESEARCH ASSOCIATES DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
00015  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
00016  * IN NO EVENT SHALL SOFTWARE RESEARCH ASSOCIATES BE LIABLE FOR ANY SPECIAL,
00017  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00018  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00019  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00020  * PERFORMANCE OF THIS SOFTWARE.
00021  *
00022  * Author: Erik M. van der Poel
00023  *         Software Research Associates, Inc., Tokyo, Japan
00024  *         erik@sra.co.jp
00025  */
00026 
00027 #include "xdvi-config.h"
00028 #include "sfDir.h"
00029 
00030 #if !defined(MOTIF) /* entire file */
00031 
00032 #include <X11/Intrinsic.h>
00033 #include <X11/StringDefs.h>
00034 #include <X11/Xos.h>
00035 #include <X11/Xaw/Text.h>
00036 #include <X11/Xaw/AsciiText.h>
00037 #undef wchar_t
00038 
00039 #include <stdio.h>
00040 
00041 #ifdef SEL_FILE_IGNORE_CASE
00042 #include <ctype.h>
00043 #endif /* def SEL_FILE_IGNORE_CASE */
00044 
00045 
00046 #ifdef SEL_FILE_IGNORE_CASE
00047 int
00048 SFcompareEntries(const void *vp, const void *vq)
00049 {
00050     SFEntry *p = vp;
00051     SFEntry *q = vq;
00052     char *r, *s;
00053     char c1, c2;
00054 
00055     r = p->real;
00056     s = q->real;
00057 
00058     c1 = *r++;
00059     if (islower(c1)) {
00060        c1 = toupper(c1);
00061     }
00062     c2 = *s++;
00063     if (islower(c2)) {
00064        c2 = toupper(c2);
00065     }
00066 
00067     while (c1 == c2) {
00068        if (!c1) {
00069            return strcmp(p->real, q->real);
00070        }
00071        c1 = *r++;
00072        if (islower(c1)) {
00073            c1 = toupper(c1);
00074        }
00075        c2 = *s++;
00076        if (islower(c2)) {
00077            c2 = toupper(c2);
00078        }
00079     }
00080 
00081     return c1 - c2;
00082 }
00083 #else /* def SEL_FILE_IGNORE_CASE */
00084 int
00085 SFcompareEntries(const void *vp, const void *vq)
00086 {
00087     const SFEntry *p = vp;
00088     const SFEntry *q = vq;
00089     return strcmp(p->real, q->real);
00090 }
00091 #endif /* def SEL_FILE_IGNORE_CASE */
00092 
00093 int
00094 SFgetDir(SFDir *dir)
00095 {
00096     SFEntry *result = NULL;
00097     int alloc = 0;
00098     int i;
00099     DIR *dirp;
00100     struct dirent *dp;
00101     char *str;
00102     int len;
00103     int maxChars;
00104     struct stat statBuf;
00105 
00106     maxChars = strlen(dir->dir) - 1;
00107 
00108     dir->entries = NULL;
00109     dir->nEntries = 0;
00110     dir->nChars = 0;
00111 
00112     result = NULL;
00113     i = 0;
00114 
00115     dirp = opendir(".");
00116     if (!dirp) {
00117        return 1;
00118     }
00119 
00120     (void)stat(".", &statBuf);
00121     dir->mtime = statBuf.st_mtime;
00122 
00123     (void)readdir(dirp);    /* throw away "." */
00124 
00125 #ifndef S_IFLNK
00126     (void)readdir(dirp);    /* throw away ".." */
00127 #endif /* ndef S_IFLNK */
00128 
00129     while ((dp = readdir(dirp))) {
00130        if (i >= alloc) {
00131            alloc = 2 * (alloc + 1);
00132            result = (SFEntry *) XtRealloc((char *)result,
00133                                       (unsigned)(alloc * sizeof(SFEntry)));
00134        }
00135        result[i].statDone = 0;
00136        str = dp->d_name;
00137        len = strlen(str);
00138        result[i].real = XtMalloc((unsigned)(len + 2));
00139        (void)strcat(strcpy(result[i].real, str), " ");
00140        if (len > maxChars) {
00141            maxChars = len;
00142        }
00143        result[i].shown = result[i].real;
00144        i++;
00145     }
00146 
00147 #if defined(SVR4) || defined(SYSV) || defined(USG)
00148     qsort((char *)result, (unsigned)i, sizeof(SFEntry), SFcompareEntries);
00149 #else /* defined(SVR4) || defined(SYSV) || defined(USG) */
00150     qsort((char *)result, i, sizeof(SFEntry), SFcompareEntries);
00151 #endif /* defined(SVR4) || defined(SYSV) || defined(USG) */
00152 
00153     dir->entries = result;
00154     dir->nEntries = i;
00155     dir->nChars = maxChars + 1;
00156 
00157     closedir(dirp);
00158 
00159     return 0;
00160 }
00161 
00162 #else
00163 /* silence `empty compilation unit' warnings */
00164 static void bar(void); static void foo() { bar(); } static void bar(void) { foo(); }
00165 #endif /* !defined(MOTIF) */