napp.c (3907B)
1 /* 2 * napp - Narthex appender 3 * 4 * By Michael Constantine Dimopoulos https://mcdim.xyz <mk@mcdim.xyz> 5 * License: GNU GPL v3 6 * 7 * napp will append to the end of, or insert to the front of, each line 8 * of stdin, characters from a specified character set, or words from a 9 * wordlist. It may or may not print stdin as is (-s) 10 * 11 * * * * * * * * 12 * 13 * This program is free software: you can redistribute it and/or modify 14 * it under the terms of the GNU General Public License as published by 15 * the Free Software Foundation, either version 3 of the License, or 16 * (at your option) any later version. 17 * 18 * This program is distributed in the hope that it will be useful, 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 * GNU General Public License for more details. 22 * 23 * You should have received a copy of the GNU General Public License 24 * along with this program. If not, see <https://www.gnu.org/licenses/>. 25 * 26 */ 27 28 #include <stdlib.h> 29 #include <stdio.h> 30 #include <string.h> 31 #include <unistd.h> 32 #include <ctype.h> 33 34 #define VERSION "v1.1.2" 35 #define BUFFER_SIZE 256 36 37 static inline void 38 usage(char *exename) 39 { 40 fprintf(stderr, "Usage: cat [FILENAME]" 41 "| %s [-c] [CHARSET] [-w] [FILE] [-s] [-f]\n", 42 exename); 43 } 44 45 static void 46 help(char *exename) 47 { 48 printf( "napp - Narthex appender %s\n" 49 "By Michael Constantine Dimopoulos <mk@mcdim.xyz>\n\n" 50 51 "-c specify charset\n" 52 "-w specify dictionary of words\n" 53 "-f insert before the word (default is after)\n" 54 "-s salt mode (do not print wordlist as is)\n" 55 "-h print this panel & exit\n" 56 "-v print current version & exit\n\n" 57 58 "Usage: cat [FILENAME] | %s [-c] [CHARSET] [-w] [FILE]" 59 "[-s] [-f]\n", VERSION, exename); 60 exit(EXIT_SUCCESS); 61 } 62 63 static inline void 64 die(char *str) 65 { 66 printf("%s\n", str); 67 exit(EXIT_SUCCESS); 68 } 69 70 static FILE * 71 save_stdin(FILE *f) 72 { 73 FILE *f2 = tmpfile(); 74 char buffer[BUFFER_SIZE]; 75 while(fgets(buffer, sizeof(buffer), f) != NULL) { 76 fprintf(f2, "%s", buffer); 77 } 78 fclose(f); 79 return f2; 80 } 81 82 static void 83 nappc(FILE *f, int front, char c) 84 { 85 char buffer[BUFFER_SIZE]; 86 while (fgets(buffer, sizeof(buffer), f) != NULL) { 87 strtok(buffer, "\n"); 88 if (front) 89 printf("%c%s\n", c, buffer); 90 else 91 printf("%s%c\n", buffer, c); 92 } 93 } 94 95 static void 96 nappw(FILE *f, int front, char *w) 97 { 98 char buffer[BUFFER_SIZE]; 99 while (fgets(buffer, sizeof(buffer), f) != NULL) { 100 strtok(buffer, "\n"); 101 if (front) 102 printf("%s%s\n", w, buffer); 103 else 104 printf("%s%s\n", buffer, w); 105 } 106 } 107 108 static void 109 print_only(FILE *f) 110 { 111 char buffer[BUFFER_SIZE]; 112 while (fgets(buffer, sizeof(buffer), f) != NULL) { 113 printf("%s",buffer); 114 } 115 } 116 117 void 118 main(int argc, char * argv[]) 119 { 120 int c1, w, s, front; 121 c1 = w = s = front = 0; 122 char cv[BUFFER_SIZE], wv[BUFFER_SIZE]; 123 int index; 124 int c; 125 extern char *optarg; 126 int optind = 0; 127 128 while ((c = getopt(argc, argv, "c:w:sfvh")) != -1 ) { 129 switch (c) { 130 case 'v': 131 die(VERSION); 132 case 'h': 133 help(argv[0]); 134 case 'c': 135 c1=1; 136 w=0; 137 strncpy(cv, optarg, BUFFER_SIZE); 138 break; 139 case 'w': 140 w=1; 141 c1=0; 142 strncpy(wv, optarg, BUFFER_SIZE); 143 break; 144 case 's': 145 s=1; 146 break; 147 case 'f': 148 front=1; 149 break; 150 case '?': 151 usage(argv[0]); 152 exit(EXIT_FAILURE); 153 break; 154 } 155 } 156 157 if (c1 != 1 && w != 1) { 158 usage(argv[0]); 159 exit(EXIT_FAILURE); 160 } 161 162 FILE *f; 163 f = save_stdin(stdin); 164 rewind(f); 165 166 if (!s) { 167 print_only(f); 168 rewind(f); 169 } 170 171 if (w) { 172 FILE *f2; 173 f2 = fopen(wv,"r"); 174 175 if (f2 == NULL) { 176 fprintf(stdout, "%s: Error opening file\n", 177 argv[0]); 178 exit(EXIT_FAILURE); 179 } 180 181 char word[BUFFER_SIZE]; 182 while(fgets(word, sizeof(word), f2) != NULL) { 183 strtok(word, "\n"); 184 nappw(f, front, word); 185 rewind(f); 186 } 187 } 188 189 if (c1) { 190 for (int i = 0; cv[i] != '\0'; i++) { 191 nappc(f, front, cv[i]); 192 rewind(f); 193 } 194 } 195 196 exit(EXIT_SUCCESS); 197 }