xref: /openbsd-src/usr.bin/patch/backupfile.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: backupfile.c,v 1.19 2006/03/11 19:41:30 otto 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.19 2006/03/11 19:41:30 otto 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 *, size_t);
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 *
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
87 max_backup_version(const char *file, const char *dir)
88 {
89 	DIR	*dirp;
90 	struct dirent	*dp;
91 	int	highest_version, this_version;
92 	size_t	file_name_length;
93 
94 	dirp = opendir(dir);
95 	if (dirp == NULL)
96 		return 0;
97 
98 	highest_version = 0;
99 	file_name_length = strlen(file);
100 
101 	while ((dp = readdir(dirp)) != NULL) {
102 		if (dp->d_namlen <= file_name_length)
103 			continue;
104 
105 		this_version = version_number(file, dp->d_name, file_name_length);
106 		if (this_version > highest_version)
107 			highest_version = this_version;
108 	}
109 	closedir(dirp);
110 	return highest_version;
111 }
112 
113 /*
114  * Return a string, allocated with malloc, containing "FILE.~VERSION~".
115  * Return 0 if out of memory.
116  */
117 static char *
118 make_version_name(const char *file, int version)
119 {
120 	char	*backup_name;
121 
122 	if (asprintf(&backup_name, "%s.~%d~", file, version) == -1)
123 		return NULL;
124 	return backup_name;
125 }
126 
127 /*
128  * If BACKUP is a numbered backup of BASE, return its version number;
129  * otherwise return 0.  BASE_LENGTH is the length of BASE. BASE should
130  * already have ".~" appended to it.
131  */
132 static int
133 version_number(const char *base, const char *backup, size_t base_length)
134 {
135 	int		version;
136 	const char	*p;
137 
138 	version = 0;
139 	if (!strncmp(base, backup, base_length) && ISDIGIT(backup[base_length])) {
140 		for (p = &backup[base_length]; ISDIGIT(*p); ++p)
141 			version = version * 10 + *p - '0';
142 		if (p[0] != '~' || p[1])
143 			version = 0;
144 	}
145 	return version;
146 }
147 
148 /*
149  * Return the newly-allocated concatenation of STR1 and STR2. If out of
150  * memory, return 0.
151  */
152 static char  *
153 concat(const char *str1, const char *str2)
154 {
155 	char	*newstr;
156 
157 	if (asprintf(&newstr, "%s%s", str1, str2) == -1)
158 		return NULL;
159 	return newstr;
160 }
161 
162 /*
163  * If ARG is an unambiguous match for an element of the null-terminated array
164  * OPTLIST, return the index in OPTLIST of the matched element, else -1 if it
165  * does not match any element or -2 if it is ambiguous (is a prefix of more
166  * than one element).
167  */
168 static int
169 argmatch(const char *arg, const char **optlist)
170 {
171 	int	i;	/* Temporary index in OPTLIST. */
172 	size_t	arglen;	/* Length of ARG. */
173 	int	matchind = -1;	/* Index of first nonexact match. */
174 	int	ambiguous = 0;	/* If nonzero, multiple nonexact match(es). */
175 
176 	arglen = strlen(arg);
177 
178 	/* Test all elements for either exact match or abbreviated matches.  */
179 	for (i = 0; optlist[i]; i++) {
180 		if (!strncmp(optlist[i], arg, arglen)) {
181 			if (strlen(optlist[i]) == arglen)
182 				/* Exact match found.  */
183 				return i;
184 			else if (matchind == -1)
185 				/* First nonexact match found.  */
186 				matchind = i;
187 			else
188 				/* Second nonexact match found.  */
189 				ambiguous = 1;
190 		}
191 	}
192 	if (ambiguous)
193 		return -2;
194 	else
195 		return matchind;
196 }
197 
198 /*
199  * Error reporting for argmatch. KIND is a description of the type of entity
200  * that was being matched. VALUE is the invalid value that was given. PROBLEM
201  * is the return value from argmatch.
202  */
203 static void
204 invalid_arg(const char *kind, const char *value, int problem)
205 {
206 	fprintf(stderr, "patch: ");
207 	if (problem == -1)
208 		fprintf(stderr, "invalid");
209 	else			/* Assume -2. */
210 		fprintf(stderr, "ambiguous");
211 	fprintf(stderr, " %s `%s'\n", kind, value);
212 }
213 
214 static const char *backup_args[] = {
215 	"never", "simple", "nil", "existing", "t", "numbered", 0
216 };
217 
218 static enum backup_type backup_types[] = {
219 	simple, simple, numbered_existing,
220 	numbered_existing, numbered, numbered
221 };
222 
223 /*
224  * Return the type of backup indicated by VERSION. Unique abbreviations are
225  * accepted.
226  */
227 enum backup_type
228 get_version(const char *version)
229 {
230 	int	i;
231 
232 	if (version == NULL || *version == '\0')
233 		return numbered_existing;
234 	i = argmatch(version, backup_args);
235 	if (i >= 0)
236 		return backup_types[i];
237 	invalid_arg("version control type", version, i);
238 	exit(2);
239 }
240