blob: 2932190344c602582124c352b41a4e0c3eab51a8 (
plain) (
blame)
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
|
#include "gbos.h"
/* rev [file]: reverse the characters of each line, from a file or stdin. */
#define MAXLINE 80
void main(void) {
char *argv[4];
char line[MAXLINE];
unsigned char argc = argv_parse(argv, 4);
unsigned char len = 0, fd = NOFD, i;
int ch;
if (argc > 0 && argv[0][0] != '-') {
fd = open(argv[0], O_READ);
if (fd == EISDIR) {
puts("rev: is a directory");
nl();
sexit(2);
}
if (fd == NOFD) {
puts("rev: no such file");
nl();
sexit(2);
}
}
for (;;) {
if (fd == NOFD) {
char c = readc();
ch = (c == EOF) ? -1 : c;
} else ch = fgetc(fd);
if (ch == '\r') continue;
if (ch >= 0 && ch != '\n') {
if (len < MAXLINE) line[len++] = (char)ch;
continue;
}
if (ch < 0 && len == 0) break;
for (i = len; i--;) putc(line[i]);
nl();
len = 0;
if (ch < 0) break;
}
if (fd != NOFD) close(fd);
}
|