summaryrefslogtreecommitdiff
path: root/Build/source/texk/xdvik/gui/sfDir.c
blob: 3d2983c18ccc9aa7dcf38e8e261095ee1d23b7f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright 1989 Software Research Associates, Inc., Tokyo, Japan
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Software Research Associates not be used
 * in advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.  Software Research Associates
 * makes no representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 * SOFTWARE RESEARCH ASSOCIATES DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
 * IN NO EVENT SHALL SOFTWARE RESEARCH ASSOCIATES BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 * Author: Erik M. van der Poel
 *         Software Research Associates, Inc., Tokyo, Japan
 *         erik@sra.co.jp
 */

#include "xdvi-config.h"
#include "sfDir.h"

#if !defined(MOTIF) /* entire file */

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Xos.h>
#include <X11/Xaw/Text.h>
#include <X11/Xaw/AsciiText.h>
#undef wchar_t

#include <stdio.h>

#ifdef SEL_FILE_IGNORE_CASE
#include <ctype.h>
#endif /* def SEL_FILE_IGNORE_CASE */


#ifdef SEL_FILE_IGNORE_CASE
int
SFcompareEntries(const void *vp, const void *vq)
{
    SFEntry *p = vp;
    SFEntry *q = vq;
    char *r, *s;
    char c1, c2;

    r = p->real;
    s = q->real;

    c1 = *r++;
    if (islower(c1)) {
	c1 = toupper(c1);
    }
    c2 = *s++;
    if (islower(c2)) {
	c2 = toupper(c2);
    }

    while (c1 == c2) {
	if (!c1) {
	    return strcmp(p->real, q->real);
	}
	c1 = *r++;
	if (islower(c1)) {
	    c1 = toupper(c1);
	}
	c2 = *s++;
	if (islower(c2)) {
	    c2 = toupper(c2);
	}
    }

    return c1 - c2;
}
#else /* def SEL_FILE_IGNORE_CASE */
int
SFcompareEntries(const void *vp, const void *vq)
{
    const SFEntry *p = vp;
    const SFEntry *q = vq;
    return strcmp(p->real, q->real);
}
#endif /* def SEL_FILE_IGNORE_CASE */

int
SFgetDir(SFDir *dir)
{
    SFEntry *result = NULL;
    int alloc = 0;
    int i;
    DIR *dirp;
    struct dirent *dp;
    char *str;
    int len;
    int maxChars;
    struct stat statBuf;

    maxChars = strlen(dir->dir) - 1;

    dir->entries = NULL;
    dir->nEntries = 0;
    dir->nChars = 0;

    result = NULL;
    i = 0;

    dirp = opendir(".");
    if (!dirp) {
	return 1;
    }

    (void)stat(".", &statBuf);
    dir->mtime = statBuf.st_mtime;

    (void)readdir(dirp);	/* throw away "." */

#ifndef S_IFLNK
    (void)readdir(dirp);	/* throw away ".." */
#endif /* ndef S_IFLNK */

    while ((dp = readdir(dirp))) {
	if (i >= alloc) {
	    alloc = 2 * (alloc + 1);
	    result = (SFEntry *) XtRealloc((char *)result,
					   (unsigned)(alloc * sizeof(SFEntry)));
	}
	result[i].statDone = 0;
	str = dp->d_name;
	len = strlen(str);
	result[i].real = XtMalloc((unsigned)(len + 2));
	(void)strcat(strcpy(result[i].real, str), " ");
	if (len > maxChars) {
	    maxChars = len;
	}
	result[i].shown = result[i].real;
	i++;
    }

#if defined(SVR4) || defined(SYSV) || defined(USG)
    qsort((char *)result, (unsigned)i, sizeof(SFEntry), SFcompareEntries);
#else /* defined(SVR4) || defined(SYSV) || defined(USG) */
    qsort((char *)result, i, sizeof(SFEntry), SFcompareEntries);
#endif /* defined(SVR4) || defined(SYSV) || defined(USG) */

    dir->entries = result;
    dir->nEntries = i;
    dir->nChars = maxChars + 1;

    closedir(dirp);

    return 0;
}

#else
/* silence `empty compilation unit' warnings */
static void bar(void); static void foo(void) { bar(); } static void bar(void) { foo(); }
#endif /* !defined(MOTIF) */