-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.c
More file actions
53 lines (43 loc) · 1.43 KB
/
env.c
File metadata and controls
53 lines (43 loc) · 1.43 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "env.h"
int get_shell_owner(ENV_t **env)
{
/*
* Don't need to free this below (pwd) as it is initilized by getpuid function using "static", so it's on the data segment not in the stack.
* It reamins on the scope till the program lives
*/
struct passwd *pwd;
char cwd[256];
char hst[256];
if (*env != NULL)
{
free((*env)->username);
free(*env);
}
*env = (ENV_t*)malloc(sizeof(ENV_t));
if (env == NULL)
return -1;
(*env)->permissions = getuid(); // Get the user UID.
pwd = getpwuid((*env)->permissions); // From the UID get informations from the specific user with this UID.
(*env)->username = (char*)malloc(sizeof(char) *strlen(pwd->pw_name) + 1);
(*env)->home_dir_path = (char*)malloc(sizeof(char)*strlen(pwd->pw_dir) + 1);
if(getcwd(cwd, sizeof(cwd)) == NULL)
{
return -1;
}
(*env)->curr_path = (char*)malloc(sizeof(char)*strlen(cwd) + 1);
if (gethostname(hst, sizeof(hst)) == -1)
{
return -1;
}
(*env)->hostname = (char*)malloc(sizeof(char)*strlen(hst) + 1);
strcpy((*env)->hostname, hst);
if ((*env)->username == NULL || (*env)->home_dir_path == NULL || (*env)->curr_path == NULL)
return -1;
else
{
strcpy((*env)->username, pwd->pw_name);
strcpy((*env)->home_dir_path, pwd->pw_dir);
strcpy((*env)->curr_path, cwd);
}
return 0;
}