Commit f232e0e9 f232e0e9d088ebea4f1c6e4577b48af3f720a5d1 by Jakob Kaivo

add --slient/--quiet options, work properly if no args

1 parent d371d25c
......@@ -3,23 +3,48 @@
#include <stdio.h>
#include <argp.h>
static int messages_count (char *);
const char *argp_program_version = "messages (" PACKAGE ") " VERSION;
const char *argp_program_bug_address = "<bug-mailutils@gnu.org>";
static char doc[] = "GNU messages -- count the number of messages in a mailbox";
static char args_doc[] = "[mailbox...]";
static struct argp_option options[] = {
{"quiet", 'q', 0, 0, "Only display number of messages"},
{"silent", 's', 0, 0, "Same as -q"},
{ 0 }
};
struct arguments
{
char **args;
int argc;
char **argv;
};
/* are we loud or quiet? */
static int silent = 0;
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
struct arguments *args = state->input;
switch (key)
{
case 'q':
case 's':
silent = 1;
break;
case ARGP_KEY_ARG:
args->argv = realloc (args->argv,
sizeof (char *) * (state->arg_num + 2));
args->argv[state->arg_num] = arg;
args->argv[state->arg_num + 1] = NULL;
args->argc++;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
......@@ -30,49 +55,58 @@ main (int argc, char **argv)
{
int i = 1;
list_t bookie;
mailbox_t mbox;
int count;
int err = 0;
struct arguments args;
args.args = NULL;
struct arguments args = {0, NULL};
argp_parse (&argp, argc, argv, 0, 0, &args);
registrar_get_list (&bookie);
list_append (bookie, path_record);
/* FIXME: if argc < 2, check on $MAIL and exit */
for (i=1; i < argc; i++)
{
if (mailbox_create_default (&mbox, argv[i]) != 0)
if (args.argc < 1 && messages_count (getenv("MAIL")) < 0)
err = 1;
else if (args.argc >= 1)
{
fprintf (stderr, "Couldn't create mailbox %s.\n", argv[i]);
for (i=0; i < args.argc; i++)
if (messages_count (args.argv[i]) < 0)
err = 1;
continue;
}
return err;
}
static int
messages_count (char *box)
{
mailbox_t mbox;
int count;
if (mailbox_create_default (&mbox, box) != 0)
{
fprintf (stderr, "Couldn't create mailbox %s.\n", box);
return -1;
}
if (mailbox_open (mbox, MU_STREAM_READ) != 0)
{
fprintf (stderr, "Couldn't open mailbox %s.\n", argv[i]);
err = 1;
continue;
fprintf (stderr, "Couldn't open mailbox %s.\n", box);
return -1;
}
if (mailbox_messages_count (mbox, &count) != 0)
{
fprintf (stderr, "Couldn't count messages in %s.\n", argv[i]);
err = 1;
continue;
fprintf (stderr, "Couldn't count messages in %s.\n", box);
return -1;
}
printf ("Number of messages in %s: %d\n", argv[i], count);
if (silent)
printf ("%d\n", count);
else
printf ("Number of messages in %s: %d\n", box, count);
if (mailbox_close (mbox) != 0)
{
fprintf (stderr, "Couldn't close %s.\n", argv[i]);
err = 1;
continue;
fprintf (stderr, "Couldn't close %s.\n", box);
return -1;
}
mailbox_destroy (&mbox);
}
return 0;
return count;
}
......