xref: /illumos-gate/usr/src/lib/libmail/common/delempty.c (revision f63f7506be0210195779706f51c58646e568cc40)
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 
23 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
24 /*	  All Rights Reserved  	*/
25 
26 /*
27  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 /*LINTLIBRARY*/
33 
34 /*
35  *  NAME
36  *	delempty - delete an empty mail box
37  *
38  *  SYNOPSIS
39  *	int delempty(mode_t mode, char *mailname)
40  *
41  *  DESCRIPTION
42  *	Delete an empty mail box if it's allowed. Check
43  *	the value of xgetenv("DEL_EMPTY_MFILE") for
44  *	"yes" [always], "no" [never] or the default [based
45  *	on the mode].
46  */
47 
48 #include "c_synonyms.h"
49 #include "libmail.h"
50 #include <sys/types.h>
51 #include <unistd.h>
52 
53 int
54 delempty(mode_t mode, char *mailname)
55 {
56 	char *del_empty = Xgetenv("DEL_EMPTY_MFILE");
57 	size_t del_len;
58 	int do_del = 0;
59 
60 	del_len = strlen(del_empty);
61 	/* "yes" means always remove the mailfile */
62 	if (casncmp(del_empty, "yes", (ssize_t)del_len))
63 		do_del = 1;
64 	/* "no" means never remove the mailfile */
65 	else if (!casncmp(del_empty, "no", (ssize_t)del_len)) {
66 		/* check for mode 0660 */
67 		if ((mode & 07777) == MFMODE)
68 			do_del = 1;
69 	}
70 
71 	if (do_del)
72 		(void) unlink(mailname);
73 	return (do_del);
74 }
75