home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d7xx / d702 / indent.lha / Indent / Indent.lha / indent-1.4backup.c < prev    next >
C/C++ Source or Header  |  1992-06-24  |  10KB  |  410 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 *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.       register char *dirname;
  210.  
  211.       filename = p + 1;
  212.       dirname = xmalloc (dirlen + 1);
  213.       strncpy (dirname, pathname, (dirlen));
  214.       dirname[dirlen] = '\0';
  215.       version = highest_version (filename, dirname);
  216.       free (dirname);
  217.       return version;
  218.     }
  219.   else if (*p == ':')
  220.     {
  221.       int dirlen = p - pathname + 1;
  222.       register char *dirname;
  223.  
  224.       filename = p + 1;
  225.       dirname = xmalloc (dirlen + 1);
  226.       strncpy (dirname, pathname, (dirlen));
  227.       dirname[dirlen] = '\0';
  228.       version = highest_version (filename, dirname);
  229.       free (dirname);
  230.       return version;
  231.     }
  232. #else
  233.   while (p > pathname && *p != '/')
  234.     p--;
  235.  
  236.   if (*p == '/')
  237.     {
  238.       int dirlen = p - pathname;
  239.       register char *dirname;
  240.  
  241.       filename = p + 1;
  242.       dirname = xmalloc (dirlen + 1);
  243.       strncpy (dirname, pathname, (dirlen));
  244.       dirname[dirlen] = '\0';
  245.       version = highest_version (filename, dirname);
  246.       free (dirname);
  247.       return version;
  248.     }
  249. #endif
  250.  
  251.   filename = pathname;
  252. #ifdef AMIGA
  253.   version = highest_version (filename, "");
  254. #else
  255.   version = highest_version (filename, ".");
  256. #endif
  257.   return version;
  258. }
  259.  
  260.  
  261. /* Generate a backup filename for PATHNAME, dependent on the
  262.    value of VERSION_CONTROL. */
  263.  
  264. static char *
  265. generate_backup_filename (version_control, pathname)
  266.      enum backup_mode version_control;
  267.      char *pathname;
  268. {
  269.   int last_numbered_version;
  270.   char *backup_name;
  271.  
  272.   if (version_control == none)
  273.     return 0;
  274.  
  275.   if (version_control == simple)
  276.     return simple_backup_name (pathname);
  277.  
  278.   last_numbered_version = max_version (pathname);
  279.   if (version_control == numbered_existing
  280.       && last_numbered_version == 0)
  281.     return simple_backup_name (pathname);
  282.  
  283.   last_numbered_version++;
  284.   backup_name = xmalloc (strlen (pathname) + 16);
  285.   if (! backup_name)
  286.     return 0;
  287.  
  288. #ifdef AMIGA
  289.   sprintf (backup_name, "%s.!%d!", pathname, last_numbered_version);
  290. #else
  291.   sprintf (backup_name, "%s.~%d~", pathname, last_numbered_version);
  292. #endif
  293.   return backup_name;
  294. }
  295. #endif /* !NODIR */
  296.  
  297. static struct version_control_values values[] =
  298. {
  299.   {none, "never"},           /* Don't make backups. */
  300.   {simple, "simple"},           /* Only simple backups */
  301.   {numbered_existing, "existing"}, /* Numbered if they already exist */
  302.   {numbered_existing, "nil"},       /* Ditto */
  303.   {numbered, "numbered"},       /* Numbered backups */
  304.   {numbered, "t"},           /* Ditto */
  305.   {unknown, 0}               /* Initial, undefined value. */
  306. };
  307.  
  308.  
  309. extern char *getenv ();
  310.  
  311. /* Determine the value of `version_control' by looking in the
  312.    environment variable "VERSION_CONTROL".  Defaults to
  313.    numbered_existing. */
  314.  
  315. enum backup_mode
  316. version_control_value ()
  317. {
  318.   char *version;
  319.   struct version_control_values *v;
  320.  
  321.   version = getenv ("VERSION_CONTROL");
  322.   if (version == 0 || *version == 0)
  323.     return numbered_existing;
  324.  
  325.   v = &values[0];
  326.   while (v->name)
  327.     {
  328.       if (strcmp (version, v->name) == 0)
  329.     return v->value;
  330.       v++;
  331.     }
  332.  
  333.   return unknown;
  334. }
  335.  
  336.  
  337. /* Initialize information used in determining backup filenames. */
  338.  
  339. void
  340. initialize_backups ()
  341. {
  342.   char *v = getenv ("SIMPLE_BACKUP_SUFFIX");
  343.  
  344.   if (v && *v)
  345.     simple_backup_suffix = v;
  346. #ifdef NODIR
  347.   version_control = simple;
  348. #else  /* !NODIR */
  349.   version_control = version_control_value ();
  350.   if (version_control == unknown)
  351.     {
  352.       fprintf (stderr, "indent:  Strange version-control value\n");
  353.       fprintf (stderr, "indent:  Using numbered-existing\n");
  354.       version_control = numbered_existing;
  355.     }
  356. #endif /* !NODIR */
  357. }
  358.  
  359. /* Prints an error message using `perror' */
  360. extern void sys_error ();
  361.  
  362. /* Make a backup copy of FILE, taking into account version-control.
  363.    See the description at the beginning of the file for details. */
  364.  
  365. void
  366. make_backup (file)
  367.      struct file_buffer *file;
  368. {
  369.   int fd;
  370.   register char *p = file->name + strlen (file->name) - 1;
  371.   char *backup_filename;
  372.   char *new_backup_name;
  373.  
  374.   backup_filename = generate_backup_filename (version_control, file->name);
  375. #ifdef AMIGA
  376.   /* The original code contains a bug here. generate_backup_filename sets
  377.      the return value to NULL if version_control == none. Just do nothing
  378.      in this case. */
  379.   if (! backup_filename)
  380.     {
  381.       if (version_control != none)
  382.         {
  383.           fprintf (stderr, "indent: Can't make backup filename of %s\n", file->name);
  384.           exit (1);
  385.         }
  386.       else
  387.         {
  388.           free (backup_filename);
  389.           return;
  390.         }
  391.     }
  392. #else
  393.   if (! backup_filename)
  394.     {
  395.       fprintf (stderr, "indent: Can't make backup filename of %s", file->name);
  396.       exit (1);
  397.     }
  398. #endif
  399.  
  400.   fd = creat (backup_filename, 0666);
  401.   if (fd < 0)
  402.     sys_error (backup_filename);
  403.   if (write (fd, file->data, file->size) != file->size)
  404.     sys_error (backup_filename);
  405.  
  406.   close (fd);
  407.   free (backup_filename);
  408. }
  409.  
  410.