comparison f-demime/main.c @ 4:612c4d0df768

f-demime: rework for unboundedly long lines
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 06 May 2023 09:02:12 +0000
parents 7e0d08176f32
children
comparison
equal deleted inserted replaced
3:7a6cd16689f5 4:612c4d0df768
13 #include <strings.h> 13 #include <strings.h>
14 #include <time.h> 14 #include <time.h>
15 #include "defs.h" 15 #include "defs.h"
16 16
17 char *att_filename_buf, *att_filename_base, *att_filename_tail; 17 char *att_filename_buf, *att_filename_base, *att_filename_tail;
18 unsigned input_lineno;
19 18
20 static void 19 static void
21 get_attachment_dir() 20 get_attachment_dir()
22 { 21 {
23 static char varname[] = "FDEMIME_ATT_DIR"; 22 static char varname[] = "FDEMIME_ATT_DIR";
58 } 57 }
59 58
60 main(argc, argv) 59 main(argc, argv)
61 char **argv; 60 char **argv;
62 { 61 {
63 char linebuf[LINE_BUF_SIZE], *cp; 62 char linebuf[LINE_BUF_SIZE];
63 int c, unterm, prev_unterm;
64 unsigned lineno;
64 65
65 if (argc > 3) { 66 if (argc > 3) {
66 fprintf(stderr, "usage: %s [infile [outfile]]\n", argv[0]); 67 fprintf(stderr, "usage: %s [infile [outfile]]\n", argv[0]);
67 exit(1); 68 exit(1);
68 } 69 }
73 } 74 }
74 if (argc > 2 && !freopen(argv[2], "w", stdout)) { 75 if (argc > 2 && !freopen(argv[2], "w", stdout)) {
75 perror(argv[2]); 76 perror(argv[2]);
76 exit(1); 77 exit(1);
77 } 78 }
78 for (input_lineno = 1; fgets(linebuf, sizeof linebuf, stdin); 79 lineno = 0;
79 input_lineno++) { 80 for (prev_unterm = 0; fgets(linebuf, sizeof linebuf, stdin);
80 cp = index(linebuf, '\n'); 81 prev_unterm = unterm) {
81 if (cp) { 82 if (!prev_unterm)
82 *cp = '\0'; 83 lineno++;
83 if (cp > linebuf && cp[-1] == '\r') 84 if (index(linebuf, '\n'))
84 *--cp = '\0'; 85 unterm = 0;
85 } else { 86 else
86 fprintf(stderr, 87 unterm = 1;
87 "f-demime warning: input line %u too long or unterminated\n", 88 if (!prev_unterm && !strncmp(linebuf, "From ", 5)) {
88 input_lineno);
89 }
90 if (!strncmp(linebuf, "From ", 5)) {
91 finish_msg_body(); 89 finish_msg_body();
92 puts(linebuf); 90 fputs(linebuf, stdout);
91 if (unterm) {
92 fprintf(stderr,
93 "f-demime warning: From line %u too long or unterminated\n",
94 lineno);
95 for (;;) {
96 c = getchar();
97 if (c == EOF || c == '\n')
98 break;
99 }
100 putchar('\n');
101 unterm = 0;
102 }
93 begin_new_message(); 103 begin_new_message();
94 continue; 104 continue;
95 } 105 }
96 message_input_line(linebuf); 106 message_input_line(linebuf, unterm, prev_unterm);
97 } 107 }
98 finish_msg_body(); 108 finish_msg_body();
99 exit(0); 109 exit(0);
100 } 110 }