xref: /illumos-gate/usr/src/lib/libmail/common/delempty.c (revision 7a6d80f1660abd4755c68cbd094d4a914681d26e)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  *  NAME
32  *	delempty - delete an empty mail box
33  *
34  *  SYNOPSIS
35  *	int delempty(mode_t mode, char *mailname)
36  *
37  *  DESCRIPTION
38  *	Delete an empty mail box if it's allowed. Check
39  *	the value of xgetenv("DEL_EMPTY_MFILE") for
40  *	"yes" [always], "no" [never] or the default [based
41  *	on the mode].
42  */
43 
44 #include "libmail.h"
45 #include <sys/types.h>
46 #include <unistd.h>
47 
48 int
49 delempty(mode_t mode, char *mailname)
50 {
51 	char *del_empty = Xgetenv("DEL_EMPTY_MFILE");
52 	size_t del_len;
53 	int do_del = 0;
54 
55 	del_len = strlen(del_empty);
56 	/* "yes" means always remove the mailfile */
57 	if (casncmp(del_empty, "yes", (ssize_t)del_len))
58 		do_del = 1;
59 	/* "no" means never remove the mailfile */
60 	else if (!casncmp(del_empty, "no", (ssize_t)del_len)) {
61 		/* check for mode 0660 */
62 		if ((mode & 07777) == MFMODE)
63 			do_del = 1;
64 	}
65 
66 	if (do_del)
67 		(void) unlink(mailname);
68 	return (do_del);
69 }
70