-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
38 lines (29 loc) · 879 Bytes
/
Copy pathshell.c
File metadata and controls
38 lines (29 loc) · 879 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h> // printf, perror
#include <unistd.h> // getcwd
#include <stdlib.h> // malloc
#include <string.h> // strcat, strcpy
#include "shell.h" // display_prompt
#include <wait.h>
#include <signal.h>
char HOME_DIR[1024];
void set_home_dir()
{
// path to the executable : readlink to read the symbolic link /proc/pid/exe
char path[1024] = "/proc/\0", temp[64];
snprintf(temp, 63, "%d", getpid());
strcat(path, temp);
strcat(path, "/exe");
if ((readlink(path, HOME_DIR, 1023)) < 0)
{
perror("");
HOME_DIR[0] = '/';
HOME_DIR[1] = '\0'; //default
}
if (strlen(HOME_DIR) > 5)
HOME_DIR[strlen(HOME_DIR) - 6] = '\0'; // HARDCODED :: EXECUTABLE NAME IS "/SHELL" SO IT IS REMOVED
}
int main()
{ // to do : check for possible memory leaks with valgrind
set_home_dir();
parse_commands();
}