Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions exec_cd.c
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @lawalalx, I have a few questions and concerns...

  1. Does your code follow the Betty style?
  2. The headers you have here are defined in our header file liteshell.h.
  3. Where is MAX_PATH defined?
  4. Finally, I see you are handling errors which is fine but is that how sh handles the errors?
  5. What error codes are we receiving on such errors?
  6. Looks like your function documentation is not complete as well

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lawalalx what's your current progress on this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will reply to every quest and make necessary corrections before the close of biz today. currently tightly engaged
Thanks

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

/**
* exec_cd - the function that is used to change the directory
* arg: the argument. that is the
* Return: Always 0
*/

char lastdir[MAX_PATH]; // initialized to zero

int exec_cd(char *arg) {
char curdir[MAX_PATH];
char path[MAX_PATH];

if (getcwd(curdir, sizeof curdir)) {
/* current directory might be unreachable: not an error */
*curdir = '\0';
}
if (arg == NULL) {
arg = getenv("HOME");
}
if (!strcmp(arg, "-")) {
if (*lastdir == '\0') {
fprintf(stderr, "no previous directory\n");
return 1;
}
arg = lastdir;
} else {
/* this should be done on all words during the parse phase */
if (*arg == '~') {
if (arg[1] == '/' || arg[1] == '\0') {
snprintf(path, sizeof path, "%s%s", getenv("HOME"), arg + 1);
arg = path;
} else {
/* ~name should expand to the home directory of user with login `name`
this can be implemented with getpwent() */
fprintf(stderr, "syntax not supported: %s\n", arg);
return 1;
}
}
}
if (chdir(arg)) {
fprintf(stderr, "chdir: %s: %s\n", strerror(errno), path);
return 1;
}
strcpy(lastdir, curdir);
return 0;
}