- struct node *first, *cur_node, *node, *last, *prev;
- size_t folder_len;
- int count, total = 0;
- char *command = NULL, *sequences_s = NULL;
-
- if (cur == NULL || cur[0] == '\0') {
- cur = "inbox";
- }
-
- check_folders(&first, &last, &cur_node, &folder_len, cur,
- folders, sequences);
-
- if (run_mode == FNEXT || run_mode == FPREV) {
- if (first == NULL) {
- /* No folders at all... */
- return NULL;
- } else if (first->n_next == NULL) {
- /* We have only one node; any desired messages in it? */
- if (first->n_field == NULL) {
- return NULL;
- } else {
+ struct node *first, *cur_node, *node, *last = NULL, *prev;
+ size_t folder_len;
+ int count, total = 0;
+ char *sequences_s = NULL;
+ int argc = 0;
+ char *argv[MAXARGS];
+ char **seqp;
+ char buf[BUFSIZ];
+
+ if (cur == NULL || cur[0] == '\0') {
+ cur = "inbox";
+ }
+
+ check_folders(&first, &last, &cur_node, &folder_len, cur,
+ folders, sequences);
+
+ if (run_mode == FNEXT || run_mode == FPREV) {
+ if (first == NULL) {
+ /* No folders at all... */
+ return NULL;
+ } else if (first->n_next == NULL) {
+ /*
+ ** We have only one node; any desired messages in it?
+ */
+ if (first->n_field == NULL) {
+ return NULL;
+ } else {
+ return first;
+ }
+ } else if (cur_node == NULL) {
+ /*
+ ** Current folder is not listed in .folders,
+ ** return first.
+ */
+ return first;
+ }
+ } else if (run_mode == UNSEEN) {
+ sequences_s = join_sequences(sequences);
+ }
+
+ for (node = first, prev = NULL;
+ node != NULL;
+ prev = node, node = node->n_next) {
+ if (run_mode == FNEXT) {
+ /*
+ ** If we have a previous node and it is the current
+ ** folder, return this node.
+ */
+ if (prev != NULL && strcmp(prev->n_name, cur) == 0) {
+ return node;
+ }
+ } else if (run_mode == FPREV) {
+ if (strcmp(node->n_name, cur) == 0) {
+ /*
+ ** Found current folder in fprev mode;
+ ** if we have a previous node in the list,
+ ** return it; else return the last node.
+ */
+ if (prev == NULL) {
+ return last;
+ }
+ return prev;
+ }
+ } else if (run_mode == UNSEEN) {
+ if (node->n_field == NULL) {
+ continue;
+ }
+
+ printf("\n%d %s messages in %s",
+ count_messages(node->n_field),
+ sequences_s,
+ node->n_name);
+ if (strcmp(node->n_name, cur) == 0) {
+ puts(" (*: current folder)");
+ } else {
+ puts("");
+ }
+ fflush(stdout);
+
+ argc = 0;
+ argv[argc++] = "scan";
+ snprintf(buf, sizeof buf, "+%s", node->n_name);
+ argv[argc++] = buf;
+ for (seqp=sequences; *seqp; seqp++) {
+ argv[argc++] = *seqp;
+ }
+ argv[argc] = (char *)NULL;
+ execprog(*argv, argv);
+ } else {
+ if (node->n_field == NULL) {
+ continue;
+ }
+
+ count = count_messages(node->n_field);
+ total += count;
+
+ printf("%-*s %6d.%c %s\n", (int) folder_len,
+ node->n_name, count,
+ (strcmp(node->n_name, cur) == 0 ? '*' : ' '),
+ node->n_field);
+ }
+ }
+
+ /*
+ ** If we're fnext, we haven't checked the last node yet. If it's the
+ ** current folder, return the first node.
+ */
+ if (run_mode == FNEXT && strcmp(last->n_name, cur) == 0) {