Blame view

messages/messages.c 4.05 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2 3
   Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008,
   2009, 2010 Free Software Foundation, Inc.
4

5
   GNU Mailutils is free software; you can redistribute it and/or modify
Wojciech Polak authored
6
   it under the terms of the GNU General Public License as published by
7
   the Free Software Foundation; either version 3, or (at your option)
8 9
   any later version.

10
   GNU Mailutils is distributed in the hope that it will be useful,
11 12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Wojciech Polak authored
13
   GNU General Public License for more details.
14

Wojciech Polak authored
15
   You should have received a copy of the GNU General Public License
16
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
17

18 19 20
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
21

22
#include <stdio.h>
23 24 25
#ifdef HAVE_MALLOC_H
# include <malloc.h>
#endif
26

27
#include <mailutils/mailutils.h>
28
#include "mailutils/libargp.h"
29

30
static int messages_count (const char *);
31

32 33
static char doc[] = N_("GNU messages -- count the number of messages in a mailbox");
static char args_doc[] = N_("[mailbox...]");
34 35

static struct argp_option options[] = {
36 37 38
  { NULL,         0, NULL,  0,
    /* TRANSLATORS: 'messages' is a program name. Do not translate it! */
    N_("messages specific switches:"), 0},
Sergey Poznyakoff authored
39 40
  {"quiet",	'q',	NULL,	0,	N_("only display number of messages")},
  {"silent",	's',	NULL,	OPTION_ALIAS, NULL },
41 42 43
  { 0 }
};

Sergey Poznyakoff authored
44
static const char *argp_capa[] = {
45
  "common",
46
  "debug",
47
  "mailbox",
48
  "locking",
Sergey Poznyakoff authored
49 50 51
  NULL
};

52 53
struct arguments
{
54 55
  int argc;
  char **argv;
56 57
};

58 59 60
/* are we loud or quiet? */
static int silent = 0;

61 62 63
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
64 65 66 67 68 69 70
  struct arguments *args = state->input;
  switch (key)
    {
    case 'q':
    case 's':
      silent = 1;
      break;
71
      
72 73 74 75 76 77 78
    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;
79
      
80 81 82
    default:
      return ARGP_ERR_UNKNOWN;
    }
83 84 85
  return 0;
}

86 87 88 89 90
static struct argp argp = {
  options,
  parse_opt,
  args_doc,
  doc,
Sergey Poznyakoff authored
91
  NULL,
92 93
  NULL, NULL
};
94 95 96 97 98 99

int
main (int argc, char **argv)
{
  int i = 1;
  int err = 0;
100
  struct arguments args = {0, NULL};
101

102
  /* Native Language Support */
Sergey Poznyakoff authored
103
  MU_APP_INIT_NLS ();
104

105 106
  /* register the formats.  */
  mu_register_all_mbox_formats ();
107

108 109 110
#ifdef WITH_TLS
  mu_gocs_register ("tls", mu_tls_module_init);
#endif
111
  mu_argp_init (NULL, NULL);
112 113 114
  if (mu_app_init (&argp, argp_capa, NULL, argc, argv, 0, NULL, &args))
    exit (1);

115
  if (args.argc < 1 && messages_count (NULL) < 0)
Wojciech Polak authored
116
    err = 1;
117
  else if (args.argc >= 1)
118
    {
Wojciech Polak authored
119 120 121 122 123
      for (i = 0; i < args.argc; i++)
	{
	  if (messages_count (args.argv[i]) < 0)
	    err = 1;
	}
124
    }
125

126 127
  return err;
}
128

129
static int
130
messages_count (const char *box)
131
{
132 133
  mu_mailbox_t mbox;
  mu_url_t url = NULL;
134
  size_t count;
135
  int status = 0;
136

137
  status =  mu_mailbox_create_default (&mbox, box);
138
  if (status != 0)
139
    {
140
      if (box)
141
	mu_error (_("could not create mailbox `%s': %s"),
142 143
		  box, mu_strerror (status));
      else
144
	mu_error (_("could not create default mailbox: %s"),
145
		  mu_strerror (status));
146
      return -1;
147
    }
148

149
  mu_mailbox_get_url (mbox, &url);
150
  box = mu_url_to_string (url);
151

152
  status =  mu_mailbox_open (mbox, MU_STREAM_READ);
153
  if (status != 0)
154
    {
155
      mu_error (_("could not open mailbox `%s': %s"),
Wojciech Polak authored
156
		box, mu_strerror (status));
157 158
      return -1;
    }
159

160
  status = mu_mailbox_messages_count (mbox, &count);
161
  if (status != 0)
162
    {
163
      mu_error (_("could not count messages in mailbox `%s': %s"),
Wojciech Polak authored
164
		box, mu_strerror (status));
165 166
      return -1;
    }
167

168
  if (silent)
169
    mu_printf ("%lu\n", (unsigned long) count);
170
  else
171 172
    mu_printf (_("Number of messages in %s: %lu\n"), box,
	       (unsigned long) count);
173

174
  status = mu_mailbox_close (mbox);
175
  if (status != 0)
176
    {
177
      mu_error (_("could not close `%s': %s"),
Wojciech Polak authored
178
		box, mu_strerror (status));
179 180
      return -1;
    }
181

182
  mu_mailbox_destroy (&mbox);
183
  return count;
184
}