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