-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargument_parser.c
54 lines (48 loc) · 1.07 KB
/
argument_parser.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
#include "argument_parser.h"
bool getargs(int *argcp, char *argv[], int max, bool *eopf)
{
static char cmd[MAXLINE];
char *cmdp;
int i;
*eopf = false;
if (fgets(cmd, sizeof(cmd), stdin) == NULL)
{
if (ferror(stdin))
printf("Error while reading command line");
*eopf = true;
return false;
}
if (strchr(cmd, '\n') == NULL)
{
while (true)
{
switch (getchar())
{
case '\n':
break;
case EOF:
if (ferror(stdin))
printf("Incorrect commnad");
default:
continue;
}
break;
}
printf("Input too long - commnad ignored\n");
return false;
}
cmdp = cmd;
for (i = 0; i < max; i++)
{
if ((argv[i] = strtok(cmdp, " \t\n")) == NULL)
break;
cmdp = NULL;
}
if (i >= max)
{
printf("Input too long - commnad ignored\n");
return false;
}
*argcp = i;
return true;
}