(main): Avoid crash if argc is 1.
[bpt/emacs.git] / lib-src / b2m.c
1 /*
2 * b2m - a filter for Babyl -> Unix mail files
3 *
4 * usage: b2m < babyl > mailbox
5 *
6 * I find this useful whenever I have to use a
7 * system which - shock horror! - doesn't run
8 * Gnu emacs. At least now I can read all my
9 * Gnumacs Babyl format mail files!
10 *
11 * it's not much but it's free!
12 *
13 * Ed Wilkinson
14 * E.Wilkinson@massey.ac.nz
15 * Mon Nov 7 15:54:06 PDT 1988
16 */
17
18 /* Serious bug: This program uses `gets', which is intrinsically
19 unreliable--long lines will cause crashes.
20 Someone should fix this program not to use `gets'. */
21 #include <stdio.h>
22 #include <time.h>
23 #include <sys/types.h>
24 #ifdef MSDOS
25 #include <fcntl.h>
26 #endif
27
28 #include <../src/config.h>
29
30 /* BSD's strings.h does not declare the type of strtok. */
31 extern char *strtok ();
32
33 #ifndef TRUE
34 #define TRUE (1)
35 #endif
36 #ifndef FALSE
37 #define FALSE (0)
38 #endif
39
40 int header = FALSE, printing;
41 time_t ltoday;
42 char from[256], labels[256], data[256], *p, *today;
43
44 main (argc, argv)
45 int argc;
46 char **argv;
47 {
48 #ifdef MSDOS
49 _fmode = O_BINARY; /* all of files are treated as binary files */
50 (stdout)->_flag &= ~_IOTEXT;
51 (stdin)->_flag &= ~_IOTEXT;
52 #endif
53 if (argc >= 2 && strcmp (argv[1], "--help") == 0)
54 {
55 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", argv[0]);
56 exit (0);
57 }
58 ltoday = time (0);
59 today = ctime (&ltoday);
60
61 /* BUG! Must not use gets in a reliable program! */
62 if (gets (data))
63 {
64 if (strncmp (data, "BABYL OPTIONS:", 14))
65 {
66 fprintf (stderr, "%s: not a Babyl mailfile!\n", argv[0]);
67 exit (-1);
68 }
69 else
70 printing = FALSE;
71 }
72 else
73 exit (-1);
74 if (printing)
75 puts (data);
76
77 while (gets (data))
78 {
79
80 #if 0
81 /* What was this for? Does somebody have something against blank
82 lines? */
83 if (!strcmp (data, ""))
84 exit (0);
85 #endif
86
87 if (!strcmp (data, "*** EOOH ***") && !printing)
88 {
89 printing = header = TRUE;
90 printf ("From %s %s", argv[0], today);
91 continue;
92 }
93
94 if (!strcmp (data, "\037\f"))
95 {
96 /* save labels */
97 gets (data);
98 p = strtok (data, " ,\r\n\t");
99 strcpy (labels, "X-Babyl-Labels: ");
100
101 while (p = strtok (NULL, " ,\r\n\t"))
102 {
103 strcat (labels, p);
104 strcat (labels, ", ");
105 }
106
107 labels[strlen (labels) - 2] = '\0';
108 printing = header = FALSE;
109 continue;
110 }
111
112 if (!strlen (data) && header)
113 {
114 header = FALSE;
115 if (strcmp (labels, "X-Babyl-Labels"))
116 puts (labels);
117 }
118
119 if (printing)
120 puts (data);
121 }
122 }