nhan.c (2098B)
1 #include <stdlib.h> 2 #include <ctype.h> 3 #include <stdio.h> 4 #include <errno.h> 5 #include <string.h> 6 7 /* 8 * Nhance - Narthex enhancer 9 * 10 * By Michael Constantine Dimopoulos 11 * https://mcdim.xyz <mk@mcdim.xyz> 12 * License: GNU GPL v3 13 * 14 */ 15 16 17 #define VERSION "v1.2" 18 #define BUFFER_SIZE 256 19 20 FILE * 21 save_stdin(FILE *f) 22 { 23 FILE *f2 = tmpfile(); 24 char buffer[BUFFER_SIZE]; 25 while(fgets(buffer, sizeof(buffer), f) != NULL) { 26 fprintf(f2, "%s", buffer); 27 } 28 fclose(f); 29 return f2; 30 } 31 32 void cap(char string[]){ 33 int i; 34 int x = strlen(string); 35 for (i=0; i<x-1; i++){ 36 if (isalpha(string[i])){ 37 string[i]= toupper(string[i]); 38 } 39 } 40 } 41 42 void 43 print_only(FILE *f) 44 { 45 char buffer[BUFFER_SIZE]; 46 while(fgets(buffer, sizeof(buffer), f) != NULL) { 47 printf("%s",buffer); 48 } 49 } 50 51 void 52 enhance(FILE *f, int full_upper) 53 { 54 char buffer[BUFFER_SIZE]; 55 while(fgets(buffer, sizeof(buffer), f) != NULL) { 56 if (isalpha(buffer[0])) { 57 if (full_upper ==0) 58 buffer[0] = toupper(buffer[0]); 59 else 60 cap(buffer); 61 printf("%s",buffer); 62 } 63 64 } 65 } 66 67 void die(char * str) 68 { 69 printf("%s\n", str); 70 exit(0); 71 } 72 73 void help(char * exename) 74 { 75 printf( "Nhance - Narthex enhancer %s\n" 76 "By Michael C. Dim. <mk@mcdim.xyz>\n\n" 77 78 "-f Append full capitalization\n" 79 "-h Print this panel & exit\n" 80 "-v Print current version & exit\n\n" 81 "Usage: cat [FILENAME] | %s [OPTIONS]\n" 82 " %s [OPTIONS] [FILENAME]\n", 83 VERSION, exename, exename); 84 exit(0); 85 } 86 87 void 88 main(int argc, char* argv[]) 89 { 90 int full_upper, file = 0, i; 91 for (i=0; i<argc; i++) { 92 if (strcmp(argv[i],"-f")==0) 93 full_upper = 1; 94 else if (strcmp(argv[i],"-v")==0) 95 die(VERSION); 96 else if (strcmp(argv[i],"-h")==0) 97 help(argv[0]); 98 else 99 file = i; 100 } 101 102 FILE *f2; 103 if (file != 0) { 104 FILE *f = fopen(argv[file], "r"); 105 106 if (f) { 107 f2 = save_stdin(f); 108 } else { 109 fprintf(stderr, "%s: %s\n", argv[0], strerror(errno)); 110 exit(1); 111 } 112 } else { 113 f2 = save_stdin(stdin); 114 } 115 116 rewind(f2); 117 print_only(f2); 118 rewind(f2); 119 enhance(f2,0); 120 if (full_upper == 1) { 121 rewind(f2); 122 enhance(f2,1); 123 } 124 fclose(f2); 125 126 exit(0); 127 }