home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gtak212.zip / 1.10 / mangle.c < prev    next >
C/C++ Source or Header  |  1992-09-12  |  7KB  |  281 lines

  1. /*****************************************************************************
  2.  * $Id: mangle.c,v 1.3 1992/09/12 15:57:36 ak Exp $
  3.  *****************************************************************************
  4.  * $Log: mangle.c,v $
  5.  * Revision 1.3  1992/09/12  15:57:36  ak
  6.  * - Usenet patches for GNU TAR 1.10
  7.  * - Bugfixes and patches of Kai Uwe Rommel:
  8.  *         filename conversion for FAT
  9.  *         EMX 0.8e
  10.  *         -0..1 alias for a: b:
  11.  *         -2..7 alias for +++TAPE$x
  12.  *
  13.  * Revision 1.2  1992/09/02  20:08:16  ak
  14.  * Version AK200
  15.  * - Tape access
  16.  * - Quick file access
  17.  * - OS/2 extended attributes
  18.  * - Some OS/2 fixes
  19.  * - Some fixes of Kai Uwe Rommel
  20.  *
  21.  * Revision 1.1.1.1  1992/09/02  19:21:59  ak
  22.  * Original GNU Tar 1.10 with some filenames changed for FAT compatibility.
  23.  *
  24.  * Revision 1.1  1992/09/02  19:21:57  ak
  25.  * Initial revision
  26.  *
  27.  *****************************************************************************/
  28.  
  29. static char *rcsid = "$Id: mangle.c,v 1.3 1992/09/12 15:57:36 ak Exp $";
  30.  
  31. /* Modified 9/2/91 to fix bug in write_mangle of doing strlen(ptr1) when ptr1
  32.    is only null terminated if malloc happens to have been kind.  This involved
  33.    adding the buffer_length operation to port.c as well, which see.  Also,
  34.    I added a flush_buffer at the end of write_mangle to repair a memory leak.
  35.     -Max Hailperin <max@nic.gac.edu> 9/2/91 */
  36.  
  37. /* I added inverse_find_mangled to map a mangled name into the normal one for
  38.    the say of verifying archives with mangled names.  See diffarch.c.
  39.     -Max Hailperin <max@nic.gac.edu> 8/1/91 */
  40.  
  41. #include <sys/types.h>
  42. #include <sys/stat.h>
  43. #include "tar.h"
  44.  
  45. #ifdef __STDC__
  46. #define VOIDSTAR void *
  47. #else
  48. #define VOIDSTAR char *
  49. #endif
  50. extern VOIDSTAR ck_malloc();
  51. extern VOIDSTAR init_buffer();
  52. extern char *quote_copy_string();
  53. extern char *get_buffer();
  54. extern char *index();
  55.  
  56. extern union record *start_header();
  57.  
  58. extern struct stat hstat;        /* Stat struct corresponding */
  59.  
  60. struct mangled {
  61.     struct mangled *next;
  62.     int type;
  63.     char mangled[NAMSIZ];
  64.     char *linked_to;
  65.     char normal[1];
  66. };
  67.  
  68.  
  69. /* Should use a hash table, etc. .  */
  70. struct mangled *first_mangle;
  71. int mangled_num = 0;
  72.  
  73. char *
  74. find_mangled (name)
  75. char *name;
  76. {
  77.     struct mangled *munge;
  78.  
  79.     for(munge=first_mangle;munge;munge=munge->next)
  80.         if(!strcmp(name,munge->normal))
  81.             return munge->mangled;
  82.     return 0;
  83. }
  84.  
  85.  
  86. char *
  87. inverse_find_mangled (name)
  88. char *name;
  89. {
  90.     struct mangled *munge;
  91.  
  92.     for(munge=first_mangle;munge;munge=munge->next)
  93.         if(!strcmp(name,munge->mangled))
  94.             return munge->normal;
  95.     return 0;
  96. }
  97.  
  98.  
  99. #ifdef S_IFLNK
  100. void add_symlink_mangle(symlink, linkto, buffer)
  101. char *symlink;
  102. char *linkto;
  103. char *buffer;
  104. {
  105.     struct mangled *munge,*kludge;
  106.  
  107.     munge=(struct mangled *)ck_malloc(sizeof(struct mangled)+strlen(symlink)+strlen(linkto)+2);
  108.     if(!first_mangle)
  109.         first_mangle=munge;
  110.     else {
  111.         for(kludge=first_mangle;kludge->next;kludge=kludge->next)
  112.             ;
  113.         kludge->next=munge;
  114.     }
  115.     munge->type=1;
  116.     munge->next=0;
  117.     strcpy(munge->normal,symlink);
  118.     munge->linked_to=munge->normal+strlen(symlink)+1;
  119.     strcpy(munge->linked_to,linkto);
  120.     sprintf(munge->mangled,"@@MaNgLeD.%d",mangled_num++);
  121.     strncpy(buffer,munge->mangled,NAMSIZ);
  122. }
  123. #endif
  124.  
  125. void
  126. add_mangle (name, buffer)
  127. char *name;
  128. char *buffer;
  129. {
  130.     struct mangled *munge,*kludge;
  131.  
  132.     munge=(struct mangled *)ck_malloc(sizeof(struct mangled)+strlen(name));
  133.     if(!first_mangle)
  134.         first_mangle=munge;
  135.     else {
  136.         for(kludge=first_mangle;kludge->next;kludge=kludge->next)
  137.             ;
  138.         kludge->next=munge;
  139.     }
  140.     munge->next=0;
  141.     munge->type=0;
  142.     strcpy(munge->normal,name);
  143.     sprintf(munge->mangled,"@@MaNgLeD.%d",mangled_num++);
  144.     strncpy(buffer,munge->mangled,NAMSIZ);
  145. }
  146.  
  147. void
  148. write_mangled()
  149. {
  150.     struct mangled *munge;
  151.     struct stat hstat;
  152.     union record *header;
  153.     char *ptr1,*ptr2;
  154.     VOIDSTAR the_buffer;
  155.     int size;
  156.     int bufsize;
  157.  
  158.     if(!first_mangle)
  159.         return;
  160.     the_buffer=init_buffer();
  161.     for(munge=first_mangle,size=0;munge;munge=munge->next) {
  162.         ptr1=quote_copy_string(munge->normal);
  163.         if(!ptr1)
  164.             ptr1=munge->normal;
  165.         if(munge->type) {
  166.             add_buffer(the_buffer,"Symlink ",8);
  167.             add_buffer(the_buffer,ptr1,strlen(ptr1));
  168.             add_buffer(the_buffer," to ",4);
  169.             
  170.             if(ptr2=quote_copy_string(munge->linked_to)) {
  171.                 add_buffer(the_buffer,ptr2,strlen(ptr2));
  172.                 free(ptr2);
  173.             } else
  174.                 add_buffer(the_buffer,munge->linked_to,strlen(munge->linked_to));
  175.         } else {
  176.             add_buffer(the_buffer,"Rename ",7);
  177.             add_buffer(the_buffer,munge->mangled,strlen(munge->mangled));
  178.             add_buffer(the_buffer," to ",4);
  179.             add_buffer(the_buffer,ptr1,strlen(ptr1));
  180.         }
  181.         add_buffer(the_buffer,"\n",1);
  182.         if(ptr1!=munge->normal)
  183.             free(ptr1);
  184.     }
  185.  
  186.     bzero(&hstat,sizeof(struct stat));
  187.     hstat.st_atime=hstat.st_mtime=hstat.st_ctime=time(0);
  188.     ptr1=get_buffer(the_buffer);
  189.     hstat.st_size=buffer_length(the_buffer);
  190.  
  191.     header=start_header("././@MaNgLeD_NaMeS",&hstat);
  192.     header->header.linkflag=LF_NAMES;
  193.     finish_header(header);
  194.     size=hstat.st_size;
  195.     header=findrec();
  196.     bufsize = endofrecs()->charptr - header->charptr;
  197.  
  198.     while(bufsize<size) {
  199.         bcopy(ptr1,header->charptr,bufsize);
  200.         ptr1+=bufsize;
  201.         size-=bufsize;
  202.         userec(header+(bufsize-1)/RECORDSIZE);
  203.         header=findrec();
  204.         bufsize = endofrecs()->charptr - header->charptr;
  205.     }
  206.     bcopy(ptr1,header->charptr,size);
  207.     bzero(header->charptr+size,bufsize-size);
  208.     userec(header+(size-1)/RECORDSIZE);
  209.         flush_buffer(the_buffer);
  210. }
  211.  
  212. void
  213. extract_mangle(head)
  214. union record *head;
  215. {
  216.     char *buf;
  217.     char *fromtape;
  218.     char *to;
  219.     char *ptr,*ptrend;
  220.     char *nam1,*nam1end;
  221.     int size;
  222.     int copied;
  223.  
  224.     size=hstat.st_size;
  225.     buf=to=ck_malloc(size+1);
  226.     buf[size]='\0';
  227.     while(size>0) {
  228.         fromtape=findrec()->charptr;
  229.         if(fromtape==0) {
  230.             msg("Unexpected EOF in mangled names!");
  231.             return;
  232.         }
  233.         copied=endofrecs()->charptr-fromtape;
  234.         if(copied>size)
  235.             copied=size;
  236.         bcopy(fromtape,to,copied);
  237.         to+=copied;
  238.         size-=copied;
  239.         userec((union record *)(fromtape+copied-1));
  240.     }
  241.     for(ptr=buf;*ptr;ptr=ptrend) {
  242.         ptrend=index(ptr,'\n');
  243.         *ptrend++='\0';
  244.  
  245.         if(!strncmp(ptr,"Rename ",7)) {
  246.             nam1=ptr+7;
  247.             nam1end=index(nam1,' ');
  248.             while(strncmp(nam1end," to ",4)) {
  249.                 nam1end++;
  250.                 nam1end=index(nam1end,' ');
  251.             }
  252.             *nam1end='\0';
  253.             if(ptrend[-2]=='/')
  254.                 ptrend[-2]='\0';
  255.             un_quote_string(nam1end+4);
  256.             if(rename(nam1,nam1end+4))
  257.                 msg_perror("Can't rename %s to %s",nam1,nam1end+4);
  258.             else if(f_verbose)
  259.                 msg("Renamed %s to %s",nam1,nam1end+4);
  260.         }
  261. #ifdef S_IFLNK
  262.         else if(!strncmp(ptr,"Symlink ",8)) {
  263.             nam1=ptr+8;
  264.             nam1end=index(nam1,' ');
  265.             while(strncmp(nam1end," to ",4)) {
  266.                 nam1end++;
  267.                 nam1end=index(nam1end,' ');
  268.             }
  269.             un_quote_string(nam1);
  270.             un_quote_string(nam1end+4);
  271.             if(symlink(nam1,nam1end+4) && (unlink(nam1end+4) || symlink(nam1,nam1end+4)))
  272.                 msg_perror("Can't symlink %s to %s",nam1,nam1end+4);
  273.             else if(f_verbose)
  274.                 msg("Symlinkd %s to %s",nam1,nam1end+4);
  275.         }
  276. #endif
  277.         else
  278.             msg("Unknown demangling command %s",ptr);
  279.     }
  280. }
  281.