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