home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Mail / mailapp-utilities-2.1-MIHS / Source / listmail.m < prev    next >
Encoding:
Text File  |  1998-02-02  |  4.7 KB  |  175 lines

  1. /* -*-C-*-
  2. *******************************************************************************
  3. *
  4. * File:         listmail.m
  5. * RCS:          /usr/local/sources/CVS/mailapp-utilities/listmail.m,v 1.8 1998/02/02 22:23:19 tom Exp
  6. * Description:  List the contents of a Mail.app table_of_contents
  7. * Author:       Carl Edman
  8. * Created:      Fri Mar 12 18:21:23 1993
  9. * Modified:     Sat Nov 30 23:29:31 1996 Tom Hageman <tom@basil.icce.rug.nl>
  10. * Language:     Objective C
  11. * Package:      mailapp-utilities
  12. * Status:       First release.
  13. *
  14. * (C) Copyright 1993, but otherwise this file is perfect freeware.
  15. *
  16. *******************************************************************************
  17. */
  18.  
  19. #import <libc.h>
  20. #import <errno.h>
  21. //#import <stdlib.h>
  22. //#import <stdio.h>
  23. //#import <string.h>
  24. //#import <time.h>
  25. #import <regex.h>
  26. //#import <sys/file.h>
  27. //#import <sys/param.h>
  28. //#import <sys/types.h>
  29. //#import <sys/stat.h>
  30. #import "compat.h"
  31. #import "mailutil.h"
  32. #import "mailtoc.h"
  33. #import "optutil.h"
  34.  
  35. #define USAGE "\
  36. Usage: %s [-nv][-t][-p prefix][-H|-V] mbox...\n"
  37.  
  38. #define HELP "\
  39.  -n           ignore mailbox locks, list regardless\n\
  40.  -v           talkative mode\n\
  41.  -t           show total number of messages\n\
  42.  -p prefix    prepend prefix to mbox, table_of_contents inside .mbox\n\
  43.  -V           show version\n\
  44.  -H           this help\n\
  45. "
  46.  
  47.  
  48. int main(int ac,char *av[])
  49. {
  50.    int c,width;
  51.    int verboseflg=0,nowaitflg=0,countonlyflg=0,lockedflg;
  52.    FILE *tocf;
  53.    char *p;
  54.    struct table_of_contents_header *toch=0;
  55.    struct message_index *mi=0;
  56.    int status=EXIT_SUCCESS;
  57.    char *mbox="mbox",*table_of_contents="table_of_contents";
  58.    POOL_INIT
  59.  
  60.    set_progname(av[0]);
  61.  
  62.    while((c=getopt(ac,av,"nvtp:VH"))!=EOF) switch(c)
  63.    {
  64.     case 'v':
  65.       verboseflg++;
  66.       break;
  67.     case 'n':
  68.       nowaitflg++;
  69.       break;
  70.     case 't':
  71.       countonlyflg++;
  72.       break;
  73.    case 'p':
  74.       mbox=malloc(strlen(optarg)+strlen("mbox")+1);
  75.       sprintf(mbox, "%smbox", optarg);
  76.       table_of_contents=malloc(strlen(optarg)+strlen("table_of_contents")+1);
  77.       sprintf(table_of_contents, "%stable_of_contents", optarg);
  78.       break;
  79.     case 'V':
  80.       status=EXIT_VERSION;
  81.       break;
  82.     case 'H':
  83.       status=EXIT_HELP;
  84.       break;
  85.     case '?':
  86.     default:
  87.       status=EXIT_USAGE;
  88.    }
  89.    if (status==EXIT_SUCCESS && optind>=ac) status=EXIT_USAGE;
  90.    handle_usage_help_version(status, USAGE, HELP);
  91.  
  92.    for(;optind<ac;optind++)
  93.    {
  94.       int priority;
  95.       time_t s,t;
  96.       
  97.       if (verboseflg) fprintf(stderr,"Entering %s...\n",av[optind]);
  98.       if (cd_mbox(av[optind],0)) goto next;
  99.       lockedflg=lock_mbox(nowaitflg);
  100.  
  101.       if ((tocf=fopen(table_of_contents,"rb"))==NULL)
  102.       {
  103.          fprintf(stderr,"%s: %s: opening: %s\n",progname(),av[optind],strerror(errno));
  104.          goto unlock;
  105.       }
  106.  
  107.       toch=get_table_of_contents_header(tocf,0);
  108.       if (!toch)
  109.       {
  110.          fprintf(stderr,"%s: %s: invalid table_of_contents\n",progname(),av[optind]);
  111.          goto unlock;
  112.       }
  113.  
  114.       if (countonlyflg)
  115.       {
  116.          printf("%d\n",toch->num_msgs);
  117.          goto unlock;
  118.       }
  119.       // XXX wishlist: countall -- message count per category.
  120.  
  121.       printf("Table of contents (magic=%d):\n",toch->magic);
  122.       printf("Number of messages:%5d   Modification Time: %.24s",toch->num_msgs,
  123.              ctime(&toch->mbox_time));
  124.       if (toch->mbox_time!=(s=mtime(mbox)))
  125.       {
  126.          printf("(!=%.24s)",ctime(&s));
  127.       }
  128.       
  129.       printf("\nAt (%.2f,%.2f) %.2fx%.2f(%.2f)\n",toch->window.origin.x,
  130.              toch->window.origin.y,toch->window.size.width,toch->window.size.height,
  131.              toch->list);
  132.  
  133.       for(c=toch->num_msgs,width=0;c>0;) { width++; c/=10; }
  134.       for(c=0;c<toch->num_msgs;c++)
  135.       {
  136.      int y, m, d;
  137.  
  138.          if ((mi=get_message_index(tocf))==0)
  139.      {
  140.             fprintf(stderr,"%s: %s: reading mbox entries: %s\n",progname(),av[optind],(errno?strerror(errno):"invalid"));
  141.             goto unlock;
  142.      }
  143.  
  144.      message_get_date(mi, &y, &m, &d);
  145.          printf("%c%c%c%c%*d %d/%02d/%02d %5d/%7d %s:%s",mi->status,mi->msgtype,mi->encrypted,mi->sync,width,c+1,y,m,d,mi->mes_length,mi->mes_offset,message_from(mi),message_subject(mi));
  146.          if ((p=message_reference(mi)) && *p)
  147.      {
  148.             t=message_attachtime(mi);
  149.             s=mtime(p);
  150.         printf(" (%s:%d@%.24s", p, message_attachsize(mi),ctime(&t));
  151.             if (t!=s) printf(" != %.24s",ctime(&s));
  152.             printf(")");
  153.      }
  154.  
  155.          if ((priority=message_priority(mi))>0)
  156.      {
  157.             printf(" (pri=%d)",priority);
  158.      }
  159.          
  160.          printf("\n");
  161.          free(mi); mi=0;
  162.       }
  163.  
  164.     unlock:
  165.       if (tocf!=NULL) fclose(tocf);
  166.       if (!lockedflg) unlock_mbox();
  167.     next:
  168.       if (toch) { free(toch); toch=0; }
  169.       if (mi) { free(mi); mi=0; }
  170.       uncd_mbox();
  171.    }
  172.    POOL_RELEASE
  173.    return EXIT_SUCCESS;
  174. }
  175.