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