-
Notifications
You must be signed in to change notification settings - Fork 0
/
leakcount.c
42 lines (40 loc) · 1.25 KB
/
leakcount.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
#define _GNU_SOURCE
/*
Zachary Ikpefua
ECE 3220
Project 1-1 Leakcount
Purpose: Count all the leaks in a given program from the command line
*/
#define ADJUST_NULL_SPACE 1 //for the null needed on execvp
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
int main(int argc, char** argv) {
int i = 0;
//add my shim library
setenv("LD_PRELOAD","./memory_shim.so",1);
if(fork() == 0) {
char * args[argc + ADJUST_NULL_SPACE];
for(i = 1; i < argc; i++) {
//argv arguments start at 1
args[i - 1] = malloc(strlen(argv[i]) + ADJUST_NULL_SPACE);
strcpy(args[i-ADJUST_NULL_SPACE],argv[i]);
}
args[i-ADJUST_NULL_SPACE] = NULL;
execvp(args[0],args);
//if execvp fails
perror("failed execvp");
//free the args array
for(i = 0; i < argc; i++) {
if(args[i] != NULL) {
free(args[i]);
}
}
}
//wait for the child
wait(NULL);
return 0;
}