1 /* $OpenBSD: backupfile.c,v 1.18 2004/08/05 21:47:24 deraadt Exp $ */
2
3 /*
4 * backupfile.c -- make Emacs style backup file names Copyright (C) 1990 Free
5 * Software Foundation, Inc.
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * without restriction.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE.
13 */
14
15 /*
16 * David MacKenzie <djm@ai.mit.edu>. Some algorithms adapted from GNU Emacs.
17 */
18
19 #ifndef lint
20 static const char rcsid[] = "$OpenBSD: backupfile.c,v 1.18 2004/08/05 21:47:24 deraadt Exp $";
21 #endif /* not lint */
22
23 #include <ctype.h>
24 #include <dirent.h>
25 #include <libgen.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include "backupfile.h"
32
33
34 #define ISDIGIT(c) (isascii (c) && isdigit (c))
35
36 /* Which type of backup file names are generated. */
37 enum backup_type backup_type = none;
38
39 /*
40 * The extension added to file names to produce a simple (as opposed to
41 * numbered) backup file name.
42 */
43 char *simple_backup_suffix = "~";
44
45 static char *concat(const char *, const char *);
46 static char *make_version_name(const char *, int);
47 static int max_backup_version(const char *, const char *);
48 static int version_number(const char *, const char *, int);
49 static int argmatch(const char *, const char **);
50 static void invalid_arg(const char *, const char *, int);
51
52 /*
53 * Return the name of the new backup file for file FILE, allocated with
54 * malloc. Return 0 if out of memory. FILE must not end with a '/' unless it
55 * is the root directory. Do not call this function if backup_type == none.
56 */
57 char *
find_backup_file_name(const char * file)58 find_backup_file_name(const char *file)
59 {
60 char *dir, *base_versions;
61 int highest_backup;
62
63 if (backup_type == simple)
64 return concat(file, simple_backup_suffix);
65 base_versions = concat(basename(file), ".~");
66 if (base_versions == NULL)
67 return NULL;
68 dir = dirname(file);
69 if (dir == NULL) {
70 free(base_versions);
71 return NULL;
72 }
73 highest_backup = max_backup_version(base_versions, dir);
74 free(base_versions);
75 if (backup_type == numbered_existing && highest_backup == 0)
76 return concat(file, simple_backup_suffix);
77 return make_version_name(file, highest_backup + 1);
78 }
79
80 /*
81 * Return the number of the highest-numbered backup file for file FILE in
82 * directory DIR. If there are no numbered backups of FILE in DIR, or an
83 * error occurs reading DIR, return 0. FILE should already have ".~" appended
84 * to it.
85 */
86 static int
max_backup_version(const char * file,const char * dir)87 max_backup_version(const char *file, const char *dir)
88 {
89 DIR *dirp;
90 struct dirent *dp;
91 int highest_version, this_version, file_name_length;
92
93 dirp = opendir(dir);
94 if (dirp == NULL)
95 return 0;
96
97 highest_version = 0;
98 file_name_length = strlen(file);
99
100 while ((dp = readdir(dirp)) != NULL) {
101 if (dp->d_namlen <= file_name_length)
102 continue;
103
104 this_version = version_number(file, dp->d_name, file_name_length);
105 if (this_version > highest_version)
106 highest_version = this_version;
107 }
108 closedir(dirp);
109 return highest_version;
110 }
111
112 /*
113 * Return a string, allocated with malloc, containing "FILE.~VERSION~".
114 * Return 0 if out of memory.
115 */
116 static char *
make_version_name(const char * file,int version)117 make_version_name(const char *file, int version)
118 {
119 char *backup_name;
120
121 if (asprintf(&backup_name, "%s.~%d~", file, version) == -1)
122 return NULL;
123 return backup_name;
124 }
125
126 /*
127 * If BACKUP is a numbered backup of BASE, return its version number;
128 * otherwise return 0. BASE_LENGTH is the length of BASE. BASE should
129 * already have ".~" appended to it.
130 */
131 static int
version_number(const char * base,const char * backup,int base_length)132 version_number(const char *base, const char *backup, int base_length)
133 {
134 int version;
135 const char *p;
136
137 version = 0;
138 if (!strncmp(base, backup, base_length) && ISDIGIT(backup[base_length])) {
139 for (p = &backup[base_length]; ISDIGIT(*p); ++p)
140 version = version * 10 + *p - '0';
141 if (p[0] != '~' || p[1])
142 version = 0;
143 }
144 return version;
145 }
146
147 /*
148 * Return the newly-allocated concatenation of STR1 and STR2. If out of
149 * memory, return 0.
150 */
151 static char *
concat(const char * str1,const char * str2)152 concat(const char *str1, const char *str2)
153 {
154 char *newstr;
155
156 if (asprintf(&newstr, "%s%s", str1, str2) == -1)
157 return NULL;
158 return newstr;
159 }
160
161 /*
162 * If ARG is an unambiguous match for an element of the null-terminated array
163 * OPTLIST, return the index in OPTLIST of the matched element, else -1 if it
164 * does not match any element or -2 if it is ambiguous (is a prefix of more
165 * than one element).
166 */
167 static int
argmatch(const char * arg,const char ** optlist)168 argmatch(const char *arg, const char **optlist)
169 {
170 int i; /* Temporary index in OPTLIST. */
171 size_t arglen; /* Length of ARG. */
172 int matchind = -1; /* Index of first nonexact match. */
173 int ambiguous = 0; /* If nonzero, multiple nonexact match(es). */
174
175 arglen = strlen(arg);
176
177 /* Test all elements for either exact match or abbreviated matches. */
178 for (i = 0; optlist[i]; i++) {
179 if (!strncmp(optlist[i], arg, arglen)) {
180 if (strlen(optlist[i]) == arglen)
181 /* Exact match found. */
182 return i;
183 else if (matchind == -1)
184 /* First nonexact match found. */
185 matchind = i;
186 else
187 /* Second nonexact match found. */
188 ambiguous = 1;
189 }
190 }
191 if (ambiguous)
192 return -2;
193 else
194 return matchind;
195 }
196
197 /*
198 * Error reporting for argmatch. KIND is a description of the type of entity
199 * that was being matched. VALUE is the invalid value that was given. PROBLEM
200 * is the return value from argmatch.
201 */
202 static void
invalid_arg(const char * kind,const char * value,int problem)203 invalid_arg(const char *kind, const char *value, int problem)
204 {
205 fprintf(stderr, "patch: ");
206 if (problem == -1)
207 fprintf(stderr, "invalid");
208 else /* Assume -2. */
209 fprintf(stderr, "ambiguous");
210 fprintf(stderr, " %s `%s'\n", kind, value);
211 }
212
213 static const char *backup_args[] = {
214 "never", "simple", "nil", "existing", "t", "numbered", 0
215 };
216
217 static enum backup_type backup_types[] = {
218 simple, simple, numbered_existing,
219 numbered_existing, numbered, numbered
220 };
221
222 /*
223 * Return the type of backup indicated by VERSION. Unique abbreviations are
224 * accepted.
225 */
226 enum backup_type
get_version(const char * version)227 get_version(const char *version)
228 {
229 int i;
230
231 if (version == NULL || *version == '\0')
232 return numbered_existing;
233 i = argmatch(version, backup_args);
234 if (i >= 0)
235 return backup_types[i];
236 invalid_arg("version control type", version, i);
237 exit(2);
238 }
239