Debian Codegraph @

texk/xdvik/gui/sfDir.c

raw
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 * [email protected]
25 */
26
27 #include "xdvi-config.h"
28 #include "sfDir.h"
29
30 #if !defined(MOTIF) /* entire file */
31
32 #include <X11/Intrinsic.h>
33 #include <X11/StringDefs.h>
34 #include <X11/Xos.h>
35 #include <X11/Xaw/Text.h>
36 #include <X11/Xaw/AsciiText.h>
37 #undef wchar_t
38
39 #include <stdio.h>
40
41 #ifdef SEL_FILE_IGNORE_CASE
42 #include <ctype.h>
43 #endif /* def SEL_FILE_IGNORE_CASE */
44
45
46 #ifdef SEL_FILE_IGNORE_CASE
47 int
48 SFcompareEntries(const void *vp, const void *vq)
49 {
50 SFEntry *p = vp;
51 SFEntry *q = vq;
52 char *r, *s;
53 char c1, c2;
54
55 r = p->real;
56 s = q->real;
57
58 c1 = *r++;
59 if (islower(c1)) {
60 c1 = toupper(c1);
61 }
62 c2 = *s++;
63 if (islower(c2)) {
64 c2 = toupper(c2);
65 }
66
67 while (c1 == c2) {
68 if (!c1) {
69 return strcmp(p->real, q->real);
70 }
71 c1 = *r++;
72 if (islower(c1)) {
73 c1 = toupper(c1);
74 }
75 c2 = *s++;
76 if (islower(c2)) {
77 c2 = toupper(c2);
78 }
79 }
80
81 return c1 - c2;
82 }
83 #else /* def SEL_FILE_IGNORE_CASE */
84 int
85 SFcompareEntries(const void *vp, const void *vq)
86 {
87 const SFEntry *p = vp;
88 const SFEntry *q = vq;
89 return strcmp(p->real, q->real);
90 }
91 #endif /* def SEL_FILE_IGNORE_CASE */
92
93 int
94 SFgetDir(SFDir *dir)
95 {
96 SFEntry *result = NULL;
97 int alloc = 0;
98 int i;
99 DIR *dirp;
100 struct dirent *dp;
101 char *str;
102 int len;
103 int maxChars;
104 struct stat statBuf;
105
106 maxChars = strlen(dir->dir) - 1;
107
108 dir->entries = NULL;
109 dir->nEntries = 0;
110 dir->nChars = 0;
111
112 result = NULL;
113 i = 0;
114
115 dirp = opendir(".");
116 if (!dirp) {
117 return 1;
118 }
119
120 (void)stat(".", &statBuf);
121 dir->mtime = statBuf.st_mtime;
122
123 (void)readdir(dirp); /* throw away "." */
124
125 #ifndef S_IFLNK
126 (void)readdir(dirp); /* throw away ".." */
127 #endif /* ndef S_IFLNK */
128
129 while ((dp = readdir(dirp))) {
130 if (i >= alloc) {
131 alloc = 2 * (alloc + 1);
132 result = (SFEntry *) XtRealloc((char *)result,
133 (unsigned)(alloc * sizeof(SFEntry)));
134 }
135 result[i].statDone = 0;
136 str = dp->d_name;
137 len = strlen(str);
138 result[i].real = XtMalloc((unsigned)(len + 2));
139 (void)strcat(strcpy(result[i].real, str), " ");
140 if (len > maxChars) {
141 maxChars = len;
142 }
143 result[i].shown = result[i].real;
144 i++;
145 }
146
147 #if defined(SVR4) || defined(SYSV) || defined(USG)
148 qsort((char *)result, (unsigned)i, sizeof(SFEntry), SFcompareEntries);
149 #else /* defined(SVR4) || defined(SYSV) || defined(USG) */
150 qsort((char *)result, i, sizeof(SFEntry), SFcompareEntries);
151 #endif /* defined(SVR4) || defined(SYSV) || defined(USG) */
152
153 dir->entries = result;
154 dir->nEntries = i;
155 dir->nChars = maxChars + 1;
156
157 closedir(dirp);
158
159 return 0;
160 }
161
162 #else
163 /* silence `empty compilation unit' warnings */
164 static void bar(void); static void foo(void) { bar(); } static void bar(void) { foo(); }
165 #endif /* !defined(MOTIF) */