-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlooping.c
176 lines (160 loc) · 3.43 KB
/
looping.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "shell.h"
/**
*looper - Handles command execution.
*@cmmds: Struct holding information of commands separated by ";"
*@args: Struct holding information of commands.
*@argv_0: Name of program held in argv[0].
*@cmd_count: Pointer to number of commands executed.
*
*Return: Nothing.
*/
void looper(cmd_t *cmmds, cmd_t *args, char *argv_0, int *cmd_count)
{
int i;
for (i = 0; i < cmmds->arg_count; i++)
{
cmmds->args[i] = rmv_space(cmmds->args[i]);
cmmds->args[i] = check_hash(cmmds->args[i]);
if (cmmds->args[i] == NULL)
continue;
args->arg_count = tokenize(cmmds->args[i], args->args, " ");
if (cases(cmmds, args, *cmd_count, i, argv_0))
{
free(cmmds->args[i]);
(*cmd_count)++;
continue;
}
cl_exec(cmmds, args, *cmd_count, argv_0);
free(cmmds->args[i]);
(*cmd_count)++;
}
}
/**
* init_cmd_t - Initializes cmd_t struct.
*
* This function initializes a cmd_t object that will be uses in main program.
*
* Return: Initialized cmd_t object
*/
cmd_t *init_cmd_t()
{
cmd_t *cmmds;
cmmds = (cmd_t *)malloc(sizeof(cmd_t));
if (!cmmds)
perror("malloc - cmmds");
cmmds->arg_count = 0;
cmmds->pipe = 0;
cmmds->foundPath = 1;
cmmds->called_setenv = 0;
return (cmmds);
}
/**
*read_file - Reads file.
*@input_file: Name of file.
*@argv_0: Name of program, held in argv[0].
*@cmmds: Pointer to struct containing commands information.
*@args: Pointer to struct containing commands information.
*
*Return: Copy of buffer containing file content
*/
char *read_file(char *input_file, char *argv_0, cmd_t *cmmds, cmd_t *args)
{
int file;
char buff[1024];
ssize_t bytesRead, tBytesRead = 0;
if (input_file == NULL)
{
e_printf("%s is null", input_file);
exit(errno);
}
file = open(input_file, O_RDONLY);
if (file == -1)
{
e_printf("%s: 0: Can't open %s\n", argv_0, input_file);
args->foundPath = 0;
clean(cmmds, args);
}
buff[0] = '\0';
while (1)
{
bytesRead = read(file, buff, sizeof(buff));
tBytesRead += bytesRead;
if (bytesRead == 0)
break;
if (bytesRead == -1 || file == -1)
{
e_printf("%s: 0: cannot read %s\n", argv_0, input_file);
args->foundPath = 0;
clean(cmmds, args);
}
}
buff[tBytesRead] = '\0';
if (close(file) == -1)
{
e_printf("%s: 0: cannot close %s\n", argv_0, input_file);
args->foundPath = 0;
clean(cmmds, args);
}
return (_strdup(buff));
}
/**
*replace_char - Replaces character in a string.
*@str: Input string.
*@c: Character to replace.
*@s: Character to replace @c.
*
*Return: Pointer to corrected string.
*/
char *replace_char(char *str, char c, char s)
{
int i, j = 0;
char *new_str;
if (!str || str == NULL)
return (NULL);
new_str = malloc(_strlen(str) + 1);
if (!new_str)
return (NULL);
for (i = 0; str[i] != '\0'; i++)
{
if (str[i] == c)
str[i] = s;
new_str[j++] = str[i];
}
new_str[j] = '\0';
free(str);
return (new_str);
}
/**
*rmv_double - Removes occurrence of double character @c in input string @cmd.
*@cmd: Input string.
*@c: Double character to remove
*
*
*
*Return: Pointer to corrected string.
*/
char *rmv_double(char *cmd, char c)
{
int i, j = 0;
char *cmd_clean;
int prev_space = (cmd[0] == c);
cmd_clean = malloc(_strlen(cmd) + 1);
for (i = 0; cmd[i] != '\0'; i++)
{
if (cmd[i] == c)
{
if (!prev_space)
cmd_clean[j++] = cmd[i];
prev_space = 1;
}
else
{
cmd_clean[j++] = cmd[i];
prev_space = 0;
}
}
cmd_clean[j] = '\0';
free(cmd);
cmd = cmd_clean;
return (cmd);
}