Skip to content

Commit

Permalink
For each command, the double pointer character args (arguments) and e…
Browse files Browse the repository at this point in the history
…ach pointer it was pointing to are alloced but not freed. A new function free_args(char **args) is added to free them.
  • Loading branch information
omer committed Dec 3, 2017
1 parent 82b95fe commit 392b397
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
21 changes: 16 additions & 5 deletions cmd_mains.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ int rm_main(char *buf)
{
char **args, flags[5];
int pflag = 0, vflag = 0, rflag = 0, hflag = 0, i = 0;

args = get_args(buf, 2);
get_flags(buf, flags);

if (flags[0] != '\0') {
do {
switch (flags[i])
Expand Down Expand Up @@ -45,6 +45,7 @@ int rm_main(char *buf)
for (i = 0; i < _argc; ++i) {
rm(args[i], vflag, pflag, rflag);
}
free_args(args);
return 0;
}

Expand Down Expand Up @@ -94,14 +95,15 @@ int ls_main(char *buf)
} else
ls(args[0], aflag, iflag, rflag);

free_args(args);
return 0;
}

int mkdir_main(char *buf)
{
char **args, flags[5];
int vflag = 0, hflag = 0, i = 0;

args = get_args(buf, 5);
get_flags(buf, flags);

Expand Down Expand Up @@ -132,10 +134,11 @@ int mkdir_main(char *buf)
goto mkdir_usage;
return 1;
} else {
for (i = 0; i < _argc; ++i)
for (i = 0; i < _argc; ++i)
makedir(args[i], vflag);
}

free_args(args);
return 0;
}

Expand Down Expand Up @@ -178,6 +181,7 @@ int cat_main(char *buf)
} else
cat(args[0], nflag);

free_args(args);
return 0;
}

Expand All @@ -199,6 +203,13 @@ int cd_main(char *buf)
else
cd(args[0]);

free_args(args);
return 0;
}


void free_args(char **args) {
for (int i = 0; i < _argc; i++) {
free(args[i]);
}
free(args);
}
2 changes: 1 addition & 1 deletion cmd_mains.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ int cat(const char *name, int nflag);
int cat_main(char *buf);
int cd(const char *name);
int cd_main(char *buf);

void free_args(char **args);
#endif

0 comments on commit 392b397

Please sign in to comment.