home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / indent / backup.c < prev    next >
C/C++ Source or Header  |  1999-05-21  |  10KB  |  414 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 "backup.h"
  47. #include "sys.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. #include <sys/dir.h>
  77. #ifdef AMIGA
  78. #define dirent direct
  79. #endif /* AMIGA */
  80. #endif /* !USG */
  81. #endif /* !DIRENT */
  82.  
  83. #if defined (HAVE_UNISTD_H)
  84. #include <unistd.h>
  85. #endif
  86.  
  87. #if defined (_POSIX_VERSION)    /* Might be defined in unistd.h.  */
  88. /* POSIX does not require that the d_ino field be present, and some
  89.    systems do not provide it. */
  90. #define REAL_DIR_ENTRY(dp) 1
  91. #else
  92. #define REAL_DIR_ENTRY(dp) ((dp)->d_ino != 0)
  93. #endif
  94.  
  95. #else /* NODIR */
  96. #define generate_backup_filename(v,f) simple_backup_name((f))
  97. #endif /* NODIR */
  98.  
  99. /* Default backup file suffix to use */
  100. #ifdef AMIGA
  101. char *simple_backup_suffix = "!";
  102. #else /* !AMIGA */
  103. char *simple_backup_suffix = "~";
  104. #endif /* !AMIGA */
  105.  
  106. /* What kinds of backup files to make -- see
  107.    table `version_control_values' below. */
  108. enum backup_mode version_control = unknown;
  109.  
  110.  
  111. /* Construct a simple backup name for PATHNAME by appending
  112.    the value of `simple_backup_suffix'. */
  113.  
  114. static char *
  115. simple_backup_name (pathname)
  116.      char *pathname;
  117. {
  118.   char *backup_name;
  119.  
  120.   backup_name = xmalloc (strlen (pathname)
  121.              + strlen (simple_backup_suffix) + 2);
  122.   sprintf (backup_name, "%s%s", pathname, simple_backup_suffix);
  123.   return backup_name;
  124. }
  125.  
  126. #ifndef NODIR
  127. /* If DIRENTRY is a numbered backup version of file BASE, return
  128.    that number.  BASE_LENGTH is the string length of BASE. */
  129.  
  130. static int
  131. version_number (base, direntry, base_length)
  132.      char *base;
  133.      char *direntry;
  134.      int base_length;
  135. {
  136.   int version;
  137.   char *p;
  138.  
  139.   version = 0;
  140.   if (!strncmp (base, direntry, base_length)
  141.       && ISDIGIT (direntry[base_length + 2]))
  142.     {
  143.       for (p = &direntry[base_length + 2]; ISDIGIT (*p); ++p)
  144.     version = version * 10 + *p - '0';
  145. #ifdef AMIGA
  146.       if (p[0] != '!' || p[1])
  147. #else /* !AMIGA */
  148.       if (p[0] != '~' || p[1])
  149. #endif /* !AMIGA */
  150.     version = 0;
  151.     }
  152.  
  153.   return version;
  154. }
  155.  
  156.  
  157. /* Return the highest version of file FILENAME in directory
  158.    DIRNAME.  Return 0 if there are no numbered versions. */
  159.  
  160. static int
  161. highest_version (filename, dirname)
  162.      char *filename, *dirname;
  163. {
  164.   DIR *dirp;
  165.   struct direct *dp;
  166.   int highest_version;
  167.   int this_version;
  168.   int file_name_length;
  169.  
  170.   dirp = opendir (dirname);
  171.   if (!dirp)
  172.     return 0;
  173.  
  174.   highest_version = 0;
  175.   file_name_length = strlen (filename);
  176.  
  177.   while ((dp = readdir (dirp)) != 0)
  178.     {
  179.       if (!REAL_DIR_ENTRY (dp) || NLENGTH (dp) <= file_name_length + 2)
  180.     continue;
  181.  
  182.       this_version = version_number (filename, dp->d_name, file_name_length);
  183.       if (this_version > highest_version)
  184.     highest_version = this_version;
  185.     }
  186.  
  187.   closedir (dirp);
  188.   return highest_version;
  189. }
  190.  
  191.  
  192. /* Return the highest version number for file PATHNAME.  If there
  193.    are no backups, or only a simple backup, return 0. */
  194.  
  195. static int
  196. max_version (pathname)
  197.      char *pathname;
  198. {
  199.   register char *p;
  200.   register char *filename;
  201.   int pathlen = strlen (pathname);
  202.   int version;
  203.  
  204.   p = pathname + pathlen - 1;
  205. #ifdef AMIGA
  206.   while (p > pathname && *p != '/' && *p != ':')
  207.     p--;
  208.   if (*p == '/')
  209.     {
  210.       int dirlen = p - pathname;
  211.       register char *dirname;
  212.  
  213.       filename = p + 1;
  214.       dirname = xmalloc (dirlen + 1);
  215.       strncpy (dirname, pathname, (dirlen));
  216.       dirname[dirlen] = '\0';
  217.       version = highest_version (filename, dirname);
  218.       free (dirname);
  219.       return version;
  220.     }
  221.   else if (*p == ':')
  222.     {
  223.       int dirlen = p - pathname + 1;
  224.       register char *dirname;
  225.  
  226.       filename = p + 1;
  227.       dirname = xmalloc (dirlen + 1);
  228.       strncpy (dirname, pathname, (dirlen));
  229.       dirname[dirlen] = '\0';
  230.       version = highest_version (filename, dirname);
  231.       free (dirname);
  232.       return version;
  233.     }
  234. #else /* !AMIGA */
  235.   while (p > pathname && *p != '/')
  236.     p--;
  237.  
  238.   if (*p == '/')
  239.     {
  240.       int dirlen = p - pathname;
  241.       register char *dirname;
  242.  
  243.       filename = p + 1;
  244.       dirname = xmalloc (dirlen + 1);
  245.       strncpy (dirname, pathname, (dirlen));
  246.       dirname[dirlen] = '\0';
  247.       version = highest_version (filename, dirname);
  248.       free (dirname);
  249.       return version;
  250.     }
  251. #endif /* !AMIGA */
  252.  
  253.   filename = pathname;
  254. #ifdef AMIGA
  255.   version = highest_version (filename, "");
  256. #else /* !AMIGA */
  257.   version = highest_version (filename, ".");
  258. #endif /* !AMIGA */
  259.   return version;
  260. }
  261.  
  262.  
  263. /* Generate a backup filename for PATHNAME, dependent on the
  264.    value of VERSION_CONTROL. */
  265.  
  266. static char *
  267. generate_backup_filename (version_control, pathname)
  268.      enum backup_mode version_control;
  269.      char *pathname;
  270. {
  271.   int last_numbered_version;
  272.   char *backup_name;
  273.  
  274.   if (version_control == none)
  275.     return 0;
  276.  
  277.   if (version_control == simple)
  278.     return simple_backup_name (pathname);
  279.  
  280.   last_numbered_version = max_version (pathname);
  281.   if (version_control == numbered_existing
  282.       && last_numbered_version == 0)
  283.     return simple_backup_name (pathname);
  284.  
  285.   last_numbered_version++;
  286.   backup_name = xmalloc (strlen (pathname) + 16);
  287.   if (!backup_name)
  288.     return 0;
  289.  
  290. #ifdef AMIGA
  291.   sprintf (backup_name, "%s.!%d!", pathname,
  292.            (int) last_numbered_version);
  293. #else /* !AMIGA */
  294.   sprintf (backup_name, "%s.~%d~", pathname,
  295.        (int) last_numbered_version);
  296. #endif /* !AMIGA */
  297.   return backup_name;
  298. }
  299.  
  300. #endif /* !NODIR */
  301.  
  302. static struct version_control_values values[] =
  303. {
  304.   {none, "never"},        /* Don't make backups. */
  305.   {simple, "simple"},        /* Only simple backups */
  306.   {numbered_existing, "existing"},    /* Numbered if they already exist */
  307.   {numbered_existing, "nil"},    /* Ditto */
  308.   {numbered, "numbered"},    /* Numbered backups */
  309.   {numbered, "t"},        /* Ditto */
  310.   {unknown, 0}            /* Initial, undefined value. */
  311. };
  312.  
  313.  
  314. extern char *getenv ();
  315.  
  316. /* Determine the value of `version_control' by looking in the
  317.    environment variable "VERSION_CONTROL".  Defaults to
  318.    numbered_existing. */
  319.  
  320. enum backup_mode
  321. version_control_value ()
  322. {
  323.   char *version;
  324.   struct version_control_values *v;
  325.  
  326.   version = getenv ("VERSION_CONTROL");
  327.   if (version == 0 || *version == 0)
  328.     return numbered_existing;
  329.  
  330.   v = &values[0];
  331.   while (v->name)
  332.     {
  333.       if (strcmp (version, v->name) == 0)
  334.     return v->value;
  335.       v++;
  336.     }
  337.  
  338.   return unknown;
  339. }
  340.  
  341.  
  342. /* Initialize information used in determining backup filenames. */
  343.  
  344. void
  345. initialize_backups ()
  346. {
  347.   char *v = getenv ("SIMPLE_BACKUP_SUFFIX");
  348.  
  349.   if (v && *v)
  350.     simple_backup_suffix = v;
  351. #ifdef NODIR
  352.   version_control = simple;
  353. #else /* !NODIR */
  354.   version_control = version_control_value ();
  355.   if (version_control == unknown)
  356.     {
  357.       fprintf (stderr, "indent:  Strange version-control value\n");
  358.       fprintf (stderr, "indent:  Using numbered-existing\n");
  359.       version_control = numbered_existing;
  360.     }
  361. #endif /* !NODIR */
  362. }
  363.  
  364. /* Prints an error message using `perror' */
  365. extern void sys_error ();
  366.  
  367. /* Make a backup copy of FILE, taking into account version-control.
  368.    See the description at the beginning of the file for details. */
  369.  
  370. void
  371. make_backup (file)
  372.      struct file_buffer *file;
  373. {
  374.   int fd;
  375.   register char *p = file->name + strlen (file->name) - 1;
  376.   char *backup_filename;
  377.   char *new_backup_name;
  378.  
  379.   backup_filename = generate_backup_filename (version_control, file->name);
  380. #ifdef AMIGA
  381.   /* The original code contains a bug here. generate_backup_filename sets
  382.      the return value to NULL if version_control == none. Just do nothing
  383.      in this case. */
  384.   if (! backup_filename)
  385.     {
  386.       if (version_control != none)
  387.         {
  388.           fprintf (stderr, "indent: Can't make backup filename of %s\n", file->name);
  389.           exit (1);
  390.         }
  391.       else
  392.         {
  393.           free (backup_filename);
  394.           return;
  395.         }
  396.     }
  397. #else /* !AMIGA */
  398.   if (!backup_filename)
  399.     {
  400.       fprintf (stderr, "indent: Can't make backup filename of %s", file->name);
  401.       exit (1);
  402.     }
  403. #endif /* !AMIGA */
  404.  
  405.   fd = creat (backup_filename, 0666);
  406.   if (fd < 0)
  407.     sys_error (backup_filename);
  408.   if (write (fd, file->data, file->size) != file->size)
  409.     sys_error (backup_filename);
  410.  
  411.   close (fd);
  412.   free (backup_filename);
  413. }
  414.