-
Notifications
You must be signed in to change notification settings - Fork 0
/
def.h
156 lines (133 loc) · 3.26 KB
/
def.h
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<stdbool.h>
#include"redir.c"
#include"exec.c"
#include"pipe.c"
#include"env.c"
struct sys_data
{
char hostname[250],username[250];
};
struct Proc
{
int proc_status;
char name[100];
int status;
};
void status_bg(int *proc_status,int proc_count,char proc_name[][100]);
int get_args(int **arg);
char **get_name(int **arg,char names[][100]);
void print_perms(int args_int,char name[50]);
void ls_init(int **arg);
char *nth_word(char *proc_path,int n,char *ret);
char p_stat(char *proc_path);
void pinfo(char **arg);
int redir(char *cmd);
int check_redirect(char *cmd);
void execute_cmd(char *command,char *tilda,int proc_status[],int* proc_count,char proc_name[][100],int *proc_id);
int pipe_run(char *cmd,char *tilda,char *proc_status,int *proc_count,char proc_name[][100],int *proc_id);
int pipe_detect(char *cmd);
void jobsppp(int *proc_status,int *proc_count,char proc_name[][100],int *proc_id);
void set_env(char *cmd_first,char **args);
void unset_env(char *cmd_first,char **args);
void kjob(int proc_status[],char **arg);
void overkill(int proc_status[]);
void get_uh_names(struct sys_data *s)
{
gethostname(s->hostname,250);
getlogin_r(s->username,250);
}
bool tilda_beg(char *a,char *b)
{
if(strncmp(a,b,strlen(b))==0)
return 1;
return 0;
}
char *current_d(char *dir,char *tilda)
{
char current_dir[550];
getcwd(current_dir,550);
/* printf("cr:%s\n",current_dir); */
if(tilda_beg(current_dir,tilda))
{
strcat(dir,¤t_dir[strlen(tilda)]);
/* printf("tilda:%s\n",dir); */
return dir;
}
strcpy(dir,current_dir);
return dir;
}
char *gen_prompt(char *strx,char *tilda)
{
struct sys_data uhname;
get_uh_names(&uhname);
char dir[500]="~";
current_d(dir,tilda);
sprintf(strx,"%s@%s: \033[22;34m%s\033[0m\n\x1b[32m>\033[0m ",uhname.username,uhname.hostname,dir);
return strx;
}
void tilda_init(char *tilda)
{
for (int i = strlen(tilda)-1; i >= 0; --i)
{
if(tilda[i]=='/')
{
tilda[i]='\0';
return;
}
}
}
void hist_init(char *command,char *tilda)
{
if(command[0]=='\33')
return;
FILE *hist,*hist_temp;
char *buff,path[1000];
size_t buff_len=0;
strcpy(path,tilda);
strcat(path,"/temp");
hist_temp=fopen(path,"w");
strcpy(path,tilda);
strcat(path,"/.hist");
hist=fopen(path,"a+");
fprintf(hist_temp, "%s",command);
int i=19;
while(getline(&buff,&buff_len,hist)>1&&i--!=0)
{
/* printf("i%d,%s\n",i,buff); */
fprintf(hist_temp,"%s",buff);
}
fclose(hist);
fclose(hist_temp);
rename("temp",".hist");
}
void p_hist(int n,char *tilda)
{
FILE *hist;
if(n>10)
n=10;
char path[1000];
strcpy(path,tilda);
strcat(path,"/.hist");
hist=fopen(path,"a+");
char *buff;
size_t buff_len=0;
int count=0;
char history[10][1000];
while(getline(&buff,&buff_len,hist)>1&&n--!=0)
{
strcpy(history[count++],buff);
/* printf("%s",buff); */
}
for (int i = 0; i < count; ++i)
{
printf("%s",history[count-1-i]);
}
}