1 /* backupfile.c -- make Emacs style backup file names
2 Copyright (C) 1990 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it without restriction.
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. */
11 /* David MacKenzie <djm@ai.mit.edu>.
12 Some algorithms adapted from GNU Emacs. */
16 #include <sys/types.h>
17 #include "backupfile.h"
29 #define NLENGTH(direct) (strlen((direct)->d_name))
31 #define NLENGTH(direct) ((direct)->d_namlen)
46 #define ISDIGIT(c) (isdigit ((unsigned char) (c)))
48 #define ISDIGIT(c) (isascii (c) && isdigit (c))
51 #if defined (HAVE_UNISTD_H)
55 #if defined (_POSIX_VERSION)
56 /* POSIX does not require that the d_ino field be present, and some
57 systems do not provide it. */
58 #define REAL_DIR_ENTRY(dp) 1
60 #define REAL_DIR_ENTRY(dp) ((dp)->d_ino != 0)
63 /* Which type of backup file names are generated. */
64 enum backup_type backup_type = none;
66 /* The extension added to file names to produce a simple (as opposed
67 to numbered) backup file name. */
68 char *simple_backup_suffix = "~";
72 static char *concat ();
73 char *find_backup_file_name ();
74 static char *make_version_name ();
75 static int max_backup_version ();
76 static int version_number ();
79 /* Return the name of the new backup file for file FILE,
80 allocated with malloc. Return 0 if out of memory.
81 FILE must not end with a '/' unless it is the root directory.
82 Do not call this function if backup_type == none. */
85 find_backup_file_name (file)
92 if (backup_type == simple)
93 return concat (file, simple_backup_suffix);
94 base_versions = concat (basename (file), ".~");
95 if (base_versions == 0)
100 free (base_versions);
103 highest_backup = max_backup_version (base_versions, dir);
104 free (base_versions);
106 if (backup_type == numbered_existing && highest_backup == 0)
107 return concat (file, simple_backup_suffix);
108 return make_version_name (file, highest_backup + 1);
111 /* Return the number of the highest-numbered backup file for file
112 FILE in directory DIR. If there are no numbered backups
113 of FILE in DIR, or an error occurs reading DIR, return 0.
114 FILE should already have ".~" appended to it. */
117 max_backup_version (file, dir)
124 int file_name_length;
126 dirp = opendir (dir);
131 file_name_length = strlen (file);
133 while ((dp = readdir (dirp)) != 0)
135 if (!REAL_DIR_ENTRY (dp) || NLENGTH (dp) <= file_name_length)
138 this_version = version_number (file, dp->d_name, file_name_length);
139 if (this_version > highest_version)
140 highest_version = this_version;
143 return highest_version;
146 /* Return a string, allocated with malloc, containing
147 "FILE.~VERSION~". Return 0 if out of memory. */
150 make_version_name (file, version)
156 backup_name = malloc (strlen (file) + 16);
157 if (backup_name == 0)
159 sprintf (backup_name, "%s.~%d~", file, version);
163 /* If BACKUP is a numbered backup of BASE, return its version number;
164 otherwise return 0. BASE_LENGTH is the length of BASE.
165 BASE should already have ".~" appended to it. */
168 version_number (base, backup, base_length)
177 if (!strncmp (base, backup, base_length) && ISDIGIT (backup[base_length]))
179 for (p = &backup[base_length]; ISDIGIT (*p); ++p)
180 version = version * 10 + *p - '0';
181 if (p[0] != '~' || p[1])
187 /* Return the newly-allocated concatenation of STR1 and STR2.
188 If out of memory, return 0. */
195 char str1_length = strlen (str1);
197 newstr = malloc (str1_length + strlen (str2) + 1);
200 strcpy (newstr, str1);
201 strcpy (newstr + str1_length, str2);
205 /* Return NAME with any leading path stripped off. */
213 base = rindex (name, '/');
214 return base ? base + 1 : name;
217 /* Return the leading directories part of PATH,
218 allocated with malloc. If out of memory, return 0.
219 Assumes that trailing slashes have already been
228 int length; /* Length of result, not including NUL. */
230 slash = rindex (path, '/');
233 /* File is in the current directory. */
239 /* Remove any trailing slashes from result. */
240 while (slash > path && *slash == '/')
243 length = slash - path + 1;
245 newpath = malloc (length + 1);
248 strncpy (newpath, path, length);
253 /* If ARG is an unambiguous match for an element of the
254 null-terminated array OPTLIST, return the index in OPTLIST
255 of the matched element, else -1 if it does not match any element
256 or -2 if it is ambiguous (is a prefix of more than one element). */
259 argmatch (arg, optlist)
263 int i; /* Temporary index in OPTLIST. */
264 int arglen; /* Length of ARG. */
265 int matchind = -1; /* Index of first nonexact match. */
266 int ambiguous = 0; /* If nonzero, multiple nonexact match(es). */
268 arglen = strlen (arg);
270 /* Test all elements for either exact match or abbreviated matches. */
271 for (i = 0; optlist[i]; i++)
273 if (!strncmp (optlist[i], arg, arglen))
275 if (strlen (optlist[i]) == arglen)
276 /* Exact match found. */
278 else if (matchind == -1)
279 /* First nonexact match found. */
282 /* Second nonexact match found. */
292 /* Error reporting for argmatch.
293 KIND is a description of the type of entity that was being matched.
294 VALUE is the invalid value that was given.
295 PROBLEM is the return value from argmatch. */
298 invalid_arg (kind, value, problem)
303 fprintf (stderr, "patch: ");
305 fprintf (stderr, "invalid");
306 else /* Assume -2. */
307 fprintf (stderr, "ambiguous");
308 fprintf (stderr, " %s `%s'\n", kind, value);
311 static char *backup_args[] =
313 "never", "simple", "nil", "existing", "t", "numbered", 0
316 static enum backup_type backup_types[] =
318 simple, simple, numbered_existing, numbered_existing, numbered, numbered
321 /* Return the type of backup indicated by VERSION.
322 Unique abbreviations are accepted. */
325 get_version (version)
330 if (version == 0 || *version == 0)
331 return numbered_existing;
332 i = argmatch (version, backup_args);
334 return backup_types[i];
335 invalid_arg ("version control type", version, i);