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