From 93c1e5f8c2e0a431894274640b54ac3bea72e205 Mon Sep 17 00:00:00 2001 From: night0721 Date: Sun, 31 Mar 2024 00:01:29 +0000 Subject: [PATCH] fix change directory from argument --- ccc.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/ccc.c b/ccc.c index 8e2c875..d062dbb 100644 --- a/ccc.c +++ b/ccc.c @@ -64,10 +64,15 @@ int main(int argc, char** argv) die("Usage: ccc filename"); if (argc == 2) { struct stat st; - if (lstat(argv[1], &st) != 0) { + if (lstat(argv[1], &st)) { perror("ccc"); die("Error from lstat"); } + /* chdir to directory from argument */ + if(S_ISDIR(st.st_mode) && chdir(argv[1])) { + perror("ccc"); + die("Error from chdir"); + } } /* check if it is interactive shell */ @@ -109,11 +114,7 @@ int main(int argc, char** argv) hashtable_init(); cwd = memalloc(PATH_MAX * sizeof(char)); - if (argc == 2) { - strcpy(cwd, argv[1]); - } else { - getcwd(cwd, PATH_MAX); - } + getcwd(cwd, PATH_MAX); p_cwd = memalloc(PATH_MAX * sizeof(char)); start_ccc(); populate_files(cwd, 0);