Skip to content

Commit

Permalink
feat: pass in shell's zeroth argument transparently
Browse files Browse the repository at this point in the history
Fixes #4.
  • Loading branch information
Rutherther committed May 16, 2024
1 parent dbbfaa6 commit 21d6b69
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,37 @@ bool usable(char const* path) {
}
}

// Get argument that should be passed as zeroth
// argument to the shell executable.
void get_shell_arg(char* buffer, char* argv, char* shell)
{
bool written = false, read_status = true;

if (argv[0] == '-') {
(*buffer++) = '-';
}

struct stat buf;
if (lstat(shell, &buf) != 0) {
perror("ERROR: shell file status cannot be read");
read_status = false;
}

if (read_status && S_ISLNK(buf.st_mode)) {
ssize_t len = readlink(shell, buffer, PATH_MAX);
if (len < 0) {
perror("ERROR: Failed to read shell link value");
} else {
written = true;
buffer[len] = '\0';
}
}

if (!written) {
strcpy(buffer, shell);
}
}

char* getshell(void) {
char* relpath = "shell";
char* xdg_config_home = getenv("XDG_CONFIG_HOME");
Expand Down Expand Up @@ -82,7 +113,10 @@ int main(int argc, char* argv[]) {

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

argv[0] = shell;
char arg_zero_buf[PATH_MAX + 1];
get_shell_arg(arg_zero_buf, argv[0], shell);
argv[0] = arg_zero_buf;

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

Expand Down

0 comments on commit 21d6b69

Please sign in to comment.