-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
48 lines (37 loc) · 992 Bytes
/
shell.c
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
#include "header.h"
void directory(char * path){
getcwd(path, MAX_LEN);
}
char* get_directory(char* home){
char* cur = (char*)malloc(sizeof(char)*MAX_LEN);
directory(cur);
if(strlen(cur) < strlen(home)) return cur;
if(!strcmp(cur,home)){
free(cur);
char* tilda = strdup("~");
return tilda;
}
if(!strncmp(home,cur,strlen(home))){
char* path = (char*)malloc(sizeof(char)*MAX_LEN);
path[0] = '~';
strcpy(path+1,cur+strlen(home));
free(cur);
return path;
}
return cur;
}
char* get_system_name(){
struct utsname buffer;
uname(&buffer);
char* systemName = (char*)malloc(sizeof(char)*MAX_LEN);
strcpy(systemName,buffer.nodename);
return systemName;
}
char* get_username(){
uid_t uid = getuid();
struct passwd *user;
user = getpwuid(uid);
char* userName = (char*)malloc(sizeof(char)*MAX_LEN);
strcpy(userName,user->pw_name);
return userName;
}