-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoto.c
65 lines (57 loc) · 1.07 KB
/
goto.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
#include <string.h>
#include <limits.h>
#include <unistd.h>
static int offset=0;
static int nchar=0;
static int getlin(char *s);
static int nextc(void);
int main(int argc, char **argv)
{
char line[_POSIX_MAX_INPUT+1];
if (argc<2 || isatty(STDIN_FILENO)) {
write(STDOUT_FILENO, "goto error\n", 11);
lseek(STDIN_FILENO, 0, SEEK_END);
return 1;
}
lseek(STDIN_FILENO, 0, SEEK_SET);
do {
if (getlin(line)) {
write(STDOUT_FILENO, "label not found\n", 16);
return 1;
}
} while (strcmp(line, argv[1])!=0);
lseek(0, offset, SEEK_SET);
return 0;
}
int getlin(char *s)
{
int ch, i;
i = 0;
nchar=0;
while ((ch=nextc())!='\0' && ch!=':') {
while(ch!='\n' && ch!='\0')
ch = nextc();
}
if (ch=='\0')
return 1;
while ((ch=nextc())==' ');
while (ch!=' ' && ch!='\n' && ch!='\0') {
s[i++] = ch;
ch = nextc();
}
while(ch != '\n')
ch = nextc();
s[i] = '\0';
return 0;
}
int nextc(void)
{
char cc;
offset++;
if (nchar++ >= _POSIX_MAX_INPUT)
write(STDOUT_FILENO, "line overflow\n", 14);
if(read(STDIN_FILENO, &cc, 1))
return cc;
else
return 0;
}