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