-
Notifications
You must be signed in to change notification settings - Fork 2
/
sx_ls.c
273 lines (192 loc) · 4.66 KB
/
sx_ls.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* sx_ls.c
LS for Samarux.
List files.
Copyright (c) 2007 - 2015 Miguel I. Garcia Lopez.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
Usage:
ls [-aksvfl ...] [dir|file ...]
Options:
-a Attributes: Print file attributes.
-k Kbytes: Print file size.
-s Sort: Sort file names.
-v Verbose: Verbose.
-f Full: Same as -aksv.
-l Long: Same as -aks.
Examples:
ls -f *.c
ls -sv myfile.c myfile.h myfile.zsm
Changes:
19 Dec 2014 : 1.00 : 1st version.
24 Dec 2014 : 1.01 : Minor changes.
04 Jan 2015 : 1.02 : Changed code in make fcx section. Re-written flags code.
08 Jan 2015 : 1.03 : Adapted to changes in shell.
11 Feb 2015 : 1.04 : Solved bug in file attributes checking.
17 Feb 2015 : 1.05 : Added -l option. Unix style output for attributes.
19 Feb 2015 : 1.06 : Work on -f & -l options.
05 Jun 2016 : 1.07 : Added support for builtin / external.
*/
/* Built-in or external
--------------------
*/
#ifdef SX_SAMARUX
#define SX_LS
#else
#include "samarux.h"
#define LsMain main
#endif
#define LS_FLG_A 1 /* Attributes */
#define LS_FLG_K 2 /* Kbytes (file size) */
#define LS_FLG_S 4 /* Sort */
#define LS_FLG_V 8 /* Verbose */
#define LS_FLG_F 15 /* Full: AKSV */
#define LS_FLG_L 7 /* Long: AKS */
LsMain(argc, argv)
int argc, argv[];
{
int i, files, flags;
char *p;
/* Defaults */
flags = 0;
/* Check arguments */
for(i = 1; i < argc; ++i)
{
p = argv[i];
if(*p == '-')
{
while(*++p)
{
switch(*p)
{
case 'a' : flags |= LS_FLG_A ; break;
case 'v' : flags |= LS_FLG_V ; break;
case 'k' : flags |= LS_FLG_K ; break;
case 's' : flags |= LS_FLG_S ; break;
case 'l' : flags |= LS_FLG_L ; break;
case 'f' : flags |= LS_FLG_F ; break;
default : return ErrorOpt();
}
}
}
else
break;
}
/* Search files */
if(i == argc)
argv[--i] = "*.*";
if((files = LsFind(argc - i, &argv[i], flags)) > 0)
return LsPrint(files, flags);
/* Return success or failure */
return files ? 1 : 0;
}
LsFind(hm, fn, flags)
int hm, fn[], flags;
{
int du, drv, usr, files, max, i, ver; char *buf;
files = 0;
max = SX_MAX_FCX;
buf = sv_fcxbuf;
ver = flags & LS_FLG_V;
/* Process all file names */
for(i = 0; i < hm; ++i)
{
/* Make FCX */
if(MakeFcx(fn[i], sv_fcx))
return ErrorFName();
/* Search files */
if(ver)
{
putstr("Searching: "); RtPrintFcx(sv_fcx);
}
if((files = RtExpandFcx(sv_fcx, buf, max)) == -1)
{
ErrorTooMany(); return -1;
}
if(ver)
printf(" - %d file(s) found\n", files);
max -= files;
buf += files * 13;
}
files = SX_MAX_FCX - max;
if(ver)
{
putchar('\n');
if(hm > 1)
printf("Total %d files(s) found\n\n", files);
}
/* Return # of files found */
return files;
}
LsPrint(files, flags)
int files, flags;
{
int i, ver;
BYTE *buf; unsigned int *fsz, *fszbuf, kb;
ver = flags & LS_FLG_V;
/* Sort file names */
if(flags & LS_FLG_S)
{
if(ver)
printf("Sorting %d file(s)\n\n", files);
RtSortFcx(sv_fcxbuf, files);
}
/* Get file size */
if(flags & LS_FLG_K)
{
if(ver)
printf("Getting size of file(s)\n\n");
if((fszbuf = malloc(files + files)) == NULL)
return ErrorMem();
buf = sv_fcxbuf; fsz = sv_fcx + UX_FCX_RRC;
for(i = 0; i < files; ++i)
{
memcpy(sv_fcx, buf, 13);
bdos_fcx_a(BF_FSIZE, sv_fcx);
fszbuf[i] = *fsz;
buf += 13;
}
}
/* Print results */
buf = sv_fcxbuf;
for(i = 0; i < files; ++i)
{
/* Print file permissions (attributes) */
if(flags & LS_FLG_A)
{
putchar(buf[10] & 128 ? '-' : 'w'); /* R/O or R/W */
putchar(buf[11] & 128 ? 's' : '-'); /* SYS or DIR */
putchar(buf[12] & 128 ? 'a' : '-'); /* ARC or UPD */
putchar(' ');
}
/* Print file size */
if(flags & LS_FLG_K)
{
kb = fszbuf[i] / 8; if(fszbuf[i] % 8) ++kb;
printf("%4u Kb %5u Rc ", kb, fszbuf[i]);
}
/* Print file name */
RtPrintFcx(buf);
putchar('\n');
buf += 13;
}
/* Free buffer */
if(flags & LS_FLG_K)
free(fszbuf);
/* Success */
return 0;
}
#undef LS_FLG_A
#undef LS_FLG_V
#undef LS_FLG_K
#undef LS_FLG_S
#undef LS_FLG_F
#undef LS_FLG_L