add --slient/--quiet options, work properly if no args
Showing
1 changed file
with
58 additions
and
24 deletions
... | @@ -3,23 +3,48 @@ | ... | @@ -3,23 +3,48 @@ |
3 | #include <stdio.h> | 3 | #include <stdio.h> |
4 | #include <argp.h> | 4 | #include <argp.h> |
5 | 5 | ||
6 | static int messages_count (char *); | ||
7 | |||
6 | const char *argp_program_version = "messages (" PACKAGE ") " VERSION; | 8 | const char *argp_program_version = "messages (" PACKAGE ") " VERSION; |
7 | const char *argp_program_bug_address = "<bug-mailutils@gnu.org>"; | 9 | const char *argp_program_bug_address = "<bug-mailutils@gnu.org>"; |
8 | static char doc[] = "GNU messages -- count the number of messages in a mailbox"; | 10 | static char doc[] = "GNU messages -- count the number of messages in a mailbox"; |
9 | static char args_doc[] = "[mailbox...]"; | 11 | static char args_doc[] = "[mailbox...]"; |
10 | 12 | ||
11 | static struct argp_option options[] = { | 13 | static struct argp_option options[] = { |
14 | {"quiet", 'q', 0, 0, "Only display number of messages"}, | ||
15 | {"silent", 's', 0, 0, "Same as -q"}, | ||
12 | { 0 } | 16 | { 0 } |
13 | }; | 17 | }; |
14 | 18 | ||
15 | struct arguments | 19 | struct arguments |
16 | { | 20 | { |
17 | char **args; | 21 | int argc; |
22 | char **argv; | ||
18 | }; | 23 | }; |
19 | 24 | ||
25 | /* are we loud or quiet? */ | ||
26 | static int silent = 0; | ||
27 | |||
20 | static error_t | 28 | static error_t |
21 | parse_opt (int key, char *arg, struct argp_state *state) | 29 | parse_opt (int key, char *arg, struct argp_state *state) |
22 | { | 30 | { |
31 | struct arguments *args = state->input; | ||
32 | switch (key) | ||
33 | { | ||
34 | case 'q': | ||
35 | case 's': | ||
36 | silent = 1; | ||
37 | break; | ||
38 | case ARGP_KEY_ARG: | ||
39 | args->argv = realloc (args->argv, | ||
40 | sizeof (char *) * (state->arg_num + 2)); | ||
41 | args->argv[state->arg_num] = arg; | ||
42 | args->argv[state->arg_num + 1] = NULL; | ||
43 | args->argc++; | ||
44 | break; | ||
45 | default: | ||
46 | return ARGP_ERR_UNKNOWN; | ||
47 | } | ||
23 | return 0; | 48 | return 0; |
24 | } | 49 | } |
25 | 50 | ||
... | @@ -30,49 +55,58 @@ main (int argc, char **argv) | ... | @@ -30,49 +55,58 @@ main (int argc, char **argv) |
30 | { | 55 | { |
31 | int i = 1; | 56 | int i = 1; |
32 | list_t bookie; | 57 | list_t bookie; |
33 | mailbox_t mbox; | ||
34 | int count; | ||
35 | int err = 0; | 58 | int err = 0; |
36 | struct arguments args; | 59 | struct arguments args = {0, NULL}; |
37 | args.args = NULL; | ||
38 | 60 | ||
39 | argp_parse (&argp, argc, argv, 0, 0, &args); | 61 | argp_parse (&argp, argc, argv, 0, 0, &args); |
40 | 62 | ||
41 | registrar_get_list (&bookie); | 63 | registrar_get_list (&bookie); |
42 | list_append (bookie, path_record); | 64 | list_append (bookie, path_record); |
43 | 65 | ||
44 | /* FIXME: if argc < 2, check on $MAIL and exit */ | 66 | if (args.argc < 1 && messages_count (getenv("MAIL")) < 0) |
45 | 67 | err = 1; | |
46 | for (i=1; i < argc; i++) | 68 | else if (args.argc >= 1) |
47 | { | ||
48 | if (mailbox_create_default (&mbox, argv[i]) != 0) | ||
49 | { | 69 | { |
50 | fprintf (stderr, "Couldn't create mailbox %s.\n", argv[i]); | 70 | for (i=0; i < args.argc; i++) |
71 | if (messages_count (args.argv[i]) < 0) | ||
51 | err = 1; | 72 | err = 1; |
52 | continue; | 73 | } |
74 | |||
75 | return err; | ||
76 | } | ||
77 | |||
78 | static int | ||
79 | messages_count (char *box) | ||
80 | { | ||
81 | mailbox_t mbox; | ||
82 | int count; | ||
83 | |||
84 | if (mailbox_create_default (&mbox, box) != 0) | ||
85 | { | ||
86 | fprintf (stderr, "Couldn't create mailbox %s.\n", box); | ||
87 | return -1; | ||
53 | } | 88 | } |
54 | if (mailbox_open (mbox, MU_STREAM_READ) != 0) | 89 | if (mailbox_open (mbox, MU_STREAM_READ) != 0) |
55 | { | 90 | { |
56 | fprintf (stderr, "Couldn't open mailbox %s.\n", argv[i]); | 91 | fprintf (stderr, "Couldn't open mailbox %s.\n", box); |
57 | err = 1; | 92 | return -1; |
58 | continue; | ||
59 | } | 93 | } |
60 | if (mailbox_messages_count (mbox, &count) != 0) | 94 | if (mailbox_messages_count (mbox, &count) != 0) |
61 | { | 95 | { |
62 | fprintf (stderr, "Couldn't count messages in %s.\n", argv[i]); | 96 | fprintf (stderr, "Couldn't count messages in %s.\n", box); |
63 | err = 1; | 97 | return -1; |
64 | continue; | ||
65 | } | 98 | } |
66 | 99 | ||
67 | printf ("Number of messages in %s: %d\n", argv[i], count); | 100 | if (silent) |
101 | printf ("%d\n", count); | ||
102 | else | ||
103 | printf ("Number of messages in %s: %d\n", box, count); | ||
68 | 104 | ||
69 | if (mailbox_close (mbox) != 0) | 105 | if (mailbox_close (mbox) != 0) |
70 | { | 106 | { |
71 | fprintf (stderr, "Couldn't close %s.\n", argv[i]); | 107 | fprintf (stderr, "Couldn't close %s.\n", box); |
72 | err = 1; | 108 | return -1; |
73 | continue; | ||
74 | } | 109 | } |
75 | mailbox_destroy (&mbox); | 110 | mailbox_destroy (&mbox); |
76 | } | 111 | return count; |
77 | return 0; | ||
78 | } | 112 | } | ... | ... |
-
Please register or sign in to post a comment