xref: /illumos-gate/usr/src/cmd/mail/cksaved.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
1 /*
2  * CDDL HEADER START
3  *
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.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 /*
26  *  NAME
27  *	cksaved - check for an orphaned save file
28  *
29  *  SYNOPSIS
30  *	void cksaved(char *user)
31  *
32  *  DESCRIPTION
33  *	cksaved() looks to see if there is a saved-mail file sitting
34  *	around which should be reinstated. These files should be sitting
35  *	around only in the case of a crash during rewriting a mail message.
36  *
37  *	The strategy is simple: if the file exists it is appended to
38  *	the end of $MAIL.  It is better that a user potentially sees the
39  *	mail twice than to lose it.
40  *
41  *	If $MAIL doesn't exist, then a simple rename() will suffice.
42  */
43 
44 #include "mail.h"
45 void
46 cksaved(user)
47 char	*user;
48 {
49 	struct stat stbuf;
50 	char command[512];
51 	char save[MAXFILENAME], mail[MAXFILENAME];
52 
53 	cat(mail, maildir, user);
54 	cat(save, mailsave, user);
55 
56 	/*
57 	 *	If no save file, or size is 0, return.
58 	 */
59 	if ((stat(save, &stbuf) != 0) || (stbuf.st_size == 0))
60 		return;
61 
62 	/*
63 	 *	Ok, we have a savefile. If no mailfile exists,
64 	 *	then we want to restore to the mailfile,
65 	 *	else we append to the mailfile.
66 	 */
67 	lock(user);
68 	if (stat(mail, &stbuf) != 0) {
69 		/*
70 		 *	Restore from the save file by linking
71 		 *	it to $MAIL then unlinking save file
72 		 */
73 		chmod(save, MFMODE);
74 #ifdef SVR3
75 		if (link(save, mail) != 0) {
76 			unlock();
77 			perror("Restore failed to link to mailfile");
78 			return;
79 		}
80 
81 		if (unlink(save) != 0) {
82 			unlock();
83 			perror("Cannot unlink saved file");
84 			return;
85 		}
86 #else
87 		if (rename(save, mail) != 0) {
88 			unlock();
89 			perror("Cannot rename saved file");
90 			return;
91 		}
92 #endif
93 
94 		(void) snprintf(command, sizeof (command),
95 		    "echo \"Your mailfile was just restored by the mail "
96 		    "program.\nPermissions of your mailfile are set "
97 		    "to 0660.\"| mail %s", user);
98 	}
99 
100 	else {
101 		FILE *Istream, *Ostream;
102 		if ((Ostream = fopen(mail, "a")) == NULL) {
103 			(void) fprintf(stderr,
104 			    "%s: Cannot open file '%s' for output\n",
105 			program, mail);
106 			unlock();
107 			return;
108 		}
109 		if ((Istream = fopen(save, "r")) == NULL) {
110 			(void) fprintf(stderr, "%s: Cannot open saved "
111 			    "file '%s' for reading\n", program, save);
112 			fclose(Ostream);
113 			unlock();
114 			return;
115 		}
116 		copystream(Istream, Ostream);
117 		fclose(Istream);
118 		fclose(Ostream);
119 
120 		if (unlink(save) != 0) {
121 			perror("Unlink of save file failed");
122 			return;
123 		}
124 
125 		(void) snprintf(command, sizeof (command),
126 		    "echo \"Your mail save file has just been appended "
127 		    "to your mail box by the mail program.\" | mail %s", user);
128 	}
129 
130 	/*
131 	 *	Try to send mail to the user whose file
132 	 *	is being restored.
133 	 */
134 	unlock();
135 	systm(command);
136 }
137