Skip to content

Commit f37ebce

Browse files
committed
added cd functionality
1 parent 9f7e927 commit f37ebce

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

lokishell.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,23 @@ void sighandler(int sig_num)
236236
printf("\n=Ctrl+Z pressed\n");
237237
}
238238

239+
void changeDirectory(char *args[])
240+
{
241+
if (args[1] == NULL)
242+
{
243+
// If no argument is provided, go to the home directory
244+
chdir(getenv("HOME"));
245+
}
246+
else
247+
{
248+
if (chdir(args[1]) != 0)
249+
{
250+
perror("chdir");
251+
printf("Error changing directory to %s\n", args[1]);
252+
}
253+
}
254+
}
255+
239256
void setPathVariables()
240257
{
241258
// Get the value of the PATH environment variable
@@ -357,7 +374,7 @@ void forkProcess(char *args[], bool isBackgroundProcess, bool isLocalProcess)
357374
int status;
358375
char fullPath[MAX_LINE];
359376

360-
//If the process is not local, it searches the path variable
377+
// If the process is not local, it searches the path variable
361378
if (isLocalProcess)
362379
{
363380
char currentDir[1024];
@@ -384,7 +401,8 @@ void forkProcess(char *args[], bool isBackgroundProcess, bool isLocalProcess)
384401
snprintf(fullPath, sizeof(fullPath), "%s/%s", pathElements[i], args[0]);
385402
break;
386403
}
387-
if (pathElements[i+1] == NULL) {
404+
if (pathElements[i + 1] == NULL)
405+
{
388406
printf("error: command not found\n");
389407
return;
390408
}
@@ -499,7 +517,7 @@ int main()
499517
}
500518
else if (!strcmp(args[0], "cd"))
501519
{
502-
printf("not yet implemented\n");
520+
changeDirectory(args);
503521
}
504522
else if (!strcmp(args[0], "13killoki"))
505523
{

0 commit comments

Comments
 (0)