home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / prog / utils / indent13.lha / indent-1.3 / backup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-05  |  9.2 KB  |  380 lines

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