backupfile.c revision 1.3 1 1.1 cgd /* backupfile.c -- make Emacs style backup file names
2 1.1 cgd Copyright (C) 1990 Free Software Foundation, Inc.
3 1.1 cgd
4 1.1 cgd This program is free software; you can redistribute it and/or modify
5 1.1 cgd it without restriction.
6 1.1 cgd
7 1.1 cgd This program is distributed in the hope that it will be useful,
8 1.1 cgd but WITHOUT ANY WARRANTY; without even the implied warranty of
9 1.1 cgd MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */
10 1.1 cgd
11 1.1 cgd /* David MacKenzie <djm (at) ai.mit.edu>.
12 1.1 cgd Some algorithms adapted from GNU Emacs. */
13 1.1 cgd
14 1.2 mycroft #ifndef lint
15 1.3 cgd static char rcsid[] = "$Id: backupfile.c,v 1.3 1994/12/24 17:30:13 cgd Exp $";
16 1.2 mycroft #endif /* not lint */
17 1.2 mycroft
18 1.1 cgd #include <stdio.h>
19 1.3 cgd #include <stdlib.h>
20 1.3 cgd #include <string.h>
21 1.1 cgd #include <ctype.h>
22 1.1 cgd #include <sys/types.h>
23 1.1 cgd #include "backupfile.h"
24 1.1 cgd #include "config.h"
25 1.1 cgd
26 1.1 cgd #ifdef DIRENT
27 1.1 cgd #include <dirent.h>
28 1.1 cgd #ifdef direct
29 1.1 cgd #undef direct
30 1.1 cgd #endif
31 1.1 cgd #define direct dirent
32 1.1 cgd #define NLENGTH(direct) (strlen((direct)->d_name))
33 1.1 cgd #else /* !DIRENT */
34 1.1 cgd #define NLENGTH(direct) ((direct)->d_namlen)
35 1.1 cgd #ifdef USG
36 1.1 cgd #ifdef SYSNDIR
37 1.1 cgd #include <sys/ndir.h>
38 1.1 cgd #else /* !SYSNDIR */
39 1.1 cgd #include <ndir.h>
40 1.1 cgd #endif /* !SYSNDIR */
41 1.1 cgd #else /* !USG */
42 1.1 cgd #ifdef SYSDIR
43 1.1 cgd #include <sys/dir.h>
44 1.1 cgd #endif /* SYSDIR */
45 1.1 cgd #endif /* !USG */
46 1.1 cgd #endif /* !DIRENT */
47 1.1 cgd
48 1.1 cgd #ifndef isascii
49 1.1 cgd #define ISDIGIT(c) (isdigit ((unsigned char) (c)))
50 1.1 cgd #else
51 1.1 cgd #define ISDIGIT(c) (isascii (c) && isdigit (c))
52 1.1 cgd #endif
53 1.1 cgd
54 1.1 cgd #if defined (HAVE_UNISTD_H)
55 1.1 cgd #include <unistd.h>
56 1.1 cgd #endif
57 1.1 cgd
58 1.1 cgd #if defined (_POSIX_VERSION)
59 1.1 cgd /* POSIX does not require that the d_ino field be present, and some
60 1.1 cgd systems do not provide it. */
61 1.1 cgd #define REAL_DIR_ENTRY(dp) 1
62 1.1 cgd #else
63 1.1 cgd #define REAL_DIR_ENTRY(dp) ((dp)->d_ino != 0)
64 1.1 cgd #endif
65 1.1 cgd
66 1.1 cgd /* Which type of backup file names are generated. */
67 1.1 cgd enum backup_type backup_type = none;
68 1.1 cgd
69 1.1 cgd /* The extension added to file names to produce a simple (as opposed
70 1.1 cgd to numbered) backup file name. */
71 1.1 cgd char *simple_backup_suffix = "~";
72 1.1 cgd
73 1.1 cgd char *basename ();
74 1.1 cgd char *dirname ();
75 1.1 cgd static char *concat ();
76 1.1 cgd char *find_backup_file_name ();
77 1.1 cgd static char *make_version_name ();
78 1.1 cgd static int max_backup_version ();
79 1.1 cgd static int version_number ();
80 1.1 cgd
81 1.1 cgd #ifndef NODIR
82 1.1 cgd /* Return the name of the new backup file for file FILE,
83 1.1 cgd allocated with malloc. Return 0 if out of memory.
84 1.1 cgd FILE must not end with a '/' unless it is the root directory.
85 1.1 cgd Do not call this function if backup_type == none. */
86 1.1 cgd
87 1.1 cgd char *
88 1.1 cgd find_backup_file_name (file)
89 1.1 cgd char *file;
90 1.1 cgd {
91 1.1 cgd char *dir;
92 1.1 cgd char *base_versions;
93 1.1 cgd int highest_backup;
94 1.1 cgd
95 1.1 cgd if (backup_type == simple)
96 1.1 cgd return concat (file, simple_backup_suffix);
97 1.1 cgd base_versions = concat (basename (file), ".~");
98 1.1 cgd if (base_versions == 0)
99 1.1 cgd return 0;
100 1.1 cgd dir = dirname (file);
101 1.1 cgd if (dir == 0)
102 1.1 cgd {
103 1.1 cgd free (base_versions);
104 1.1 cgd return 0;
105 1.1 cgd }
106 1.1 cgd highest_backup = max_backup_version (base_versions, dir);
107 1.1 cgd free (base_versions);
108 1.1 cgd free (dir);
109 1.1 cgd if (backup_type == numbered_existing && highest_backup == 0)
110 1.1 cgd return concat (file, simple_backup_suffix);
111 1.1 cgd return make_version_name (file, highest_backup + 1);
112 1.1 cgd }
113 1.1 cgd
114 1.1 cgd /* Return the number of the highest-numbered backup file for file
115 1.1 cgd FILE in directory DIR. If there are no numbered backups
116 1.1 cgd of FILE in DIR, or an error occurs reading DIR, return 0.
117 1.1 cgd FILE should already have ".~" appended to it. */
118 1.1 cgd
119 1.1 cgd static int
120 1.1 cgd max_backup_version (file, dir)
121 1.1 cgd char *file, *dir;
122 1.1 cgd {
123 1.1 cgd DIR *dirp;
124 1.1 cgd struct direct *dp;
125 1.1 cgd int highest_version;
126 1.1 cgd int this_version;
127 1.1 cgd int file_name_length;
128 1.1 cgd
129 1.1 cgd dirp = opendir (dir);
130 1.1 cgd if (!dirp)
131 1.1 cgd return 0;
132 1.1 cgd
133 1.1 cgd highest_version = 0;
134 1.1 cgd file_name_length = strlen (file);
135 1.1 cgd
136 1.1 cgd while ((dp = readdir (dirp)) != 0)
137 1.1 cgd {
138 1.1 cgd if (!REAL_DIR_ENTRY (dp) || NLENGTH (dp) <= file_name_length)
139 1.1 cgd continue;
140 1.1 cgd
141 1.1 cgd this_version = version_number (file, dp->d_name, file_name_length);
142 1.1 cgd if (this_version > highest_version)
143 1.1 cgd highest_version = this_version;
144 1.1 cgd }
145 1.1 cgd closedir (dirp);
146 1.1 cgd return highest_version;
147 1.1 cgd }
148 1.1 cgd
149 1.1 cgd /* Return a string, allocated with malloc, containing
150 1.1 cgd "FILE.~VERSION~". Return 0 if out of memory. */
151 1.1 cgd
152 1.1 cgd static char *
153 1.1 cgd make_version_name (file, version)
154 1.1 cgd char *file;
155 1.1 cgd int version;
156 1.1 cgd {
157 1.1 cgd char *backup_name;
158 1.1 cgd
159 1.1 cgd backup_name = malloc (strlen (file) + 16);
160 1.1 cgd if (backup_name == 0)
161 1.1 cgd return 0;
162 1.1 cgd sprintf (backup_name, "%s.~%d~", file, version);
163 1.1 cgd return backup_name;
164 1.1 cgd }
165 1.1 cgd
166 1.1 cgd /* If BACKUP is a numbered backup of BASE, return its version number;
167 1.1 cgd otherwise return 0. BASE_LENGTH is the length of BASE.
168 1.1 cgd BASE should already have ".~" appended to it. */
169 1.1 cgd
170 1.1 cgd static int
171 1.1 cgd version_number (base, backup, base_length)
172 1.1 cgd char *base;
173 1.1 cgd char *backup;
174 1.1 cgd int base_length;
175 1.1 cgd {
176 1.1 cgd int version;
177 1.1 cgd char *p;
178 1.1 cgd
179 1.1 cgd version = 0;
180 1.1 cgd if (!strncmp (base, backup, base_length) && ISDIGIT (backup[base_length]))
181 1.1 cgd {
182 1.1 cgd for (p = &backup[base_length]; ISDIGIT (*p); ++p)
183 1.1 cgd version = version * 10 + *p - '0';
184 1.1 cgd if (p[0] != '~' || p[1])
185 1.1 cgd version = 0;
186 1.1 cgd }
187 1.1 cgd return version;
188 1.1 cgd }
189 1.1 cgd
190 1.1 cgd /* Return the newly-allocated concatenation of STR1 and STR2.
191 1.1 cgd If out of memory, return 0. */
192 1.1 cgd
193 1.1 cgd static char *
194 1.1 cgd concat (str1, str2)
195 1.1 cgd char *str1, *str2;
196 1.1 cgd {
197 1.1 cgd char *newstr;
198 1.1 cgd char str1_length = strlen (str1);
199 1.1 cgd
200 1.1 cgd newstr = malloc (str1_length + strlen (str2) + 1);
201 1.1 cgd if (newstr == 0)
202 1.1 cgd return 0;
203 1.1 cgd strcpy (newstr, str1);
204 1.1 cgd strcpy (newstr + str1_length, str2);
205 1.1 cgd return newstr;
206 1.1 cgd }
207 1.1 cgd
208 1.1 cgd /* Return NAME with any leading path stripped off. */
209 1.1 cgd
210 1.1 cgd char *
211 1.1 cgd basename (name)
212 1.1 cgd char *name;
213 1.1 cgd {
214 1.1 cgd char *base;
215 1.1 cgd
216 1.1 cgd base = rindex (name, '/');
217 1.1 cgd return base ? base + 1 : name;
218 1.1 cgd }
219 1.1 cgd
220 1.1 cgd /* Return the leading directories part of PATH,
221 1.1 cgd allocated with malloc. If out of memory, return 0.
222 1.1 cgd Assumes that trailing slashes have already been
223 1.1 cgd removed. */
224 1.1 cgd
225 1.1 cgd char *
226 1.1 cgd dirname (path)
227 1.1 cgd char *path;
228 1.1 cgd {
229 1.1 cgd char *newpath;
230 1.1 cgd char *slash;
231 1.1 cgd int length; /* Length of result, not including NUL. */
232 1.1 cgd
233 1.1 cgd slash = rindex (path, '/');
234 1.1 cgd if (slash == 0)
235 1.1 cgd {
236 1.1 cgd /* File is in the current directory. */
237 1.1 cgd path = ".";
238 1.1 cgd length = 1;
239 1.1 cgd }
240 1.1 cgd else
241 1.1 cgd {
242 1.1 cgd /* Remove any trailing slashes from result. */
243 1.1 cgd while (slash > path && *slash == '/')
244 1.1 cgd --slash;
245 1.1 cgd
246 1.1 cgd length = slash - path + 1;
247 1.1 cgd }
248 1.1 cgd newpath = malloc (length + 1);
249 1.1 cgd if (newpath == 0)
250 1.1 cgd return 0;
251 1.1 cgd strncpy (newpath, path, length);
252 1.1 cgd newpath[length] = 0;
253 1.1 cgd return newpath;
254 1.1 cgd }
255 1.1 cgd
256 1.1 cgd /* If ARG is an unambiguous match for an element of the
257 1.1 cgd null-terminated array OPTLIST, return the index in OPTLIST
258 1.1 cgd of the matched element, else -1 if it does not match any element
259 1.1 cgd or -2 if it is ambiguous (is a prefix of more than one element). */
260 1.1 cgd
261 1.1 cgd int
262 1.1 cgd argmatch (arg, optlist)
263 1.1 cgd char *arg;
264 1.1 cgd char **optlist;
265 1.1 cgd {
266 1.1 cgd int i; /* Temporary index in OPTLIST. */
267 1.1 cgd int arglen; /* Length of ARG. */
268 1.1 cgd int matchind = -1; /* Index of first nonexact match. */
269 1.1 cgd int ambiguous = 0; /* If nonzero, multiple nonexact match(es). */
270 1.1 cgd
271 1.1 cgd arglen = strlen (arg);
272 1.1 cgd
273 1.1 cgd /* Test all elements for either exact match or abbreviated matches. */
274 1.1 cgd for (i = 0; optlist[i]; i++)
275 1.1 cgd {
276 1.1 cgd if (!strncmp (optlist[i], arg, arglen))
277 1.1 cgd {
278 1.1 cgd if (strlen (optlist[i]) == arglen)
279 1.1 cgd /* Exact match found. */
280 1.1 cgd return i;
281 1.1 cgd else if (matchind == -1)
282 1.1 cgd /* First nonexact match found. */
283 1.1 cgd matchind = i;
284 1.1 cgd else
285 1.1 cgd /* Second nonexact match found. */
286 1.1 cgd ambiguous = 1;
287 1.1 cgd }
288 1.1 cgd }
289 1.1 cgd if (ambiguous)
290 1.1 cgd return -2;
291 1.1 cgd else
292 1.1 cgd return matchind;
293 1.1 cgd }
294 1.1 cgd
295 1.1 cgd /* Error reporting for argmatch.
296 1.1 cgd KIND is a description of the type of entity that was being matched.
297 1.1 cgd VALUE is the invalid value that was given.
298 1.1 cgd PROBLEM is the return value from argmatch. */
299 1.1 cgd
300 1.1 cgd void
301 1.1 cgd invalid_arg (kind, value, problem)
302 1.1 cgd char *kind;
303 1.1 cgd char *value;
304 1.1 cgd int problem;
305 1.1 cgd {
306 1.1 cgd fprintf (stderr, "patch: ");
307 1.1 cgd if (problem == -1)
308 1.1 cgd fprintf (stderr, "invalid");
309 1.1 cgd else /* Assume -2. */
310 1.1 cgd fprintf (stderr, "ambiguous");
311 1.1 cgd fprintf (stderr, " %s `%s'\n", kind, value);
312 1.1 cgd }
313 1.1 cgd
314 1.1 cgd static char *backup_args[] =
315 1.1 cgd {
316 1.1 cgd "never", "simple", "nil", "existing", "t", "numbered", 0
317 1.1 cgd };
318 1.1 cgd
319 1.1 cgd static enum backup_type backup_types[] =
320 1.1 cgd {
321 1.1 cgd simple, simple, numbered_existing, numbered_existing, numbered, numbered
322 1.1 cgd };
323 1.1 cgd
324 1.1 cgd /* Return the type of backup indicated by VERSION.
325 1.1 cgd Unique abbreviations are accepted. */
326 1.1 cgd
327 1.1 cgd enum backup_type
328 1.1 cgd get_version (version)
329 1.1 cgd char *version;
330 1.1 cgd {
331 1.1 cgd int i;
332 1.1 cgd
333 1.1 cgd if (version == 0 || *version == 0)
334 1.1 cgd return numbered_existing;
335 1.1 cgd i = argmatch (version, backup_args);
336 1.1 cgd if (i >= 0)
337 1.1 cgd return backup_types[i];
338 1.1 cgd invalid_arg ("version control type", version, i);
339 1.1 cgd exit (1);
340 1.1 cgd }
341 1.1 cgd #endif /* NODIR */
342