comparison f-demime/main.c @ 0:7e0d08176f32

f-demime starting code
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 06 May 2023 06:14:03 +0000
parents
children 612c4d0df768
comparison
equal deleted inserted replaced
-1:000000000000 0:7e0d08176f32
1 /*
2 * This module contains the main() function for f-demime. The following
3 * functions are called to pass input to further processing:
4 *
5 * begin_new_message(): called after processing a "From " line
6 * message_input_line(): called for all message lines after the "From " line
7 * finish_msg_body(): called when hitting EOF or a new "From " line
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <strings.h>
14 #include <time.h>
15 #include "defs.h"
16
17 char *att_filename_buf, *att_filename_base, *att_filename_tail;
18 unsigned input_lineno;
19
20 static void
21 get_attachment_dir()
22 {
23 static char varname[] = "FDEMIME_ATT_DIR";
24 char *env, *cp;
25 unsigned dir_name_len;
26 time_t curtime;
27 struct tm *tm;
28
29 env = getenv(varname);
30 if (!env) {
31 fprintf(stderr,
32 "error: required environment variable %s is not set\n",
33 varname);
34 exit(1);
35 }
36 if (!env[0]) {
37 fprintf(stderr,
38 "error: empty string value for %s is not allowed\n",
39 varname);
40 exit(1);
41 }
42 dir_name_len = strlen(env);
43 att_filename_buf = malloc(dir_name_len + 31); /* Y10K extra margin */
44 if (!att_filename_buf) {
45 fprintf(stderr,
46 "error: unable to malloc buffer for attachment filenames\n");
47 exit(1);
48 }
49 strcpy(att_filename_buf, env);
50 cp = att_filename_buf + dir_name_len;
51 *cp++ = '/';
52 att_filename_base = cp;
53 time(&curtime);
54 tm = gmtime(&curtime);
55 sprintf(cp, "%u%02u%02ua", tm->tm_year + 1900, tm->tm_mon + 1,
56 tm->tm_mday);
57 att_filename_tail = index(cp, '\0');
58 }
59
60 main(argc, argv)
61 char **argv;
62 {
63 char linebuf[LINE_BUF_SIZE], *cp;
64
65 if (argc > 3) {
66 fprintf(stderr, "usage: %s [infile [outfile]]\n", argv[0]);
67 exit(1);
68 }
69 get_attachment_dir();
70 if (argc > 1 && !freopen(argv[1], "r", stdin)) {
71 perror(argv[1]);
72 exit(1);
73 }
74 if (argc > 2 && !freopen(argv[2], "w", stdout)) {
75 perror(argv[2]);
76 exit(1);
77 }
78 for (input_lineno = 1; fgets(linebuf, sizeof linebuf, stdin);
79 input_lineno++) {
80 cp = index(linebuf, '\n');
81 if (cp) {
82 *cp = '\0';
83 if (cp > linebuf && cp[-1] == '\r')
84 *--cp = '\0';
85 } else {
86 fprintf(stderr,
87 "f-demime warning: input line %u too long or unterminated\n",
88 input_lineno);
89 }
90 if (!strncmp(linebuf, "From ", 5)) {
91 finish_msg_body();
92 puts(linebuf);
93 begin_new_message();
94 continue;
95 }
96 message_input_line(linebuf);
97 }
98 finish_msg_body();
99 exit(0);
100 }