-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflags-parser.c
41 lines (36 loc) · 1.14 KB
/
flags-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
//
// Created by z on 12/17/22.
//
#include "flags-parser.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include "shell-builtins.h"
void parse_flags(FLAGS *flags, int argc, char **argv) {
bzero(flags->command, MAX_COMMAND_SIZE + 1);
int option;
while ((option = getopt(argc, argv, "c:x")) != -1) {
switch (option) {
case 'c':
(void)strncpy(flags->command, optarg, strnlen(optarg, MAX_COMMAND_SIZE));
flags->command[strnlen(optarg, MAX_COMMAND_SIZE)] = '\t';
flags->command[strnlen(optarg, MAX_COMMAND_SIZE) + 1] = '\0';
flags->c = true;
break;
case 'x':
flags->x = true;
break;
default:
(void)fprintf(stderr, "Usage: %s [ −x] [ −c command]\n", PROGRAM_NAME);
exit(EXIT_FAILURE);
}
}
argc -= optind;
if (argc != 0) {
(void)fprintf(stderr, "Usage: %s [ −x] [ −c command]", PROGRAM_NAME);
set_last_command_status(EXIT_FAILURE);
exit(EXIT_FAILURE);
}
}