wc: Added wc
[mutos-utils.git] / seq.c
blobb2808095d1083c420999cc2f50c86e9669f36d76
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 <stdlib.h> // atoi()
18 #include <stdbool.h>
20 #define VERSION "0.01"
22 struct {
23 bool w;
24 } flags;
26 char* sep = "\n";
27 char* term = NULL;
29 void seq(char* first, char* increment, char* last)
31 unsigned int width = 0;
33 if (flags.w) {
34 width = strlen(last);
37 for (int i = atoi(first); i <= atoi(last); i+=atoi(increment))
39 printf("%0*d%s", width, i, sep);
42 if (term) {
43 printf("%s", term);
47 int main(int argc, char* argv[])
49 flags.w = false;
51 if (argc == 1) {
52 fprintf(stderr, "%s: missing operand\n"
53 "Run '%s --help' for usage.\n",
54 argv[0], argv[0]);
55 return 1;
59 // flag parsing
60 int arg = 0;
61 for (arg = 1; arg < argc; arg++)
63 if (strcmp(argv[arg], "-s") == 0) {
64 sep = argv[arg+1];
65 arg++; // don't parse separator as a flag
66 } else if (strcmp(argv[arg], "-t") == 0) {
67 term = argv[arg+1];
68 arg++; // don't parse terminator as a flag
69 } else if (strcmp(argv[arg], "-w") == 0 || strcmp(argv[arg], "--equal-width") == 0) {
70 flags.w = true;
71 } else if (strcmp(argv[arg], "-h") == 0 || strcmp(argv[arg], "--help") == 0) {
72 printf("Usage: %s [first] [last]\n", argv[0]);
73 printf("\n"
74 "If [first] is omitted, it is assumed to be 1.\n");
75 return 0;
76 } else if (strcmp(argv[arg], "-v") == 0 || strcmp(argv[arg], "--version") == 0) {
77 printf("seq (mutos) v"VERSION"\n");
78 return 0;
79 } else {
80 break;
84 int args_left = argc - arg;
86 switch (args_left)
88 case 0:
89 fprintf(stderr, "%s: too few arguments\n",
90 argv[0]);
91 return 1;
92 case 1:
93 seq("1", "1", argv[arg]);
94 break;
95 case 2:
96 seq(argv[arg], "1", argv[arg+1]);
97 break;
98 case 3:
99 seq(argv[arg], argv[arg+1], argv[arg+2]);
100 break;
101 default:
102 fprintf(stderr, "%s: too many arguments\n",
103 argv[0]);
104 return 1;
107 return 0;