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