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