forked from alexfru/dflat20
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdirect.c
184 lines (163 loc) · 4.08 KB
/
direct.c
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* ---------- direct.c --------- */
#include "dflat.h"
#include <dirent.h>
#include <sys/stat.h>
/* Check if fspec is a directory, if so chdir and return TRUE */
int CheckAndChangeDir(char *fspec)
{
char *cp;
char path[MAXPATH];
if (chdir(fspec) == 0)
return TRUE;
strcpy(path, fspec);
cp = strrchr(path, '/');
if (cp) {
if (cp != path)
*cp = '\0';
if (chdir(path) == 0)
return TRUE;
}
return FALSE;
}
/*
* Routine to see if a text string is matched by a wildcard pattern.
* Returns TRUE if the text is matched, or FALSE if it is not matched
* or if the pattern is invalid.
* * matches zero or more characters
* ? matches a single character
* [abc] matches 'a', 'b' or 'c'
* \c quotes character c
*
* Adapted from code written by Ingo Wilken.
* Copyright (c) 1993 by David I. Bell
* Permission is granted to use, distribute, or modify this source,
* provided that this copyright notice remains intact.
*/
static int match(char *text, char *pattern)
{
char *retrypat;
char *retrytxt;
int ch;
int found;
if (pattern[0] == '\0')
return TRUE;
retrypat = NULL;
retrytxt = NULL;
while (*text || *pattern) {
ch = *pattern++;
switch (ch) {
case '*':
retrypat = pattern;
retrytxt = text;
break;
case '[':
found = FALSE;
while ((ch = *pattern++) != ']') {
if (ch == '\\')
ch = *pattern++;
if (ch == '\0')
return FALSE;
if (*text == ch)
found = TRUE;
}
if (!found) {
pattern = retrypat;
text = ++retrytxt;
}
/* fall into next case */
case '?':
if (*text++ == '\0')
return FALSE;
break;
case '\\':
ch = *pattern++;
if (ch == '\0')
return FALSE;
/* fall into next case */
default:
if (*text == ch) {
if (*text)
text++;
break;
}
if (*text) {
pattern = retrypat;
text = ++retrytxt;
break;
}
return FALSE;
}
if (pattern == NULL)
return FALSE;
}
return TRUE;
}
static int dircmp(const void *c1, const void *c2)
{
return strcasecmp(*(char **)c1, *(char **)c2);
}
static BOOL BuildList(WINDOW wnd, char *fspec, BOOL dirs)
{
CTLWINDOW *ct = FindCommand(wnd->extension,
dirs? ID_DIRECTORY : ID_FILES,LISTBOX);
WINDOW lwnd;
char **dirlist = NULL;
if (ct != NULL) {
DIR *dirp;
int i = 0, j;
struct dirent *dp;
struct stat sb;
lwnd = ct->wnd;
SendMessage(lwnd, CLEARTEXT, 0, 0);
dirp = opendir(".");
if (dirp) {
while ((dp = readdir(dirp)) != NULL) {
if (dp->d_name[0] == '.' && dp->d_name[1] != '.')
continue;
if (stat(dp->d_name, &sb) < 0)
continue;
if (S_ISDIR(sb.st_mode) == dirs) {
if (match(dp->d_name, fspec)) {
dirlist = DFrealloc(dirlist, sizeof(char *)*(i+1));
dirlist[i] = DFmalloc(strlen(dp->d_name)+1);
strcpy(dirlist[i++], dp->d_name);
}
}
}
closedir(dirp);
}
if (dirlist != NULL) {
int j;
/* -- sort file or directory list box data -- */
qsort(dirlist, i, sizeof(void *), dircmp);
/* ---- send sorted list to list box ---- */
for (j = 0; j < i; j++) {
SendMessage(lwnd,ADDTEXT,(PARAM)dirlist[j],0);
free(dirlist[j]);
}
free(dirlist);
}
SendMessage(lwnd, SHOW_WINDOW, 0, 0);
}
return TRUE;
}
BOOL BuildFileList(WINDOW wnd, char *fspec)
{
return BuildList(wnd, fspec, FALSE);
}
void BuildDirectoryList(WINDOW wnd)
{
BuildList(wnd, "*", TRUE);
}
void BuildPathDisplay(WINDOW wnd)
{
char path[MAXPATH];
CTLWINDOW *ct = FindCommand(wnd->extension, ID_PATH,TEXT);
if (ct != NULL) {
int len;
WINDOW lwnd = ct->wnd;
getcwd(path, sizeof(path));
SendMessage(lwnd,SETTEXT,(PARAM)path,0);
SendMessage(lwnd, PAINT, 0, 0);
}
}