home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / bbs / gnu / fileutils-3.9-src.lha / src / amiga / fileutils-3.9 / lib / backupfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-22  |  6.2 KB  |  255 lines

  1. /* backupfile.c -- make Emacs style backup file names
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* David MacKenzie <djm@gnu.ai.mit.edu>.
  19.    Some algorithms adapted from GNU Emacs. */
  20.  
  21. #ifdef HAVE_CONFIG_H
  22. #if defined (CONFIG_BROKETS)
  23. /* We use <config.h> instead of "config.h" so that a compilation
  24.    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
  25.    (which it would do because it found this file in $srcdir).  */
  26. #include <config.h>
  27. #else
  28. #include "config.h"
  29. #endif
  30. #endif
  31.  
  32. #include <stdio.h>
  33. #include <ctype.h>
  34. #include <sys/types.h>
  35. #include "backupfile.h"
  36. #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
  37. #include <string.h>
  38. #ifndef index
  39. #define index strchr
  40. #endif
  41. #ifndef rindex
  42. #define rindex strrchr
  43. #endif
  44. #else
  45. #include <strings.h>
  46. #endif
  47.  
  48. #if defined(DIRENT) || defined(_POSIX_VERSION)
  49. #include <dirent.h>
  50. #define NLENGTH(direct) (strlen((direct)->d_name))
  51. #else /* not (DIRENT or _POSIX_VERSION) */
  52. #define dirent direct
  53. #define NLENGTH(direct) ((direct)->d_namlen)
  54. #ifdef SYSNDIR
  55. #include <sys/ndir.h>
  56. #endif /* SYSNDIR */
  57. #ifdef SYSDIR
  58. #include <sys/dir.h>
  59. #endif /* SYSDIR */
  60. #ifdef NDIR
  61. #include <ndir.h>
  62. #endif /* NDIR */
  63. #endif /* DIRENT or _POSIX_VERSION */
  64.  
  65. #ifdef VOID_CLOSEDIR
  66. /* Fake a return value. */
  67. #define CLOSEDIR(d) (closedir (d), 0)
  68. #else
  69. #define CLOSEDIR(d) closedir (d)
  70. #endif
  71.  
  72. #ifdef STDC_HEADERS
  73. #include <stdlib.h>
  74. #else
  75. char *malloc ();
  76. #endif
  77.  
  78. #if !defined (isascii) || defined (STDC_HEADERS)
  79. #undef isascii
  80. #define isascii(c) 1
  81. #endif
  82.  
  83. #define ISDIGIT(c) (isascii ((unsigned char ) c) \
  84.             && isdigit ((unsigned char) (c)))
  85.  
  86. #if defined (HAVE_UNISTD_H)
  87. #include <unistd.h>
  88. #endif
  89.  
  90. #if defined (_POSIX_VERSION)
  91. /* POSIX does not require that the d_ino field be present, and some
  92.    systems do not provide it. */
  93. #define REAL_DIR_ENTRY(dp) 1
  94. #else
  95. #define REAL_DIR_ENTRY(dp) ((dp)->d_ino != 0)
  96. #endif
  97.  
  98. /* Which type of backup file names are generated. */
  99. enum backup_type backup_type = none;
  100.  
  101. /* The extension added to file names to produce a simple (as opposed
  102.    to numbered) backup file name. */
  103. #ifdef AMIGA
  104. char *simple_backup_suffix = "!";
  105. #else
  106. char *simple_backup_suffix = "~";
  107. #endif
  108.  
  109. char *basename ();
  110. char *dirname ();
  111. static char *concat ();
  112. char *find_backup_file_name ();
  113. static char *make_version_name ();
  114. static int max_backup_version ();
  115. static int version_number ();
  116.  
  117. /* Return the name of the new backup file for file FILE,
  118.    allocated with malloc.  Return 0 if out of memory.
  119.    FILE must not end with a '/' unless it is the root directory.
  120.    Do not call this function if backup_type == none. */
  121.  
  122. char *
  123. find_backup_file_name (file)
  124.      char *file;
  125. {
  126.   char *dir;
  127.   char *base_versions;
  128.   int highest_backup;
  129.  
  130.   if (backup_type == simple)
  131.     return concat (file, simple_backup_suffix);
  132. #ifdef AMIGA
  133.   base_versions = concat (basename (file), ".!");
  134. #else
  135.   base_versions = concat (basename (file), ".~");
  136. #endif
  137.   if (base_versions == 0)
  138.     return 0;
  139.   dir = dirname (file);
  140.   if (dir == 0)
  141.     {
  142.       free (base_versions);
  143.       return 0;
  144.     }
  145.   highest_backup = max_backup_version (base_versions, dir);
  146.   free (base_versions);
  147.   free (dir);
  148.   if (backup_type == numbered_existing && highest_backup == 0)
  149.     return concat (file, simple_backup_suffix);
  150.   return make_version_name (file, highest_backup + 1);
  151. }
  152.  
  153. /* Return the number of the highest-numbered backup file for file
  154.    FILE in directory DIR.  If there are no numbered backups
  155.    of FILE in DIR, or an error occurs reading DIR, return 0.
  156.    FILE should already have ".~" appended to it. */
  157.  
  158. static int
  159. max_backup_version (file, dir)
  160.      char *file, *dir;
  161. {
  162.   DIR *dirp;
  163.   struct dirent *dp;
  164.   int highest_version;
  165.   int this_version;
  166.   int file_name_length;
  167.  
  168.   dirp = opendir (dir);
  169.   if (!dirp)
  170.     return 0;
  171.  
  172.   highest_version = 0;
  173.   file_name_length = strlen (file);
  174.  
  175.   while ((dp = readdir (dirp)) != 0)
  176.     {
  177.       if (!REAL_DIR_ENTRY (dp) || NLENGTH (dp) <= file_name_length)
  178.     continue;
  179.  
  180.       this_version = version_number (file, dp->d_name, file_name_length);
  181.       if (this_version > highest_version)
  182.     highest_version = this_version;
  183.     }
  184.   if (CLOSEDIR (dirp))
  185.     return 0;
  186.   return highest_version;
  187. }
  188.  
  189. /* Return a string, allocated with malloc, containing
  190.    "FILE.~VERSION~".  Return 0 if out of memory. */
  191.  
  192. static char *
  193. make_version_name (file, version)
  194.      char *file;
  195.      int version;
  196. {
  197.   char *backup_name;
  198.  
  199.   backup_name = malloc (strlen (file) + 16);
  200.   if (backup_name == 0)
  201.     return 0;
  202. #ifdef AMIGA
  203.   sprintf (backup_name, "%s.!%d!", file, version);
  204. #else
  205.   sprintf (backup_name, "%s.~%d~", file, version);
  206. #endif
  207.   return backup_name;
  208. }
  209.  
  210. /* If BACKUP is a numbered backup of BASE, return its version number;
  211.    otherwise return 0.  BASE_LENGTH is the length of BASE.
  212.    BASE should already have ".~" appended to it. */
  213.  
  214. static int
  215. version_number (base, backup, base_length)
  216.      char *base;
  217.      char *backup;
  218.      int base_length;
  219. {
  220.   int version;
  221.   char *p;
  222.  
  223.   version = 0;
  224.   if (!strncmp (base, backup, base_length) && ISDIGIT (backup[base_length]))
  225.     {
  226.       for (p = &backup[base_length]; ISDIGIT (*p); ++p)
  227.     version = version * 10 + *p - '0';
  228. #ifdef AMIGA
  229.       if (p[0] != '!' || p[1])
  230. #else
  231.       if (p[0] != '~' || p[1])
  232. #endif
  233.     version = 0;
  234.     }
  235.   return version;
  236. }
  237.  
  238. /* Return the newly-allocated concatenation of STR1 and STR2.
  239.    If out of memory, return 0. */
  240.  
  241. static char *
  242. concat (str1, str2)
  243.      char *str1, *str2;
  244. {
  245.   char *newstr;
  246.   int str1_length = strlen (str1);
  247.  
  248.   newstr = malloc (str1_length + strlen (str2) + 1);
  249.   if (newstr == 0)
  250.     return 0;
  251.   strcpy (newstr, str1);
  252.   strcpy (newstr + str1_length, str2);
  253.   return newstr;
  254. }
  255.