home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / tar-1.11.8-src.tgz / tar.out / fsf / tar / src / mangle.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  3KB  |  124 lines

  1. /* mangle.c -- encode long filenames
  2.    Copyright (C) 1988, 1992, 1994 Free Software Foundation, Inc.
  3.  
  4.    This file is part of GNU Tar.
  5.  
  6.    GNU Tar is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    GNU Tar is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with GNU Tar; see the file COPYING.  If not, write to
  18.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "system.h"
  21.  
  22. #include <time.h>
  23. time_t time ();
  24.  
  25. #include "tar.h"
  26.  
  27. extern struct stat hstat;    /* stat struct corresponding */
  28.  
  29. struct mangled
  30.   {
  31.     struct mangled *next;
  32.     int type;
  33.     char mangled[NAMSIZ];
  34.     char *linked_to;
  35.     char normal[1];
  36.   };
  37.  
  38. /* Should use a hash table, etc. .  */
  39. struct mangled *first_mangle;
  40. int mangled_num = 0;
  41.  
  42. /*---.
  43. | ?  |
  44. `---*/
  45.  
  46. void
  47. extract_mangle (void)
  48. {
  49.   char *buf;
  50.   char *fromtape;
  51.   char *to;
  52.   char *ptr, *ptrend;
  53.   char *nam1, *nam1end;
  54.   int size;
  55.   int copied;
  56.  
  57.   size = hstat.st_size;
  58.   buf = to = xmalloc ((size_t) (size + 1));
  59.   buf[size] = '\0';
  60.   while (size > 0)
  61.     {
  62.       fromtape = findrec ()->charptr;
  63.       if (fromtape == 0)
  64.     {
  65.       ERROR ((0, 0, _("Unexpected EOF in mangled names")));
  66.       return;
  67.     }
  68.       copied = endofrecs ()->charptr - fromtape;
  69.       if (copied > size)
  70.     copied = size;
  71.       memcpy (to, fromtape, (size_t) copied);
  72.       to += copied;
  73.       size -= copied;
  74.       userec ((union record *) (fromtape + copied - 1));
  75.     }
  76.   for (ptr = buf; *ptr; ptr = ptrend)
  77.     {
  78.       ptrend = strchr (ptr, '\n');
  79.       *ptrend++ = '\0';
  80.  
  81.       if (!strncmp (ptr, "Rename ", 7))
  82.     {
  83.       nam1 = ptr + 7;
  84.       nam1end = strchr (nam1, ' ');
  85.       while (strncmp (nam1end, " to ", 4))
  86.         {
  87.           nam1end++;
  88.           nam1end = strchr (nam1end, ' ');
  89.         }
  90.       *nam1end = '\0';
  91.       if (ptrend[-2] == '/')
  92.         ptrend[-2] = '\0';
  93.       un_quote_string (nam1end + 4);
  94.       if (rename (nam1, nam1end + 4))
  95.         ERROR ((0, errno, _("Cannot rename %s to %s"), nam1, nam1end + 4));
  96.       else if (flag_verbose)
  97.         WARN ((0, 0, _("Renamed %s to %s"), nam1, nam1end + 4));
  98.     }
  99. #ifdef S_ISLNK
  100.       else if (!strncmp (ptr, "Symlink ", 8))
  101.     {
  102.       nam1 = ptr + 8;
  103.       nam1end = strchr (nam1, ' ');
  104.       while (strncmp (nam1end, " to ", 4))
  105.         {
  106.           nam1end++;
  107.           nam1end = strchr (nam1end, ' ');
  108.         }
  109.       *nam1end = '\0';
  110.       un_quote_string (nam1);
  111.       un_quote_string (nam1end + 4);
  112.       if (symlink (nam1, nam1end + 4)
  113.           && (unlink (nam1end + 4) || symlink (nam1, nam1end + 4)))
  114.         ERROR ((0, errno, _("Cannot symlink %s to %s"),
  115.             nam1, nam1end + 4));
  116.       else if (flag_verbose)
  117.         WARN ((0, 0, _("Symlinked %s to %s"), nam1, nam1end + 4));
  118.     }
  119. #endif
  120.       else
  121.     ERROR ((0, 0, _("Unknown demangling command %s"), ptr));
  122.     }
  123. }
  124.