1 /*------------ Telecommunications & Signal Processing Lab -------------
2                          McGill University
3 
4 Routine:
5   void FLbackup (const char Fname[])
6 
7 Purpose:
8   Rename an existing file
9 
10 Description:
11   This subroutine backs up a file.  If a file with the given name exists, say
12   xxx, that file is renamed to xxx~.  Any previous file with the name xxx~ is
13   removed.
14 
15   Only regular files are backed up.  If a backup file is created, a warning
16   message indicating the new name is issued.  One use of this routine is to
17   a copy of an existing file before opening a new file.
18 
19   This routine will terminate execution if the file cannot be backed up.
20 
21 Parameters:
22    -> const char Fname[]
23       File name
24 
25 Author / revision:
26   P. Kabal  Copyright (C) 2003
27   $Revision: 1.33 $  $Date: 2003/05/09 01:36:43 $
28 
29 ----------------------------------------------------------------------*/
30 
31 #include <string.h>
32 
33 #include <libtsp.h>
34 #include <libtsp/nucleus.h>
35 #include <libtsp/FLmsg.h>
36 
37 #define MINV(a, b)	(((a) < (b)) ? (a) : (b))
38 
39 
40 void
FLbackup(const char Fname[])41 FLbackup (const char Fname[])
42 
43 {
44   char Fback[FILENAME_MAX];
45   int n;
46 
47   /* Rename only if the file is a regular file */
48   if (FLexist (Fname)) {
49 
50 /* Add a ~ to the overall file name */
51     n = STcopyMax (Fname, Fback, FILENAME_MAX-2);
52     Fback[n] = '~';
53     Fback[n+1] = '\0';
54 
55 /* Rename the file; rename is not guaranteed to delete a previous backup */
56     if (FLexist (Fback)) {
57       if (remove (Fback) != 0)
58 	UTerror ("FLbackup: %s: \"%s\"", FLM_DelBackErr, Fback);
59     }
60     if (rename (Fname, Fback) == 0)
61       UTwarn ("FLbackup - %s \"%s\"", FLM_Rename, Fback);
62     else
63       UTerror ("FLbackup: %s: \"%s\"", FLM_RenameErr, Fname);
64   }
65   return;
66 }
67