home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / GNU / patch_2_1.lha / patch-2.1backupfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-23  |  11.0 KB  |  472 lines

  1. /* backupfile.c -- make Emacs style backup file names
  2.    Copyright (C) 1990, 1991, 1992 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 2, 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. /* Written by David MacKenzie <djm@gnu.ai.mit.edu>.
  19.    Some algorithms adapted from GNU Emacs. */
  20.  
  21. #include "config.h"
  22. #include <stdio.h>
  23. #include <ctype.h>
  24. #include <sys/types.h>
  25. #include "backupfile.h"
  26. #ifdef STDC_HEADERS
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #else
  30. char *malloc ();
  31. #endif
  32.  
  33. #if defined (HAVE_UNISTD_H)
  34. #include <unistd.h>
  35. #endif
  36.  
  37. #ifdef AMIGA
  38. #include <sys/dir.h>
  39. #define direct dirent
  40. #define NLENGTH(direct) ((direct)->d_namlen)
  41. #else
  42. #if defined(DIRENT) || defined(_POSIX_VERSION)
  43. #include <dirent.h>
  44. #define NLENGTH(direct) (strlen((direct)->d_name))
  45. #else /* not (DIRENT or _POSIX_VERSION) */
  46. #define dirent direct
  47. #define NLENGTH(direct) ((direct)->d_namlen)
  48. #ifdef SYSNDIR
  49. #include <sys/ndir.h>
  50. #endif /* SYSNDIR */
  51. #ifdef SYSDIR
  52. #include <sys/dir.h>
  53. #endif /* SYSDIR */
  54. #ifdef NDIR
  55. #include <ndir.h>
  56. #endif /* NDIR */
  57. #endif /* DIRENT or _POSIX_VERSION */
  58. #endif /* AMIGA */
  59.  
  60. #ifndef isascii
  61. #define ISDIGIT(c) (isdigit ((unsigned char) (c)))
  62. #else
  63. #define ISDIGIT(c) (isascii (c) && isdigit (c))
  64. #endif
  65.  
  66. #if defined (_POSIX_VERSION)
  67. /* POSIX does not require that the d_ino field be present, and some
  68.    systems do not provide it. */
  69. #define REAL_DIR_ENTRY(dp) 1
  70. #else
  71. #define REAL_DIR_ENTRY(dp) ((dp)->d_ino != 0)
  72. #endif
  73.  
  74. #ifdef AMIGA
  75. #define max(a,b) (((a)>(b))?(a):(b))
  76. #endif /* AMIGA */
  77.  
  78. /* Which type of backup file names are generated. */
  79. enum backup_type backup_type = none;
  80.  
  81. /* The extension added to file names to produce a simple (as opposed
  82.    to numbered) backup file name. */
  83. #ifndef AMIGA
  84. char *simple_backup_suffix = "~";
  85. #else /* AMIGA */
  86. char *simple_backup_suffix = "!";
  87. #endif /* AMIGA */
  88.  
  89. char *basename ();
  90. char *dirname ();
  91. static char *concat ();
  92. char *find_backup_file_name ();
  93. static char *make_version_name ();
  94. static int max_backup_version ();
  95. static int version_number ();
  96.  
  97. /* Return NAME with any leading path stripped off.  */
  98.  
  99. char *
  100. basename (name)
  101.      char *name;
  102. {
  103. #ifndef AMIGA
  104.   char *r = name, *p = name;
  105.  
  106.   while (*p)
  107.     if (*p++ == '/')
  108.       r = p;
  109.   return r;
  110. #else /* AMIGA */
  111.   char *base1, *base2, *base;
  112.  
  113.   base1 = rindex (name, '/');
  114.   base2 = rindex (name, ':');
  115.   base = max (base1, base2);
  116.   return base ? base + 1 : name;
  117. #endif /* AMIGA */
  118. }
  119.  
  120. #ifndef NODIR
  121. /* Return the name of the new backup file for file FILE,
  122.    allocated with malloc.  Return 0 if out of memory.
  123.    FILE must not end with a '/' unless it is the root directory.
  124.    Do not call this function if backup_type == none. */
  125.  
  126. char *
  127. find_backup_file_name (file)
  128.      char *file;
  129. {
  130.   char *dir;
  131.   char *base_versions;
  132.   int highest_backup;
  133.  
  134.   if (backup_type == simple)
  135.     {
  136.       char *s = malloc (strlen (file) + strlen (simple_backup_suffix) + 1);
  137.       strcpy (s, file);
  138. #ifndef AMIGA
  139.       addext (s, simple_backup_suffix, '~');
  140. #else /* AMIGA */
  141.       addext (s, simple_backup_suffix, '!');
  142. #endif /* AMIGA */
  143.       return s;
  144.     }
  145. #ifndef AMIGA
  146.   base_versions = concat (basename (file), ".~");
  147. #else /* AMIGA */
  148.   base_versions = concat (basename (file), ".!");
  149. #endif /* AMIGA */
  150.   if (base_versions == 0)
  151.     return 0;
  152.   dir = dirname (file);
  153.   if (dir == 0)
  154.     {
  155.       free (base_versions);
  156.       return 0;
  157.     }
  158.   highest_backup = max_backup_version (base_versions, dir);
  159.   free (base_versions);
  160.   free (dir);
  161.   if (backup_type == numbered_existing && highest_backup == 0)
  162.     return concat (file, simple_backup_suffix);
  163.   return make_version_name (file, highest_backup + 1);
  164. }
  165.  
  166. /* Return the number of the highest-numbered backup file for file
  167.    FILE in directory DIR.  If there are no numbered backups
  168.    of FILE in DIR, or an error occurs reading DIR, return 0.
  169.    FILE should already have ".~" appended to it. */
  170.  
  171. static int
  172. max_backup_version (file, dir)
  173.      char *file, *dir;
  174. {
  175.   DIR *dirp;
  176.   struct dirent *dp;
  177.   int highest_version;
  178.   int this_version;
  179.   int file_name_length;
  180.   
  181.   dirp = opendir (dir);
  182.   if (!dirp)
  183.     return 0;
  184.   
  185.   highest_version = 0;
  186.   file_name_length = strlen (file);
  187.  
  188.   while ((dp = readdir (dirp)) != 0)
  189.     {
  190.       if (!REAL_DIR_ENTRY (dp) || NLENGTH (dp) <= file_name_length)
  191.     continue;
  192.       
  193.       this_version = version_number (file, dp->d_name, file_name_length);
  194.       if (this_version > highest_version)
  195.     highest_version = this_version;
  196.     }
  197.   closedir (dirp);
  198.   return highest_version;
  199. }
  200.  
  201. /* Return a string, allocated with malloc, containing
  202.    "FILE.~VERSION~".  Return 0 if out of memory. */
  203.  
  204. static char *
  205. make_version_name (file, version)
  206.      char *file;
  207.      int version;
  208. {
  209.   char *backup_name;
  210.  
  211.   backup_name = malloc (strlen (file) + 16);
  212.   if (backup_name == 0)
  213.     return 0;
  214. #ifndef AMIGA
  215.   sprintf (backup_name, "%s.~%d~", file, version);
  216. #else /* AMIGA */
  217.   sprintf (backup_name, "%s.!%d!", file, version);
  218. #endif /* AMIGA */
  219.   return backup_name;
  220. }
  221.  
  222. /* If BACKUP is a numbered backup of BASE, return its version number;
  223.    otherwise return 0.  BASE_LENGTH is the length of BASE.
  224.    BASE should already have ".~" appended to it. */
  225.  
  226. static int
  227. version_number (base, backup, base_length)
  228.      char *base;
  229.      char *backup;
  230.      int base_length;
  231. {
  232.   int version;
  233.   char *p;
  234.   
  235.   version = 0;
  236.   if (!strncmp (base, backup, base_length) && ISDIGIT (backup[base_length]))
  237.     {
  238.       for (p = &backup[base_length]; ISDIGIT (*p); ++p)
  239.     version = version * 10 + *p - '0';
  240. #ifndef AMIGA
  241.       if (p[0] != '~' || p[1])
  242. #else /* AMIGA */
  243.       if (p[0] != '!' || p[1])
  244. #endif /* AMIGA */
  245.     version = 0;
  246.     }
  247.   return version;
  248. }
  249.  
  250. /* Return the newly-allocated concatenation of STR1 and STR2.
  251.    If out of memory, return 0. */
  252.  
  253. static char *
  254. concat (str1, str2)
  255.      char *str1, *str2;
  256. {
  257.   char *newstr;
  258.   char str1_length = strlen (str1);
  259.  
  260.   newstr = malloc (str1_length + strlen (str2) + 1);
  261.   if (newstr == 0)
  262.     return 0;
  263.   strcpy (newstr, str1);
  264.   strcpy (newstr + str1_length, str2);
  265.   return newstr;
  266. }
  267.  
  268. /* Return the leading directories part of PATH,
  269.    allocated with malloc.  If out of memory, return 0.
  270.    Assumes that trailing slashes have already been
  271.    removed.  */
  272.  
  273. char *
  274. dirname (path)
  275.      char *path;
  276. {
  277. #ifndef AMIGA
  278.   char *newpath;
  279.   char *slash;
  280.   int length;    /* Length of result, not including NUL. */
  281.  
  282.   slash = basename (path);
  283.   if (slash == path)
  284.     {
  285.       /* File is in the current directory.  */
  286.       path = ".";
  287.       length = 1;
  288.     }
  289.   else
  290.     {
  291.       /* Remove any trailing slashes from result. */
  292.       while (*--slash == '/' && slash > path)
  293.         ;
  294.  
  295.       length = slash - path + 1;
  296.     }
  297.   newpath = malloc (length + 1);
  298.   if (newpath == 0)
  299.     return 0;
  300.   strncpy (newpath, path, length);
  301.   newpath[length] = 0;
  302.   return newpath;
  303. #else /* AMIGA */
  304.   char *newpath;
  305.   char *slash, *colon, *dirsep;
  306.   int length;    /* Length of result, not including NUL. */
  307.  
  308.   slash = rindex (path, '/');
  309.   colon = rindex (path, ':');
  310.   dirsep = max (slash, colon);
  311.   if (dirsep == 0)
  312.     {
  313.       /* File is in the current directory.  */
  314.       path = "";
  315.       length = 0;
  316.     }
  317.   else
  318.     {
  319.       /* Remove one trailing slash from result, if exists. */
  320.       if (dirsep > path && *dirsep == '/')
  321.         --dirsep;
  322.  
  323.       length = dirsep - path + 1;
  324.     }
  325.   newpath = malloc (length + 1);
  326.   if (newpath == 0)
  327.     return 0;
  328.   strncpy (newpath, path, length);
  329.   newpath[length] = 0;
  330.   return newpath;
  331. #endif /* AMIGA */
  332. }
  333.  
  334. /* If ARG is an unambiguous match for an element of the
  335.    null-terminated array OPTLIST, return the index in OPTLIST
  336.    of the matched element, else -1 if it does not match any element
  337.    or -2 if it is ambiguous (is a prefix of more than one element). */
  338.  
  339. int
  340. argmatch (arg, optlist)
  341.      char *arg;
  342.      char **optlist;
  343. {
  344.   int i;            /* Temporary index in OPTLIST. */
  345.   int arglen;            /* Length of ARG. */
  346.   int matchind = -1;        /* Index of first nonexact match. */
  347.   int ambiguous = 0;        /* If nonzero, multiple nonexact match(es). */
  348.   
  349.   arglen = strlen (arg);
  350.   
  351.   /* Test all elements for either exact match or abbreviated matches.  */
  352.   for (i = 0; optlist[i]; i++)
  353.     {
  354.       if (!strncmp (optlist[i], arg, arglen))
  355.     {
  356.       if (strlen (optlist[i]) == arglen)
  357.         /* Exact match found.  */
  358.         return i;
  359.       else if (matchind == -1)
  360.         /* First nonexact match found.  */
  361.         matchind = i;
  362.       else
  363.         /* Second nonexact match found.  */
  364.         ambiguous = 1;
  365.     }
  366.     }
  367.   if (ambiguous)
  368.     return -2;
  369.   else
  370.     return matchind;
  371. }
  372.  
  373. /* Error reporting for argmatch.
  374.    KIND is a description of the type of entity that was being matched.
  375.    VALUE is the invalid value that was given.
  376.    PROBLEM is the return value from argmatch. */
  377.  
  378. void
  379. invalid_arg (kind, value, problem)
  380.      char *kind;
  381.      char *value;
  382.      int problem;
  383. {
  384.   fprintf (stderr, "patch: ");
  385.   if (problem == -1)
  386.     fprintf (stderr, "invalid");
  387.   else                /* Assume -2. */
  388.     fprintf (stderr, "ambiguous");
  389.   fprintf (stderr, " %s `%s'\n", kind, value);
  390. }
  391.  
  392. static char *backup_args[] =
  393. {
  394.   "never", "simple", "nil", "existing", "t", "numbered", 0
  395. };
  396.  
  397. static enum backup_type backup_types[] =
  398. {
  399.   simple, simple, numbered_existing, numbered_existing, numbered, numbered
  400. };
  401.  
  402. /* Return the type of backup indicated by VERSION.
  403.    Unique abbreviations are accepted. */
  404.  
  405. enum backup_type
  406. get_version (version)
  407.      char *version;
  408. {
  409.   int i;
  410.  
  411.   if (version == 0 || *version == 0)
  412.     return numbered_existing;
  413.   i = argmatch (version, backup_args);
  414.   if (i >= 0)
  415.     return backup_types[i];
  416.   invalid_arg ("version control type", version, i);
  417.   exit (1);
  418. }
  419. #endif /* NODIR */
  420.  
  421. /* Append to FILENAME the extension EXT, unless the result would be too long,
  422.    in which case just append the character E.  */
  423.  
  424. void
  425. addext (filename, ext, e)
  426.      char *filename, *ext;
  427.      int e;
  428. {
  429.   char *s = basename (filename);
  430.   int slen = strlen (s), extlen = strlen (ext);
  431.   long slen_max = -1;
  432.  
  433. #if HAVE_PATHCONF && defined (_PC_NAME_MAX)
  434. #ifndef _POSIX_NAME_MAX
  435. #define _POSIX_NAME_MAX 14
  436. #endif
  437.   if (slen + extlen <= _POSIX_NAME_MAX)
  438.     /* The file name is so short there's no need to call pathconf.  */
  439.     slen_max = _POSIX_NAME_MAX;
  440.   else if (s == filename)
  441.     slen_max = pathconf (".", _PC_NAME_MAX);
  442.   else
  443.     {
  444.       char c = *s;
  445.       *s = 0;
  446.       slen_max = pathconf (filename, _PC_NAME_MAX);
  447.       *s = c;
  448.     }
  449. #endif
  450.   if (slen_max == -1) {
  451. #ifdef HAVE_LONG_FILE_NAMES
  452.     slen_max = 255;
  453. #else
  454.     slen_max = 14;
  455. #endif
  456.   }
  457.   if (slen + extlen <= slen_max)
  458.     strcpy (s + slen, ext);
  459.   else
  460.     {
  461.       if (slen_max <= slen) {
  462.     /* Try to preserve difference between .h .c etc.  */
  463.     if (slen == slen_max && s[slen - 2] == '.')
  464.       s[slen - 2] = s[slen - 1];
  465.  
  466.     slen = slen_max - 1;
  467.       }
  468.       s[slen] = e;
  469.       s[slen + 1] = 0;
  470.     }
  471. }
  472.