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