getopt options
This commit is contained in:
parent
a0eacd886c
commit
66228d76aa
1 changed files with 36 additions and 1 deletions
37
wc.c
37
wc.c
|
@ -1,6 +1,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <getopt.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
bool mode_bytes = false;
|
bool mode_bytes = false;
|
||||||
|
@ -18,6 +19,15 @@ int intlen(int number) {
|
||||||
|
|
||||||
void wc(char **list, int count) {
|
void wc(char **list, int count) {
|
||||||
char buffer[BUFSIZ];
|
char buffer[BUFSIZ];
|
||||||
|
/**
|
||||||
|
* Items = [
|
||||||
|
* [0] => lines,
|
||||||
|
* [1] => words,
|
||||||
|
* [2] => chars,
|
||||||
|
* [3] => bytes,
|
||||||
|
* [4] => max-line-length
|
||||||
|
* ]
|
||||||
|
*/
|
||||||
int *lines = calloc(count, sizeof(int));
|
int *lines = calloc(count, sizeof(int));
|
||||||
int *words = calloc(count, sizeof(int));
|
int *words = calloc(count, sizeof(int));
|
||||||
int *bytes = calloc(count, sizeof(int));
|
int *bytes = calloc(count, sizeof(int));
|
||||||
|
@ -95,7 +105,31 @@ void wc(char **list, int count) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
int opt;
|
||||||
char *list[2];
|
char *list[2];
|
||||||
|
// short_options = "c, m, l, L, w"
|
||||||
|
// long_options = "bytes,chars,lines,max-line-length,total,help,version,words"
|
||||||
|
while ((opt = getopt(argc, argv, "cmlLw")) != -1) {
|
||||||
|
switch (opt) {
|
||||||
|
case 'c':
|
||||||
|
mode_bytes = true;
|
||||||
|
break;
|
||||||
|
case 'm':
|
||||||
|
mode_chars = true;
|
||||||
|
break;
|
||||||
|
case 'l':
|
||||||
|
mode_lines = true;
|
||||||
|
break;
|
||||||
|
case 'L':
|
||||||
|
mode_mllen = true;
|
||||||
|
break;
|
||||||
|
case 'w':
|
||||||
|
mode_words = true;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
/* Default mode */
|
/* Default mode */
|
||||||
if ((mode_bytes || mode_chars || mode_lines || mode_mllen || mode_words) == 0) {
|
if ((mode_bytes || mode_chars || mode_lines || mode_mllen || mode_words) == 0) {
|
||||||
mode_bytes = true;
|
mode_bytes = true;
|
||||||
|
@ -106,7 +140,8 @@ int main(int argc, char **argv) {
|
||||||
list[1] = "/dev/stdin";
|
list[1] = "/dev/stdin";
|
||||||
wc(list, 2);
|
wc(list, 2);
|
||||||
} else {
|
} else {
|
||||||
wc(argv, argc);
|
optind--;
|
||||||
|
wc(argv + optind, argc - optind);
|
||||||
}
|
}
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue