Created
March 28, 2019 15:23
-
-
Save yoh2/331af6410b81520d2d10883b6e81386a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
const char *get_dir(int argc, char *argv[]) | |
{ | |
if(argc <= 1) | |
{ | |
const char *home = getenv("HOME"); | |
return (home == NULL) ? "/" : home; | |
} | |
else | |
{ | |
return argv[1]; | |
} | |
} | |
const char *get_sh(void) | |
{ | |
const char *sh = getenv("SHELL"); | |
return (sh == NULL) ? "/bin/sh" : sh; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
const char *dir = get_dir(argc, argv); | |
if(chdir(dir) != 0) | |
{ | |
perror(dir); | |
return 1; | |
} | |
const char *sh = get_sh(); | |
if(execlp(sh, sh, (const char *)NULL) != 0) | |
{ | |
perror(sh); | |
return 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment