Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- FILE **init_in(int ac, char **av) {
- FILE **in = (FILE **) malloc(sizeof(FILE *) * (ac - 1 - 1));
- for (int i = 0; i < ac - 1; i++) in[i] = fopen(av[i], "rb");
- return in;
- }
- unsigned char **init_reader(int ac) {
- unsigned char **reader =
- (unsigned char **) malloc(sizeof(unsigned char *) * (ac - 1 - 1));
- for (int i = 0; i < ac - 1; i++)
- reader[i] = (unsigned char *) malloc(sizeof(unsigned char));
- return reader;
- }
- int reader_read(int ac, FILE **in, unsigned char **reader) {
- int read_count;
- for (int i = 0; i < ac - 1; i++)
- read_count = fread(reader[i], sizeof(unsigned char), 1, in[i]);
- return read_count;
- }
- void close_in(int ac, FILE **in) {
- for (int i = 0; i < ac - 1; i++) fclose(in[i]);
- }
- int main(int ac, char **av) {
- if (ac < 5) return EXIT_FAILURE;
- FILE **in = init_in(ac, av);
- FILE *out = fopen(av[ac - 1], "wb");
- unsigned char **r = init_reader(ac);
- while (true) {
- int status = reader_read(ac, in, r);
- if (status < 1) break;
- unsigned char *w = 0;
- unsigned char sum = 0;
- for (int i = 0; i < ac - 1; i++) sum += *r[i];
- if (sum == 0) *w = 0;
- else *w = (unsigned char) ((1UL << 8) - 1) * *r[0] / sum;
- fwrite(w, sizeof(unsigned char), 1, out);
- }
- fclose(out);
- close_in(ac, in);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement