Skip to content

Commit 5d1cda8

Browse files
committed
Refactored makefile and added new functions move() and fix momary leak
1 parent 4756f8e commit 5d1cda8

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ FLAGS = -Wall -g
66
run:clean myShell
77
./myShell
88

9-
leak:
10-
valgrind --leak-check=full ./myShell
9+
leak: clean myShell
10+
valgrind --leak-check=full --show-leak-kinds=all ./myShell
1111

1212
myShell: myShell.o myFunction.o
1313
$(CC) $(FLAGS) -o myShell myShell.o myFunction.o

myFunction.c

+29-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ void delete(char **path)
191191
printf("-myShell: delete: %s: No such file or directory\n", path[1]);
192192
}
193193

194-
// Corrected the function return type from 'oid' to 'void'
195194
void get_dir()
196195
{
197196
// Declare a directory stream pointer 'dir' and a directory entry pointer 'entry'
@@ -264,3 +263,32 @@ void mypipe(char **argv1,char ** argv2){
264263
}
265264
}
266265

266+
void move(char **arguments){
267+
if (arguments[1] == NULL || arguments[2] == NULL){
268+
printf("move: missing file operand\n");
269+
return;
270+
}
271+
if (arguments[3] != NULL){
272+
printf("move: too many arguments\n");
273+
return;
274+
}
275+
FILE *source, *target;
276+
char ch;
277+
source = fopen(arguments[1], "r");
278+
if (source == NULL){
279+
printf("move: cannot stat '%s': No such file or directory\n", arguments[1]);
280+
return;
281+
}
282+
target = fopen(arguments[2], "w");
283+
if (target == NULL){
284+
printf("move: cannot create regular file '%s': No such file or directory\n", arguments[2]);
285+
return;
286+
}
287+
while ((ch = fgetc(source)) != EOF){
288+
fputc(ch, target);
289+
}
290+
fclose(source);
291+
fclose(target);
292+
printf("File moved successfully.\n");
293+
}
294+

myFunction.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ void cp(char ** arguments);
4343
void delete(char **path);
4444
void get_dir();
4545
void SystemCall(char **arguments);
46-
void mypipe(char **argv1, char **argv2);
46+
void mypipe(char **argv1, char **argv2);
47+
void move(char **arguments);

myShell.c

+13-10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ int pipeCheck(char **arguments)
2323
{
2424
if (strcmp(arguments[i], "|") == 0)
2525
{
26+
free(arguments[i]);// free the pipe character because we can't free null
2627
arguments[i] = NULL;
2728
return 1;
2829
}
@@ -85,10 +86,8 @@ int main()
8586
}
8687

8788
char **arguments = splitArgument(input);
88-
8989
int size = argumentArraySize(arguments);
9090
int isPipe = pipeCheck(arguments);
91-
9291
if (strcmp(arguments[0], "exit") == 0){
9392
freeArguments(arguments);
9493
logout(input);
@@ -103,7 +102,10 @@ int main()
103102
delete(arguments);
104103
}else if (strcmp(arguments[0], "dir") == 0){
105104
get_dir();
106-
}else if(isPipe){
105+
}else if (strcmp(arguments[0], "mv") == 0){
106+
move(arguments);
107+
}
108+
else if(isPipe){
107109
char ***args = splitArgumentsArray(arguments, size);
108110
mypipe(args[0], args[1]);
109111
wait(NULL);
@@ -112,19 +114,20 @@ int main()
112114
free(args[i]);
113115
}
114116
free(args);
115-
for (int i = 0; i < size; i++)
116-
{
117-
free(arguments[i]);
118-
}
119-
free(arguments);
120117
}
121118
else{
122119
SystemCall(arguments);
123120
}
124-
if(isPipe == 0)
121+
for (int i = 0; i < size; i++)
125122
{
126-
freeArguments(arguments);
123+
if(arguments[i] != NULL){
124+
puts(arguments[i]);
125+
}
126+
}
127+
for (int i = 0; i < size; i++) {
128+
free(arguments[i]);
127129
}
130+
free(arguments);
128131
free(input);
129132
}
130133
return 0;

0 commit comments

Comments
 (0)