ArchiSys/TD/TD3/td3_lucas/ex_2/main.c
2024-03-09 18:46:06 +01:00

58 lines
1.1 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int cpm(char* src, char* dst, int n, int buff_size) {
FILE* fp_in = fopen(src, "r");
FILE* fp_out = fopen(dst, "w");
char buffer[buff_size];
for (int i=0; i < n; i++) {
fseek(fp_in, 0, SEEK_SET);
while (fgets(buffer, buff_size-1, fp_in)) {
fputs(buffer, fp_out);
fseek(fp_out, -1, SEEK_CUR);
}
}
fclose(fp_in);
fclose(fp_out);
return 0;
}
int main(int argc, char* argv[]) {
char* src = NULL;
char* dst = NULL;
int n = 1;
int buff_size = 16;
for (int i=1; i < argc; i++) {
if (!strcmp(argv[i], "-s")) {
src = argv[i+1];
i++;
} else if (!strcmp(argv[i], "-d")) {
dst = argv[i+1];
i++;
} else if (!strcmp(argv[i], "-n")) {
n = atoi(argv[i+1]);
i++;
} else if (!strcmp(argv[i], "-b")) {
buff_size = atoi(argv[i+1]);
i++;
} else {
printf("Unknown option %s\n", argv[i]);
}
}
if (!src || !dst) {
printf("Usage: %s -s <src> -d <dst> -n <nb of copies> -b <buffer size>\n", argv[0]);
return 1;
}
return cpm(src, dst, n, buff_size);
}