File tree 4 files changed +46
-14
lines changed
4 files changed +46
-14
lines changed Original file line number Diff line number Diff line change @@ -6,8 +6,8 @@ FLAGS = -Wall -g
6
6
run :clean myShell
7
7
./myShell
8
8
9
- leak :
10
- valgrind --leak-check=full ./myShell
9
+ leak : clean myShell
10
+ valgrind --leak-check=full --show-leak-kinds=all ./myShell
11
11
12
12
myShell : myShell.o myFunction.o
13
13
$(CC ) $(FLAGS ) -o myShell myShell.o myFunction.o
Original file line number Diff line number Diff line change @@ -191,7 +191,6 @@ void delete(char **path)
191
191
printf ("-myShell: delete: %s: No such file or directory\n" , path [1 ]);
192
192
}
193
193
194
- // Corrected the function return type from 'oid' to 'void'
195
194
void get_dir ()
196
195
{
197
196
// Declare a directory stream pointer 'dir' and a directory entry pointer 'entry'
@@ -264,3 +263,32 @@ void mypipe(char **argv1,char ** argv2){
264
263
}
265
264
}
266
265
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
+
Original file line number Diff line number Diff line change @@ -43,4 +43,5 @@ void cp(char ** arguments);
43
43
void delete (char * * path );
44
44
void get_dir ();
45
45
void SystemCall (char * * arguments );
46
- void mypipe (char * * argv1 , char * * argv2 );
46
+ void mypipe (char * * argv1 , char * * argv2 );
47
+ void move (char * * arguments );
Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ int pipeCheck(char **arguments)
23
23
{
24
24
if (strcmp (arguments [i ], "|" ) == 0 )
25
25
{
26
+ free (arguments [i ]);// free the pipe character because we can't free null
26
27
arguments [i ] = NULL ;
27
28
return 1 ;
28
29
}
@@ -85,10 +86,8 @@ int main()
85
86
}
86
87
87
88
char * * arguments = splitArgument (input );
88
-
89
89
int size = argumentArraySize (arguments );
90
90
int isPipe = pipeCheck (arguments );
91
-
92
91
if (strcmp (arguments [0 ], "exit" ) == 0 ){
93
92
freeArguments (arguments );
94
93
logout (input );
@@ -103,7 +102,10 @@ int main()
103
102
delete (arguments );
104
103
}else if (strcmp (arguments [0 ], "dir" ) == 0 ){
105
104
get_dir ();
106
- }else if (isPipe ){
105
+ }else if (strcmp (arguments [0 ], "mv" ) == 0 ){
106
+ move (arguments );
107
+ }
108
+ else if (isPipe ){
107
109
char * * * args = splitArgumentsArray (arguments , size );
108
110
mypipe (args [0 ], args [1 ]);
109
111
wait (NULL );
@@ -112,19 +114,20 @@ int main()
112
114
free (args [i ]);
113
115
}
114
116
free (args );
115
- for (int i = 0 ; i < size ; i ++ )
116
- {
117
- free (arguments [i ]);
118
- }
119
- free (arguments );
120
117
}
121
118
else {
122
119
SystemCall (arguments );
123
120
}
124
- if ( isPipe == 0 )
121
+ for ( int i = 0 ; i < size ; i ++ )
125
122
{
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 ]);
127
129
}
130
+ free (arguments );
128
131
free (input );
129
132
}
130
133
return 0 ;
You can’t perform that action at this time.
0 commit comments