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