home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / util / editor / gnuid16.sit / indent-1.6 / backup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-23  |  8.7 KB  |  358 lines

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