nin.c (1694B)
1 #include <stdlib.h> 2 #include <stdio.h> 3 #include <string.h> 4 #include <ctype.h> 5 6 /* 7 * Ninc - Narthex incrementor 8 * 9 * By Michael Constantine Dimopoulos 10 * https://mcdim.xyz <mk@mcdim.xyz> 11 * License: GNU GPL v3 12 * 13 */ 14 15 #define VERSION "v0.1" 16 #define BUFFER_SIZE 256 17 18 static int 19 isnumber(char * str) 20 { 21 for (int i = 0; str[i] != '\0'; i++) { 22 if (isdigit(str[i]) != 0) 23 return 1; 24 } 25 return 0; 26 } 27 28 static void 29 ninc(FILE *f, int min, int max, int numerical) 30 { 31 char buffer[BUFFER_SIZE]; 32 while (fgets(buffer, sizeof(buffer), f) != NULL) { 33 strtok(buffer, "\n"); 34 puts(buffer); 35 for (int i = min; i <= max; i++) { 36 if ((numerical == 0 && isnumber(buffer) == 0) || numerical == 1) { 37 printf("%s%d\n", buffer, i); 38 } 39 } 40 } 41 } 42 43 static void 44 help(char * exename) 45 { 46 printf( "Ninc - Narthex incrementor %s\n" 47 "By Michael C. Dim. <mk@mcdim.xyz>\n\n" 48 49 "-n Increment numerical lines as well\n" 50 "-h Print this panel & exit\n" 51 "-v Print current version & exit\n\n" 52 53 "Usage: cat [FILENAME] | %s [MIN] [MAX] [OPTIONS]\n", 54 VERSION, exename); 55 exit(0); 56 } 57 58 void 59 die(char * str) 60 { 61 printf("%d\n", str); 62 } 63 64 void 65 main(int argc, char * argv[]) 66 { 67 int min = 1; 68 int max = 10; 69 int numerical = 0; 70 71 if (argc == 2) { 72 if (strcmp(argv[1], "-h") == 0) 73 help(argv[0]); 74 else if (strcmp(argv[1], "-v") ==0) 75 printf("%s\n", VERSION); 76 } else if (argc == 3) { 77 min = atoi(argv[1]); 78 max = atoi(argv[2]); 79 } else if (argc == 4) { 80 min = atoi(argv[1]); 81 max = atoi(argv[2]); 82 if (strcmp(argv[3], "-n") == 0) 83 numerical = 1; 84 } else { 85 fprintf(stderr, "%s: wrong number of arguments\n", argv[0]); 86 exit(0); 87 } 88 89 if (min <= max) { 90 ninc(stdin, min, max, numerical); 91 } 92 exit(0); 93 }