Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible solution for transparently passing in zeroth argument #5

Closed
wants to merge 1 commit into from
Closed
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
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