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