-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_program.c
More file actions
57 lines (46 loc) · 1.27 KB
/
simple_program.c
File metadata and controls
57 lines (46 loc) · 1.27 KB
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
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
int main(int argc, char **argv) {
int c;
int has_option = 0;
char *echo_msg = NULL;
char *name = NULL;
// h: Help
// e [msg]: Echo message
// n [name]: Print Hi [name]!
// p: Pong!
// j
while ((c = getopt(argc, argv, "he:n:pj")) != -1) {
has_option = 1;
switch (c) {
case 'h':
printf("Usage: %s [Options]\n", argv[0]);
puts("\nOptions:");
puts("\t-h: Print help message");
puts("\t-e [msg]: Echo message");
puts("\t-p: Pong!");
return 0;
case 'e':
echo_msg = optarg;
break;
case 'n':
name = optarg;
break;
case 'p':
puts("Pong!");
break;
case 'j':
puts("Jerry is the most handsome professor in the universe!\n");
break;
}
}
if (!has_option) {
fprintf(stderr, "%s: Missing options\nTry '%s -h' for help.\n", argv[0], argv[0]);
return 1;
}
if (echo_msg != NULL)
puts(echo_msg);
if (name != NULL)
printf("Hi %s!\n", name);
}