home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / XAP / XFILEMAN / XFILEMAN.TAR / xfilemanager / tar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-22  |  7.9 KB  |  317 lines

  1. /*
  2.  * Copyright 1993 by Ove Kalkan, Cremlingen, Germany
  3.  *
  4.  * Permission to use, copy, modify, distribute and sell this software and it's
  5.  * documentation for any purpose is hereby granted without fee, rpovided that
  6.  * the above copyright notice and this permission appear in supporting
  7.  * documentation, and that the name of Ove Kalkan not to be used in
  8.  * advertising or publicity pertaining to distributiopn of the software without
  9.  * specific, written prior permission. Ove Kalkan makes no representations
  10.  * about the suitability of this software for any purpose. It is provided
  11.  * as is without express or implied warranty.
  12.  *
  13.  * OVE KALKAN DISPLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  14.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABLILITY AND FITNESS, IN NO
  15.  * EVENT SHALL OVE KALKAN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  16.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  17.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  18.  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  19.  * PERFORMANCE OF THIS SOFTWARE.
  20.  *
  21.  * $Header: filename,v 1.0 yyyy/mm/dd hh:mm:ss loginname Exp $
  22.  */
  23.  
  24.  
  25. #include "global.h"
  26.  
  27.  
  28. /*
  29.  * Global variables
  30.  */
  31.  
  32.  
  33. /*********************************************************
  34.  * name:    fillTar
  35.  * description:    Einlesen eine Tar-Files und fuellen eines Folders
  36.  * input:    Folder_Glyph    *folder
  37.  * output:    none
  38.  * author:    Ove Kalkan
  39.  * date:    24.Aug.1993
  40.  *********************************************************/
  41. void    fillTar (Folder_Glyph *folder)
  42. {
  43.     FILE    *pipe;
  44.     char    s[1024];
  45.     char    *got = getPath(folder->dir);
  46.  
  47.     folder->file_count = 0;
  48.     sprintf(s,"tar -tvf %s",got);
  49.     free(got);
  50.     if (!(pipe = popen(s,"r")))
  51.         return;
  52.     readTar(folder,pipe);
  53.     pclose(pipe);
  54. }
  55.  
  56.  
  57. /*********************************************************
  58.  * name:    fillCTar
  59.  * description:    Einlesen eine CTar-Files und fuellen eines Folders
  60.  * input:    Folder_Glyph    *folder
  61.  * output:    none
  62.  * author:    Ove Kalkan
  63.  * date:    24.Aug.1993
  64.  *********************************************************/
  65. void    fillCTar (Folder_Glyph *folder)
  66. {
  67.     FILE    *pipe;
  68.     char    s[1024];
  69.     char    *got = getPath(folder->dir);
  70.  
  71.     folder->file_count = 0;
  72.     sprintf(s,"zcat < '%s' | tar -tvf -",got);
  73.     free(got);
  74.     if (!(pipe = popen(s,"r")))
  75.         return;
  76.     readTar(folder,pipe);
  77.     pclose(pipe);
  78. }
  79.  
  80.  
  81. /*********************************************************
  82.  * name:    readTar
  83.  * description:    Auslesen des Tar-Listings von einer Pipe
  84.  * input:    Folder_Glyph *folder
  85.  *        FILE *pipe
  86.  * output:    none
  87.  * author:    Ove Kalkan
  88.  * date:    24.Aug.1993
  89.  *********************************************************/
  90. void    readTar (Folder_Glyph *folder, FILE *pipe)
  91. {
  92.     char    s[1024];
  93.  
  94. #ifdef DEBUG
  95.     debug("tar.c: readTar - start\n");
  96. #endif
  97.     if (folder->file_count) {
  98.         int    i;
  99.         for (i = 0; i < folder->file_count; i++) {
  100.             if (folder->file[i]) {
  101.                 if (folder->file[i]->name)
  102.                     free(folder->file[i]->name);
  103.                 free(folder->file[i]);
  104.             }
  105.         }
  106.     }
  107.     folder->size = 0;
  108.     folder->total_count = 0;
  109.     folder->file_count = 0;
  110.     folder->file = NULL;
  111.  
  112.     /*
  113.      * Neuen Folder erstellen
  114.      */
  115.     s[1023] = '\0';
  116.     if (!defaults.multi_window) {    /* Erster Eintrag = root_dir */
  117.         File_Glyph    *nf;
  118.  
  119.         /*
  120.          * Zunaechst einmal das File-Feld erweitern
  121.          */
  122.         if (!folder->file) {
  123.             if (!(folder->file = (File_Glyph **) malloc(sizeof(File_Glyph *))))
  124.                 FATAL_ERROR("readTar: malloc failed");
  125.         }
  126.         else {
  127.             if (!(folder->file = (File_Glyph **) realloc((void *) folder->file,
  128.                                 sizeof(File_Glyph *))))
  129.                 FATAL_ERROR("readTar: realloc failed");
  130.         }
  131.         if (!(nf = (File_Glyph *) malloc(sizeof(File_Glyph))))
  132.             FATAL_ERROR("readTar: alloc failed");
  133.         folder->file[0] = nf;
  134.  
  135.         if (!(nf->name = (char *) malloc(3)))
  136.             FATAL_ERROR("readTar: alloc error");
  137.         sprintf(nf->name,"..");
  138.         nf->prog_type = FILE_ROOT;
  139.         nf->size = 0;
  140.         nf->mode = 0;
  141.         nf->uid = 0;
  142.         nf->gid = 0;
  143.         folder->file_count++;
  144.     }
  145. #ifdef DEBUG
  146.     debug("Pipe open, starting read\n");
  147. #endif
  148.     while (fgets(s,1023,pipe)) {
  149.         File_Glyph    *nf;
  150.         char        *a;
  151.  
  152.         /*
  153.          * Zunaechst einmal das File-Feld erweitern
  154.          */
  155.         if (!folder->file) {
  156.             if (!(folder->file = (File_Glyph **) malloc(sizeof(File_Glyph *))))
  157.                 FATAL_ERROR("readTar: malloc failed");
  158.         }
  159.         else {
  160.             if (!(folder->file = (File_Glyph **) realloc((void *) folder->file,
  161.                                 sizeof(File_Glyph *) * (folder->file_count+1))))
  162.                 FATAL_ERROR("readTar: realloc failed");
  163.         }
  164.         if (!(nf = (File_Glyph *) malloc(sizeof(File_Glyph))))
  165.             FATAL_ERROR("readTar: alloc failed");
  166.         folder->file[folder->file_count] = nf;
  167.         folder->file_count++;        
  168.  
  169.         /*
  170.          * Neuen File-entry fuellen
  171.          */
  172.         a = strrchr(s,' ');    /* Name */
  173.         a++;
  174.         if (!(nf->name = (char *) malloc(strlen(a)+1)))
  175.             FATAL_ERROR("readTar: Malloc failed");
  176.         sprintf(nf->name,"%s",a);
  177.         nf->name[strlen(a)-1] = '\0';
  178. #ifdef DEBUG
  179.         debug("%d: (%s)\n",folder->file_count,nf->name);
  180. #endif
  181.         a -= 19;    /* This depends on how tar -tv prints it's list */
  182.         *a = '\0';
  183.         a = strrchr(s,' ');    /* Size */
  184.         nf->size = atoi(a);
  185.         if (!nf->size) {
  186.             nf->prog_type = FILE_DIR;
  187.             folder->size += 1024;
  188.             folder->total_count++;
  189.         }
  190.         else {
  191.             nf->prog_type = FILE_PLAIN;
  192.             folder->size += nf->size;
  193.         }
  194.         nf->mode = 0;
  195.         if (s[0] == 'r')
  196.             nf->mode |= S_IRUSR;
  197.         if (s[1] == 'w')
  198.             nf->mode |= S_IWUSR;
  199.         if (s[2] == 'x')
  200.             nf->mode |= S_IXUSR;
  201.         if (s[3] == 'r')
  202.             nf->mode |= S_IRGRP;
  203.         if (s[4] == 'w')
  204.             nf->mode |= S_IWGRP;
  205.         if (s[5] == 'x')
  206.             nf->mode |= S_IXGRP;
  207.         if (s[6] == 'r')
  208.             nf->mode |= S_IROTH;
  209.         if (s[7] == 'w')
  210.             nf->mode |= S_IWOTH;
  211.         if (s[8] == 'x')
  212.             nf->mode |= S_IXOTH;
  213.         
  214.         nf->uid = 0;
  215.         nf->gid = 0;
  216.     }
  217.     /*
  218.      * Infofeld setzen
  219.      */
  220.     {    Arg    args[1];
  221.         char    s[100];
  222.  
  223.         sprintf(s,"%d Files (%d Directories)     Unpacked Size: %d KBytes",
  224.                         folder->file_count - (defaults.multi_window ? 0 : 1),
  225.                         folder->total_count,
  226.                         folder->size/1024);
  227.         XtSetArg(args[0],XtNlabel,s);
  228.         XtSetValues(folder->info,args,1);
  229.     }
  230. #ifdef DEBUG
  231.     debug("readTar - end\n");
  232. #endif
  233. }
  234.  
  235.  
  236.  
  237. /*********************************************************
  238.  * name:    start_extract
  239.  * description:    Files aus Tar-Files heraus extrahieren
  240.  * input:    none
  241.  * output:    none
  242.  * author:    Ove Kalkan
  243.  * date:    27.Aug.1993
  244.  *********************************************************/
  245. void    start_extract (void)
  246. {
  247.     if (selc_fo && selc_fo->fs_type != FS_NORMAL) {
  248.         char    *got;
  249.         char    *cd_path;
  250.         char    *file;
  251.         char    s[1024];
  252.  
  253.         if (selc_f == 16000 || selc_fo->file[selc_f]->prog_type == FILE_ROOT)
  254.             return;
  255.         got = getPath(selc_fo->dir);    /* Das Archive-File */
  256.         cd_path = getPath(selc_fo->dir->parent);
  257.         file = selc_fo->file[selc_f]->name;
  258.         if (selc_fo->fs_type == FS_TAR)
  259.             sprintf(s,"cd %s; tar -xf %s %s",cd_path,got,file);
  260.         else if (selc_fo->fs_type == FS_CTAR)
  261.             sprintf(s,"cd %s; zcat %s | tar -xf - %s",cd_path,got,file);
  262.         else {
  263.             free(got);
  264.             free(cd_path);
  265.             return;
  266.         }
  267.         system(s);
  268.         refreshFolderByPathname (cd_path);
  269.         free(got);
  270.         free(cd_path);
  271.     }
  272.     else if (multi_fo && multi_fo->fs_type != FS_NORMAL) {
  273.         char    *mark,*pmark;
  274.         char    *got;
  275.         char    *cd_path;
  276.         char    *file = NULL;
  277.         char    s[4096];
  278.         int    i;
  279.         int    j = 1;
  280.  
  281.         if (multi_vs == 16000 || multi_fo->file[multi_vs]->prog_type == FILE_ROOT)
  282.             return;
  283.         got = getPath(multi_fo->dir);    /* Das Archive-File */
  284.         cd_path = getPath(multi_fo->dir->parent);
  285.  
  286.         for (i = multi_vs; i <= multi_ve; i++) {
  287.             j = j + strlen(multi_fo->file[i]->name) + 1;
  288.             if (!file) {
  289.                 if (!(file = (char *) malloc (j)))
  290.                     FATAL_ERROR("start_extract: malloc failed.");
  291.                 file[0] = '\0';
  292.             }
  293.             else {
  294.                 if (!(file = (char *) realloc ((void *) file,j)))
  295.                     FATAL_ERROR("start_extract: malloc failed.");
  296.             }
  297.             sprintf(file,"%s%s ",file,multi_fo->file[i]->name);
  298.         }
  299.  
  300.         if (multi_fo->fs_type == FS_TAR)
  301.             sprintf(s,"cd %s; tar -xf %s %s",cd_path,got,file);
  302.         else if (multi_fo->fs_type == FS_CTAR)
  303.             sprintf(s,"cd %s; zcat %s | tar -xf - %s",cd_path,got,file);
  304.         else {
  305.             free(got);
  306.             free(cd_path);
  307.             free(file);
  308.             return;
  309.         }
  310.         system(s);
  311.         refreshFolderByPathname (cd_path);
  312.         free(got);
  313.         free(file);
  314.         free(cd_path);
  315.     }
  316. }
  317.