-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
84 lines (69 loc) · 1.49 KB
/
shell.c
File metadata and controls
84 lines (69 loc) · 1.49 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
84
#include "shell.h"
void help() {
/* prints the help string */
const char help_str[200] = \
"h[elp]\n\
d[ir]\n\
q[uit]\n\
hi[story]\n\
du[mp] [start, end]\n\
e[dit] address, value\n\
f[ill] start, end, value\n\
reset\n\
opcode mnemonic\n\
opcodelist\n\
assemble filename\n\
type filename\n\
symbol\n";
printf("%s",help_str);
}
void dir() {
/*
* lists all the files contained in the current directory
* [ref] en.wikibooks.org/wiki/C_Programming/POSIX_Reference/dirent.h
* [ref] en.wikibooks.org/wiki/C_Programming/POSIX_Reference/sys/stat.h
*/
struct dirent *entry;
DIR *dp;
struct stat fstat;
dp = opendir(".");
while((entry = readdir(dp))) {
lstat(entry->d_name,&fstat); /* get file mode */
printf("%s", entry->d_name);
if(S_ISDIR(fstat.st_mode)) printf("/"); /* directory */
else if(fstat.st_mode & S_IXUSR) printf("*"); /* executable */
printf("\n");
}
closedir(dp);
}
void type(char filename[]) {
/*
* print out the content of the file
* Arguments:
* char filename[]
*/
struct stat fstat;
FILE *fp = fopen(filename,"r");
char line[200];
if(!fp) {
fprintf(stderr,"%s not found!\n",filename);
return;
}
lstat(filename,&fstat); /* get file mode */
if(S_ISREG(fstat.st_mode))
while(fscanf(fp,"%[^\n]s",line)!=EOF) {
fgetc(fp);
printf("%s\n",line);
line[0]='\0';
}
fclose(fp);
}
void history(NODE *head) {
/* traverse the list and print */
NODE *curr = head;
int i=1;
while(curr) {
printf("%d\t%s\n",i++,curr->cmd);
curr = curr->link;
}
}