comparison lib-src/fakemail.c @ 398:74fd4e045ea6 r21-2-29

Import from CVS: tag r21-2-29
author cvs
date Mon, 13 Aug 2007 11:13:30 +0200
parents 6719134a07c2
children de805c49cfc1
comparison
equal deleted inserted replaced
397:f4aeb21a5bad 398:74fd4e045ea6
19 Boston, MA 02111-1307, USA. */ 19 Boston, MA 02111-1307, USA. */
20 20
21 /* Synched up with: FSF 19.28. */ 21 /* Synched up with: FSF 19.28. */
22 22
23 #define NO_SHORTNAMES 23 #define NO_SHORTNAMES
24 #include <../src/config.h> 24 #include <config.h>
25 25
26 #if defined (BSD) && !defined (BSD4_1) && !defined (USE_FAKEMAIL) 26 #if defined (BSD) && !defined (BSD4_1) && !defined (USE_FAKEMAIL)
27 /* This program is not used in BSD, so just avoid loader complaints. */ 27 /* This program is not used in BSD, so just avoid loader complaints. */
28 int 28 int
29 main (int argc, char *argv[]) 29 main (int argc, char *argv[])
114 * and works regardless of the length of the line. 114 * and works regardless of the length of the line.
115 */ 115 */
116 116
117 struct linebuffer 117 struct linebuffer
118 { 118 {
119 long size; 119 size_t size;
120 char *buffer; 120 char *buffer;
121 }; 121 };
122 122
123 struct linebuffer lb; 123 struct linebuffer lb;
124 124
142 142
143 #ifndef MAIL_PROGRAM_NAME 143 #ifndef MAIL_PROGRAM_NAME
144 #define MAIL_PROGRAM_NAME "/bin/mail" 144 #define MAIL_PROGRAM_NAME "/bin/mail"
145 #endif 145 #endif
146 146
147 static CONST char *my_name; 147 static const char *my_name;
148 static char *the_date; 148 static char *the_date;
149 static char *the_user; 149 static char *the_user;
150 static line_list file_preface; 150 static line_list file_preface;
151 static stream_list the_streams; 151 static stream_list the_streams;
152 static boolean no_problems = true; 152 static boolean no_problems = true;
160 #ifdef CURRENT_USER 160 #ifdef CURRENT_USER
161 extern struct passwd *getpwuid (); 161 extern struct passwd *getpwuid ();
162 extern unsigned short geteuid (); 162 extern unsigned short geteuid ();
163 static struct passwd *my_entry; 163 static struct passwd *my_entry;
164 #define cuserid(s) \ 164 #define cuserid(s) \
165 (my_entry = getpwuid (((int) geteuid ())), \ 165 (my_entry = getpwuid ((int) geteuid ()), \
166 my_entry->pw_name) 166 my_entry->pw_name)
167 #endif 167 #endif
168 168
169 /* Utilities */ 169 /* Utilities */
170 170
171 /* Print error message. `s1' is printf control string, `s2' is arg for it. */ 171 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
172 172
173 static void 173 static void
174 error (CONST char *s1, CONST char *s2) 174 error (const char *s1, const char *s2)
175 { 175 {
176 printf ("%s: ", my_name); 176 printf ("%s: ", my_name);
177 printf (s1, s2); 177 printf (s1, s2);
178 printf ("\n"); 178 printf ("\n");
179 no_problems = false; 179 no_problems = false;
180 } 180 }
181 181
182 /* Print error message and exit. */ 182 /* Print error message and exit. */
183 183
184 static void 184 static void
185 fatal (CONST char *s1, CONST char *s2) 185 fatal (const char *s1, const char *s2)
186 { 186 {
187 error (s1, s2); 187 error (s1, s2);
188 exit (1); 188 exit (1);
189 } 189 }
190 190
191 /* Like malloc but get fatal error if memory is exhausted. */ 191 /* Like malloc but get fatal error if memory is exhausted. */
192 192
193 static char * 193 static void *
194 xmalloc (size_t size) 194 xmalloc (size_t size)
195 { 195 {
196 char *result = malloc (((unsigned) size)); 196 void *result = malloc (size);
197 if (result == ((char *) NULL)) 197 if (result == NULL)
198 fatal ("virtual memory exhausted", (char *) 0); 198 fatal ("virtual memory exhausted", (char *) 0);
199 return result; 199 return result;
200 } 200 }
201 201
202 static char * 202 static void *
203 xrealloc (char *ptr, size_t size) 203 xrealloc (void *ptr, size_t size)
204 { 204 {
205 char *result = realloc (ptr, ((unsigned) size)); 205 void *result = realloc (ptr, size);
206 if (result == ((char *) NULL)) 206 if (result == NULL)
207 fatal ("virtual memory exhausted", (char *) 0); 207 fatal ("virtual memory exhausted", (char *) 0);
208 return result; 208 return result;
209 } 209 }
210 210
211 /* Initialize a linebuffer for use */ 211 /* Initialize a linebuffer for use */
212 212
213 static void 213 static void
214 init_linebuffer (struct linebuffer *linebuffer) 214 init_linebuffer (struct linebuffer *linebuffer)
215 { 215 {
216 linebuffer->size = INITIAL_LINE_SIZE; 216 linebuffer->size = INITIAL_LINE_SIZE;
217 linebuffer->buffer = ((char *) xmalloc (INITIAL_LINE_SIZE)); 217 linebuffer->buffer = (char *) xmalloc (INITIAL_LINE_SIZE);
218 } 218 }
219 219
220 /* Read a line of text from `stream' into `linebuffer'. 220 /* Read a line of text from `stream' into `linebuffer'.
221 * Return the length of the line. 221 * Return the length of the line.
222 */ 222 */
232 { 232 {
233 int c = getc (stream); 233 int c = getc (stream);
234 if (p == end) 234 if (p == end)
235 { 235 {
236 linebuffer->size *= 2; 236 linebuffer->size *= 2;
237 buffer = ((char *) xrealloc ((char *) buffer, 237 buffer = (char *) xrealloc (buffer, linebuffer->size);
238 (size_t) (linebuffer->size)));
239 p = buffer + (p - linebuffer->buffer); 238 p = buffer + (p - linebuffer->buffer);
240 end = buffer + linebuffer->size; 239 end = buffer + linebuffer->size;
241 linebuffer->buffer = buffer; 240 linebuffer->buffer = buffer;
242 } 241 }
243 if (c < 0 || c == '\n') 242 if (c < 0 || c == '\n')
277 276
278 static boolean 277 static boolean
279 has_keyword (char *field) 278 has_keyword (char *field)
280 { 279 {
281 char *ignored; 280 char *ignored;
282 return (get_keyword (field, &ignored) != ((char *) NULL)); 281 return (get_keyword (field, &ignored) != (char *) NULL);
283 } 282 }
284 283
285 static char * 284 static char *
286 add_field (line_list the_list, register char *field, register char *where) 285 add_field (line_list the_list, register char *field, register char *where)
287 { 286 {
321 time (&idiotic_interface); 320 time (&idiotic_interface);
322 the_date = ctime (&idiotic_interface); 321 the_date = ctime (&idiotic_interface);
323 /* the_date has an unwanted newline at the end */ 322 /* the_date has an unwanted newline at the end */
324 date_length = strlen (the_date) - 1; 323 date_length = strlen (the_date) - 1;
325 the_date[date_length] = '\0'; 324 the_date[date_length] = '\0';
325 #ifdef WINDOWSNT
326 temp = "(null)";
327 #else
326 temp = cuserid ((char *) NULL); 328 temp = cuserid ((char *) NULL);
329 #endif
327 user_length = strlen (temp); 330 user_length = strlen (temp);
328 the_user = alloc_string ((size_t) (user_length + 1)); 331 the_user = alloc_string ((size_t) (user_length + 1));
329 strcpy (the_user, temp); 332 strcpy (the_user, temp);
330 the_string = alloc_string ((size_t) (3 + prefix_length + 333 the_string = alloc_string ((size_t) (3 + prefix_length +
331 user_length + 334 user_length +
394 { 397 {
395 FILE *the_stream = fopen (name, "a"); 398 FILE *the_stream = fopen (name, "a");
396 if (the_stream != ((FILE *) NULL)) 399 if (the_stream != ((FILE *) NULL))
397 { 400 {
398 add_a_stream (the_stream, my_fclose); 401 add_a_stream (the_stream, my_fclose);
399 if (the_user == ((char *) NULL)) 402 if (the_user == (char *) NULL)
400 file_preface = make_file_preface (); 403 file_preface = make_file_preface ();
401 write_line_list (file_preface, the_stream); 404 write_line_list (file_preface, the_stream);
402 return true; 405 return true;
403 } 406 }
404 return false; 407 return false;
414 fputs (s, rem->handle); 417 fputs (s, rem->handle);
415 return; 418 return;
416 } 419 }
417 420
418 static void 421 static void
419 put_line (CONST char *string) 422 put_line (const char *string)
420 { 423 {
421 register stream_list rem; 424 register stream_list rem;
422 for (rem = the_streams; 425 for (rem = the_streams;
423 rem != ((stream_list) NULL); 426 rem != ((stream_list) NULL);
424 rem = rem->rest_streams) 427 rem = rem->rest_streams)
425 { 428 {
426 CONST char *s = string; 429 const char *s = string;
427 int column = 0; 430 int column = 0;
428 431
429 /* Divide STRING into lines. */ 432 /* Divide STRING into lines. */
430 while (*s != 0) 433 while (*s != 0)
431 { 434 {
432 CONST char *breakpos; 435 const char *breakpos;
433 436
434 /* Find the last char that fits. */ 437 /* Find the last char that fits. */
435 for (breakpos = s; *breakpos && column < 78; ++breakpos) 438 for (breakpos = s; *breakpos && column < 78; ++breakpos)
436 { 439 {
437 if (*breakpos == '\t') 440 if (*breakpos == '\t')
632 char *mail_program_name; 635 char *mail_program_name;
633 char buf[BUFLEN + 1]; 636 char buf[BUFLEN + 1];
634 register int size; 637 register int size;
635 FILE *the_pipe; 638 FILE *the_pipe;
636 639
637 #if !(__STDC__ || defined(STDC_HEADERS))
638 extern char *getenv ();
639 #endif
640
641 mail_program_name = getenv ("FAKEMAILER"); 640 mail_program_name = getenv ("FAKEMAILER");
642 if (!(mail_program_name && *mail_program_name)) 641 if (!(mail_program_name && *mail_program_name))
643 mail_program_name = (char *) MAIL_PROGRAM_NAME; 642 mail_program_name = (char *) MAIL_PROGRAM_NAME;
644 name_length = strlen (mail_program_name); 643 name_length = strlen (mail_program_name);
645 644
646 my_name = MY_NAME; 645 my_name = MY_NAME;
647 the_streams = ((stream_list) NULL); 646 the_streams = ((stream_list) NULL);
648 the_date = ((char *) NULL); 647 the_date = (char *) NULL;
649 the_user = ((char *) NULL); 648 the_user = (char *) NULL;
650 649
651 the_header = read_header (); 650 the_header = read_header ();
652 command_line = alloc_string ((size_t) (name_length + 651 command_line = alloc_string ((size_t) (name_length +
653 args_size (the_header))); 652 args_size (the_header)));
654 strcpy (command_line, mail_program_name); 653 strcpy (command_line, mail_program_name);
655 parse_header (the_header, &command_line[name_length]); 654 parse_header (the_header, &command_line[name_length]);
656 655
657 the_pipe = popen (command_line, "w"); 656 the_pipe = popen (command_line, "w");
658 if (the_pipe == ((FILE *) NULL)) 657 if (the_pipe == ((FILE *) NULL))
659 fatal ("cannot open pipe to real mailer", (char *) 0); 658 fatal ("cannot open pipe to real mailer", (char *) NULL);
660 659
661 add_a_stream (the_pipe, pclose); 660 add_a_stream (the_pipe, pclose);
662 661
663 write_header (the_header); 662 write_header (the_header);
664 663