dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / mail / cksaved.c
blob4eaa0979b67e3a73e3648de4f3d34fe447b28154
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 * cksaved - check for an orphaned save file
31 * SYNOPSIS
32 * void cksaved(char *user)
34 * DESCRIPTION
35 * cksaved() looks to see if there is a saved-mail file sitting
36 * around which should be reinstated. These files should be sitting
37 * around only in the case of a crash during rewriting a mail message.
39 * The strategy is simple: if the file exists it is appended to
40 * the end of $MAIL. It is better that a user potentially sees the
41 * mail twice than to lose it.
43 * If $MAIL doesn't exist, then a simple rename() will suffice.
46 #include "mail.h"
47 void
48 cksaved(user)
49 char *user;
51 struct stat stbuf;
52 char command[512];
53 char save[MAXFILENAME], mail[MAXFILENAME];
55 cat(mail, maildir, user);
56 cat(save, mailsave, user);
59 * If no save file, or size is 0, return.
61 if ((stat(save, &stbuf) != 0) || (stbuf.st_size == 0))
62 return;
65 * Ok, we have a savefile. If no mailfile exists,
66 * then we want to restore to the mailfile,
67 * else we append to the mailfile.
69 lock(user);
70 if (stat(mail, &stbuf) != 0) {
72 * Restore from the save file by linking
73 * it to $MAIL then unlinking save file
75 chmod(save, MFMODE);
76 #ifdef SVR3
77 if (link(save, mail) != 0) {
78 unlock();
79 perror("Restore failed to link to mailfile");
80 return;
83 if (unlink(save) != 0) {
84 unlock();
85 perror("Cannot unlink saved file");
86 return;
88 #else
89 if (rename(save, mail) != 0) {
90 unlock();
91 perror("Cannot rename saved file");
92 return;
94 #endif
96 (void) snprintf(command, sizeof (command),
97 "echo \"Your mailfile was just restored by the mail "
98 "program.\nPermissions of your mailfile are set "
99 "to 0660.\"| mail %s", user);
102 else {
103 FILE *Istream, *Ostream;
104 if ((Ostream = fopen(mail, "a")) == NULL) {
105 (void) fprintf(stderr,
106 "%s: Cannot open file '%s' for output\n",
107 program, mail);
108 unlock();
109 return;
111 if ((Istream = fopen(save, "r")) == NULL) {
112 (void) fprintf(stderr, "%s: Cannot open saved "
113 "file '%s' for reading\n", program, save);
114 fclose(Ostream);
115 unlock();
116 return;
118 copystream(Istream, Ostream);
119 fclose(Istream);
120 fclose(Ostream);
122 if (unlink(save) != 0) {
123 perror("Unlink of save file failed");
124 return;
127 (void) snprintf(command, sizeof (command),
128 "echo \"Your mail save file has just been appended "
129 "to your mail box by the mail program.\" | mail %s", user);
133 * Try to send mail to the user whose file
134 * is being restored.
136 unlock();
137 systm(command);