Mercurial > msvpvf
comparison src/main.c @ 47:7cb4ca7cf257
Use a common.c file to hold concurrent functions
Also I fixed that 1.4 KB bug I think
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Sat, 21 May 2022 18:41:54 -0400 |
| parents | 7cb9fad3f5ee |
| children | 652343b56a60 |
comparison
equal
deleted
inserted
replaced
| 46:7cb9fad3f5ee | 47:7cb4ca7cf257 |
|---|---|
| 4 #include <stdlib.h> | 4 #include <stdlib.h> |
| 5 #include <string.h> | 5 #include <string.h> |
| 6 #include <unistd.h> | 6 #include <unistd.h> |
| 7 #include <getopt.h> | 7 #include <getopt.h> |
| 8 #include <libgen.h> | 8 #include <libgen.h> |
| 9 #include "../include/common.h" | |
| 9 #ifdef _MSC_VER | 10 #ifdef _MSC_VER |
| 10 #define strdup(p) _strdup(p) | 11 #define strdup(p) _strdup(p) |
| 11 #endif | 12 #endif |
| 12 | 13 |
| 13 static struct option options_long[] = { | 14 static struct option options_long[] = { |
| 27 } | 28 } |
| 28 } | 29 } |
| 29 return str; | 30 return str; |
| 30 } | 31 } |
| 31 | 32 |
| 32 void set_data(unsigned char magic[], uint16_t version, FILE* target) { | |
| 33 int i; | |
| 34 fseek(target, 0x46, SEEK_SET); | |
| 35 fputc(version, target); | |
| 36 for (i=0; i<=sizeof(*magic); ++i) { | |
| 37 fseek(target, 0x18+i, SEEK_SET); | |
| 38 fputc(magic[i], target); | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 int copy_file(char* source_file, char* target_file) { | |
| 43 char ch; | |
| 44 FILE *source, *target; | |
| 45 | |
| 46 source = fopen(source_file, "rb"); | |
| 47 | |
| 48 if (source == NULL) | |
| 49 return 1; | |
| 50 | |
| 51 target = fopen(target_file, "wb"); | |
| 52 | |
| 53 if (target == NULL) { | |
| 54 fclose(source); | |
| 55 return 1; | |
| 56 } | |
| 57 | |
| 58 while( ( ch = fgetc(source) ) != EOF ) | |
| 59 fputc(ch, target); | |
| 60 | |
| 61 fclose(target); | |
| 62 fclose(source); | |
| 63 return 0; | |
| 64 } | |
| 65 | |
| 66 int main(int argc, char *argv[]) { | 33 int main(int argc, char *argv[]) { |
| 67 int c, option_index = 0; | 34 int c, option_index = 0; |
| 68 unsigned char magic[16]; | 35 unsigned char magic[16]; |
| 69 FILE* outfile; | 36 FILE* outfile; |
| 70 struct arguments { | 37 struct arguments { |
| 71 char input[256]; | 38 char input[256]; |
| 72 char output[256]; | 39 char output[256]; |
| 73 int version; | 40 int version; |
| 74 char type[256]; | 41 char type[4]; |
| 75 } args; | 42 } args; |
| 76 strcpy(args.input, " "); | 43 strcpy(args.input, " "); |
| 77 strcpy(args.output, " "); | 44 strcpy(args.output, " "); |
| 78 args.version = -1; | 45 args.version = -1; |
| 79 strcpy(args.type, " "); | 46 strcpy(args.type, " "); |
| 82 switch(c) { | 49 switch(c) { |
| 83 case 'i': | 50 case 'i': |
| 84 strncpy(args.input, optarg, sizeof(args.input)-1); /* subtract 1 to make sure it's "null-safe" */ | 51 strncpy(args.input, optarg, sizeof(args.input)-1); /* subtract 1 to make sure it's "null-safe" */ |
| 85 break; | 52 break; |
| 86 case 'o': | 53 case 'o': |
| 87 strncpy(args.output, optarg, sizeof(args.input)-1); | 54 strncpy(args.output, optarg, sizeof(args.output)-1); |
| 88 break; | 55 break; |
| 89 case 'v': | 56 case 'v': |
| 90 args.version = abs(atoi(strdup(optarg))); /* abs() for possible negative inputs */ | 57 args.version = abs(atoi(strdup(optarg))); /* abs() for possible negative inputs */ |
| 91 break; | 58 break; |
| 92 case 't': | 59 case 't': |
| 93 strncpy(args.type, optarg, sizeof(args.input)-1); | 60 strncpy(args.type, optarg, sizeof(args.type)-1); |
| 94 break; | 61 break; |
| 95 case 'h': | 62 case 'h': |
| 96 default: | 63 default: |
| 97 printf("msvpvf by Paper\nusage: %s (-i/--input) infile [(-o/--output) outfile] (-v/--version) version (-t/--type) [vf, veg]\n", argv[0]); | 64 printf("msvpvf by Paper\nusage: %s (-i/--input) infile [(-o/--output) outfile] (-v/--version) version (-t/--type) [vf, veg]\n", argv[0]); |
| 98 return 0; | 65 return 0; |
| 146 } | 113 } |
| 147 fflush(stdout); | 114 fflush(stdout); |
| 148 if (strcmp(args.output, " ") == 0) { /* string manipulation hell */ | 115 if (strcmp(args.output, " ") == 0) { /* string manipulation hell */ |
| 149 char* temp = (char*)calloc(256, sizeof(char)); | 116 char* temp = (char*)calloc(256, sizeof(char)); |
| 150 temp[0] = '\0'; | 117 temp[0] = '\0'; |
| 151 char str_version[16]; | 118 char str_version[4]; |
| 152 sprintf(str_version, "V%d", args.version); | 119 sprintf(str_version, "V%d", args.version); |
| 153 strncat(temp, str_version, 2); | 120 strncat(temp, str_version, 4); |
| 154 strncat(temp, "_", 1); | 121 strncat(temp, "_", 2); |
| 155 strncat(temp, basename(args.input), 248); | 122 strncat(temp, basename(args.input), 248); |
| 156 strcpy(temp, strremove(temp, strrchr(basename(args.input), ('.')))); /* remove file extension */ | 123 strcpy(temp, strremove(temp, strrchr(basename(args.input), ('.')))); /* remove file extension */ |
| 157 strncat(temp, ".", 1); | 124 strncat(temp, ".", 2); |
| 158 strncat(temp, args.type, 3); | 125 strncat(temp, args.type, 4); |
| 159 strncpy(args.output, temp, 255); | 126 strncpy(args.output, temp, 255); |
| 160 free(temp); | 127 free(temp); |
| 161 } | 128 } |
| 162 if (strcmp(args.type, "veg") == 0) { | 129 if (strcmp(args.type, "veg") == 0) { |
| 163 const unsigned char T[] = {0xEF, 0x29, 0xC4, 0x46, 0x4A, 0x90, 0xD2, 0x11, 0x87, 0x22, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A}; | 130 const unsigned char T[] = {0xEF, 0x29, 0xC4, 0x46, 0x4A, 0x90, 0xD2, 0x11, 0x87, 0x22, 0x00, 0xC0, 0x4F, 0x8E, 0xDB, 0x8A}; |
