comparison lib-src/movemail.c @ 54:05472e90ae02 r19-16-pre2

Import from CVS: tag r19-16-pre2
author cvs
date Mon, 13 Aug 2007 08:57:55 +0200
parents 56c54cf7c5b6
children 131b0175ea99
comparison
equal deleted inserted replaced
53:875393c1a535 54:05472e90ae02
59 #include <sys/types.h> 59 #include <sys/types.h>
60 #include <sys/stat.h> 60 #include <sys/stat.h>
61 #include <sys/file.h> 61 #include <sys/file.h>
62 #include <stdio.h> 62 #include <stdio.h>
63 #include <errno.h> 63 #include <errno.h>
64 #include <../src/syswait.h> 64 #include "../src/syswait.h"
65 #include "../src/systime.h"
65 #include <stdlib.h> 66 #include <stdlib.h>
66 #include <string.h> 67 #include <string.h>
67 #ifdef MAIL_USE_POP 68 #ifdef MAIL_USE_POP
68 #include "pop.h" 69 #include "pop.h"
69 #endif 70 #endif
71
72 #ifndef HAVE_STRERROR
73 static char * strerror (int errnum);
74 #endif /* HAVE_STRERROR */
70 75
71 #ifdef MSDOS 76 #ifdef MSDOS
72 #undef access 77 #undef access
73 #endif /* MSDOS */ 78 #endif /* MSDOS */
74 79
90 client or something other than Samba, the locking call might work 95 client or something other than Samba, the locking call might work
91 properly - make sure it does before you enable this! */ 96 properly - make sure it does before you enable this! */
92 #define DISABLE_DIRECT_ACCESS 97 #define DISABLE_DIRECT_ACCESS
93 #endif /* WINDOWSNT */ 98 #endif /* WINDOWSNT */
94 99
95 #ifdef USG 100 #if defined (HAVE_UNISTD_H) || defined (USG)
96 #include <fcntl.h>
97 #include <unistd.h> 101 #include <unistd.h>
102 #endif /* unistd.h */
98 #ifndef F_OK 103 #ifndef F_OK
99 #define F_OK 0 104 #define F_OK 0
100 #define X_OK 1 105 #define X_OK 1
101 #define W_OK 2 106 #define W_OK 2
102 #define R_OK 4 107 #define R_OK 4
103 #endif 108 #endif /* No F_OK */
104 #endif /* USG */ 109
105 110 #if defined (HAVE_FCNTL_H) || defined (USG)
106 #ifdef HAVE_UNISTD_H 111 #include <fcntl.h>
107 #include <unistd.h> 112 #endif /* fcntl.h */
108 #endif
109 113
110 #if defined (XENIX) || defined (WINDOWSNT) 114 #if defined (XENIX) || defined (WINDOWSNT)
111 #include <sys/locking.h> 115 #include <sys/locking.h>
112 #endif 116 #endif
113 117
127 #undef open 131 #undef open
128 #undef read 132 #undef read
129 #undef write 133 #undef write
130 #undef close 134 #undef close
131 135
132 #ifndef errno 136 static void fatal (char *, char*);
133 extern int errno; 137 static void error (char *, char *, char *);
134 #endif 138 static void pfatal_with_name (char *);
135 char *strerror (); 139 static void pfatal_and_delete (char *);
136 140 static char *concat (char *, char *, char *);
137 void fatal (char *, char*); 141 static long *xmalloc (unsigned int);
138 void error (); 142 static int popmail (char *, char *, char *);
139 void pfatal_with_name (); 143 #ifdef MAIL_USE_POP
140 void pfatal_and_delete (); 144 static int pop_retr (popserver server, int msgno, int (*action)(), int arg);
141 char *concat (); 145 #endif
142 long *xmalloc (); 146 static int mbx_write (char *, FILE *);
143 int popmail (); 147 static int mbx_delimit_begin (FILE *);
144 int pop_retr (); 148 static int mbx_delimit_end (FILE *);
145 int mbx_write ();
146 int mbx_delimit_begin ();
147 int mbx_delimit_end ();
148 149
149 /* Nonzero means this is name of a lock file to delete on fatal error. */ 150 /* Nonzero means this is name of a lock file to delete on fatal error. */
150 char *delete_lockname; 151 char *delete_lockname;
151 152
152 int 153 int
153 main (argc, argv) 154 main (int argc, char *argv[])
154 int argc;
155 char **argv;
156 { 155 {
157 char *inname, *outname; 156 char *inname, *outname;
158 int indesc, outdesc; 157 int indesc, outdesc;
159 int nread; 158 int nread;
160 WAITTYPE status; 159 int status;
161 160
162 #ifndef MAIL_USE_SYSTEM_LOCK 161 #ifndef MAIL_USE_SYSTEM_LOCK
163 struct stat st; 162 struct stat st;
164 long now; 163 long now;
165 int tem; 164 int tem;
191 pfatal_with_name (outname); 190 pfatal_with_name (outname);
192 191
193 /* Also check that outname's directory is writable to the real uid. */ 192 /* Also check that outname's directory is writable to the real uid. */
194 { 193 {
195 char *buf = (char *) xmalloc (strlen (outname) + 1); 194 char *buf = (char *) xmalloc (strlen (outname) + 1);
196 char *p; 195 char *cp;
197 strcpy (buf, outname); 196 strcpy (buf, outname);
198 p = buf + strlen (buf); 197 cp = buf + strlen (buf);
199 while (p > buf && !IS_DIRECTORY_SEP (p[-1])) 198 while (cp > buf && !IS_DIRECTORY_SEP (cp[-1]))
200 *--p = 0; 199 *--cp = 0;
201 if (p == buf) 200 if (cp == buf)
202 *p++ = '.'; 201 *cp++ = '.';
203 if (access (buf, W_OK) != 0) 202 if (access (buf, W_OK) != 0)
204 pfatal_with_name (buf); 203 pfatal_with_name (buf);
205 free (buf); 204 free (buf);
206 } 205 }
207 206
395 } 394 }
396 395
397 wait (&status); 396 wait (&status);
398 if (!WIFEXITED (status)) 397 if (!WIFEXITED (status))
399 exit (1); 398 exit (1);
400 else if (WRETCODE (status) != 0) 399 else if (WEXITSTATUS (status) != 0)
401 exit (WRETCODE (status)); 400 exit (WEXITSTATUS (status));
402 401
403 #if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK) 402 #if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK)
404 unlink (lockname); 403 unlink (lockname);
405 #endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */ 404 #endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */
406 405
409 return 0; 408 return 0;
410 } 409 }
411 410
412 /* Print error message and exit. */ 411 /* Print error message and exit. */
413 412
414 void 413 static void
415 fatal (s1, s2) 414 fatal (char *s1, char *s2)
416 char *s1, *s2;
417 { 415 {
418 if (delete_lockname) 416 if (delete_lockname)
419 unlink (delete_lockname); 417 unlink (delete_lockname);
420 error (s1, s2); 418 error (s1, s2, NULL);
421 exit (1); 419 exit (1);
422 } 420 }
423 421
424 /* Print error message. `s1' is printf control string, `s2' is arg for it. */ 422 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
425 423
426 void 424 static void
427 error (s1, s2, s3) 425 error (char *s1, char *s2, char *s3)
428 char *s1, *s2, *s3;
429 { 426 {
430 fprintf (stderr, "movemail: "); 427 fprintf (stderr, "movemail: ");
431 fprintf (stderr, s1, s2, s3); 428 fprintf (stderr, s1, s2, s3);
432 fprintf (stderr, "\n"); 429 fprintf (stderr, "\n");
433 } 430 }
434 431
435 void 432 static void
436 pfatal_with_name (name) 433 pfatal_with_name (char *name)
437 char *name;
438 { 434 {
439 char *s = concat ("", strerror (errno), " for %s"); 435 char *s = concat ("", strerror (errno), " for %s");
440 fatal (s, name); 436 fatal (s, name);
441 } 437 }
442 438
443 void 439 static void
444 pfatal_and_delete (name) 440 pfatal_and_delete (char *name)
445 char *name;
446 { 441 {
447 char *s = concat ("", strerror (errno), " for %s"); 442 char *s = concat ("", strerror (errno), " for %s");
448 unlink (name); 443 unlink (name);
449 fatal (s, name); 444 fatal (s, name);
450 } 445 }
451 446
452 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */ 447 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
453 448
454 char * 449 static char *
455 concat (s1, s2, s3) 450 concat (char *s1, char *s2, char *s3)
456 char *s1, *s2, *s3;
457 { 451 {
458 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3); 452 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
459 char *result = (char *) xmalloc (len1 + len2 + len3 + 1); 453 char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
460 454
461 strcpy (result, s1); 455 strcpy (result, s1);
466 return result; 460 return result;
467 } 461 }
468 462
469 /* Like malloc but get fatal error if memory is exhausted. */ 463 /* Like malloc but get fatal error if memory is exhausted. */
470 464
471 long * 465 static long *
472 xmalloc (size) 466 xmalloc (unsigned int size)
473 unsigned size;
474 { 467 {
475 long *result = (long *) malloc (size); 468 long *result = (long *) malloc (size);
476 if (!result) 469 if (!result)
477 fatal ("virtual memory exhausted", 0); 470 fatal ("virtual memory exhausted", 0);
478 return result; 471 return result;
491 #include <winsock.h> 484 #include <winsock.h>
492 #endif 485 #endif
493 #include <stdio.h> 486 #include <stdio.h>
494 #include <pwd.h> 487 #include <pwd.h>
495 488
496 #ifdef USG
497 #include <fcntl.h>
498 /* Cancel substitutions made by config.h for Emacs. */
499 #undef open
500 #undef read
501 #undef write
502 #undef close
503 #endif /* USG */
504
505 #define NOTOK (-1) 489 #define NOTOK (-1)
506 #define OK 0 490 #define OK 0
507 #define DONE 1 491 #define DONE 1
508 492
509 char *progname; 493 char *progname;
511 FILE *sfo; 495 FILE *sfo;
512 char ibuffer[BUFSIZ]; 496 char ibuffer[BUFSIZ];
513 char obuffer[BUFSIZ]; 497 char obuffer[BUFSIZ];
514 char Errmsg[80]; 498 char Errmsg[80];
515 499
516 popmail (user, outfile, password) 500 static int
517 char *user; 501 popmail (char *user, char *outfile, char *password)
518 char *outfile;
519 char *password;
520 { 502 {
521 int nmsgs, nbytes; 503 int nmsgs, nbytes;
522 register int i; 504 register int i;
523 int mbfi; 505 int mbfi;
524 FILE *mbf; 506 FILE *mbf;
528 extern char *strerror (); 510 extern char *strerror ();
529 511
530 server = pop_open (0, user, password, POP_NO_GETPASS); 512 server = pop_open (0, user, password, POP_NO_GETPASS);
531 if (! server) 513 if (! server)
532 { 514 {
533 error (pop_error); 515 error (pop_error, NULL, NULL);
534 return (1); 516 return (1);
535 } 517 }
536 518
537 if (pop_stat (server, &nmsgs, &nbytes)) 519 if (pop_stat (server, &nmsgs, &nbytes))
538 { 520 {
539 error (pop_error); 521 error (pop_error, NULL, NULL);
540 return (1); 522 return (1);
541 } 523 }
542 524
543 if (!nmsgs) 525 if (!nmsgs)
544 { 526 {
556 fchown (mbfi, getuid (), -1); 538 fchown (mbfi, getuid (), -1);
557 539
558 if ((mbf = fdopen (mbfi, "wb")) == NULL) 540 if ((mbf = fdopen (mbfi, "wb")) == NULL)
559 { 541 {
560 pop_close (server); 542 pop_close (server);
561 error ("Error in fdopen: %s", strerror (errno)); 543 error ("Error in fdopen: %s", strerror (errno), NULL);
562 close (mbfi); 544 close (mbfi);
563 unlink (outfile); 545 unlink (outfile);
564 return (1); 546 return (1);
565 } 547 }
566 548
567 for (i = 1; i <= nmsgs; i++) 549 for (i = 1; i <= nmsgs; i++)
568 { 550 {
569 mbx_delimit_begin (mbf); 551 mbx_delimit_begin (mbf);
570 if (pop_retr (server, i, mbx_write, mbf) != OK) 552 if (pop_retr (server, i, mbx_write, mbf) != OK)
571 { 553 {
572 error (Errmsg); 554 error (Errmsg, NULL, NULL);
573 close (mbfi); 555 close (mbfi);
574 return (1); 556 return (1);
575 } 557 }
576 mbx_delimit_end (mbf); 558 mbx_delimit_end (mbf);
577 fflush (mbf); 559 fflush (mbf);
578 if (ferror (mbf)) 560 if (ferror (mbf))
579 { 561 {
580 error ("Error in fflush: %s", strerror (errno)); 562 error ("Error in fflush: %s", strerror (errno), NULL);
581 pop_close (server); 563 pop_close (server);
582 close (mbfi); 564 close (mbfi);
583 return (1); 565 return (1);
584 } 566 }
585 } 567 }
591 * not made in previous versions of movemail. */ 573 * not made in previous versions of movemail. */
592 574
593 #ifdef BSD 575 #ifdef BSD
594 if (fsync (mbfi) < 0) 576 if (fsync (mbfi) < 0)
595 { 577 {
596 error ("Error in fsync: %s", strerror (errno)); 578 error ("Error in fsync: %s", strerror (errno), NULL);
597 return (1); 579 return (1);
598 } 580 }
599 #endif 581 #endif
600 582
601 if (close (mbfi) == -1) 583 if (close (mbfi) == -1)
602 { 584 {
603 error ("Error in close: %s", strerror (errno)); 585 error ("Error in close: %s", strerror (errno), NULL);
604 return (1); 586 return (1);
605 } 587 }
606 588
607 for (i = 1; i <= nmsgs; i++) 589 for (i = 1; i <= nmsgs; i++)
608 { 590 {
609 if (pop_delete (server, i)) 591 if (pop_delete (server, i))
610 { 592 {
611 error (pop_error); 593 error (pop_error, NULL, NULL);
612 pop_close (server); 594 pop_close (server);
613 return (1); 595 return (1);
614 } 596 }
615 } 597 }
616 598
617 if (pop_quit (server)) 599 if (pop_quit (server))
618 { 600 {
619 error (pop_error); 601 error (pop_error, NULL, NULL);
620 return (1); 602 return (1);
621 } 603 }
622 604
623 return (0); 605 return (0);
624 } 606 }
625 607
626 pop_retr (server, msgno, action, arg) 608 static int
627 popserver server; 609 pop_retr (popserver server, int msgno, int (*action)(), int arg)
628 int (*action)(); 610 {
629 {
630 extern char *strerror ();
631 char *line; 611 char *line;
632 int ret; 612 int ret;
633 613
634 if (pop_retrieve_first (server, msgno, &line)) 614 if (pop_retrieve_first (server, msgno, &line))
635 { 615 {
666 && (a[1] == 'r') \ 646 && (a[1] == 'r') \
667 && (a[2] == 'o') \ 647 && (a[2] == 'o') \
668 && (a[3] == 'm') \ 648 && (a[3] == 'm') \
669 && (a[4] == ' ')) 649 && (a[4] == ' '))
670 650
671 int 651 static int
672 mbx_write (line, mbf) 652 mbx_write (char *line, FILE *mbf)
673 char *line;
674 FILE *mbf;
675 { 653 {
676 if (IS_FROM_LINE (line)) 654 if (IS_FROM_LINE (line))
677 { 655 {
678 if (fputc ('>', mbf) == EOF) 656 if (fputc ('>', mbf) == EOF)
679 return (NOTOK); 657 return (NOTOK);
683 if (fputc (0x0a, mbf) == EOF) 661 if (fputc (0x0a, mbf) == EOF)
684 return (NOTOK); 662 return (NOTOK);
685 return (OK); 663 return (OK);
686 } 664 }
687 665
688 int 666 static int
689 mbx_delimit_begin (mbf) 667 mbx_delimit_begin (FILE *mbf)
690 FILE *mbf;
691 { 668 {
692 if (fputs ("\f\n0, unseen,,\n", mbf) == EOF) 669 if (fputs ("\f\n0, unseen,,\n", mbf) == EOF)
693 return (NOTOK); 670 return (NOTOK);
694 return (OK); 671 return (OK);
695 } 672 }
696 673
697 mbx_delimit_end (mbf) 674 static int
698 FILE *mbf; 675 mbx_delimit_end (FILE *mbf)
699 { 676 {
700 if (putc ('\037', mbf) == EOF) 677 if (putc ('\037', mbf) == EOF)
701 return (NOTOK); 678 return (NOTOK);
702 return (OK); 679 return (OK);
703 } 680 }
704 681
705 #endif /* MAIL_USE_POP */ 682 #endif /* MAIL_USE_POP */
706 683
707 #ifndef HAVE_STRERROR 684 #ifndef HAVE_STRERROR
708 char * 685 static char *
709 strerror (errnum) 686 strerror (int errnum)
710 int errnum;
711 { 687 {
712 extern char *sys_errlist[]; 688 extern char *sys_errlist[];
713 extern int sys_nerr; 689 extern int sys_nerr;
714 690
715 if (errnum >= 0 && errnum < sys_nerr) 691 if (errnum >= 0 && errnum < sys_nerr)