annotate src/common.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
children 652343b56a60
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
1 #include <stdio.h>
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
2 #include <stdint.h>
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
3
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
4 void set_data(unsigned char magic[], uint16_t version, FILE* target) {
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
5 int i;
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
6 fseek(target, 0x46, SEEK_SET);
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7 fputc(version, target);
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8 for (i=0; i<=sizeof(*magic); ++i) {
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9 fseek(target, 0x18+i, SEEK_SET);
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10 fputc(magic[i], target);
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
11 }
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
12 }
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14 int copy_file(char* source_file, char* target_file) {
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15 char ch[4096];
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
16 FILE *source, *target;
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18 source = fopen(source_file, "rb");
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
19
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
20 if (source == NULL)
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
21 return 1;
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
22
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
23 target = fopen(target_file, "wb");
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
24
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
25 if (target == NULL) {
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
26 fclose(source);
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
27 return 1;
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
28 }
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
29
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
30 while (fgetc(source) != EOF) {
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
31 size_t b = fread(ch, 1, sizeof(ch), source);
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
32 if (b)
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
33 fwrite(ch, 1, b, target);
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
34 }
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
35
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36 fclose(target);
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
37 fclose(source);
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
38 return 0;
7cb4ca7cf257 Use a common.c file to hold concurrent functions
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39 }