1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
/* backup.c -- make Emacs style backup file names
Copyright (C) 1992 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it without restriction.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
GNU/Emacs style backups --
This behaviour is controlled by two environment variables,
VERSION_CONTROL and SIMPLE_BACKUP_SUFFIX.
VERSION_CONTROL determines what kinds of backups are made. If it's
value is "numbered", then the first modification of some file
"eraserhead.c" will yield a backup file "eraserhead.c.~1~", the
second modification will yield "eraserhead.c.~2~", and so on. It
does not matter if the version numbers are not a sequence; the next
version will be one greater than the highest in that directory.
If the value of VERSION_CONTROL is "numbered_existing", then such
numbered backups will be made if there are already numbered backup
versions of the file. Otherwise, the backup name will be that of
the original file with "~" (tilde) appended. E.g., "eraserhead.c~".
If the value of VERSION_CONTROL is "simple", then the backup name
will be that of the original file with "~" appended, regardless of
whether or not there exist numbered versions in the directory.
For simple backups, the value of SIMPLE_BACKUP_SUFFIX will be used
rather than "~" if it is set.
If VERSION_CONTROL is unset, "numbered_existing" is assumed. For
Emacs lovers, "nil" is equivalent to "numbered_existing" and "t" is
equivalent to "numbered".
Finally, if VERSION_CONTROL is "none" or "never", backups are not
made. I suggest you avoid this behaviour. */
/* Written by jla, based on code from djm (see `patch') */
#include "backup.h"
#include "sys.h"
#include <ctype.h>
#ifndef isascii
#define ISDIGIT(c) (isdigit ((unsigned char) (c)))
#else
#define ISDIGIT(c) (isascii (c) && isdigit (c))
#endif
#ifndef NODIR
#include <sys/types.h>
#ifdef DIRENT
#include <dirent.h>
#ifdef direct
#undef direct
#endif
#define direct dirent
#define NLENGTH(direct) (strlen((direct)->d_name))
#else /* !DIRENT */
#define NLENGTH(direct) ((direct)->d_namlen)
#ifdef USG
#ifdef SYSNDIR
#include <sys/ndir.h>
#else /* !SYSNDIR */
#include <ndir.h>
#endif /* !SYSNDIR */
#else /* !USG */
#include <sys/dir.h>
#endif /* !USG */
#endif /* !DIRENT */
#if defined (HAVE_UNISTD_H)
#include <unistd.h>
#endif
#if defined (_POSIX_VERSION) /* Might be defined in unistd.h. */
/* POSIX does not require that the d_ino field be present, and some
systems do not provide it. */
#define REAL_DIR_ENTRY(dp) 1
#else
#define REAL_DIR_ENTRY(dp) ((dp)->d_ino != 0)
#endif
#else /* NODIR */
#define generate_backup_filename(v,f) simple_backup_name((f))
#endif /* NODIR */
/* Default backup file suffix to use */
char *simple_backup_suffix = "~";
/* What kinds of backup files to make -- see
table `version_control_values' below. */
enum backup_mode version_control = unknown;
/* Construct a simple backup name for PATHNAME by appending
the value of `simple_backup_suffix'. */
static char *
simple_backup_name (pathname)
char *pathname;
{
char *backup_name;
backup_name = xmalloc (strlen (pathname)
+ strlen (simple_backup_suffix) + 2);
sprintf (backup_name, "%s%s", pathname, simple_backup_suffix);
return backup_name;
}
#ifndef NODIR
/* If DIRENTRY is a numbered backup version of file BASE, return
that number. BASE_LENGTH is the string length of BASE. */
static int
version_number (base, direntry, base_length)
char *base;
char *direntry;
int base_length;
{
int version;
char *p;
version = 0;
if (!strncmp (base, direntry, base_length)
&& ISDIGIT (direntry[base_length + 2]))
{
for (p = &direntry[base_length + 2]; ISDIGIT (*p); ++p)
version = version * 10 + *p - '0';
if (p[0] != '~' || p[1])
version = 0;
}
return version;
}
/* Return the highest version of file FILENAME in directory
DIRNAME. Return 0 if there are no numbered versions. */
static int
highest_version (filename, dirname)
char *filename, *dirname;
{
DIR *dirp;
struct direct *dp;
int highest_version;
int this_version;
int file_name_length;
dirp = opendir (dirname);
if (!dirp)
return 0;
highest_version = 0;
file_name_length = strlen (filename);
while ((dp = readdir (dirp)) != 0)
{
if (!REAL_DIR_ENTRY (dp) || NLENGTH (dp) <= file_name_length + 2)
continue;
this_version = version_number (filename, dp->d_name, file_name_length);
if (this_version > highest_version)
highest_version = this_version;
}
closedir (dirp);
return highest_version;
}
/* Return the highest version number for file PATHNAME. If there
are no backups, or only a simple backup, return 0. */
static int
max_version (pathname)
char *pathname;
{
register char *p;
register char *filename;
int pathlen = strlen (pathname);
int version;
p = pathname + pathlen - 1;
while (p > pathname && *p != '/')
p--;
if (*p == '/')
{
int dirlen = p - pathname;
register char *dirname;
filename = p + 1;
dirname = xmalloc (dirlen + 1);
strncpy (dirname, pathname, (dirlen));
dirname[dirlen] = '\0';
version = highest_version (filename, dirname);
free (dirname);
return version;
}
filename = pathname;
version = highest_version (filename, ".");
return version;
}
/* Generate a backup filename for PATHNAME, dependent on the
value of VERSION_CONTROL. */
static char *
generate_backup_filename (version_control, pathname)
enum backup_mode version_control;
char *pathname;
{
int last_numbered_version;
char *backup_name;
if (version_control == none)
return 0;
if (version_control == simple)
return simple_backup_name (pathname);
last_numbered_version = max_version (pathname);
if (version_control == numbered_existing
&& last_numbered_version == 0)
return simple_backup_name (pathname);
last_numbered_version++;
backup_name = xmalloc (strlen (pathname) + 16);
if (!backup_name)
return 0;
sprintf (backup_name, "%s.~%d~", pathname, last_numbered_version);
return backup_name;
}
#endif /* !NODIR */
static struct version_control_values values[] =
{
{none, "never"}, /* Don't make backups. */
{simple, "simple"}, /* Only simple backups */
{numbered_existing, "existing"}, /* Numbered if they already exist */
{numbered_existing, "nil"}, /* Ditto */
{numbered, "numbered"}, /* Numbered backups */
{numbered, "t"}, /* Ditto */
{unknown, 0} /* Initial, undefined value. */
};
extern char *getenv ();
/* Determine the value of `version_control' by looking in the
environment variable "VERSION_CONTROL". Defaults to
numbered_existing. */
enum backup_mode
version_control_value ()
{
char *version;
struct version_control_values *v;
version = getenv ("VERSION_CONTROL");
if (version == 0 || *version == 0)
return numbered_existing;
v = &values[0];
while (v->name)
{
if (strcmp (version, v->name) == 0)
return v->value;
v++;
}
return unknown;
}
/* Initialize information used in determining backup filenames. */
void
initialize_backups ()
{
char *v = getenv ("SIMPLE_BACKUP_SUFFIX");
if (v && *v)
simple_backup_suffix = v;
#ifdef NODIR
version_control = simple;
#else /* !NODIR */
version_control = version_control_value ();
if (version_control == unknown)
{
fprintf (stderr, "indent: Strange version-control value\n");
fprintf (stderr, "indent: Using numbered-existing\n");
version_control = numbered_existing;
}
#endif /* !NODIR */
}
/* Prints an error message using `perror' */
extern void sys_error ();
/* Make a backup copy of FILE, taking into account version-control.
See the description at the beginning of the file for details. */
void
make_backup (file)
struct file_buffer *file;
{
int fd;
register char *p = file->name + strlen (file->name) - 1;
char *backup_filename;
char *new_backup_name;
backup_filename = generate_backup_filename (version_control, file->name);
if (!backup_filename)
{
fprintf (stderr, "indent: Can't make backup filename of %s", file->name);
exit (1);
}
fd = creat (backup_filename, 0666);
if (fd < 0)
sys_error (backup_filename);
if (write (fd, file->data, file->size) != file->size)
sys_error (backup_filename);
close (fd);
free (backup_filename);
}
|