4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
25 #pragma ident "%Z%%M% %I% %E% SMI"
29 * copyback - copy temp or whatever back to /var/mail
35 * Copy the reduced contents of lettmp back to
36 * the mail file. First copy any new mail from
37 * the mail file to the end of lettmp.
46 mode_t mailmode
, omask
;
48 void (*hstat
)(), (*istat
)(), (*qstat
)();
50 istat
= signal(SIGINT
, SIG_IGN
);
51 qstat
= signal(SIGQUIT
, SIG_IGN
);
52 hstat
= signal(SIGHUP
, SIG_IGN
);
54 stat(mailfile
, &stbuf
);
55 mailmode
= stbuf
.st_mode
;
58 * Has new mail arrived?
60 if (stbuf
.st_size
!= let
[nlet
].adr
) {
61 malf
= doopen(mailfile
, "r", E_FILE
);
62 fseek(malf
, let
[nlet
].adr
, 0);
64 tmpf
= doopen(lettmp
, "a", E_TMP
);
66 * Append new mail assume only one new letter
68 if (!copystream(malf
, tmpf
)) {
75 tmpf
= doopen(lettmp
, "r+", E_TMP
);
76 if (nlet
== (MAXLET
-2)) {
80 let
[++nlet
].adr
= stbuf
.st_size
;
85 * Copy mail back to mail file
90 * The invoker must own the mailfile being copied to
92 if ((stbuf
.st_uid
!= my_euid
) && (stbuf
.st_uid
!= my_uid
)) {
98 * If user specified the '-f' option we dont do
99 * the routines to handle :saved files.
100 * As we would(incorrectly) restore to the user's
101 * mailfile upon next execution!
104 (void) strlcpy(savefile
, mailfile
, sizeof (savefile
));
106 cat(savefile
, mailsave
, my_name
);
109 if ((malf
= fopen(savefile
, "w")) == NULL
) {
111 errmsg(E_FILE
, "Cannot open savefile");
113 errmsg(E_FILE
, "Cannot re-write the alternate file");
118 if (chown(savefile
, mf_uid
, mf_gid
) == -1) {
119 errmsg(E_FILE
, "Cannot chown savefile");
125 for (i
= 0; i
< nlet
; i
++) {
127 * Note: any action other than an undelete, or a
128 * plain read causes the letter acted upon to be
131 if (let
[i
].change
== ' ') {
132 if (copylet(i
, malf
, ORDINARY
) == FALSE
) {
133 errmsg(E_FILE
, "Cannot copy mail to savefile");
134 (void) fprintf(stderr
, "%s: A copy of your "
135 "mailfile is in '%s'\n", program
, lettmp
);
136 done(1); /* keep temp file */
144 if (unlink(mailfile
) != 0) {
145 errmsg(E_FILE
, "Cannot unlink mailfile");
148 chmod(savefile
, mailmode
);
150 if (rename(savefile
, mailfile
) != 0) {
151 errmsg(E_FILE
, "Cannot rename savefile to mailfile");
155 if (link(savefile
, mailfile
) != 0) {
156 errmsg(E_FILE
, "Cannot link savefile to mailfile");
159 if (unlink(savefile
) != 0) {
160 errmsg(E_FILE
, "Cannot unlink save file");
170 delempty(stbuf
.st_mode
, mailfile
);
174 printf("New mail arrived\n");
178 (void) signal(SIGINT
, istat
);
179 (void) signal(SIGQUIT
, qstat
);
180 (void) signal(SIGHUP
, hstat
);