~ubuntu-branches/ubuntu/lucid/graphviz/lucid-security

« back to all changes in this revision

Viewing changes to lefty/ws/x11/libfilereq/Dir.c

  • Committer: Bazaar Package Importer
  • Author(s): Stephen M Moraco
  • Date: 2002-02-05 18:52:12 UTC
  • Revision ID: james.westby@ubuntu.com-20020205185212-8i04c70te00rc40y
Tags: upstream-1.7.16
ImportĀ upstreamĀ versionĀ 1.7.16

Show diffs side-by-side

added added

removed removed

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