nin.c (3659B)
1 /* 2 * ninc - Narthex incrementor 3 * 4 * By Michael Constantine Dimopoulos https://mcdim.xyz <mk@mcdim.xyz> 5 * License: GNU GPL v3 6 * 7 * ninc will iterate over stdin and after printing the dictionary as is 8 * it will reprint it but will also multiply each line with the 9 * difference of max-min, and will append n to each line, where n is 10 * increased after every line from min to max inclusive. (I know, I know. 11 * Just try to use it and it will make more sense). 12 * 13 * * * * * * * * 14 * 15 * This program is free software: you can redistribute it and/or modify 16 * it under the terms of the GNU General Public License as published by 17 * the Free Software Foundation, either version 3 of the License, or 18 * (at your option) any later version. 19 * 20 * This program is distributed in the hope that it will be useful, 21 * but WITHOUT ANY WARRANTY; without even the implied warranty of 22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 * GNU General Public License for more details. 24 * 25 * You should have received a copy of the GNU General Public License 26 * along with this program. If not, see <https://www.gnu.org/licenses/>. 27 * 28 */ 29 30 #include <stdlib.h> 31 #include <unistd.h> 32 #include <stdio.h> 33 #include <string.h> 34 #include <ctype.h> 35 36 #define VERSION "v1.2.2" 37 #define BUFFER_SIZE 256 38 39 static void 40 help(char *exename) 41 { 42 printf( "ninc - Narthex incrementor %s\n" 43 "By Michael Constantine Dimopoulos <mk@mcdim.xyz>\n\n" 44 45 "-d use delimiters (specified in a string)\n" 46 "-n increment numerical lines as well\n" 47 "-h print this panel & exit\n" 48 "-v print current version & exit\n\n" 49 50 "Usage: cat [FILENAME] | %s [MIN] [MAX] [OPTIONS]\n", 51 VERSION, exename); 52 exit(EXIT_SUCCESS); 53 } 54 55 static inline void 56 die(char *str) 57 { 58 printf("%s\n", str); 59 exit(EXIT_SUCCESS); 60 } 61 62 static FILE * 63 save_stdin(FILE *f) 64 { 65 FILE *f2 = tmpfile(); 66 char buffer[BUFFER_SIZE]; 67 while (fgets(buffer, sizeof(buffer), f) != NULL) { 68 fprintf(f2, "%s", buffer); 69 } 70 fclose(f); 71 return f2; 72 } 73 74 static int 75 isnumber(char *str) 76 { 77 for (int i = 0; str[i] != '\0'; i++) { 78 if (isdigit(str[i]) != 0) 79 return 1; 80 } 81 return 0; 82 } 83 84 static inline int 85 check_num(int num, char * buffer) 86 { 87 return ((num == 0 && isnumber(buffer) == 0) || num == 1); 88 } 89 90 static void 91 ninc(FILE *f, int min, int max, int numerical, char del) 92 { 93 char buffer[BUFFER_SIZE]; 94 while (fgets(buffer, sizeof(buffer), f) != NULL) { 95 strtok(buffer, "\n"); 96 for (int i = min; i <= max; i++) { 97 if (check_num(numerical, buffer) == 1) { 98 if (del == ' ') 99 printf("%s%d\n", buffer, i); 100 else 101 printf("%s%c%d\n", buffer, del, i); 102 } 103 } 104 } 105 } 106 107 static void 108 print_only(FILE *f) 109 { 110 char buffer[BUFFER_SIZE]; 111 while (fgets(buffer, sizeof(buffer), f) != NULL) { 112 printf("%s",buffer); 113 } 114 } 115 116 void 117 main(int argc, char * argv[]) 118 { 119 int min = 1; 120 int max = 10; 121 int d = 0; 122 char del[10]; 123 int numerical = 0; 124 int index; 125 int c; 126 127 opterr = 0; 128 129 while ((c = getopt(argc, argv, "d:nvh")) != -1 ) 130 switch (c) { 131 case 'v': 132 die(VERSION); 133 case 'h': 134 help(argv[0]); 135 case 'd': 136 d=1; 137 strncpy(del, optarg, 10); 138 break; 139 case 'n': 140 numerical=1; 141 break; 142 case '?': 143 exit(EXIT_FAILURE); 144 break; 145 } 146 147 148 if (optind < argc) { 149 min = atoi(argv[optind]); 150 max = atoi(argv[optind+1]); 151 } else { 152 fprintf(stderr, "%s: wrong number of arguments\n", argv[0]); 153 exit(EXIT_FAILURE); 154 } 155 156 if (min > max) { 157 int temp = max; 158 max = min; 159 min = temp; 160 } 161 162 FILE *f; 163 f = save_stdin(stdin); 164 rewind(f); 165 print_only(f); 166 rewind(f); 167 ninc(f, min, max, numerical, ' '); 168 169 for (int i = 0; i < strlen(del); i++) { 170 rewind(f); 171 ninc(f, min, max, numerical, del[i]); 172 } 173 174 175 exit(EXIT_SUCCESS); 176 177 }