vorking #2 both guys
This commit is contained in:
62
lab_2/kirill/main_d.c
Normal file
62
lab_2/kirill/main_d.c
Normal file
@@ -0,0 +1,62 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <dlfcn.h>
|
||||
|
||||
#define MAX_LINE 4096
|
||||
|
||||
typedef int (*replace_func_t)(char*, int, int*);
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc != 5) {
|
||||
fprintf(stderr, "Usage: %s <in> <out> <cap> <path_to_so>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
FILE *fin = fopen(argv[1], "r");
|
||||
if (!fin) { perror("fopen input"); return 1; }
|
||||
FILE *fout = fopen(argv[2], "w");
|
||||
if (!fout) { perror("fopen output"); fclose(fin); return 1; }
|
||||
|
||||
int cap = atoi(argv[3]);
|
||||
if (cap < 0) {
|
||||
fprintf(stderr, "invalid cap\n");
|
||||
fclose(fin);
|
||||
fclose(fout);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void *lib = dlopen(argv[4], RTLD_LAZY);
|
||||
if (!lib) {
|
||||
fprintf(stderr, "dlopen error: %s\n", dlerror());
|
||||
fclose(fin);
|
||||
fclose(fout);
|
||||
return 1;
|
||||
}
|
||||
|
||||
replace_func_t replace = (replace_func_t)dlsym(lib, "replace_char_line");
|
||||
if (!replace) {
|
||||
fprintf(stderr, "dlsym error: %s\n", dlerror());
|
||||
dlclose(lib);
|
||||
fclose(fin);
|
||||
fclose(fout);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int total = 0;
|
||||
char line[MAX_LINE];
|
||||
|
||||
while (fgets(line, sizeof(line), fin)) {
|
||||
if (cap > 0) {
|
||||
int key = (unsigned char)line[0];
|
||||
int repl_line = replace(line, key, &cap);
|
||||
total += repl_line;
|
||||
}
|
||||
fputs(line, fout);
|
||||
}
|
||||
|
||||
dlclose(lib);
|
||||
fclose(fin);
|
||||
fclose(fout);
|
||||
printf("total_replacements: %d\n", total);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user