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