xref: /minix3/usr.bin/patch/backupfile.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1757e8328SLionel Sambuc /*
2757e8328SLionel Sambuc  * $OpenBSD: backupfile.c,v 1.19 2006/03/11 19:41:30 otto Exp $
3757e8328SLionel Sambuc  * $DragonFly: src/usr.bin/patch/backupfile.c,v 1.5 2008/08/11 00:05:06 joerg Exp $
4*0a6a1f1dSLionel Sambuc  * $NetBSD: backupfile.c,v 1.15 2014/04/11 17:30:03 christos Exp $
5757e8328SLionel Sambuc  */
6757e8328SLionel Sambuc 
7757e8328SLionel Sambuc /*
8757e8328SLionel Sambuc  * backupfile.c -- make Emacs style backup file names
9757e8328SLionel Sambuc  *
10757e8328SLionel Sambuc  * Copyright (C) 1990 Free Software Foundation, Inc.
11757e8328SLionel Sambuc  *
12757e8328SLionel Sambuc  * This program is free software; you can redistribute it and/or modify it
13757e8328SLionel Sambuc  * without restriction.
14757e8328SLionel Sambuc  *
15757e8328SLionel Sambuc  * This program is distributed in the hope that it will be useful, but WITHOUT
16757e8328SLionel Sambuc  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17757e8328SLionel Sambuc  * FITNESS FOR A PARTICULAR PURPOSE.
18757e8328SLionel Sambuc  */
19757e8328SLionel Sambuc 
20757e8328SLionel Sambuc /*
21757e8328SLionel Sambuc  * David MacKenzie <djm@ai.mit.edu>. Some algorithms adapted from GNU Emacs.
22757e8328SLionel Sambuc  */
23757e8328SLionel Sambuc 
24757e8328SLionel Sambuc #include <sys/cdefs.h>
25*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: backupfile.c,v 1.15 2014/04/11 17:30:03 christos Exp $");
26757e8328SLionel Sambuc 
27757e8328SLionel Sambuc #include <ctype.h>
28757e8328SLionel Sambuc #include <dirent.h>
29757e8328SLionel Sambuc #include <libgen.h>
30757e8328SLionel Sambuc #include <stdio.h>
31757e8328SLionel Sambuc #include <stdlib.h>
32757e8328SLionel Sambuc #include <string.h>
33757e8328SLionel Sambuc #include <unistd.h>
34757e8328SLionel Sambuc 
35757e8328SLionel Sambuc #include "backupfile.h"
36757e8328SLionel Sambuc 
37757e8328SLionel Sambuc 
38757e8328SLionel Sambuc #define ISDIGIT(c) (isascii ((unsigned char)c) && isdigit ((unsigned char)c))
39757e8328SLionel Sambuc 
40757e8328SLionel Sambuc /* Which type of backup file names are generated. */
41757e8328SLionel Sambuc enum backup_type backup_type = none;
42757e8328SLionel Sambuc 
43757e8328SLionel Sambuc /*
44757e8328SLionel Sambuc  * The extension added to file names to produce a simple (as opposed to
45757e8328SLionel Sambuc  * numbered) backup file name.
46757e8328SLionel Sambuc  */
47757e8328SLionel Sambuc const char	*simple_backup_suffix = "~";
48757e8328SLionel Sambuc 
49757e8328SLionel Sambuc static char	*concat(const char *, const char *);
50757e8328SLionel Sambuc static char	*make_version_name(const char *, int);
51757e8328SLionel Sambuc static int	max_backup_version(const char *, const char *);
52757e8328SLionel Sambuc static int	version_number(const char *, const char *, size_t);
53757e8328SLionel Sambuc static int	argmatch(const char *, const char **);
54757e8328SLionel Sambuc static void	invalid_arg(const char *, const char *, int);
55757e8328SLionel Sambuc 
56757e8328SLionel Sambuc /*
57757e8328SLionel Sambuc  * Return the name of the new backup file for file FILE, allocated with
58757e8328SLionel Sambuc  * malloc.  Return 0 if out of memory. FILE must not end with a '/' unless it
59757e8328SLionel Sambuc  * is the root directory. Do not call this function if backup_type == none.
60757e8328SLionel Sambuc  */
61757e8328SLionel Sambuc char *
find_backup_file_name(const char * file)62757e8328SLionel Sambuc find_backup_file_name(const char *file)
63757e8328SLionel Sambuc {
64757e8328SLionel Sambuc 	char	*dir, *base_versions, *tmp_file;
65757e8328SLionel Sambuc 	int	highest_backup;
66757e8328SLionel Sambuc 
67757e8328SLionel Sambuc 	if (backup_type == simple)
68757e8328SLionel Sambuc 		return concat(file, simple_backup_suffix);
69757e8328SLionel Sambuc 	tmp_file = strdup(file);
70757e8328SLionel Sambuc 	if (tmp_file == NULL)
71757e8328SLionel Sambuc 		return NULL;
72757e8328SLionel Sambuc 	base_versions = concat(basename(tmp_file), ".~");
73757e8328SLionel Sambuc 	free(tmp_file);
74757e8328SLionel Sambuc 	if (base_versions == NULL)
75757e8328SLionel Sambuc 		return NULL;
76757e8328SLionel Sambuc 	tmp_file = strdup(file);
77757e8328SLionel Sambuc 	if (tmp_file == NULL) {
78757e8328SLionel Sambuc 		free(base_versions);
79757e8328SLionel Sambuc 		return NULL;
80757e8328SLionel Sambuc 	}
81757e8328SLionel Sambuc 	dir = dirname(tmp_file);
82757e8328SLionel Sambuc 	if (dir == NULL) {
83757e8328SLionel Sambuc 		free(base_versions);
84757e8328SLionel Sambuc 		free(tmp_file);
85757e8328SLionel Sambuc 		return NULL;
86757e8328SLionel Sambuc 	}
87757e8328SLionel Sambuc 	highest_backup = max_backup_version(base_versions, dir);
88757e8328SLionel Sambuc 	free(base_versions);
89757e8328SLionel Sambuc 	free(tmp_file);
90757e8328SLionel Sambuc 	if (backup_type == numbered_existing && highest_backup == 0)
91757e8328SLionel Sambuc 		return concat(file, simple_backup_suffix);
92757e8328SLionel Sambuc 	return make_version_name(file, highest_backup + 1);
93757e8328SLionel Sambuc }
94757e8328SLionel Sambuc 
95757e8328SLionel Sambuc /*
96757e8328SLionel Sambuc  * Return the number of the highest-numbered backup file for file FILE in
97757e8328SLionel Sambuc  * directory DIR.  If there are no numbered backups of FILE in DIR, or an
98757e8328SLionel Sambuc  * error occurs reading DIR, return 0. FILE should already have ".~" appended
99757e8328SLionel Sambuc  * to it.
100757e8328SLionel Sambuc  */
101757e8328SLionel Sambuc static int
max_backup_version(const char * file,const char * dir)102757e8328SLionel Sambuc max_backup_version(const char *file, const char *dir)
103757e8328SLionel Sambuc {
104757e8328SLionel Sambuc 	DIR	*dirp;
105757e8328SLionel Sambuc 	struct dirent	*dp;
106757e8328SLionel Sambuc 	int	highest_version, this_version;
107757e8328SLionel Sambuc 	size_t	file_name_length;
108757e8328SLionel Sambuc 
109757e8328SLionel Sambuc 	dirp = opendir(dir);
110757e8328SLionel Sambuc 	if (dirp == NULL)
111757e8328SLionel Sambuc 		return 0;
112757e8328SLionel Sambuc 
113757e8328SLionel Sambuc 	highest_version = 0;
114757e8328SLionel Sambuc 	file_name_length = strlen(file);
115757e8328SLionel Sambuc 
116757e8328SLionel Sambuc 	while ((dp = readdir(dirp)) != NULL) {
117757e8328SLionel Sambuc 		if (dp->d_namlen <= file_name_length)
118757e8328SLionel Sambuc 			continue;
119757e8328SLionel Sambuc 
120757e8328SLionel Sambuc 		this_version = version_number(file, dp->d_name, file_name_length);
121757e8328SLionel Sambuc 		if (this_version > highest_version)
122757e8328SLionel Sambuc 			highest_version = this_version;
123757e8328SLionel Sambuc 	}
124757e8328SLionel Sambuc 	closedir(dirp);
125757e8328SLionel Sambuc 	return highest_version;
126757e8328SLionel Sambuc }
127757e8328SLionel Sambuc 
128757e8328SLionel Sambuc /*
129757e8328SLionel Sambuc  * Return a string, allocated with malloc, containing "FILE.~VERSION~".
130757e8328SLionel Sambuc  * Return 0 if out of memory.
131757e8328SLionel Sambuc  */
132757e8328SLionel Sambuc static char *
make_version_name(const char * file,int version)133757e8328SLionel Sambuc make_version_name(const char *file, int version)
134757e8328SLionel Sambuc {
135757e8328SLionel Sambuc 	char	*backup_name;
136757e8328SLionel Sambuc 
137757e8328SLionel Sambuc 	if (asprintf(&backup_name, "%s.~%d~", file, version) == -1)
138757e8328SLionel Sambuc 		return NULL;
139757e8328SLionel Sambuc 	return backup_name;
140757e8328SLionel Sambuc }
141757e8328SLionel Sambuc 
142757e8328SLionel Sambuc /*
143757e8328SLionel Sambuc  * If BACKUP is a numbered backup of BASE, return its version number;
144757e8328SLionel Sambuc  * otherwise return 0.  BASE_LENGTH is the length of BASE. BASE should
145757e8328SLionel Sambuc  * already have ".~" appended to it.
146757e8328SLionel Sambuc  */
147757e8328SLionel Sambuc static int
version_number(const char * base,const char * backup,size_t base_length)148757e8328SLionel Sambuc version_number(const char *base, const char *backup, size_t base_length)
149757e8328SLionel Sambuc {
150757e8328SLionel Sambuc 	int		version;
151757e8328SLionel Sambuc 	const char	*p;
152757e8328SLionel Sambuc 
153757e8328SLionel Sambuc 	version = 0;
154757e8328SLionel Sambuc 	if (!strncmp(base, backup, base_length) && ISDIGIT(backup[base_length])) {
155757e8328SLionel Sambuc 		for (p = &backup[base_length]; ISDIGIT(*p); ++p)
156757e8328SLionel Sambuc 			version = version * 10 + *p - '0';
157757e8328SLionel Sambuc 		if (p[0] != '~' || p[1])
158757e8328SLionel Sambuc 			version = 0;
159757e8328SLionel Sambuc 	}
160757e8328SLionel Sambuc 	return version;
161757e8328SLionel Sambuc }
162757e8328SLionel Sambuc 
163757e8328SLionel Sambuc /*
164757e8328SLionel Sambuc  * Return the newly-allocated concatenation of STR1 and STR2. If out of
165757e8328SLionel Sambuc  * memory, return 0.
166757e8328SLionel Sambuc  */
167757e8328SLionel Sambuc static char  *
concat(const char * str1,const char * str2)168757e8328SLionel Sambuc concat(const char *str1, const char *str2)
169757e8328SLionel Sambuc {
170757e8328SLionel Sambuc 	char	*newstr;
171757e8328SLionel Sambuc 
172757e8328SLionel Sambuc 	if (asprintf(&newstr, "%s%s", str1, str2) == -1)
173757e8328SLionel Sambuc 		return NULL;
174757e8328SLionel Sambuc 	return newstr;
175757e8328SLionel Sambuc }
176757e8328SLionel Sambuc 
177757e8328SLionel Sambuc /*
178757e8328SLionel Sambuc  * If ARG is an unambiguous match for an element of the null-terminated array
179757e8328SLionel Sambuc  * OPTLIST, return the index in OPTLIST of the matched element, else -1 if it
180757e8328SLionel Sambuc  * does not match any element or -2 if it is ambiguous (is a prefix of more
181757e8328SLionel Sambuc  * than one element).
182757e8328SLionel Sambuc  */
183757e8328SLionel Sambuc static int
argmatch(const char * arg,const char ** optlist)184757e8328SLionel Sambuc argmatch(const char *arg, const char **optlist)
185757e8328SLionel Sambuc {
186757e8328SLionel Sambuc 	int	i;	/* Temporary index in OPTLIST. */
187757e8328SLionel Sambuc 	size_t	arglen;	/* Length of ARG. */
188757e8328SLionel Sambuc 	int	matchind = -1;	/* Index of first nonexact match. */
189757e8328SLionel Sambuc 	int	ambiguous = 0;	/* If nonzero, multiple nonexact match(es). */
190757e8328SLionel Sambuc 
191757e8328SLionel Sambuc 	arglen = strlen(arg);
192757e8328SLionel Sambuc 
193757e8328SLionel Sambuc 	/* Test all elements for either exact match or abbreviated matches.  */
194757e8328SLionel Sambuc 	for (i = 0; optlist[i]; i++) {
195757e8328SLionel Sambuc 		if (!strncmp(optlist[i], arg, arglen)) {
196757e8328SLionel Sambuc 			if (strlen(optlist[i]) == arglen)
197757e8328SLionel Sambuc 				/* Exact match found.  */
198757e8328SLionel Sambuc 				return i;
199757e8328SLionel Sambuc 			else if (matchind == -1)
200757e8328SLionel Sambuc 				/* First nonexact match found.  */
201757e8328SLionel Sambuc 				matchind = i;
202757e8328SLionel Sambuc 			else
203757e8328SLionel Sambuc 				/* Second nonexact match found.  */
204757e8328SLionel Sambuc 				ambiguous = 1;
205757e8328SLionel Sambuc 		}
206757e8328SLionel Sambuc 	}
207757e8328SLionel Sambuc 	if (ambiguous)
208757e8328SLionel Sambuc 		return -2;
209757e8328SLionel Sambuc 	else
210757e8328SLionel Sambuc 		return matchind;
211757e8328SLionel Sambuc }
212757e8328SLionel Sambuc 
213757e8328SLionel Sambuc /*
214757e8328SLionel Sambuc  * Error reporting for argmatch. KIND is a description of the type of entity
215757e8328SLionel Sambuc  * that was being matched. VALUE is the invalid value that was given. PROBLEM
216757e8328SLionel Sambuc  * is the return value from argmatch.
217757e8328SLionel Sambuc  */
218757e8328SLionel Sambuc static void
invalid_arg(const char * kind,const char * value,int problem)219757e8328SLionel Sambuc invalid_arg(const char *kind, const char *value, int problem)
220757e8328SLionel Sambuc {
221757e8328SLionel Sambuc 	fprintf(stderr, "patch: ");
222757e8328SLionel Sambuc 	if (problem == -1)
223757e8328SLionel Sambuc 		fprintf(stderr, "invalid");
224757e8328SLionel Sambuc 	else			/* Assume -2. */
225757e8328SLionel Sambuc 		fprintf(stderr, "ambiguous");
226757e8328SLionel Sambuc 	fprintf(stderr, " %s `%s'\n", kind, value);
227757e8328SLionel Sambuc }
228757e8328SLionel Sambuc 
229757e8328SLionel Sambuc static const char *backup_args[] = {
230*0a6a1f1dSLionel Sambuc 	"none", "never", "simple", "nil", "existing", "t", "numbered", 0
231757e8328SLionel Sambuc };
232757e8328SLionel Sambuc 
233757e8328SLionel Sambuc static enum backup_type backup_types[] = {
234*0a6a1f1dSLionel Sambuc 	none, simple, simple, numbered_existing,
235757e8328SLionel Sambuc 	numbered_existing, numbered, numbered
236757e8328SLionel Sambuc };
237757e8328SLionel Sambuc 
238757e8328SLionel Sambuc /*
239757e8328SLionel Sambuc  * Return the type of backup indicated by VERSION. Unique abbreviations are
240757e8328SLionel Sambuc  * accepted.
241757e8328SLionel Sambuc  */
242757e8328SLionel Sambuc enum backup_type
get_version(const char * version)243757e8328SLionel Sambuc get_version(const char *version)
244757e8328SLionel Sambuc {
245757e8328SLionel Sambuc 	int	i;
246757e8328SLionel Sambuc 
247757e8328SLionel Sambuc 	if (version == NULL || *version == '\0')
248757e8328SLionel Sambuc 		return numbered_existing;
249757e8328SLionel Sambuc 	i = argmatch(version, backup_args);
250757e8328SLionel Sambuc 	if (i >= 0)
251757e8328SLionel Sambuc 		return backup_types[i];
252757e8328SLionel Sambuc 	invalid_arg("version control type", version, i);
253757e8328SLionel Sambuc 	exit(2);
254757e8328SLionel Sambuc }
255