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