#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); }