]> git.somenet.org - pub/jan/sysprog.git/blob - mycat/mycat.c
all the old sysprog files
[pub/jan/sysprog.git] / mycat / mycat.c
1 /**************
2 * Name:         mycat.c
3 * Author:       Jan Vales (e0726236@student.tuwien.ac.at)
4 * Date:         9.11.2009
5 * Purpose:      reimplementation of "cat"
6 * Usage:        Usage: cat [opt]
7 * Output:       prints stdin to stdout
8 * BspNr:        1h
9 **************/
10
11 #include <stdio.h>
12 #include <errno.h>
13 #include <strings.h>
14 #include <string.h>
15 #include <assert.h>
16 #include <stdlib.h>
17 #include <getopt.h>
18 #include <unistd.h>
19
20 /*Usage info, in case someone did something wrong*/
21 void usage(char **argv){
22         (void)fprintf(stderr,"%s Usage: mycat [opt]\n-E: replace newlines\n-T: replace Tabs\n-v: Print nonprintables (implies -E and -T)\n", argv[0]);
23 }
24
25 /* main program. reads input from stdin and prints requested stuff to stdout*/
26 int main(int argc, char **argv){
27         int isOptE = 0;
28         int isOptT = 0;
29         int isOptv = 0;
30         unsigned char c;
31
32         char temp;
33         while((temp = getopt(argc, argv, "ETv")) != -1){
34                 switch(temp){
35                         case 'E': isOptE = 1; break;
36                         case 'T': isOptT = 1; break;
37                         case 'v': isOptv = 1; break;
38                         default: usage(argv); return 1;
39                 }
40         }
41
42         for(;;){
43                 c = getchar();
44                 if(feof(stdin))break;
45
46                 if((isOptT | isOptv) & (c == '\t')){
47                         (void)fprintf(stdout,"^I");
48                         continue;
49                 }
50                 if((isOptE | isOptv) & (c == '\n')){
51                         (void)fprintf(stdout,"$%c", c);
52                         continue;
53                 }
54                 if(isOptv & (c >= 128)){
55                         (void)fprintf(stdout,"M-");
56                         c -= 128;
57                 }
58                 if(isOptv & (c == 127)){
59                         (void)fprintf(stdout,"^?");
60                         continue;
61                 }
62                 if(isOptv & (c <= 32)){
63                         (void)fprintf(stdout,"^%c", (c+64));
64                         continue;
65                 }
66                 (void)fprintf(stdout,"%c", c);
67         }
68         return 0;
69 }
70