-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslip13_1.c
More file actions
111 lines (111 loc) · 2.17 KB
/
slip13_1.c
File metadata and controls
111 lines (111 loc) · 2.17 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
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
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <dirent.h>
#include <unistd.h>
int make_toks(char *s, char *tok[])
{
int i = 0;
char *p;
p = strtok(s, " ");
while(p != NULL)
{
tok[i++] = p;
p = strtok(NULL, " ");
}
tok[i] = NULL;
return i;
}
void typeline(char *op, char *fn)
{
int fh, i, j, n;
char c;
fh = open(fn, O_RDONLY);
if(fh == -1)
{
printf("File %s not found.\n", fn);
return;
}
if(strcmp(op, "a") == 0)
{
while(read(fh, &c, 1) > 0)
printf("%c", c);
close(fh);
return;
}
n = atoi(op);
if(n > 0)
{
i = 0;
while(read(fh, &c, 1) > 0)
{
printf("%c", c);
if(c == '\n')
i++;
if(i == n)
break;
}
}
if(n < 0)
{
i = 0;
while(read(fh, &c, 1) > 0)
{
if(c == '\n') i++;
}
lseek(fh, 0, SEEK_SET);
j = 0;
while(read(fh, &c, 1) > 0)
{
if(c == '\n')
j++;
if(j == i+n+1)
break;
}
while(read(fh, &c, 1) > 0)
{
printf("%c", c);
}
}
close(fh);
}
int main()
{
char buff[80], *args[10];
while(1)
{
printf ("\n");
printf("\nmyshell$ ");
fgets(buff, 80, stdin);
buff[strlen(buff)-1] = '\0';
int n = make_toks(buff, args);
switch (n)
{
case 1: if(strcmp(args[0], "exit") == 0)
exit(1);
if (!fork())
execlp (args [0], args[0], NULL);
break;
case 2:
if (!fork ())
execlp (args [0], args[0], args[1], NULL);
break;
case 3: if (strcmp(args[0], "typeline") == 0)
typeline (args[1], args[2]);
else
{
if (!fork ())
execlp (args [0], args[0], args[1], args[2], NULL);
}
break;
case 4:
if (!fork ())
execlp (args [0], args [0], args [1], args [2], args [3], NULL);
break;
}
}
return 0;
}