-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathyes.c
57 lines (47 loc) · 1.09 KB
/
yes.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
/*
* Author: Nagachaitanya Vellanki
*
* yes - output a string repeatedly until killed
*/
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
typedef enum {false, true} bool;
void err_exit(const char *format, ...) {
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
exit(EXIT_FAILURE);
}
void printUsage() {
fprintf(stdout, "Usage: yes [STRING]\n or: yes [OPTION]\nRepeatedly output a line with all specified STRING(s), or `y'.\n");
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[]) {
int32_t option, i;
while((option = getopt(argc, argv, "h")) != -1) {
switch(option) {
case 'h': printUsage();
default: err_exit("Try 'yes -h' for more information.\n", option);
}
}
while(true) {
if(argc == 1) {
printf("y\n");
} else {
for(i = optind; i < argc; i++) {
printf("%s", argv[i]);
if(i == (argc - 1)) {
printf("\n");
} else {
printf(" ");
}
}
}
}
exit(EXIT_SUCCESS);
}