echo: Added echo
[mutos-utils.git] / cat.c
blob79f01acd35683bcdc1f41c8dfcaa156006298c4b
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 #define VERSION "0.01"
21 void usage(char* program)
23 printf("Usage: %s [options] [file ...]\n", program);
24 printf("Concatenates file(s) and/or standard input to standard output.\n"
25 "\n"
26 " --help Print this message.\n"
27 " --version Show version info.\n");
30 int main(int argc, char* argv[])
32 // read from stdin if run with no args
33 if (argc == 1) {
34 int c = fgetc(stdin);
35 while (c != EOF)
37 printf("%c", c);
38 c = fgetc(stdin);
41 return 0;
45 int arg = 0;
46 for (arg = 1; arg < argc; arg++)
48 if (strcmp(argv[arg], "--help") == 0 && argc == 2) {
49 usage(argv[0]);
50 } else if (strcmp(argv[arg], "--version") == 0 && argc == 2) {
51 printf("cat (mutos) v"VERSION"\n");
52 return 0;
53 } else if (strcmp(argv[arg], "--") == 0) { // the next args will be the actual args
54 arg++; // jump to first actual args
55 break; // and break out of flag loop
56 } else {
57 // done parsing flags
58 break;
62 for (; arg < argc; arg++)
64 FILE *current_file = NULL;
66 if (strcmp(argv[arg], "-") == 0) {
67 current_file = stdin;
68 } else {
69 current_file = fopen(argv[arg], "r");
73 if (current_file) {
74 int c = fgetc(current_file);
75 while (c != EOF)
77 printf("%c", c);
78 c = fgetc(current_file);
80 fclose(current_file);
81 } else {
82 fprintf(stderr, "%s: %s: %s\n", argv[0], argv[arg], strerror(errno));
86 return 0;