-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.c
140 lines (113 loc) · 2.99 KB
/
find.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
/*===========================================================================
find.c
Main body of the "find" command. See the find_help() function for
command line usage.
Copyright (c)2021 Kevin Boone, GPL v3.0
===========================================================================*/
#include "stdio.h"
#include "fcntl.h"
#include "errno.h"
#include "ctype.h"
#include "config.h"
#include "compat.h"
#include "defs.h"
#include "bdos.h"
#include "dirs.h"
#include "getopt.h"
#include "error.h"
#include "term.h"
#define DF_VERB 0x01
#define DF_PAGE 0x02
/*===========================================================================
globals
===========================================================================*/
/* Terminal size. */
int tm_rows;
int tm_cols;
/* Current number of lines written, for paging purposes. */
int lines = 0;
/*===========================================================================
find_help
===========================================================================*/
void find_help ()
{
/* TODO */
printf ("Usage: find [/p] {pattern}\r\n");
printf ("Searches drives for files matching the pattern.\r\n");
printf ("Options:\r\n");
printf (" /p page mode\n");
}
/*===========================================================================
find_on_drive
===========================================================================*/
void find_on_drive (drive, pattern, d_flag)
Drive drive;
char *pattern;
uint8_t d_flag;
{
dirent **dirs = dirs_list (drive, pattern, 0);
printf ("df=%02x\n", d_flag);
if (dirs)
{
int i = 0;
while (dirs[i])
{
printf ("%c:%s\r\n", drive + 'A' - 1, dirs[i]->sname);
i++;
if (lines++ == tm_rows - 2 && (d_flag & DF_PAGE))
{
while (lines == tm_rows - 1)
{
int c = tm_g_rchar();
switch (c)
{
case I_INTR: exit(0);
case 13: case 10: lines = tm_rows - 2; break;
case ' ': lines = 0;
default: break;
}
}
}
}
dirs_free (dirs);
}
}
/*===========================================================================
main
===========================================================================*/
int main (argc, argv)
int argc;
char **argv;
{
int i, opt, myargs;
uint8_t d_flag = 0;
argv[0] = "find";
while ((opt = getopt (argc, argv, "HVP")) != -1)
{
switch (opt)
{
case 'H':
find_help ();
exit (0);
case 'V': d_flag |= DF_VERB; break;
case 'P': d_flag |= DF_PAGE; break;
default: exit (-1);
}
}
tm_size (&tm_rows, &tm_cols);
myargs = argc - optind;
if (myargs == 1)
{
int i;
for (i = 1; i <= 26; i++)
{
find_on_drive (i, argv[optind], d_flag);
}
}
else
{
fprintf (stderr, "Specify one file pattern.\r\n");
exit (EINVAL);
}
return 0;
}