home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / f / futi14as.zip / BACKUPFI.C < prev    next >
C/C++ Source or Header  |  1992-02-22  |  9KB  |  344 lines

  1. /* backupfile.c -- make Emacs style backup file names
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program 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.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* David MacKenzie <djm@ai.mit.edu>.
  19.    Some algorithms adapted from GNU Emacs. */
  20. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  21.    This port is also distributed under the terms of the
  22.    GNU General Public License as published by the
  23.    Free Software Foundation.
  24.  
  25.    Please note that this file is not identical to the
  26.    original GNU release, you should have received this
  27.    code as patch to the official release.  */
  28.  
  29. #ifdef MSDOS
  30. static char RCS_Id[] =
  31.   "$Header: e:/gnu/fileutil/RCS/backupfi.c 1.4.0.2 90/09/19 12:27:27 tho Exp $";
  32. #endif /* MSDOS */
  33.  
  34. #include <stdio.h>
  35. #include <ctype.h>
  36. #include <sys/types.h>
  37. #include "backupfile.h"
  38. #if defined(USG) || defined(_STDC_HEADERS)
  39. #define index strchr
  40. #define rindex strrchr
  41. #include <string.h>
  42. #else
  43. #include <strings.h>
  44. #endif
  45.  
  46. #ifdef DIRENT
  47. #include <dirent.h>
  48. #ifdef direct
  49. #undef direct
  50. #endif
  51. #define direct dirent
  52. #define NLENGTH(direct) (strlen((direct)->d_name))
  53. #else
  54. #define NLENGTH(direct) ((direct)->d_namlen)
  55. #ifdef USG
  56. #ifdef SYSNDIR
  57. #include <sys/ndir.h>
  58. #else
  59. #include <ndir.h>
  60. #endif
  61. #else /* must be BSD */
  62. #include <sys/dir.h>
  63. #endif
  64. #endif
  65.  
  66. #ifdef STDC_HEADERS
  67. #include <stdlib.h>
  68. #define ISDIGIT(c) (isdigit ((unsigned char) (c)))
  69. #else
  70. #define ISDIGIT(c) (isascii (c) && isdigit (c))
  71.  
  72. char *malloc ();
  73. #endif
  74.  
  75. #ifdef MSDOS
  76. extern char *find_backup_file_name (char *file);
  77. static int max_backup_version (char *file, char *dir);
  78. static char *make_version_name (char *file, int version);
  79. static int version_number (char *base, char *backup, int base_length);
  80. static char *dirname (char *path);
  81. static char *basename (char *name);
  82. static char *concat (char *str1, char *str2);
  83. static char *copystring (char *str);
  84. static char *chop_filename (char *path, int n);
  85. #endif /* MSDOS */
  86.  
  87. /* Which type of backup file names are generated. */
  88. enum backup_type backup_type = none;
  89.  
  90. /* The extension added to file names to produce a simple (as opposed
  91.    to numbered) backup file name. */
  92. char *simple_backup_suffix = "~";
  93.  
  94. static char *basename ();
  95. static char *concat ();
  96. static char *copystring ();
  97. static char *dirname ();
  98. char *find_backup_file_name ();
  99. static char *make_version_name ();
  100. static int max_backup_version ();
  101. static int version_number ();
  102.  
  103. /* Return the name of the new backup file for file FILE,
  104.    allocated with malloc.  Return 0 if out of memory.
  105.    FILE must not end with a '/' unless it is the root directory.
  106.    Do not call this function if backup_type == none. */
  107.  
  108. char *
  109. find_backup_file_name (file)
  110.      char *file;
  111. {
  112.   char *dir;
  113.   char *base_versions;
  114.   int highest_backup;
  115.  
  116.   if (backup_type == simple)
  117.     return concat (file, simple_backup_suffix);
  118.   base_versions = concat (basename (file), ".~");
  119.   if (base_versions == 0)
  120.     return 0;
  121.   dir = dirname (file);
  122.   if (dir == 0)
  123.     {
  124.       free (base_versions);
  125.       return 0;
  126.     }
  127.   highest_backup = max_backup_version (base_versions, dir);
  128.   free (base_versions);
  129.   free (dir);
  130.   if (backup_type == numbered_existing && highest_backup == 0)
  131.     return concat (file, simple_backup_suffix);
  132.   return make_version_name (file, highest_backup + 1);
  133. }
  134.  
  135. /* Return the number of the highest-numbered backup file for file
  136.    FILE in directory DIR.  If there are no numbered backups
  137.    of FILE in DIR, return 0.
  138.    FILE should already have ".~" appended to it. */
  139.  
  140. static int
  141. max_backup_version (file, dir)
  142.      char *file, *dir;
  143. {
  144.   DIR *dirp;
  145.   struct direct *dp;
  146.   int highest_version;
  147.   int this_version;
  148.   int file_name_length;
  149.   
  150.   dirp = opendir (dir);
  151.   if (!dirp)
  152.     return 0;
  153.   
  154.   highest_version = 0;
  155.   file_name_length = strlen (file);
  156.  
  157.   while ((dp = readdir (dirp)) != 0)
  158.     {
  159. #ifdef MSDOS
  160.       if (NLENGTH (dp) <= file_name_length)
  161. #else /* not MSDOS */
  162.       if (dp->d_ino == 0 || NLENGTH (dp) <= file_name_length)
  163. #endif /* not MSDOS */
  164.     continue;
  165.       
  166.       this_version = version_number (file, dp->d_name, file_name_length);
  167.       if (this_version > highest_version)
  168.     highest_version = this_version;
  169.     }
  170.   closedir (dirp);
  171.   return highest_version;
  172. }
  173.  
  174. #ifdef MSDOS
  175. static char suffix[5];
  176. #endif /* not MSDOS */
  177.  
  178. /* Return a string, allocated with malloc, containing
  179.    "FILE.~VERSION~".  Return 0 if out of memory. */
  180.  
  181. static char *
  182. make_version_name (file, version)
  183.      char *file;
  184.      int version;
  185. {
  186. #ifdef MSDOS
  187.   sprintf (suffix, ".~%.1d~", version);
  188.   return concat (file, suffix);
  189. #else /* not MSDOS */
  190.   char *backup_name;
  191.  
  192.   backup_name = malloc (strlen (file) + 16);
  193.   if (backup_name == 0)
  194.     return 0;
  195.   sprintf (backup_name, "%s.~%d~", file, version);
  196.   return backup_name;
  197. #endif /* not MSDOS */
  198. }
  199.  
  200. /* If BACKUP is a numbered backup of BASE, return its version number;
  201.    otherwise return 0.  BASE_LENGTH is the length of BASE.
  202.    BASE should already have ".~" appended to it. */
  203.  
  204. static int
  205. version_number (base, backup, base_length)
  206.      char *base;
  207.      char *backup;
  208.      int base_length;
  209. {
  210.   int version;
  211.   char *p;
  212.  
  213.   version = 0;
  214.   if (!strncmp (base, backup, base_length) && ISDIGIT (backup[base_length]))
  215.     {
  216.       for (p = &backup[base_length]; ISDIGIT (*p); ++p)
  217.     version = version * 10 + *p - '0';
  218. #ifdef MSDOS
  219.       if (*p && *p != '~')
  220. #else /* not MSDOS */
  221.       if (p[0] != '~' || p[1])
  222. #endif /* not MSDOS */
  223.     version = 0;
  224.     }
  225.   return version;
  226. }
  227.  
  228. /* Return the leading directories part of PATH,
  229.    allocated with malloc.  If out of memory, return 0. */
  230.  
  231. static char *
  232. dirname (path)
  233.      char *path;
  234. {
  235.   char *newpath;
  236.   char *slash;
  237.  
  238.   slash = rindex (path, '/');
  239.   if (slash == 0)
  240.     return copystring (".");
  241.  
  242.   newpath = malloc (strlen (path) + 1);
  243.   if (newpath == 0)
  244.     return 0;
  245.   strcpy (newpath, path);
  246.   slash += newpath - path;
  247.   /* Remove any trailing slashes and final element. */
  248.   while (slash > newpath && *slash == '/')
  249.     --slash;
  250.   slash[1] = 0;
  251.   return newpath;
  252. }
  253.  
  254. /* Return NAME with any leading path stripped off.  */
  255.  
  256. static char *
  257. basename (name)
  258.      char *name;
  259. {
  260.   char *base;
  261.  
  262.   base = rindex (name, '/');
  263.   return base ? base + 1 : name;
  264. }
  265.  
  266. /* Return the newly-allocated concatenation of STR1 and STR2.
  267.    If out of memory, return 0. */
  268.  
  269. static char *
  270. concat (str1, str2)
  271.      char *str1, *str2;
  272. {
  273.   char *newstr;
  274.   char str1_length = strlen (str1);
  275.  
  276. #ifdef MSDOS
  277.   /* The MS-DOS version tries to squeeze the given string into a valid
  278.      MS-DOS patch name.  STR1 is chopped and a leading period is removed
  279.      from STR2.  Kludge: a leading period is counted in the length of STR2,
  280.      this is because we will know, that in this case a digit will be appended
  281.      afterwards.  */
  282.   /* chop_filename () might add a '.', so allocate one more byte.  */
  283.   newstr = malloc (str1_length + strlen (str2) + 2);
  284.   if (newstr == 0)
  285.     return 0;
  286.   strcpy (newstr, str1);
  287.   chop_filename (newstr, min (2, strlen(str2)));
  288.   if (*str2 == '.')
  289.     str2++;
  290.   strcat (newstr, str2);
  291. #else /* not MSDOS */
  292.   newstr = malloc (str1_length + strlen (str2) + 1);
  293.   if (newstr == 0)
  294.     return 0;
  295.   strcpy (newstr, str1);
  296.   strcpy (newstr + str1_length, str2);
  297. #endif /* not MSDOS */
  298.   return newstr;
  299. }
  300.  
  301. /* Return a newly allocated copy of STR. */
  302.  
  303. static char *
  304. copystring (str)
  305.      char *str;
  306. {
  307.   char *newstr;
  308.   
  309.   newstr = malloc (strlen (str) + 1);
  310.   if (newstr == 0)
  311.     return 0;
  312.   strcpy (newstr, str);
  313.   return newstr;
  314. }
  315.  
  316.  
  317. #ifdef MSDOS
  318. /* Shorten a MS-DOS path to accomodate a backup suffix.  */
  319.  
  320. char *
  321. chop_filename (char *path, int n)
  322. {
  323.   char *base;
  324.   char *suffix;
  325.  
  326.   base = strrchr (path, '/');
  327.   if (base == (char *)0)
  328.     base = path;
  329.   else
  330.     base++;
  331.  
  332.   suffix = strchr (base, '.');
  333.   if (suffix == (char *)0)
  334.     strcat (base, ".");        /* is ok, since we have allocated enough! */
  335.   else if (strlen (suffix) >= 4 - n)
  336.     suffix[4-n] = '\0';
  337.  
  338.   return path;
  339. }
  340. #endif /* MSDOS */
  341.  
  342.  
  343.  
  344.