+/* Return the number of files matching the query, or -1 for an error */
+static int
+count_files (notmuch_query_t *query)
+{
+ notmuch_messages_t *messages;
+ notmuch_message_t *message;
+ notmuch_filenames_t *filenames;
+ notmuch_status_t status;
+ int count = 0;
+
+ status = notmuch_query_search_messages_st (query, &messages);
+ if (print_status_query ("notmuch count", query, status))
+ return -1;
+
+ for (;
+ notmuch_messages_valid (messages);
+ notmuch_messages_move_to_next (messages)) {
+ message = notmuch_messages_get (messages);
+ filenames = notmuch_message_get_filenames (message);
+
+ for (;
+ notmuch_filenames_valid (filenames);
+ notmuch_filenames_move_to_next (filenames))
+ count++;
+
+ notmuch_filenames_destroy (filenames);
+ notmuch_message_destroy (message);
+ }
+
+ notmuch_messages_destroy (messages);
+
+ return count;
+}
+
+/* return 0 on success, -1 on failure */