home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / indent-1.9.1-base.tgz / indent-1.9.1-base.tar / fsf / indent / backup.c < prev    next >
C/C++ Source or Header  |  1994-01-29  |  9KB  |  359 lines

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