-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredir.c
72 lines (69 loc) · 1.37 KB
/
redir.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int check_redir(char *str)
{
int i;
i = 0;
char temp;
while ((str[i] != '>' && str[i] != '<') && str[i])
i++;;
if (!str[i])
return (2);
if (str[i] == '<' || str[i] == '>')
{
temp = str[i++];
if (str[i] == temp)
i++;
else if (str[i] == '<' || str[i] == '>')
printf("error <>\n");
}
if (!str[i])
return (1);
else
return (0);
}
int redir_type(char *str)
{
if (strncmp(str, ">>", 2) == 0)
return (1);
else if (strncmp(str, ">", 1) == 0)
return (2);
else if (strncmp(str, "<<", 2) == 0)
return (3);
else
return (4);
}
int main()
{
char *str = strdup("ls>1 >> 2 <3 <<4");
char* tmp = strtok(str, " ");
int j = 0;
char** mass;
mass = (char **)malloc(sizeof(char*) * 5);
mass[j] = tmp;
j++;
while (j < 5)
{
tmp = strtok(NULL, " ");
mass[j] = tmp;
j++;
}
for (int i = 0; i < 5; i++)
printf("%s\n", mass[i]);
j = 0;
while (j < 5)
{
int check = check_redir(mass[j]);
//printf("%s %d\n", mass[j], check_redir(mass[j]));
if (check == 1)
{
int type = redir_type(mass[j]);
if (mass[j + 1])
create_files(mass[j + 1], type);
else
printf("error end of massive\n");
}
j++;
}
}