-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrep.c
64 lines (53 loc) · 1.15 KB
/
crep.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
#include<stdio.h>
#include<stdlib.h>
#include<getopt.h>
void print_usage() {
printf("Usage: crep -c 2 -d , \"Hello World\"\n");
printf("\nOptions:\n\t-c: for count to specify \
number of times message should be repeated.\n\t-d: \
for delimeter to print between message.");
printf("\n\nNOTE: message should be given as last \
argument.\n\teg. `crep -c 3 -d , hello world` will \
only take world as message value.");
}
void do_rep(int count, char* delim, char* message) {
while(--count) {
printf("%s%s", message, delim);
}
printf("%s", message);
}
int main(int arg_count, char **arg_array) {
if(arg_count < 2){
print_usage();
exit(1);
}
opterr = 0;
int opt, count;
char *delim="", *message;
while((opt = getopt(arg_count, arg_array, "d::c:h")) != -1) {
switch (opt) {
case 'd':
delim = optarg;
break;
case 'c':
count = atoi(optarg);
break;
case '?':
case 'h':
default:
print_usage();
exit(1);
}
}
message = arg_array[arg_count-1];
if(count<=0) {
delim = "";
printf("Invalid Options: -h for help.");
exit(0);
}
if(delim == NULL) {
delim = "";
}
do_rep(count, delim, message);
return 0;
}