-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistory.c
More file actions
83 lines (81 loc) · 1.8 KB
/
history.c
File metadata and controls
83 lines (81 loc) · 1.8 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
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
#include "header.h"
void history(int num){
if(num>10){
num=10;
}
const char fnam[]="myshell_his.txt";
int fd = open(fnam,O_RDWR | O_CREAT |O_APPEND,0644);
if(fd<0){
perror("Error opening file");
exit(1);
}
char buffer[1000055];
long long int sz = read(fd,buffer,1000055);
if(sz<0){
perror("Error reading file");
exit(1);
}
int args=0;
char *tok;
const char de[]="\n";
tok = strtok(buffer,de);
while(tok!=NULL&&args<num){
printf("%s\n",tok);
args++;
tok = strtok(NULL,de);
}
close(fd);
}
void write_cmd(char* buff){
const char fnam[]="myshell_his.txt";
int fd = open(fnam,O_RDWR | O_CREAT |O_APPEND,0644);
if(fd<0){
perror("Error opening file");
exit(1);
}
char buffer[1000055];
long long int sz = read(fd,buffer,1000055);
if(sz<0){
perror("Error reading file");
exit(1);
}
int args=0;
char *argv[100005];
char *tok;
const char de[]="\n";
tok = strtok(buffer,de);
while(tok!=NULL){
argv[args]=tok;
args++;
tok = strtok(NULL,de);
}
if(args>=20){
args=19;
}
close(fd);
fd = open(fnam,O_RDWR | O_CREAT |O_TRUNC,0644);
if(fd<0){
perror("Error opening file");
exit(1);
}
long long int len=strlen(buff),len2;
char inse[1000055];
strcpy(inse,buff);
long long int j = strlen(inse);
for(int i=0;i<args;i++){
len = strlen(argv[i])+j;
len2 = j;
while(j<len){
inse[j]=argv[i][j-len2];
j++;
}
inse[j++]='\n';
}
inse[j]='\0';
long long int sz3=write(fd,inse,strlen(inse));
if(sz3<0){
perror("Error at write");
exit(1);
}
close(fd);
}