xref: /titanic_41/usr/src/cmd/mail/cksaved.c (revision cde2885fdf538266ee2a3b08dee2d5075ce8fa2b)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
26 
27 /*
28  *  NAME
29  *	cksaved - check for an orphaned save file
30  *
31  *  SYNOPSIS
32  *	void cksaved(char *user)
33  *
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.
38  *
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.
42  *
43  *	If $MAIL doesn't exist, then a simple rename() will suffice.
44  */
45 
46 #include "mail.h"
47 void
48 cksaved(user)
49 char	*user;
50 {
51 	struct stat stbuf;
52 	char command[512];
53 	char save[MAXFILENAME], mail[MAXFILENAME];
54 
55 	cat(mail, maildir, user);
56 	cat(save, mailsave, user);
57 
58 	/*
59 	 *	If no save file, or size is 0, return.
60 	 */
61 	if ((stat(save, &stbuf) != 0) || (stbuf.st_size == 0))
62 		return;
63 
64 	/*
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.
68 	 */
69 	lock(user);
70 	if (stat(mail, &stbuf) != 0) {
71 		/*
72 		 *	Restore from the save file by linking
73 		 *	it to $MAIL then unlinking save file
74 		 */
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;
81 		}
82 
83 		if (unlink(save) != 0) {
84 			unlock();
85 			perror("Cannot unlink saved file");
86 			return;
87 		}
88 #else
89 		if (rename(save, mail) != 0) {
90 			unlock();
91 			perror("Cannot rename saved file");
92 			return;
93 		}
94 #endif
95 
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);
100 	}
101 
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;
110 		}
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;
117 		}
118 		copystream(Istream, Ostream);
119 		fclose(Istream);
120 		fclose(Ostream);
121 
122 		if (unlink(save) != 0) {
123 			perror("Unlink of save file failed");
124 			return;
125 		}
126 
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);
130 	}
131 
132 	/*
133 	 *	Try to send mail to the user whose file
134 	 *	is being restored.
135 	 */
136 	unlock();
137 	systm(command);
138 }
139