]> code.delx.au - gnu-emacs/blob - lib-src/movemail.c
(find-compressed-version): Don't set `error' here.
[gnu-emacs] / lib-src / movemail.c
1 /* movemail foo bar -- move file foo to file bar,
2 locking file foo the way /bin/mail respects.
3 Copyright (C) 1986, 1992, 1993, 1994, 1996 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* Important notice: defining MAIL_USE_FLOCK or MAIL_USE_LOCKF *will
23 cause loss of mail* if you do it on a system that does not normally
24 use flock as its way of interlocking access to inbox files. The
25 setting of MAIL_USE_FLOCK and MAIL_USE_LOCKF *must agree* with the
26 system's own conventions. It is not a choice that is up to you.
27
28 So, if your system uses lock files rather than flock, then the only way
29 you can get proper operation is to enable movemail to write lockfiles there.
30 This means you must either give that directory access modes
31 that permit everyone to write lockfiles in it, or you must make movemail
32 a setuid or setgid program. */
33
34 /*
35 * Modified January, 1986 by Michael R. Gretzinger (Project Athena)
36 *
37 * Added POP (Post Office Protocol) service. When compiled -DMAIL_USE_POP
38 * movemail will accept input filename arguments of the form
39 * "po:username". This will cause movemail to open a connection to
40 * a pop server running on $MAILHOST (environment variable). Movemail
41 * must be setuid to root in order to work with POP.
42 *
43 * New module: popmail.c
44 * Modified routines:
45 * main - added code within #ifdef MAIL_USE_POP; added setuid (getuid ())
46 * after POP code.
47 * New routines in movemail.c:
48 * get_errmsg - return pointer to system error message
49 *
50 * Modified August, 1993 by Jonathan Kamens (OpenVision Technologies)
51 *
52 * Move all of the POP code into a separate file, "pop.c".
53 * Use strerror instead of get_errmsg.
54 *
55 */
56
57 #define NO_SHORTNAMES /* Tell config not to load remap.h */
58 #include <../src/config.h>
59 #include <sys/types.h>
60 #include <sys/stat.h>
61 #include <sys/file.h>
62 #include <stdio.h>
63 #include <errno.h>
64 #include <../src/syswait.h>
65 #include <getopt.h>
66 #ifdef MAIL_USE_POP
67 #include "pop.h"
68 #endif
69
70 #ifdef MSDOS
71 #undef access
72 #endif /* MSDOS */
73
74 #ifndef DIRECTORY_SEP
75 #define DIRECTORY_SEP '/'
76 #endif
77 #ifndef IS_DIRECTORY_SEP
78 #define IS_DIRECTORY_SEP(_c_) ((_c_) == DIRECTORY_SEP)
79 #endif
80
81 #ifdef WINDOWSNT
82 #include "ntlib.h"
83 #undef access
84 #undef unlink
85 #define fork() 0
86 #define sys_wait(var) (*(var) = 0)
87 /* Unfortunately, Samba doesn't seem to properly lock Unix files even
88 though the locking call succeeds (and indeed blocks local access from
89 other NT programs). If you have direct file access using an NFS
90 client or something other than Samba, the locking call might work
91 properly - make sure it does before you enable this!
92
93 [18-Feb-97 andrewi] I now believe my comment above to be incorrect,
94 since it was based on a misunderstanding of how locking calls are
95 implemented and used on Unix. */
96 //#define DISABLE_DIRECT_ACCESS
97
98 /* Ensure all file i/o is in binary mode. */
99 #include <fcntl.h>
100 int _fmode = _O_BINARY;
101 #endif /* WINDOWSNT */
102
103 /* Cancel substitutions made by config.h for Emacs. */
104 #undef open
105 #undef read
106 #undef write
107 #undef close
108
109 #ifdef USG
110 #include <fcntl.h>
111 #include <unistd.h>
112 #ifndef F_OK
113 #define F_OK 0
114 #define X_OK 1
115 #define W_OK 2
116 #define R_OK 4
117 #endif
118 #endif /* USG */
119
120 #ifdef HAVE_UNISTD_H
121 #include <unistd.h>
122 #endif
123
124 #ifdef STDC_HEADERS
125 #include <stdlib.h>
126 #endif
127
128 #if defined (XENIX) || defined (WINDOWSNT)
129 #include <sys/locking.h>
130 #endif
131
132 #ifdef MAIL_USE_LOCKF
133 #define MAIL_USE_SYSTEM_LOCK
134 #endif
135
136 #ifdef MAIL_USE_FLOCK
137 #define MAIL_USE_SYSTEM_LOCK
138 #endif
139
140 #ifdef MAIL_USE_MMDF
141 extern int lk_open (), lk_close ();
142 #endif
143
144 #if !defined (MAIL_USE_SYSTEM_LOCK) && !defined (MAIL_USE_MMDF) && \
145 defined (HAVE_LIBMAIL) && defined (HAVE_MAILLOCK_H)
146 #include <maillock.h>
147 /* We can't use maillock unless we know what directory system mail
148 files appear in. */
149 #ifdef MAILDIR
150 #define MAIL_USE_MAILLOCK
151 static char *mail_spool_name ();
152 #endif
153 #endif
154
155 #ifndef errno
156 extern int errno;
157 #endif
158 char *strerror ();
159 extern char *rindex ();
160
161 void fatal ();
162 void error ();
163 void pfatal_with_name ();
164 void pfatal_and_delete ();
165 char *concat ();
166 long *xmalloc ();
167 int popmail ();
168 int pop_retr ();
169 int mbx_write ();
170 int mbx_delimit_begin ();
171 int mbx_delimit_end ();
172
173 /* Nonzero means this is name of a lock file to delete on fatal error. */
174 char *delete_lockname;
175
176 int
177 main (argc, argv)
178 int argc;
179 char **argv;
180 {
181 char *inname, *outname;
182 int indesc, outdesc;
183 int nread;
184 WAITTYPE status;
185 int c, preserve_mail = 0;
186
187 #ifndef MAIL_USE_SYSTEM_LOCK
188 struct stat st;
189 long now;
190 int tem;
191 char *lockname, *p;
192 char *tempname;
193 int desc;
194 #endif /* not MAIL_USE_SYSTEM_LOCK */
195
196 #ifdef MAIL_USE_MAILLOCK
197 char *spool_name;
198 #endif
199
200 #ifdef MAIL_USE_POP
201 int pop_flags = POP_NO_GETPASS | POP_NO_GSSAPI;
202 # define ARGSTR "gkp"
203 #else /* ! MAIL_USE_POP */
204 # define ARGSTR "p"
205 #endif /* MAIL_USE_POP */
206
207 delete_lockname = 0;
208
209 /*
210 'g' enables Kerberos and disables GSS-API.
211 'k' enables GSS-API and disables Kerberos.
212
213 By default, Kerberos is enabled (if it is compiled in) and
214 GSS-API is disabled, i.e., "-k" is the default. However, I'm
215 putting the flag in anyway, in case we decide to add new
216 authentication methods or change the default later.
217 */
218
219 while ((c = getopt (argc, argv, ARGSTR)) != EOF)
220 {
221 switch (c) {
222 #ifdef MAIL_USE_POP
223 case 'g':
224 pop_flags |= POP_NO_KERBEROS;
225 pop_flags &= ~POP_NO_GSSAPI;
226 break;
227 case 'k':
228 pop_flags |= POP_NO_GSSAPI;
229 pop_flags &= ~POP_NO_KERBEROS;
230 break;
231 #endif
232 case 'p':
233 preserve_mail++;
234 break;
235 default:
236 goto usage;
237 }
238 }
239
240 if (
241 #ifdef MAIL_USE_POP
242 (argc - optind < 2) || (argc - optind > 3)
243 #else
244 (argc - optind != 2)
245 #endif
246 )
247 {
248 usage:
249 fprintf (stderr, "Usage: movemail %s[-p] inbox destfile%s\n",
250 #ifdef MAIL_USE_POP
251 "[-g|-k] ", " [POP-password]"
252 #else
253 "", ""
254 #endif
255 );
256 exit (1);
257 }
258
259 inname = argv[optind];
260 outname = argv[optind+1];
261
262 #ifdef MAIL_USE_MMDF
263 mmdf_init (argv[0]);
264 #endif
265
266 if (*outname == 0)
267 fatal ("Destination file name is empty", 0);
268
269 /* Check access to output file. */
270 if (access (outname, F_OK) == 0 && access (outname, W_OK) != 0)
271 pfatal_with_name (outname);
272
273 /* Also check that outname's directory is writable to the real uid. */
274 {
275 char *buf = (char *) xmalloc (strlen (outname) + 1);
276 char *p;
277 strcpy (buf, outname);
278 p = buf + strlen (buf);
279 while (p > buf && !IS_DIRECTORY_SEP (p[-1]))
280 *--p = 0;
281 if (p == buf)
282 *p++ = '.';
283 if (access (buf, W_OK) != 0)
284 pfatal_with_name (buf);
285 free (buf);
286 }
287
288 #ifdef MAIL_USE_POP
289 if (!strncmp (inname, "po:", 3))
290 {
291 int status;
292
293 status = popmail (inname + 3, outname, preserve_mail,
294 (argc - optind == 3) ? argv[optind+2] : NULL, pop_flags);
295 exit (status);
296 }
297
298 setuid (getuid ());
299 #endif /* MAIL_USE_POP */
300
301 #ifndef DISABLE_DIRECT_ACCESS
302
303 /* Check access to input file. */
304 if (access (inname, R_OK | W_OK) != 0)
305 pfatal_with_name (inname);
306
307 #ifndef MAIL_USE_MMDF
308 #ifndef MAIL_USE_SYSTEM_LOCK
309 #ifdef MAIL_USE_MAILLOCK
310 spool_name = mail_spool_name (inname);
311 if (! spool_name)
312 #endif
313 {
314 /* Use a lock file named after our first argument with .lock appended:
315 If it exists, the mail file is locked. */
316 /* Note: this locking mechanism is *required* by the mailer
317 (on systems which use it) to prevent loss of mail.
318
319 On systems that use a lock file, extracting the mail without locking
320 WILL occasionally cause loss of mail due to timing errors!
321
322 So, if creation of the lock file fails
323 due to access permission on the mail spool directory,
324 you simply MUST change the permission
325 and/or make movemail a setgid program
326 so it can create lock files properly.
327
328 You might also wish to verify that your system is one
329 which uses lock files for this purpose. Some systems use other methods.
330
331 If your system uses the `flock' system call for mail locking,
332 define MAIL_USE_SYSTEM_LOCK in config.h or the s-*.h file
333 and recompile movemail. If the s- file for your system
334 should define MAIL_USE_SYSTEM_LOCK but does not, send a bug report
335 to bug-gnu-emacs@prep.ai.mit.edu so we can fix it. */
336
337 lockname = concat (inname, ".lock", "");
338 tempname = (char *) xmalloc (strlen (inname) + strlen ("EXXXXXX") + 1);
339 strcpy (tempname, inname);
340 p = tempname + strlen (tempname);
341 while (p != tempname && !IS_DIRECTORY_SEP (p[-1]))
342 p--;
343 *p = 0;
344 strcpy (p, "EXXXXXX");
345 mktemp (tempname);
346 unlink (tempname);
347
348 while (1)
349 {
350 /* Create the lock file, but not under the lock file name. */
351 /* Give up if cannot do that. */
352 desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0666);
353 if (desc < 0)
354 {
355 char *message = (char *) xmalloc (strlen (tempname) + 50);
356 sprintf (message, "%s--see source file lib-src/movemail.c",
357 tempname);
358 pfatal_with_name (message);
359 }
360 close (desc);
361
362 tem = link (tempname, lockname);
363 unlink (tempname);
364 if (tem >= 0)
365 break;
366 sleep (1);
367
368 /* If lock file is five minutes old, unlock it.
369 Five minutes should be good enough to cope with crashes
370 and wedgitude, and long enough to avoid being fooled
371 by time differences between machines. */
372 if (stat (lockname, &st) >= 0)
373 {
374 now = time (0);
375 if (st.st_ctime < now - 300)
376 unlink (lockname);
377 }
378 }
379
380 delete_lockname = lockname;
381 }
382 #endif /* not MAIL_USE_SYSTEM_LOCK */
383 #endif /* not MAIL_USE_MMDF */
384
385 if (fork () == 0)
386 {
387 int lockcount = 0;
388 int status = 0;
389 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
390 long touched_lock, now;
391 #endif
392
393 setuid (getuid ());
394
395 #ifndef MAIL_USE_MMDF
396 #ifdef MAIL_USE_SYSTEM_LOCK
397 indesc = open (inname, O_RDWR);
398 #else /* if not MAIL_USE_SYSTEM_LOCK */
399 indesc = open (inname, O_RDONLY);
400 #endif /* not MAIL_USE_SYSTEM_LOCK */
401 #else /* MAIL_USE_MMDF */
402 indesc = lk_open (inname, O_RDONLY, 0, 0, 10);
403 #endif /* MAIL_USE_MMDF */
404
405 if (indesc < 0)
406 pfatal_with_name (inname);
407
408 #if defined (BSD_SYSTEM) || defined (XENIX)
409 /* In case movemail is setuid to root, make sure the user can
410 read the output file. */
411 /* This is desirable for all systems
412 but I don't want to assume all have the umask system call */
413 umask (umask (0) & 0333);
414 #endif /* BSD_SYSTEM || XENIX */
415 outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666);
416 if (outdesc < 0)
417 pfatal_with_name (outname);
418
419 /* This label exists so we can retry locking
420 after a delay, if it got EAGAIN or EBUSY. */
421 retry_lock:
422
423 /* Try to lock it. */
424 #ifdef MAIL_USE_MAILLOCK
425 if (spool_name)
426 {
427 /* The "0 - " is to make it a negative number if maillock returns
428 non-zero. */
429 status = 0 - maillock (spool_name, 1);
430 #ifdef HAVE_TOUCHLOCK
431 touched_lock = time (0);
432 #endif
433 lockcount = 5;
434 }
435 else
436 #endif /* MAIL_USE_MAILLOCK */
437 {
438 #ifdef MAIL_USE_SYSTEM_LOCK
439 #ifdef MAIL_USE_LOCKF
440 status = lockf (indesc, F_LOCK, 0);
441 #else /* not MAIL_USE_LOCKF */
442 #ifdef XENIX
443 status = locking (indesc, LK_RLCK, 0L);
444 #else
445 #ifdef WINDOWSNT
446 status = locking (indesc, LK_RLCK, -1L);
447 #else
448 status = flock (indesc, LOCK_EX);
449 #endif
450 #endif
451 #endif /* not MAIL_USE_LOCKF */
452 #endif /* MAIL_USE_SYSTEM_LOCK */
453 }
454
455 /* If it fails, retry up to 5 times
456 for certain failure codes. */
457 if (status < 0)
458 {
459 if (++lockcount <= 5)
460 {
461 #ifdef EAGAIN
462 if (errno == EAGAIN)
463 {
464 sleep (1);
465 goto retry_lock;
466 }
467 #endif
468 #ifdef EBUSY
469 if (errno == EBUSY)
470 {
471 sleep (1);
472 goto retry_lock;
473 }
474 #endif
475 }
476
477 pfatal_with_name (inname);
478 }
479
480 {
481 char buf[1024];
482
483 while (1)
484 {
485 nread = read (indesc, buf, sizeof buf);
486 if (nread != write (outdesc, buf, nread))
487 {
488 int saved_errno = errno;
489 unlink (outname);
490 errno = saved_errno;
491 pfatal_with_name (outname);
492 }
493 if (nread < sizeof buf)
494 break;
495 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
496 if (spool_name)
497 {
498 now = time (0);
499 if (now - touched_lock > 60)
500 {
501 touchlock ();
502 touched_lock = now;
503 }
504 }
505 #endif /* MAIL_USE_MAILLOCK */
506 }
507 }
508
509 #ifdef BSD_SYSTEM
510 if (fsync (outdesc) < 0)
511 pfatal_and_delete (outname);
512 #endif
513
514 /* Check to make sure no errors before we zap the inbox. */
515 if (close (outdesc) != 0)
516 pfatal_and_delete (outname);
517
518 #ifdef MAIL_USE_SYSTEM_LOCK
519 if (! preserve_mail)
520 {
521 #if defined (STRIDE) || defined (XENIX) || defined (WINDOWSNT)
522 /* Stride, xenix have file locking, but no ftruncate.
523 This mess will do. */
524 close (open (inname, O_CREAT | O_TRUNC | O_RDWR, 0666));
525 #else
526 ftruncate (indesc, 0L);
527 #endif /* STRIDE or XENIX */
528 }
529 #endif /* MAIL_USE_SYSTEM_LOCK */
530
531 #ifdef MAIL_USE_MMDF
532 lk_close (indesc, 0, 0, 0);
533 #else
534 close (indesc);
535 #endif
536
537 #ifndef MAIL_USE_SYSTEM_LOCK
538 if (! preserve_mail)
539 {
540 /* Delete the input file; if we can't, at least get rid of its
541 contents. */
542 #ifdef MAIL_UNLINK_SPOOL
543 /* This is generally bad to do, because it destroys the permissions
544 that were set on the file. Better to just empty the file. */
545 if (unlink (inname) < 0 && errno != ENOENT)
546 #endif /* MAIL_UNLINK_SPOOL */
547 creat (inname, 0600);
548 }
549 #endif /* not MAIL_USE_SYSTEM_LOCK */
550
551 #ifdef MAIL_USE_MAILLOCK
552 /* This has to occur in the child, i.e., in the process that
553 acquired the lock! */
554 if (spool_name)
555 mailunlock ();
556 #endif
557 exit (0);
558 }
559
560 wait (&status);
561 if (!WIFEXITED (status))
562 exit (1);
563 else if (WRETCODE (status) != 0)
564 exit (WRETCODE (status));
565
566 #if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK)
567 #ifdef MAIL_USE_MAILLOCK
568 if (! spool_name)
569 #endif /* MAIL_USE_MAILLOCK */
570 unlink (lockname);
571 #endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */
572
573 #endif /* ! DISABLE_DIRECT_ACCESS */
574
575 return 0;
576 }
577
578 #ifdef MAIL_USE_MAILLOCK
579 /* This function uses stat to confirm that the mail directory is
580 identical to the directory of the input file, rather than just
581 string-comparing the two paths, because one or both of them might
582 be symbolic links pointing to some other directory. */
583 static char *
584 mail_spool_name (inname)
585 char *inname;
586 {
587 struct stat stat1, stat2;
588 char *indir, *fname;
589 int status;
590
591 if (! (fname = rindex (inname, '/')))
592 return NULL;
593
594 fname++;
595
596 if (stat (MAILDIR, &stat1) < 0)
597 return NULL;
598
599 indir = (char *) xmalloc (fname - inname + 1);
600 strncpy (indir, inname, fname - inname);
601 indir[fname-inname] = '\0';
602
603
604 status = stat (indir, &stat2);
605
606 free (indir);
607
608 if (status < 0)
609 return NULL;
610
611 if (stat1.st_dev == stat2.st_dev
612 && stat1.st_ino == stat2.st_ino)
613 return fname;
614
615 return NULL;
616 }
617 #endif /* MAIL_USE_MAILLOCK */
618 \f
619 /* Print error message and exit. */
620
621 void
622 fatal (s1, s2)
623 char *s1, *s2;
624 {
625 if (delete_lockname)
626 unlink (delete_lockname);
627 error (s1, s2);
628 exit (1);
629 }
630
631 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
632
633 void
634 error (s1, s2, s3)
635 char *s1, *s2, *s3;
636 {
637 fprintf (stderr, "movemail: ");
638 fprintf (stderr, s1, s2, s3);
639 fprintf (stderr, "\n");
640 }
641
642 void
643 pfatal_with_name (name)
644 char *name;
645 {
646 char *s = concat ("", strerror (errno), " for %s");
647 fatal (s, name);
648 }
649
650 void
651 pfatal_and_delete (name)
652 char *name;
653 {
654 char *s = concat ("", strerror (errno), " for %s");
655 unlink (name);
656 fatal (s, name);
657 }
658
659 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
660
661 char *
662 concat (s1, s2, s3)
663 char *s1, *s2, *s3;
664 {
665 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
666 char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
667
668 strcpy (result, s1);
669 strcpy (result + len1, s2);
670 strcpy (result + len1 + len2, s3);
671 *(result + len1 + len2 + len3) = 0;
672
673 return result;
674 }
675
676 /* Like malloc but get fatal error if memory is exhausted. */
677
678 long *
679 xmalloc (size)
680 unsigned size;
681 {
682 long *result = (long *) malloc (size);
683 if (!result)
684 fatal ("virtual memory exhausted", 0);
685 return result;
686 }
687 \f
688 /* This is the guts of the interface to the Post Office Protocol. */
689
690 #ifdef MAIL_USE_POP
691
692 #ifndef WINDOWSNT
693 #include <sys/socket.h>
694 #include <netinet/in.h>
695 #include <netdb.h>
696 #else
697 #undef _WINSOCKAPI_
698 #include <winsock.h>
699 #endif
700 #include <pwd.h>
701
702 #define NOTOK (-1)
703 #define OK 0
704 #define DONE 1
705
706 char *progname;
707 FILE *sfi;
708 FILE *sfo;
709 char ibuffer[BUFSIZ];
710 char obuffer[BUFSIZ];
711 char Errmsg[80];
712
713 popmail (user, outfile, preserve, password, flags)
714 char *user;
715 char *outfile;
716 int preserve;
717 char *password;
718 int flags;
719 {
720 int nmsgs, nbytes;
721 register int i;
722 int mbfi;
723 FILE *mbf;
724 char *getenv ();
725 popserver server;
726
727 server = pop_open (0, user, password, flags);
728 if (! server)
729 {
730 error (pop_error);
731 return (1);
732 }
733
734 if (pop_stat (server, &nmsgs, &nbytes))
735 {
736 error (pop_error);
737 return (1);
738 }
739
740 if (!nmsgs)
741 {
742 pop_close (server);
743 return (0);
744 }
745
746 mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
747 if (mbfi < 0)
748 {
749 pop_close (server);
750 error ("Error in open: %s, %s", strerror (errno), outfile);
751 return (1);
752 }
753 fchown (mbfi, getuid (), -1);
754
755 if ((mbf = fdopen (mbfi, "wb")) == NULL)
756 {
757 pop_close (server);
758 error ("Error in fdopen: %s", strerror (errno));
759 close (mbfi);
760 unlink (outfile);
761 return (1);
762 }
763
764 for (i = 1; i <= nmsgs; i++)
765 {
766 mbx_delimit_begin (mbf);
767 if (pop_retr (server, i, mbf) != OK)
768 {
769 error (Errmsg);
770 close (mbfi);
771 return (1);
772 }
773 mbx_delimit_end (mbf);
774 fflush (mbf);
775 if (ferror (mbf))
776 {
777 error ("Error in fflush: %s", strerror (errno));
778 pop_close (server);
779 close (mbfi);
780 return (1);
781 }
782 }
783
784 /* On AFS, a call to write only modifies the file in the local
785 * workstation's AFS cache. The changes are not written to the server
786 * until a call to fsync or close is made. Users with AFS home
787 * directories have lost mail when over quota because these checks were
788 * not made in previous versions of movemail. */
789
790 #ifdef BSD_SYSTEM
791 if (fsync (mbfi) < 0)
792 {
793 error ("Error in fsync: %s", strerror (errno));
794 return (1);
795 }
796 #endif
797
798 if (close (mbfi) == -1)
799 {
800 error ("Error in close: %s", strerror (errno));
801 return (1);
802 }
803
804 if (! preserve)
805 for (i = 1; i <= nmsgs; i++)
806 {
807 if (pop_delete (server, i))
808 {
809 error (pop_error);
810 pop_close (server);
811 return (1);
812 }
813 }
814
815 if (pop_quit (server))
816 {
817 error (pop_error);
818 return (1);
819 }
820
821 return (0);
822 }
823
824 int
825 pop_retr (server, msgno, arg)
826 popserver server;
827 FILE *arg;
828 {
829 extern char *strerror ();
830 char *line;
831 int ret;
832
833 if (pop_retrieve_first (server, msgno, &line))
834 {
835 strncpy (Errmsg, pop_error, sizeof (Errmsg));
836 Errmsg[sizeof (Errmsg)-1] = '\0';
837 return (NOTOK);
838 }
839
840 while ((ret = pop_retrieve_next (server, &line)) >= 0)
841 {
842 if (! line)
843 break;
844
845 if (mbx_write (line, ret, arg) != OK)
846 {
847 strcpy (Errmsg, strerror (errno));
848 pop_close (server);
849 return (NOTOK);
850 }
851 }
852
853 if (ret)
854 {
855 strncpy (Errmsg, pop_error, sizeof (Errmsg));
856 Errmsg[sizeof (Errmsg)-1] = '\0';
857 return (NOTOK);
858 }
859
860 return (OK);
861 }
862
863 /* Do this as a macro instead of using strcmp to save on execution time. */
864 #define IS_FROM_LINE(a) ((a[0] == 'F') \
865 && (a[1] == 'r') \
866 && (a[2] == 'o') \
867 && (a[3] == 'm') \
868 && (a[4] == ' '))
869
870 int
871 mbx_write (line, len, mbf)
872 char *line;
873 int len;
874 FILE *mbf;
875 {
876 #ifdef MOVEMAIL_QUOTE_POP_FROM_LINES
877 if (IS_FROM_LINE (line))
878 {
879 if (fputc ('>', mbf) == EOF)
880 return (NOTOK);
881 }
882 #endif
883 if (line[0] == '\037')
884 {
885 if (fputs ("^_", mbf) == EOF)
886 return (NOTOK);
887 line++;
888 len--;
889 }
890 if (fwrite (line, 1, len, mbf) != len)
891 return (NOTOK);
892 if (fputc (0x0a, mbf) == EOF)
893 return (NOTOK);
894 return (OK);
895 }
896
897 int
898 mbx_delimit_begin (mbf)
899 FILE *mbf;
900 {
901 if (fputs ("\f\n0, unseen,,\n", mbf) == EOF)
902 return (NOTOK);
903 return (OK);
904 }
905
906 mbx_delimit_end (mbf)
907 FILE *mbf;
908 {
909 if (putc ('\037', mbf) == EOF)
910 return (NOTOK);
911 return (OK);
912 }
913
914 #endif /* MAIL_USE_POP */
915 \f
916 #ifndef HAVE_STRERROR
917 char *
918 strerror (errnum)
919 int errnum;
920 {
921 extern char *sys_errlist[];
922 extern int sys_nerr;
923
924 if (errnum >= 0 && errnum < sys_nerr)
925 return sys_errlist[errnum];
926 return (char *) "Unknown error";
927 }
928
929 #endif /* ! HAVE_STRERROR */