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