]> code.delx.au - gnu-emacs/blob - lib-src/movemail.c
(find-function-search-for-symbol): Look
[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 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 delete_lockname = 0;
201
202 while ((c = getopt (argc, argv, "p")) != EOF)
203 {
204 switch (c) {
205 case 'p':
206 preserve_mail++;
207 break;
208 default:
209 exit(1);
210 }
211 }
212
213 if (
214 #ifdef MAIL_USE_POP
215 (argc - optind < 2) || (argc - optind > 3)
216 #else
217 (argc - optind != 2)
218 #endif
219 )
220 {
221 fprintf (stderr, "Usage: movemail [-p] inbox destfile%s\n",
222 #ifdef MAIL_USE_POP
223 " [POP-password]"
224 #else
225 ""
226 #endif
227 );
228 exit (1);
229 }
230
231 inname = argv[optind];
232 outname = argv[optind+1];
233
234 #ifdef MAIL_USE_MMDF
235 mmdf_init (argv[0]);
236 #endif
237
238 if (*outname == 0)
239 fatal ("Destination file name is empty", 0);
240
241 /* Check access to output file. */
242 if (access (outname, F_OK) == 0 && access (outname, W_OK) != 0)
243 pfatal_with_name (outname);
244
245 /* Also check that outname's directory is writable to the real uid. */
246 {
247 char *buf = (char *) xmalloc (strlen (outname) + 1);
248 char *p;
249 strcpy (buf, outname);
250 p = buf + strlen (buf);
251 while (p > buf && !IS_DIRECTORY_SEP (p[-1]))
252 *--p = 0;
253 if (p == buf)
254 *p++ = '.';
255 if (access (buf, W_OK) != 0)
256 pfatal_with_name (buf);
257 free (buf);
258 }
259
260 #ifdef MAIL_USE_POP
261 if (!strncmp (inname, "po:", 3))
262 {
263 int status;
264
265 status = popmail (inname + 3, outname, preserve_mail,
266 (argc - optind == 3) ? argv[optind+2] : NULL);
267 exit (status);
268 }
269
270 setuid (getuid ());
271 #endif /* MAIL_USE_POP */
272
273 #ifndef DISABLE_DIRECT_ACCESS
274
275 /* Check access to input file. */
276 if (access (inname, R_OK | W_OK) != 0)
277 pfatal_with_name (inname);
278
279 #ifndef MAIL_USE_MMDF
280 #ifndef MAIL_USE_SYSTEM_LOCK
281 #ifdef MAIL_USE_MAILLOCK
282 spool_name = mail_spool_name (inname);
283 if (! spool_name)
284 #endif
285 {
286 /* Use a lock file named after our first argument with .lock appended:
287 If it exists, the mail file is locked. */
288 /* Note: this locking mechanism is *required* by the mailer
289 (on systems which use it) to prevent loss of mail.
290
291 On systems that use a lock file, extracting the mail without locking
292 WILL occasionally cause loss of mail due to timing errors!
293
294 So, if creation of the lock file fails
295 due to access permission on the mail spool directory,
296 you simply MUST change the permission
297 and/or make movemail a setgid program
298 so it can create lock files properly.
299
300 You might also wish to verify that your system is one
301 which uses lock files for this purpose. Some systems use other methods.
302
303 If your system uses the `flock' system call for mail locking,
304 define MAIL_USE_SYSTEM_LOCK in config.h or the s-*.h file
305 and recompile movemail. If the s- file for your system
306 should define MAIL_USE_SYSTEM_LOCK but does not, send a bug report
307 to bug-gnu-emacs@prep.ai.mit.edu so we can fix it. */
308
309 lockname = concat (inname, ".lock", "");
310 tempname = (char *) xmalloc (strlen (inname) + strlen ("EXXXXXX") + 1);
311 strcpy (tempname, inname);
312 p = tempname + strlen (tempname);
313 while (p != tempname && !IS_DIRECTORY_SEP (p[-1]))
314 p--;
315 *p = 0;
316 strcpy (p, "EXXXXXX");
317 mktemp (tempname);
318 unlink (tempname);
319
320 while (1)
321 {
322 /* Create the lock file, but not under the lock file name. */
323 /* Give up if cannot do that. */
324 desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0666);
325 if (desc < 0)
326 {
327 char *message = (char *) xmalloc (strlen (tempname) + 50);
328 sprintf (message, "%s--see source file lib-src/movemail.c",
329 tempname);
330 pfatal_with_name (message);
331 }
332 close (desc);
333
334 tem = link (tempname, lockname);
335 unlink (tempname);
336 if (tem >= 0)
337 break;
338 sleep (1);
339
340 /* If lock file is five minutes old, unlock it.
341 Five minutes should be good enough to cope with crashes
342 and wedgitude, and long enough to avoid being fooled
343 by time differences between machines. */
344 if (stat (lockname, &st) >= 0)
345 {
346 now = time (0);
347 if (st.st_ctime < now - 300)
348 unlink (lockname);
349 }
350 }
351
352 delete_lockname = lockname;
353 }
354 #endif /* not MAIL_USE_SYSTEM_LOCK */
355 #endif /* not MAIL_USE_MMDF */
356
357 if (fork () == 0)
358 {
359 int lockcount = 0;
360 int status = 0;
361 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
362 long touched_lock, now;
363 #endif
364
365 setuid (getuid ());
366
367 #ifndef MAIL_USE_MMDF
368 #ifdef MAIL_USE_SYSTEM_LOCK
369 indesc = open (inname, O_RDWR);
370 #else /* if not MAIL_USE_SYSTEM_LOCK */
371 indesc = open (inname, O_RDONLY);
372 #endif /* not MAIL_USE_SYSTEM_LOCK */
373 #else /* MAIL_USE_MMDF */
374 indesc = lk_open (inname, O_RDONLY, 0, 0, 10);
375 #endif /* MAIL_USE_MMDF */
376
377 if (indesc < 0)
378 pfatal_with_name (inname);
379
380 #if defined (BSD_SYSTEM) || defined (XENIX)
381 /* In case movemail is setuid to root, make sure the user can
382 read the output file. */
383 /* This is desirable for all systems
384 but I don't want to assume all have the umask system call */
385 umask (umask (0) & 0333);
386 #endif /* BSD_SYSTEM || XENIX */
387 outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666);
388 if (outdesc < 0)
389 pfatal_with_name (outname);
390
391 /* This label exists so we can retry locking
392 after a delay, if it got EAGAIN or EBUSY. */
393 retry_lock:
394
395 /* Try to lock it. */
396 #ifdef MAIL_USE_MAILLOCK
397 if (spool_name)
398 {
399 /* The "0 - " is to make it a negative number if maillock returns
400 non-zero. */
401 status = 0 - maillock (spool_name, 1);
402 #ifdef HAVE_TOUCHLOCK
403 touched_lock = time (0);
404 #endif
405 lockcount = 5;
406 }
407 else
408 #endif /* MAIL_USE_MAILLOCK */
409 {
410 #ifdef MAIL_USE_SYSTEM_LOCK
411 #ifdef MAIL_USE_LOCKF
412 status = lockf (indesc, F_LOCK, 0);
413 #else /* not MAIL_USE_LOCKF */
414 #ifdef XENIX
415 status = locking (indesc, LK_RLCK, 0L);
416 #else
417 #ifdef WINDOWSNT
418 status = locking (indesc, LK_RLCK, -1L);
419 #else
420 status = flock (indesc, LOCK_EX);
421 #endif
422 #endif
423 #endif /* not MAIL_USE_LOCKF */
424 #endif /* MAIL_USE_SYSTEM_LOCK */
425 }
426
427 /* If it fails, retry up to 5 times
428 for certain failure codes. */
429 if (status < 0)
430 {
431 if (++lockcount <= 5)
432 {
433 #ifdef EAGAIN
434 if (errno == EAGAIN)
435 {
436 sleep (1);
437 goto retry_lock;
438 }
439 #endif
440 #ifdef EBUSY
441 if (errno == EBUSY)
442 {
443 sleep (1);
444 goto retry_lock;
445 }
446 #endif
447 }
448
449 pfatal_with_name (inname);
450 }
451
452 {
453 char buf[1024];
454
455 while (1)
456 {
457 nread = read (indesc, buf, sizeof buf);
458 if (nread != write (outdesc, buf, nread))
459 {
460 int saved_errno = errno;
461 unlink (outname);
462 errno = saved_errno;
463 pfatal_with_name (outname);
464 }
465 if (nread < sizeof buf)
466 break;
467 #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK)
468 if (spool_name)
469 {
470 now = time (0);
471 if (now - touched_lock > 60)
472 {
473 touchlock ();
474 touched_lock = now;
475 }
476 }
477 #endif /* MAIL_USE_MAILLOCK */
478 }
479 }
480
481 #ifdef BSD_SYSTEM
482 if (fsync (outdesc) < 0)
483 pfatal_and_delete (outname);
484 #endif
485
486 /* Check to make sure no errors before we zap the inbox. */
487 if (close (outdesc) != 0)
488 pfatal_and_delete (outname);
489
490 #ifdef MAIL_USE_SYSTEM_LOCK
491 if (! preserve_mail)
492 {
493 #if defined (STRIDE) || defined (XENIX) || defined (WINDOWSNT)
494 /* Stride, xenix have file locking, but no ftruncate.
495 This mess will do. */
496 close (open (inname, O_CREAT | O_TRUNC | O_RDWR, 0666));
497 #else
498 ftruncate (indesc, 0L);
499 #endif /* STRIDE or XENIX */
500 }
501 #endif /* MAIL_USE_SYSTEM_LOCK */
502
503 #ifdef MAIL_USE_MMDF
504 lk_close (indesc, 0, 0, 0);
505 #else
506 close (indesc);
507 #endif
508
509 #ifndef MAIL_USE_SYSTEM_LOCK
510 if (! preserve_mail)
511 {
512 /* Delete the input file; if we can't, at least get rid of its
513 contents. */
514 #ifdef MAIL_UNLINK_SPOOL
515 /* This is generally bad to do, because it destroys the permissions
516 that were set on the file. Better to just empty the file. */
517 if (unlink (inname) < 0 && errno != ENOENT)
518 #endif /* MAIL_UNLINK_SPOOL */
519 creat (inname, 0600);
520 }
521 #endif /* not MAIL_USE_SYSTEM_LOCK */
522
523 #ifdef MAIL_USE_MAILLOCK
524 /* This has to occur in the child, i.e., in the process that
525 acquired the lock! */
526 if (spool_name)
527 mailunlock ();
528 #endif
529 exit (0);
530 }
531
532 wait (&status);
533 if (!WIFEXITED (status))
534 exit (1);
535 else if (WRETCODE (status) != 0)
536 exit (WRETCODE (status));
537
538 #if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK)
539 #ifdef MAIL_USE_MAILLOCK
540 if (! spool_name)
541 #endif /* MAIL_USE_MAILLOCK */
542 unlink (lockname);
543 #endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */
544
545 #endif /* ! DISABLE_DIRECT_ACCESS */
546
547 return 0;
548 }
549
550 #ifdef MAIL_USE_MAILLOCK
551 /* This function uses stat to confirm that the mail directory is
552 identical to the directory of the input file, rather than just
553 string-comparing the two paths, because one or both of them might
554 be symbolic links pointing to some other directory. */
555 static char *
556 mail_spool_name (inname)
557 char *inname;
558 {
559 struct stat stat1, stat2;
560 char *indir, *fname;
561 int status;
562
563 if (! (fname = rindex (inname, '/')))
564 return NULL;
565
566 fname++;
567
568 if (stat (MAILDIR, &stat1) < 0)
569 return NULL;
570
571 indir = (char *) xmalloc (fname - inname + 1);
572 strncpy (indir, inname, fname - inname);
573 indir[fname-inname] = '\0';
574
575
576 status = stat (indir, &stat2);
577
578 free (indir);
579
580 if (status < 0)
581 return NULL;
582
583 if (stat1.st_dev == stat2.st_dev
584 && stat1.st_ino == stat2.st_ino)
585 return fname;
586
587 return NULL;
588 }
589 #endif /* MAIL_USE_MAILLOCK */
590 \f
591 /* Print error message and exit. */
592
593 void
594 fatal (s1, s2)
595 char *s1, *s2;
596 {
597 if (delete_lockname)
598 unlink (delete_lockname);
599 error (s1, s2);
600 exit (1);
601 }
602
603 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
604
605 void
606 error (s1, s2, s3)
607 char *s1, *s2, *s3;
608 {
609 fprintf (stderr, "movemail: ");
610 fprintf (stderr, s1, s2, s3);
611 fprintf (stderr, "\n");
612 }
613
614 void
615 pfatal_with_name (name)
616 char *name;
617 {
618 char *s = concat ("", strerror (errno), " for %s");
619 fatal (s, name);
620 }
621
622 void
623 pfatal_and_delete (name)
624 char *name;
625 {
626 char *s = concat ("", strerror (errno), " for %s");
627 unlink (name);
628 fatal (s, name);
629 }
630
631 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
632
633 char *
634 concat (s1, s2, s3)
635 char *s1, *s2, *s3;
636 {
637 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
638 char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
639
640 strcpy (result, s1);
641 strcpy (result + len1, s2);
642 strcpy (result + len1 + len2, s3);
643 *(result + len1 + len2 + len3) = 0;
644
645 return result;
646 }
647
648 /* Like malloc but get fatal error if memory is exhausted. */
649
650 long *
651 xmalloc (size)
652 unsigned size;
653 {
654 long *result = (long *) malloc (size);
655 if (!result)
656 fatal ("virtual memory exhausted", 0);
657 return result;
658 }
659 \f
660 /* This is the guts of the interface to the Post Office Protocol. */
661
662 #ifdef MAIL_USE_POP
663
664 #ifndef WINDOWSNT
665 #include <sys/socket.h>
666 #include <netinet/in.h>
667 #include <netdb.h>
668 #else
669 #undef _WINSOCKAPI_
670 #include <winsock.h>
671 #endif
672 #include <pwd.h>
673
674 #define NOTOK (-1)
675 #define OK 0
676 #define DONE 1
677
678 char *progname;
679 FILE *sfi;
680 FILE *sfo;
681 char ibuffer[BUFSIZ];
682 char obuffer[BUFSIZ];
683 char Errmsg[80];
684
685 popmail (user, outfile, preserve, password)
686 char *user;
687 char *outfile;
688 int preserve;
689 char *password;
690 {
691 int nmsgs, nbytes;
692 register int i;
693 int mbfi;
694 FILE *mbf;
695 char *getenv ();
696 popserver server;
697
698 server = pop_open (0, user, password, POP_NO_GETPASS);
699 if (! server)
700 {
701 error (pop_error);
702 return (1);
703 }
704
705 if (pop_stat (server, &nmsgs, &nbytes))
706 {
707 error (pop_error);
708 return (1);
709 }
710
711 if (!nmsgs)
712 {
713 pop_close (server);
714 return (0);
715 }
716
717 mbfi = open (outfile, O_WRONLY | O_CREAT | O_EXCL, 0666);
718 if (mbfi < 0)
719 {
720 pop_close (server);
721 error ("Error in open: %s, %s", strerror (errno), outfile);
722 return (1);
723 }
724 fchown (mbfi, getuid (), -1);
725
726 if ((mbf = fdopen (mbfi, "wb")) == NULL)
727 {
728 pop_close (server);
729 error ("Error in fdopen: %s", strerror (errno));
730 close (mbfi);
731 unlink (outfile);
732 return (1);
733 }
734
735 for (i = 1; i <= nmsgs; i++)
736 {
737 mbx_delimit_begin (mbf);
738 if (pop_retr (server, i, mbf) != OK)
739 {
740 error (Errmsg);
741 close (mbfi);
742 return (1);
743 }
744 mbx_delimit_end (mbf);
745 fflush (mbf);
746 if (ferror (mbf))
747 {
748 error ("Error in fflush: %s", strerror (errno));
749 pop_close (server);
750 close (mbfi);
751 return (1);
752 }
753 }
754
755 /* On AFS, a call to write only modifies the file in the local
756 * workstation's AFS cache. The changes are not written to the server
757 * until a call to fsync or close is made. Users with AFS home
758 * directories have lost mail when over quota because these checks were
759 * not made in previous versions of movemail. */
760
761 #ifdef BSD_SYSTEM
762 if (fsync (mbfi) < 0)
763 {
764 error ("Error in fsync: %s", strerror (errno));
765 return (1);
766 }
767 #endif
768
769 if (close (mbfi) == -1)
770 {
771 error ("Error in close: %s", strerror (errno));
772 return (1);
773 }
774
775 if (! preserve)
776 for (i = 1; i <= nmsgs; i++)
777 {
778 if (pop_delete (server, i))
779 {
780 error (pop_error);
781 pop_close (server);
782 return (1);
783 }
784 }
785
786 if (pop_quit (server))
787 {
788 error (pop_error);
789 return (1);
790 }
791
792 return (0);
793 }
794
795 int
796 pop_retr (server, msgno, arg)
797 popserver server;
798 FILE *arg;
799 {
800 extern char *strerror ();
801 char *line;
802 int ret;
803
804 if (pop_retrieve_first (server, msgno, &line))
805 {
806 strncpy (Errmsg, pop_error, sizeof (Errmsg));
807 Errmsg[sizeof (Errmsg)-1] = '\0';
808 return (NOTOK);
809 }
810
811 while ((ret = pop_retrieve_next (server, &line)) >= 0)
812 {
813 if (! line)
814 break;
815
816 if (mbx_write (line, ret, arg) != OK)
817 {
818 strcpy (Errmsg, strerror (errno));
819 pop_close (server);
820 return (NOTOK);
821 }
822 }
823
824 if (ret)
825 {
826 strncpy (Errmsg, pop_error, sizeof (Errmsg));
827 Errmsg[sizeof (Errmsg)-1] = '\0';
828 return (NOTOK);
829 }
830
831 return (OK);
832 }
833
834 /* Do this as a macro instead of using strcmp to save on execution time. */
835 #define IS_FROM_LINE(a) ((a[0] == 'F') \
836 && (a[1] == 'r') \
837 && (a[2] == 'o') \
838 && (a[3] == 'm') \
839 && (a[4] == ' '))
840
841 int
842 mbx_write (line, len, mbf)
843 char *line;
844 int len;
845 FILE *mbf;
846 {
847 #ifdef MOVEMAIL_QUOTE_POP_FROM_LINES
848 if (IS_FROM_LINE (line))
849 {
850 if (fputc ('>', mbf) == EOF)
851 return (NOTOK);
852 }
853 #endif
854 if (line[0] == '\037')
855 {
856 if (fputs ("^_", mbf) == EOF)
857 return (NOTOK);
858 line++;
859 len--;
860 }
861 if (fwrite (line, 1, len, mbf) != len)
862 return (NOTOK);
863 if (fputc (0x0a, mbf) == EOF)
864 return (NOTOK);
865 return (OK);
866 }
867
868 int
869 mbx_delimit_begin (mbf)
870 FILE *mbf;
871 {
872 if (fputs ("\f\n0, unseen,,\n", mbf) == EOF)
873 return (NOTOK);
874 return (OK);
875 }
876
877 mbx_delimit_end (mbf)
878 FILE *mbf;
879 {
880 if (putc ('\037', mbf) == EOF)
881 return (NOTOK);
882 return (OK);
883 }
884
885 #endif /* MAIL_USE_POP */
886 \f
887 #ifndef HAVE_STRERROR
888 char *
889 strerror (errnum)
890 int errnum;
891 {
892 extern char *sys_errlist[];
893 extern int sys_nerr;
894
895 if (errnum >= 0 && errnum < sys_nerr)
896 return sys_errlist[errnum];
897 return (char *) "Unknown error";
898 }
899
900 #endif /* ! HAVE_STRERROR */