xref: /netbsd-src/external/ibm-public/postfix/dist/src/global/remove.c (revision a30b880ed60a24c405edba78187a04247f4d9d33)
1 /*	$NetBSD: remove.c,v 1.1.1.2 2013/01/02 18:59:00 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	REMOVE 3
6 /* SUMMARY
7 /*	remove or stash away file
8 /* SYNOPSIS
9 /*	\fBint	REMOVE(path)\fR
10 /*	\fBconst char *path;\fR
11 /* DESCRIPTION
12 /*	\fBREMOVE()\fR removes a file, or renames it to a unique name,
13 /*	depending on the setting of the boolean \fBvar_dont_remove\fR
14 /*	flag.
15 /* DIAGNOSTICS
16 /*	The result is 0 in case of success, -1 in case of trouble.
17 /*	The global \fBerrno\fR variable reflects the nature of the
18 /*	problem.
19 /* FILES
20 /*	saved/*, stashed-away files.
21 /* SEE ALSO
22 /*	remove(3)
23 /* LICENSE
24 /* .ad
25 /* .fi
26 /*	The Secure Mailer license must be distributed with this software.
27 /* AUTHOR(S)
28 /*	Wietse Venema
29 /*	IBM T.J. Watson Research
30 /*	P.O. Box 704
31 /*	Yorktown Heights, NY 10598, USA
32 /*--*/
33 
34 /* System library. */
35 
36 #include <sys_defs.h>
37 #include <sys/stat.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <warn_stat.h>
43 
44 /* Utility library. */
45 
46 #include <vstring.h>
47 
48 /* Global library. */
49 
50 #include <mail_params.h>
51 
52 /* REMOVE - squirrel away a file instead of removing it */
53 
REMOVE(const char * path)54 int     REMOVE(const char *path)
55 {
56     static VSTRING *dest;
57     char   *slash;
58     struct stat st;
59 
60     if (var_dont_remove == 0) {
61 	return (remove(path));
62     } else {
63 	if (dest == 0)
64 	    dest = vstring_alloc(10);
65 	vstring_sprintf(dest, "saved/%s", ((slash = strrchr(path, '/')) != 0) ?
66 			slash + 1 : path);
67 	for (;;) {
68 	    if (stat(vstring_str(dest), &st) < 0)
69 		break;
70 	    vstring_strcat(dest, "+");
71 	}
72 	return (rename(path, vstring_str(dest)));
73     }
74 }
75