wc: Added wc
[mutos-utils.git] / cat.c
blob125444d2797fd26e72e24eae7a75467f22d063dc
1 /*
2 Copyright © 2013 Alastair Stuart
4 This program is open source software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
15 #include <stdio.h>
16 #include <string.h>
17 #include <errno.h>
19 #include <getopt.h>
21 #define VERSION "0.01"
23 void usage(char* program)
25 printf("Usage: %s [options] [file ...]\n", program);
26 printf("Concatenates file(s) and/or standard input to standard output.\n"
27 "\n"
28 " -u (ignored)\n"
29 "\n"
30 " --help Print this message.\n"
31 " --version Show version info.\n");
34 int main(int argc, char* argv[])
36 // read from stdin if run with no args
37 if (argc == 1) {
38 int c = fgetc(stdin);
39 while (c != EOF)
41 printf("%c", c);
42 c = fgetc(stdin);
45 return 0;
49 int arg = 0;
50 for (arg = 1; arg < argc; arg++)
52 if (strcmp(argv[arg], "-u") == 0) {
53 ; // no-op
54 } else if (strcmp(argv[arg], "--help") == 0){
55 usage(argv[0]);
56 } else if (strcmp(argv[arg], "--version") == 0) {
57 printf("cat (mutos) v"VERSION"\n");
58 return 0;
59 } else if (strcmp(argv[arg], "--") == 0) { // the next args will be the actual args
60 arg++; // jump to first actual args
61 break; // and break out of flag loop
62 } else {
63 // done parsing flags
64 break;
68 for (; arg < argc; arg++)
70 FILE *current_file = NULL;
72 if (strcmp(argv[arg], "-") == 0) {
73 current_file = stdin;
74 } else {
75 current_file = fopen(argv[arg], "r");
79 if (current_file) {
80 int c = fgetc(current_file);
81 while (c != EOF)
83 printf("%c", c);
84 c = fgetc(current_file);
86 fclose(current_file);
87 } else {
88 fprintf(stderr, "%s: %s: %s\n", argv[0], strerror(errno), argv[arg]);
92 return 0;