Merge commit '00f1a4f432b3d8aad1aa270e91c44c57f03ef407'
[unleashed.git] / usr / src / cmd / mail / copyback.c
blob18e6486b0b25ccdfc029e86adef08f7437228042
1 /*
2 * CDDL HEADER START
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
7 * with the License.
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]
20 * CDDL HEADER END
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
25 #pragma ident "%Z%%M% %I% %E% SMI"
28 * NAME
29 * copyback - copy temp or whatever back to /var/mail
31 * SYNOPSIS
32 * void copyback()
34 * DESCRIPTION
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.
40 #include "mail.h"
41 void
42 copyback()
44 register int i, n;
45 int new = 0;
46 mode_t mailmode, omask;
47 struct stat stbuf;
48 void (*hstat)(), (*istat)(), (*qstat)();
50 istat = signal(SIGINT, SIG_IGN);
51 qstat = signal(SIGQUIT, SIG_IGN);
52 hstat = signal(SIGHUP, SIG_IGN);
53 lock(my_name);
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);
63 fclose(tmpf);
64 tmpf = doopen(lettmp, "a", E_TMP);
66 * Append new mail assume only one new letter
68 if (!copystream(malf, tmpf)) {
69 fclose(malf);
70 tmperr();
71 done(0);
73 fclose(malf);
74 fclose(tmpf);
75 tmpf = doopen(lettmp, "r+", E_TMP);
76 if (nlet == (MAXLET-2)) {
77 errmsg(E_SPACE, "");
78 done(0);
80 let[++nlet].adr = stbuf.st_size;
81 new = 1;
85 * Copy mail back to mail file
87 omask = umask(0117);
90 * The invoker must own the mailfile being copied to
92 if ((stbuf.st_uid != my_euid) && (stbuf.st_uid != my_uid)) {
93 errmsg(E_OWNR, "");
94 done(0);
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!
103 if (flgf) {
104 (void) strlcpy(savefile, mailfile, sizeof (savefile));
105 } else {
106 cat(savefile, mailsave, my_name);
109 if ((malf = fopen(savefile, "w")) == NULL) {
110 if (!flgf) {
111 errmsg(E_FILE, "Cannot open savefile");
112 } else {
113 errmsg(E_FILE, "Cannot re-write the alternate file");
115 done(0);
118 if (chown(savefile, mf_uid, mf_gid) == -1) {
119 errmsg(E_FILE, "Cannot chown savefile");
120 done(0);
122 umask(omask);
123 n = 0;
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
129 * deleted
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 */
138 n++;
141 fclose(malf);
143 if (!flgf) {
144 if (unlink(mailfile) != 0) {
145 errmsg(E_FILE, "Cannot unlink mailfile");
146 done(0);
148 chmod(savefile, mailmode);
149 #ifdef SVR4
150 if (rename(savefile, mailfile) != 0) {
151 errmsg(E_FILE, "Cannot rename savefile to mailfile");
152 done(0);
154 #else
155 if (link(savefile, mailfile) != 0) {
156 errmsg(E_FILE, "Cannot link savefile to mailfile");
157 done(0);
159 if (unlink(savefile) != 0) {
160 errmsg(E_FILE, "Cannot unlink save file");
161 done(0);
163 #endif
167 * Empty mailbox?
169 if (n == 0) {
170 delempty(stbuf.st_mode, mailfile);
173 if (new && !flgf) {
174 printf("New mail arrived\n");
177 unlock();
178 (void) signal(SIGINT, istat);
179 (void) signal(SIGQUIT, qstat);
180 (void) signal(SIGHUP, hstat);