-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.c
55 lines (42 loc) · 1 KB
/
map.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 128
#define FUNCTION 1
#define FIRST_ARG 2
int exec_function(char *, char *);
int main(int argc, char *argv[]) {
int return_code = 0;
for (int i = 0; i < argc - FIRST_ARG; i++) {
return_code = exec_function(argv[FUNCTION], argv[i + FIRST_ARG]);
if (return_code != 0) {
return return_code;
}
};
return 0;
}
int exec_function(char *func_name, char *arg) {
char buf[BUFSIZE];
FILE *fp;
char *cmd;
int i = 0;
i = strlen(func_name) + 1 + // leave memory for a space between
strlen(arg);
// add memory for the null terminator
cmd = (char *)malloc(i + 1);
strcat(strcat(cmd, func_name), " ");
strcat(cmd, arg);
fp = popen(cmd, "r");
if (fp == NULL) {
printf("Error opening pipe!\n");
return -1;
}
while (fgets(buf, BUFSIZE, fp) != NULL) {
printf("%s", buf);
}
if (pclose(fp)) {
printf("Command not found or exited with error status\n");
return -1;
}
return 0;
}