backupfile.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* backupfile.c -- make Emacs style backup file names
  2. Copyright (C) 1990,1991,1992,1993,1995,1997 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; see the file COPYING.
  13. If not, write to the Free Software Foundation,
  14. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.
  16. Some algorithms adapted from GNU Emacs. */
  17. #if HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include <argmatch.h>
  21. #include <backupfile.h>
  22. #include <stdio.h>
  23. #include <sys/types.h>
  24. #if HAVE_STRING_H
  25. # include <string.h>
  26. #else
  27. # include <strings.h>
  28. #endif
  29. #if HAVE_DIRENT_H
  30. # include <dirent.h>
  31. # define NLENGTH(direct) strlen ((direct)->d_name)
  32. #else
  33. # define dirent direct
  34. # define NLENGTH(direct) ((size_t) (direct)->d_namlen)
  35. # if HAVE_SYS_NDIR_H
  36. # include <sys/ndir.h>
  37. # endif
  38. # if HAVE_SYS_DIR_H
  39. # include <sys/dir.h>
  40. # endif
  41. # if HAVE_NDIR_H
  42. # include <ndir.h>
  43. # endif
  44. #endif
  45. #if CLOSEDIR_VOID
  46. /* Fake a return value. */
  47. # define CLOSEDIR(d) (closedir (d), 0)
  48. #else
  49. # define CLOSEDIR(d) closedir (d)
  50. #endif
  51. #if STDC_HEADERS
  52. # include <stdlib.h>
  53. #else
  54. char *malloc ();
  55. #endif
  56. #if HAVE_DIRENT_H || HAVE_NDIR_H || HAVE_SYS_DIR_H || HAVE_SYS_NDIR_H
  57. # define HAVE_DIR 1
  58. #else
  59. # define HAVE_DIR 0
  60. #endif
  61. #if HAVE_LIMITS_H
  62. # include <limits.h>
  63. #endif
  64. #ifndef CHAR_BIT
  65. #define CHAR_BIT 8
  66. #endif
  67. /* Upper bound on the string length of an integer converted to string.
  68. 302 / 1000 is ceil (log10 (2.0)). Subtract 1 for the sign bit;
  69. add 1 for integer division truncation; add 1 more for a minus sign. */
  70. #define INT_STRLEN_BOUND(t) ((sizeof (t) * CHAR_BIT - 1) * 302 / 1000 + 2)
  71. /* ISDIGIT differs from isdigit, as follows:
  72. - Its arg may be any int or unsigned int; it need not be an unsigned char.
  73. - It's guaranteed to evaluate its argument exactly once.
  74. - It's typically faster.
  75. Posix 1003.2-1992 section 2.5.2.1 page 50 lines 1556-1558 says that
  76. only '0' through '9' are digits. Prefer ISDIGIT to isdigit unless
  77. it's important to use the locale's definition of `digit' even when the
  78. host does not conform to Posix. */
  79. #define ISDIGIT(c) ((unsigned) (c) - '0' <= 9)
  80. #if D_INO_IN_DIRENT
  81. # define REAL_DIR_ENTRY(dp) ((dp)->d_ino != 0)
  82. #else
  83. # define REAL_DIR_ENTRY(dp) 1
  84. #endif
  85. /* Which type of backup file names are generated. */
  86. enum backup_type backup_type = none;
  87. /* The extension added to file names to produce a simple (as opposed
  88. to numbered) backup file name. */
  89. const char *simple_backup_suffix = ".orig";
  90. static int max_backup_version __BACKUPFILE_P ((const char *, const char *));
  91. static int version_number __BACKUPFILE_P ((const char *, const char *, size_t));
  92. /* Return the name of the new backup file for file FILE,
  93. allocated with malloc. Return 0 if out of memory.
  94. FILE must not end with a '/' unless it is the root directory.
  95. Do not call this function if backup_type == none. */
  96. char *
  97. find_backup_file_name (file)
  98. const char *file;
  99. {
  100. size_t backup_suffix_size_max;
  101. size_t file_len = strlen (file);
  102. size_t numbered_suffix_size_max = INT_STRLEN_BOUND (int) + 4;
  103. char *s;
  104. const char *suffix = simple_backup_suffix;
  105. /* Allow room for simple or `.~N~' backups. */
  106. backup_suffix_size_max = strlen (simple_backup_suffix) + 1;
  107. if (HAVE_DIR && backup_suffix_size_max < numbered_suffix_size_max)
  108. backup_suffix_size_max = numbered_suffix_size_max;
  109. s = malloc (file_len + backup_suffix_size_max + numbered_suffix_size_max);
  110. if (s)
  111. {
  112. strcpy (s, file);
  113. #if HAVE_DIR
  114. if (backup_type != simple)
  115. {
  116. int highest_backup;
  117. size_t dir_len = base_name (s) - s;
  118. strcpy (s + dir_len, ".");
  119. highest_backup = max_backup_version (file + dir_len, s);
  120. if (! (backup_type == numbered_existing && highest_backup == 0))
  121. {
  122. char *numbered_suffix = s + (file_len + backup_suffix_size_max);
  123. sprintf (numbered_suffix, ".~%d~", highest_backup + 1);
  124. suffix = numbered_suffix;
  125. }
  126. strcpy (s, file);
  127. }
  128. #endif /* HAVE_DIR */
  129. addext (s, suffix, '~');
  130. }
  131. return s;
  132. }
  133. #if HAVE_DIR
  134. /* Return the number of the highest-numbered backup file for file
  135. FILE in directory DIR. If there are no numbered backups
  136. of FILE in DIR, or an error occurs reading DIR, return 0.
  137. */
  138. static int
  139. max_backup_version (file, dir)
  140. const char *file;
  141. const char *dir;
  142. {
  143. DIR *dirp;
  144. struct dirent *dp;
  145. int highest_version;
  146. int this_version;
  147. size_t file_name_length;
  148. dirp = opendir (dir);
  149. if (!dirp)
  150. return 0;
  151. highest_version = 0;
  152. file_name_length = strlen (file);
  153. while ((dp = readdir (dirp)) != 0)
  154. {
  155. if (!REAL_DIR_ENTRY (dp) || NLENGTH (dp) < file_name_length + 4)
  156. continue;
  157. this_version = version_number (file, dp->d_name, file_name_length);
  158. if (this_version > highest_version)
  159. highest_version = this_version;
  160. }
  161. if (CLOSEDIR (dirp))
  162. return 0;
  163. return highest_version;
  164. }
  165. /* If BACKUP is a numbered backup of BASE, return its version number;
  166. otherwise return 0. BASE_LENGTH is the length of BASE.
  167. */
  168. static int
  169. version_number (base, backup, base_length)
  170. const char *base;
  171. const char *backup;
  172. size_t base_length;
  173. {
  174. int version;
  175. const char *p;
  176. version = 0;
  177. if (strncmp (base, backup, base_length) == 0
  178. && backup[base_length] == '.'
  179. && backup[base_length + 1] == '~')
  180. {
  181. for (p = &backup[base_length + 2]; ISDIGIT (*p); ++p)
  182. version = version * 10 + *p - '0';
  183. if (p[0] != '~' || p[1])
  184. version = 0;
  185. }
  186. return version;
  187. }
  188. #endif /* HAVE_DIR */
  189. static const char * const backup_args[] =
  190. {
  191. "never", "simple", "nil", "existing", "t", "numbered", 0
  192. };
  193. static const enum backup_type backup_types[] =
  194. {
  195. simple, simple, numbered_existing, numbered_existing, numbered, numbered
  196. };
  197. /* Return the type of backup indicated by VERSION.
  198. Unique abbreviations are accepted. */
  199. enum backup_type
  200. get_version (version)
  201. const char *version;
  202. {
  203. int i;
  204. if (version == 0 || *version == 0)
  205. return numbered_existing;
  206. i = argmatch (version, backup_args);
  207. if (i < 0)
  208. {
  209. invalid_arg ("version control type", version, i);
  210. exit (2);
  211. }
  212. return backup_types[i];
  213. }