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
|
/*
** LSD - A simple directory lister
** A public domain C demo program by Bob Stout
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <dos.h>
/* For portability, make everything look like MSC 6+ */
#if defined(__TURBOC__)
#include <dir.h>
#define _dos_findfirst(f,a,b) findfirst(f,b,a)
#define _dos_findnext(b) findnext(b)
#define find_t ffblk
#define _A_SUBDIR FA_DIREC
#define attrib ff_attrib
#define name ff_name
#define size ff_fsize
#define wr_time ff_ftime
#define wr_date ff_fdate
#define _dos_getdiskfree getdfree
#define diskfree_t dfree
#define avail_clusters df_avail
#define sectors_per_cluster df_sclus
#define bytes_per_sector df_bsec
#else /* assume MSC/QC */
#include <errno.h>
#endif
#ifdef TRUE
#undef TRUE
#endif
#ifdef FALSE
#undef FALSE
#endif
#ifdef ERROR
#undef ERROR
#endif
enum LOGICAL {ERROR = -1, SUCCESS, FALSE = 0, TRUE};
#ifndef CAST
#define CAST(new_type,old_object) (*((new_type *)&(old_object)))
#endif
#define LAST_CHAR(s) (((char *)s)[strlen(s) - 1])
struct DOS_TIME {
unsigned int ss : 5;
unsigned int mm : 6;
unsigned int hh : 5;
} ;
#define dos_time(t) CAST(struct DOS_TIME, (t))
struct DOS_DATE {
unsigned int da : 5;
unsigned int mo : 4;
unsigned int yr : 7;
} ;
#define dos_date(t) CAST(struct DOS_DATE, (t))
/*
** DOS DIR improved work-alike w/ improved formatting & attribute display
**
** supports /W switch
*/
main(int argc, char *argv[])
{
int i, files = 0, dirs = 0, argptr = 0, errflag = FALSE, cols, drive;
long siz_tot = 0L;
char *p, *fname, *ext, name[13], buf[67], numbuf[12];
struct find_t ff;
#ifndef __ZTC__
struct diskfree_t df;
#endif
int one_column(), five_column();
int (*display)(char *, char *, long, unsigned, unsigned, unsigned)
= one_column;
char *sprintfc(char *, long);
char *capitalize(const char *);
strcpy(buf, fname = "*.*");
if(argc != 1) for (i = 1; i < argc; ++i)
{
if ('/' == argv[i][0])
{
if ('W' == toupper(argv[i][1]))
display = five_column;
else
{
puts("\aUsage: LSD [/W] [file]");
errflag = TRUE;
}
}
else if (!argptr)
argptr = i;
}
if (argptr)
{
fname = argv[argptr];
strcpy(buf, fname);
if ('\\' == LAST_CHAR(buf) || ':' == LAST_CHAR(buf))
strcat(buf, "*.*");
else
{
if (SUCCESS == _dos_findfirst(buf, _A_SUBDIR, &ff))
{
if (ff.attrib & _A_SUBDIR && '.' != *ff.name)
{
if ('\\' != LAST_CHAR(buf))
strcat(buf, "\\");
strcat(buf, "*.*");
}
}
else errflag = TRUE;
}
}
if (':' == buf[1])
drive = toupper(*buf) - '@';
else drive = 0;
if (!errflag && !(_dos_findfirst(buf, 0xff, &ff))) do
{
siz_tot += ff.size;
if (ff.attrib & _A_SUBDIR)
++dirs;
else ++files;
strcpy(name, ff.name);
if (NULL != (p = strchr(name, '.')) && p != name)
{
*p = '\0';
ext = ++p;
}
else ext = "";
cols = (*display)(name, ext, ff.size,
ff.attrib, ff.wr_date, ff.wr_time);
} while (SUCCESS == _dos_findnext(&ff));
else
{
fprintf(stderr, "Cannot do directory on '%s'\n", fname);
exit(-1);
}
if (cols)
fputc('\n', stdout);
sprintfc(numbuf,siz_tot);
printf("\n%3d Files totalling %s bytes\n", files, numbuf);
printf("%3d Director%s\n", dirs, (1 == dirs) ? "y" : "ies");
#ifndef __ZTC__
_dos_getdiskfree(drive, &df);
sprintfc(numbuf, (long)df.avail_clusters * df.sectors_per_cluster *
df.bytes_per_sector);
#else /* if ZTC */
sprintfc(numbuf, dos_getdiskfreespace(drive));
#endif
printf("%s bytes free\n", numbuf);
return 0;
}
/*
** The single column directory entry display function
*/
int one_column(char *name,
char *ext,
long size,
unsigned attribs,
unsigned date,
unsigned time)
{
register int i, mask;
static char *atr = "RHSVDA", szbuf[12];
sprintfc(szbuf, size);
printf("%-8s %-3s %12s ", capitalize(name), capitalize(ext), szbuf);
for (i = 0, mask = 1; i < 6; ++i, mask <<= 1)
{
if (attribs & mask)
fputc(atr[i], stdout);
else fputc('.' , stdout);
}
printf("%4d-%02d-%02d%4d:%02d:%02d\n",
dos_date(date).mo,
dos_date(date).da,
(dos_date(date).yr + 80) % 100,
dos_time(time).hh,
dos_time(time).mm,
dos_time(time).ss);
return 0;
}
/*
** The five column directory entry display function
*/
int five_column(char *name,
char *ext,
long size,
unsigned attribs,
unsigned date,
unsigned time)
{
static int cols = 0;
printf("%-8s %-3s%s", capitalize(name), capitalize(ext),
(5 > ++cols) ? " " : "");
if (0 == (cols %= 5))
putchar('\n');
return (cols);
}
/*
** Display a long int using commas as thousands separators
*/
char *sprintfc(char *string, long num)
{
if (num > 999999L)
{
sprintf(string, "%d,%03d,%03d",
(int)(num / 1000000L),
(int)((num % 1000000L) / 1000L),
(int)(num % 1000L));
}
else
{
if (num > 999L)
{
sprintf(string, "%d,%03d",
(int)(num / 1000L),
(int)(num % 1000L));
}
else sprintf(string, "%d", (int)num);
}
return string;
}
/*
** Capitalize a name or extension in place
*/
char *capitalize(const char *string)
{
int flag = 0;
char *ptr = (char *)string;
do
{
if (isalpha(*ptr))
{
if (flag)
*ptr = (char)tolower(*ptr);
else
{
flag = 1;
*ptr = (char)toupper(*ptr);
}
}
else flag = 0;
} while (*++ptr);
return (char *)string;
}
|