1246074Sgabor/*-
2246074Sgabor * Copyright (C) 1990 Free Software Foundation, Inc.
3246074Sgabor *
4246074Sgabor * This program is free software; you can redistribute it and/or modify it
5246074Sgabor * without restriction.
6246074Sgabor *
7246074Sgabor * This program is distributed in the hope that it will be useful, but WITHOUT
8246074Sgabor * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9246074Sgabor * FITNESS FOR A PARTICULAR PURPOSE.
10246074Sgabor *
11246074Sgabor * backupfile.c -- make Emacs style backup file names
12246074Sgabor *
13246074Sgabor * David MacKenzie <djm@ai.mit.edu>. Some algorithms adapted from GNU Emacs.
14246074Sgabor *
15246091Sdelphij * $OpenBSD: backupfile.c,v 1.20 2009/10/27 23:59:41 deraadt Exp $
16246091Sdelphij * $FreeBSD$
17246074Sgabor */
18246074Sgabor
19246074Sgabor#include <ctype.h>
20246074Sgabor#include <dirent.h>
21246074Sgabor#include <libgen.h>
22246074Sgabor#include <stdio.h>
23246074Sgabor#include <stdlib.h>
24246074Sgabor#include <string.h>
25246074Sgabor#include <unistd.h>
26246074Sgabor
27246074Sgabor#include "backupfile.h"
28246074Sgabor
29246074Sgabor
30246074Sgabor#define ISDIGIT(c) (isascii ((unsigned char)c) && isdigit ((unsigned char)c))
31246074Sgabor
32246074Sgabor/* Which type of backup file names are generated. */
33246074Sgaborenum backup_type backup_type = none;
34246074Sgabor
35246074Sgabor/*
36246074Sgabor * The extension added to file names to produce a simple (as opposed to
37246074Sgabor * numbered) backup file name.
38246074Sgabor */
39246074Sgaborconst char	*simple_backup_suffix = "~";
40246074Sgabor
41246074Sgaborstatic char	*concat(const char *, const char *);
42246074Sgaborstatic char	*make_version_name(const char *, int);
43246074Sgaborstatic int	max_backup_version(const char *, const char *);
44246074Sgaborstatic int	version_number(const char *, const char *, size_t);
45246074Sgaborstatic int	argmatch(const char *, const char **);
46246074Sgaborstatic void	invalid_arg(const char *, const char *, int);
47246074Sgabor
48246074Sgabor/*
49246074Sgabor * Return the name of the new backup file for file FILE, allocated with
50246074Sgabor * malloc.  Return 0 if out of memory. FILE must not end with a '/' unless it
51246074Sgabor * is the root directory. Do not call this function if backup_type == none.
52246074Sgabor */
53246074Sgaborchar *
54246074Sgaborfind_backup_file_name(const char *file)
55246074Sgabor{
56246074Sgabor	char	*dir, *base_versions, *tmp_file;
57246074Sgabor	int	highest_backup;
58246074Sgabor
59246074Sgabor	if (backup_type == simple)
60246074Sgabor		return concat(file, simple_backup_suffix);
61246074Sgabor	tmp_file = strdup(file);
62246074Sgabor	if (tmp_file == NULL)
63246074Sgabor		return NULL;
64246074Sgabor	base_versions = concat(basename(tmp_file), ".~");
65246074Sgabor	free(tmp_file);
66246074Sgabor	if (base_versions == NULL)
67246074Sgabor		return NULL;
68246074Sgabor	tmp_file = strdup(file);
69246074Sgabor	if (tmp_file == NULL) {
70246074Sgabor		free(base_versions);
71246074Sgabor		return NULL;
72246074Sgabor	}
73246074Sgabor	dir = dirname(tmp_file);
74246074Sgabor	if (dir == NULL) {
75246074Sgabor		free(base_versions);
76246074Sgabor		free(tmp_file);
77246074Sgabor		return NULL;
78246074Sgabor	}
79246074Sgabor	highest_backup = max_backup_version(base_versions, dir);
80246074Sgabor	free(base_versions);
81246074Sgabor	free(tmp_file);
82246074Sgabor	if (backup_type == numbered_existing && highest_backup == 0)
83246074Sgabor		return concat(file, simple_backup_suffix);
84246074Sgabor	return make_version_name(file, highest_backup + 1);
85246074Sgabor}
86246074Sgabor
87246074Sgabor/*
88246074Sgabor * Return the number of the highest-numbered backup file for file FILE in
89246074Sgabor * directory DIR.  If there are no numbered backups of FILE in DIR, or an
90246074Sgabor * error occurs reading DIR, return 0. FILE should already have ".~" appended
91246074Sgabor * to it.
92246074Sgabor */
93246074Sgaborstatic int
94246074Sgabormax_backup_version(const char *file, const char *dir)
95246074Sgabor{
96246074Sgabor	DIR	*dirp;
97246074Sgabor	struct dirent	*dp;
98246074Sgabor	int	highest_version, this_version;
99246074Sgabor	size_t	file_name_length;
100246074Sgabor
101246074Sgabor	dirp = opendir(dir);
102246074Sgabor	if (dirp == NULL)
103246074Sgabor		return 0;
104246074Sgabor
105246074Sgabor	highest_version = 0;
106246074Sgabor	file_name_length = strlen(file);
107246074Sgabor
108246074Sgabor	while ((dp = readdir(dirp)) != NULL) {
109246074Sgabor		if (dp->d_namlen <= file_name_length)
110246074Sgabor			continue;
111246074Sgabor
112246074Sgabor		this_version = version_number(file, dp->d_name, file_name_length);
113246074Sgabor		if (this_version > highest_version)
114246074Sgabor			highest_version = this_version;
115246074Sgabor	}
116246074Sgabor	closedir(dirp);
117246074Sgabor	return highest_version;
118246074Sgabor}
119246074Sgabor
120246074Sgabor/*
121246074Sgabor * Return a string, allocated with malloc, containing "FILE.~VERSION~".
122246074Sgabor * Return 0 if out of memory.
123246074Sgabor */
124246074Sgaborstatic char *
125246074Sgabormake_version_name(const char *file, int version)
126246074Sgabor{
127246074Sgabor	char	*backup_name;
128246074Sgabor
129246074Sgabor	if (asprintf(&backup_name, "%s.~%d~", file, version) == -1)
130246074Sgabor		return NULL;
131246074Sgabor	return backup_name;
132246074Sgabor}
133246074Sgabor
134246074Sgabor/*
135246074Sgabor * If BACKUP is a numbered backup of BASE, return its version number;
136246074Sgabor * otherwise return 0.  BASE_LENGTH is the length of BASE. BASE should
137246074Sgabor * already have ".~" appended to it.
138246074Sgabor */
139246074Sgaborstatic int
140246074Sgaborversion_number(const char *base, const char *backup, size_t base_length)
141246074Sgabor{
142246074Sgabor	int		version;
143246074Sgabor	const char	*p;
144246074Sgabor
145246074Sgabor	version = 0;
146246074Sgabor	if (!strncmp(base, backup, base_length) && ISDIGIT(backup[base_length])) {
147246074Sgabor		for (p = &backup[base_length]; ISDIGIT(*p); ++p)
148246074Sgabor			version = version * 10 + *p - '0';
149246074Sgabor		if (p[0] != '~' || p[1])
150246074Sgabor			version = 0;
151246074Sgabor	}
152246074Sgabor	return version;
153246074Sgabor}
154246074Sgabor
155246074Sgabor/*
156246074Sgabor * Return the newly-allocated concatenation of STR1 and STR2. If out of
157246074Sgabor * memory, return 0.
158246074Sgabor */
159246074Sgaborstatic char  *
160246074Sgaborconcat(const char *str1, const char *str2)
161246074Sgabor{
162246074Sgabor	char	*newstr;
163246074Sgabor
164246074Sgabor	if (asprintf(&newstr, "%s%s", str1, str2) == -1)
165246074Sgabor		return NULL;
166246074Sgabor	return newstr;
167246074Sgabor}
168246074Sgabor
169246074Sgabor/*
170246074Sgabor * If ARG is an unambiguous match for an element of the null-terminated array
171246074Sgabor * OPTLIST, return the index in OPTLIST of the matched element, else -1 if it
172246074Sgabor * does not match any element or -2 if it is ambiguous (is a prefix of more
173246074Sgabor * than one element).
174246074Sgabor */
175246074Sgaborstatic int
176246074Sgaborargmatch(const char *arg, const char **optlist)
177246074Sgabor{
178246074Sgabor	int	i;	/* Temporary index in OPTLIST. */
179246074Sgabor	size_t	arglen;	/* Length of ARG. */
180246074Sgabor	int	matchind = -1;	/* Index of first nonexact match. */
181246074Sgabor	int	ambiguous = 0;	/* If nonzero, multiple nonexact match(es). */
182246074Sgabor
183246074Sgabor	arglen = strlen(arg);
184246074Sgabor
185246074Sgabor	/* Test all elements for either exact match or abbreviated matches.  */
186246074Sgabor	for (i = 0; optlist[i]; i++) {
187246074Sgabor		if (!strncmp(optlist[i], arg, arglen)) {
188246074Sgabor			if (strlen(optlist[i]) == arglen)
189246074Sgabor				/* Exact match found.  */
190246074Sgabor				return i;
191246074Sgabor			else if (matchind == -1)
192246074Sgabor				/* First nonexact match found.  */
193246074Sgabor				matchind = i;
194246074Sgabor			else
195246074Sgabor				/* Second nonexact match found.  */
196246074Sgabor				ambiguous = 1;
197246074Sgabor		}
198246074Sgabor	}
199246074Sgabor	if (ambiguous)
200246074Sgabor		return -2;
201246074Sgabor	else
202246074Sgabor		return matchind;
203246074Sgabor}
204246074Sgabor
205246074Sgabor/*
206246074Sgabor * Error reporting for argmatch. KIND is a description of the type of entity
207246074Sgabor * that was being matched. VALUE is the invalid value that was given. PROBLEM
208246074Sgabor * is the return value from argmatch.
209246074Sgabor */
210246074Sgaborstatic void
211246074Sgaborinvalid_arg(const char *kind, const char *value, int problem)
212246074Sgabor{
213246074Sgabor	fprintf(stderr, "patch: ");
214246074Sgabor	if (problem == -1)
215246074Sgabor		fprintf(stderr, "invalid");
216246074Sgabor	else			/* Assume -2. */
217246074Sgabor		fprintf(stderr, "ambiguous");
218246074Sgabor	fprintf(stderr, " %s `%s'\n", kind, value);
219246074Sgabor}
220246074Sgabor
221246074Sgaborstatic const char *backup_args[] = {
222246074Sgabor	"never", "simple", "nil", "existing", "t", "numbered", 0
223246074Sgabor};
224246074Sgabor
225246074Sgaborstatic enum backup_type backup_types[] = {
226246074Sgabor	simple, simple, numbered_existing,
227246074Sgabor	numbered_existing, numbered, numbered
228246074Sgabor};
229246074Sgabor
230246074Sgabor/*
231246074Sgabor * Return the type of backup indicated by VERSION. Unique abbreviations are
232246074Sgabor * accepted.
233246074Sgabor */
234246074Sgaborenum backup_type
235246074Sgaborget_version(const char *version)
236246074Sgabor{
237246074Sgabor	int	i;
238246074Sgabor
239246074Sgabor	if (version == NULL || *version == '\0')
240246074Sgabor		return numbered_existing;
241246074Sgabor	i = argmatch(version, backup_args);
242246074Sgabor	if (i >= 0)
243246074Sgabor		return backup_types[i];
244246074Sgabor	invalid_arg("version control type", version, i);
245246074Sgabor	exit(2);
246246074Sgabor}
247