-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexcute_file.c
95 lines (86 loc) · 1.52 KB
/
excute_file.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
#include "shell.h"
/**
* read_file - Read Command From File
* @filename:Filename
* @argv:Program Name
* Return: -1 or 0
*/
void read_file(char *filename, char **argv)
{
FILE *fp;
char *line = NULL;
size_t len = 0;
int counter = 0;
fp = fopen(filename, "r");
if (fp == NULL)
{
exit(EXIT_FAILURE);
}
while ((getline(&line, &len, fp)) != -1)
{
counter++;
treat_file(line, counter, fp, argv);
}
if (line)
free(line);
fclose(fp);
exit(0);
}
/**
* treat_file - PARSE Check Command Fork Wait Excute in Line of File
* @line: Line From A File
* @counter:Error Counter
* @fp:File Descriptor
* @argv:Program Name
* Return : Excute A line void
*/
void treat_file(char *line, int counter, FILE *fp, char **argv)
{
char **cmd;
int st = 0;
cmd = parse_cmd(line);
if (_strncmp(cmd[0], "exit", 4) == 0)
{
exit_bul_for_file(cmd, line, fp);
}
else if (check_builtin(cmd) == 0)
{
st = handle_builtin(cmd, st);
free(cmd);
}
else
{
st = check_cmd(cmd, line, counter, argv);
free(cmd);
}
}
/**
* exit_bul_for_file - Exit Shell Case Of File
* @line: Line From A File
* @cmd: Parsed Command
* @fd:File Descriptor
* Return : Case Of Exit in A File Line
*/
void exit_bul_for_file(char **cmd, char *line, FILE *fd)
{
int statue, i = 0;
if (cmd[1] == NULL)
{
free(line);
free(cmd);
fclose(fd);
exit(errno);
}
while (cmd[1][i])
{
if (_isalpha(cmd[1][i++]) < 0)
{
perror("illegal number");
}
}
statue = _atoi(cmd[1]);
free(line);
free(cmd);
fclose(fd);
exit(statue);
}