1 /* $NetBSD: remove.c,v 1.1.1.1 2009/06/23 10:08:47 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 43 /* Utility library. */ 44 45 #include <vstring.h> 46 47 /* Global library. */ 48 49 #include <mail_params.h> 50 51 /* REMOVE - squirrel away a file instead of removing it */ 52 53 int REMOVE(const char *path) 54 { 55 static VSTRING *dest; 56 char *slash; 57 struct stat st; 58 59 if (var_dont_remove == 0) { 60 return (remove(path)); 61 } else { 62 if (dest == 0) 63 dest = vstring_alloc(10); 64 vstring_sprintf(dest, "saved/%s", ((slash = strrchr(path, '/')) != 0) ? 65 slash + 1 : path); 66 for (;;) { 67 if (stat(vstring_str(dest), &st) < 0) 68 break; 69 vstring_strcat(dest, "+"); 70 } 71 return (rename(path, vstring_str(dest))); 72 } 73 } 74