Skip to content

Commit

Permalink
Deref argv0 and fix initial dash
Browse files Browse the repository at this point in the history
Fixes #4, #7
Closes #5

Co-authored-by: Rutherther <[email protected]>
  • Loading branch information
viperML and Rutherther committed May 16, 2024
1 parent dbbfaa6 commit 03e31c7
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,32 @@ char* getshell(void) {
return strdup(DEFAULT_SHELL);
}

/// Changes argv0 depending on the filetype
/// - Regular file: uses the basename
/// - Symlink: dereferences the symlink once and uses the basename
void argv0_deref(char** argv0_p) {
struct stat sb = {};
if (lstat(*argv0_p, &sb) != 0) {
fprintf(stderr, "WARN: Failed to stat the shell\n");
return;
}

if (S_ISREG(sb.st_mode)) {
*argv0_p = strdup(basename(*argv0_p));
return;
}

if (S_ISLNK(sb.st_mode)) {
char buf[PATH_MAX + 1];
if (readlink(*argv0_p, buf, PATH_MAX) == -1) {
fprintf(stderr, "Failed to readlink\n");
return;
}
*argv0_p = strdup(basename(buf));
return;
}
}

int main(int argc, char* argv[]) {
(void)argc;
char* shell = getshell();
Expand All @@ -82,7 +108,15 @@ int main(int argc, char* argv[]) {

fprintf(stderr, "INFO: Using %s\n", shell);

bool login_dash = argv[0][0] == '-';

argv[0] = shell;
argv0_deref(&argv[0]);

if (login_dash) {
asprintf(&argv[0], "-%s", argv[0]);
}

execvp(shell, argv);
free(shell);

Expand Down

0 comments on commit 03e31c7

Please sign in to comment.