home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / apple2 / 63 < prev    next >
Encoding:
Internet Message Format  |  1991-05-03  |  6.8 KB

  1. From: jac@yoko.rutgers.edu (Jonathan A. Chandross)
  2. Newsgroups: comp.sources.apple2
  3. Subject: v001SRC044:  tar -- Unpack Tar Archives For Orca (GS)
  4. Message-ID: <May.2.17.09.51.1991.318@yoko.rutgers.edu>
  5. Date: 2 May 91 21:09:53 GMT
  6. Approved: jac@paul.rutgers.edu
  7.  
  8.  
  9. Submitted-by: Jawaid Bazyar (bazyar@cs.uiuc.edu)
  10. Posting-number: Volume 1, Source:44
  11. Archive-name: util/gs/shell/orca/tar
  12. Architecture: ONLY_2gs
  13. Version-number: 1.2
  14.  
  15. This package will unpack tar archives.
  16.  
  17. Requires Orca shell.
  18.  
  19. Enjoy.
  20.  
  21. ###################################
  22.  
  23. =tar.doc
  24. -===========
  25. -    TAR
  26. -===========
  27. -version 1.2
  28. -
  29. -tar -options [archive]\n"
  30. -
  31. -options:  t  - list files in archive (test)
  32. -          x  - extract files from archive
  33. -          f  - use file [archive] instead of tape
  34. -          v  - verbose mode
  35. -
  36. - tar is the standard Unix Tape ARchive utility.  This GS version only
  37. -supports reading file-based tar files.  As soon as I get my hands on
  38. -a SCSI tape drive, I'll include support for those.
  39. -
  40. - Also in the works is support for creating archives.
  41. -
  42. ------
  43. -
  44. -Jawaid Bazyar
  45. -Derek Taubert
  46. -
  47. -Copyright 1990 by Procyon Software
  48. -Freeware - distribute but don't sell!
  49. -
  50. -This utility is FreeWare.  Distribute them as much as you like, just
  51. -don't sell them or distribute modified versions.  Send me your comments -
  52. -I'm eager to hear from you for suggestions and improvements.
  53. -
  54. -Also, if you make any modifications to the code please do not redistribute
  55. -them. Instead, send me the changed source along with an explanation and
  56. -I will consider including your change in the next version.
  57. -
  58. -    Jawaid Bazyar
  59. -    1120 Maple Street
  60. -    Mt. Vernon, IL 62864
  61. -
  62. -    Internet/ARPAnet     bazyar@cs.uiuc.edu
  63. -    GEnie                J.BAZYAR
  64. -
  65. =tar.c
  66. -
  67. -/*
  68. -
  69. -        tar.c   - a GS version of the venerable Unix tape archive 
  70. -                  utility.
  71. -
  72. -        Copyright 1991, Procyon Software
  73. -        This code and the executable derived from it are hereby
  74. -        put in the public domain.
  75. -        Distribution/modification is free, pursuant to the rules
  76. -        outlined in the SHELLSTUFF.DOC file.
  77. -
  78. -*/
  79. -
  80. -#include <types.h>
  81. -#include <stdio.h>
  82. -#include <stdlib.h>
  83. -#include <string.h>
  84. -#include <stddef.h>
  85. -#include <gsos.h>
  86. -
  87. -byte buffer[1024];
  88. -char filename[255];
  89. -FILE *tarfile;
  90. -
  91. -int optVerbose, optFile, optExtract,
  92. -    optTest;
  93. -
  94. -/* pull a file out of the archive one block at a time */
  95. -
  96. -GSString255Ptr MakeGSString1(char *s)
  97. -{
  98. -GSString255Ptr n;
  99. -    n = malloc(sizeof(GSString255));
  100. -    strcpy((char *) n->text,s);
  101. -    n->length = strlen(s);
  102. -    return n;
  103. -}
  104. -
  105. -int extractFile(char *name, longword blocks, longword length)
  106. -{
  107. -word excess;
  108. -int got,get,i,e;
  109. -FILE *output;
  110. -char *d,dirName[256];
  111. -CreateRecGS c;
  112. -FileInfoRecGS inf;
  113. -
  114. -    if (optVerbose) printf("extracting %s (%d blocks)\n",name, blocks);
  115. -    blocks = length / 1024;
  116. -    excess = length % 1024;
  117. -    if (excess) blocks++;
  118. -
  119. -    if (name[0] == '/') {
  120. -        fprintf(stderr, "Can't extract to a volume name!\n");
  121. -        exit(1);
  122. -    }
  123. -    d = name;
  124. -    while ((d = strchr(d, '/')) != NULL) {
  125. -
  126. -        strncpy(dirName, name, d-name);
  127. -        dirName[(int) (d-name)] = '\0';
  128. -
  129. -        inf.pCount = 3;
  130. -        inf.pathname = MakeGSString1(dirName);
  131. -        GetFileInfoGS(&inf);
  132. -        if (e = toolerror()) {
  133. -          switch (e) {
  134. -            case 0x46:
  135. -            case 0x44: break;
  136. -            default:   fprintf(stderr, "error statting file %s (%x)\n",
  137. -                           dirName,e);
  138. -                       exit(1); break;
  139. -          }
  140. -        }
  141. -        else if (inf.fileType != 0x0F) {
  142. -            fprintf(stderr, "can't overwrite file %s\n", dirName);
  143. -            exit(1);
  144. -        }
  145. -
  146. -        if (e) {
  147. -            c.pCount = 3;
  148. -            c.pathname = inf.pathname;
  149. -            c.access = 0xC3;
  150. -            c.fileType = 0x0F;
  151. -            CreateGS(&c);
  152. -            if (e = toolerror()) {
  153. -                fprintf(stderr, "fatal GS/OS error %x\n",e);
  154. -                exit(1);
  155. -            }
  156. -        }
  157. -        free(inf.pathname);
  158. -        while (*d == '/') d++;
  159. -    }
  160. -
  161. -    if (!blocks) return 0;
  162. -    output = fopen(name, "wb");
  163. -    for (i = 0; i < blocks; i++) {
  164. -        if ((i == blocks-1) && excess) get = excess;
  165. -        else get = 1024;
  166. -        got = fread(buffer, sizeof(byte), (size_t) get, tarfile);
  167. -        if (got != get) { fprintf(stderr, "read error\n"); exit(1); }
  168. -
  169. -        if (fwrite(buffer, sizeof(byte), (size_t) got, output) < get)
  170. -          { fprintf(stderr, "write error\n"); exit(1); }
  171. -    }
  172. -    fclose(output);
  173. -}
  174. -
  175. -int testFile(char *name, longword blocks, longword length)
  176. -{
  177. -    printf("%s (%ld blocks)\n",name, blocks);
  178. -}
  179. -
  180. -void usage(void)
  181. -{
  182. -    fprintf(stderr,"Usage: tar -options [archive]\n"
  183. -           "  options:  t  - list files in archive (test)\n"
  184. -           "            x  - extract files from archive\n"
  185. -           "            f  - use file [archive] instead of tape\n"
  186. -           "            v  - verbose mode\n");
  187. -    exit(1);
  188. -}
  189. -
  190. -void parseOpts(char *opts)
  191. -{
  192. -char *i = opts;
  193. -
  194. -    while (*i != '\0') {
  195. -        switch (*i) {
  196. -            case 'x': if (optTest) usage();
  197. -                      optExtract = 1; break;
  198. -            case 't': if (optExtract) usage();
  199. -                      optTest = 1; break;
  200. -            case 'f': optFile = 1; break;
  201. -            case 'v': optVerbose = 1; break;
  202. -            default: usage();
  203. -        }
  204. -        i++;
  205. -    }
  206. -}
  207. -
  208. -int main(int argc, char *argv[])
  209. -{
  210. -longword block;
  211. -longword size;
  212. -longword fileBlocks;
  213. -word got;
  214. -int SessionPB = 0;
  215. -
  216. -    block = 0;
  217. -    optVerbose = optFile = optExtract = optTest = 0;
  218. -
  219. -    if (argc == 1) usage();
  220. -    if (argv[1][0] == '-') parseOpts(&argv[1][1]);
  221. -    else parseOpts(argv[1]);
  222. -
  223. -    if (optFile) tarfile = fopen(argv[2], "rb");
  224. -    else { fprintf(stderr, "no SCSI tape found\n"); exit(1); }
  225. -
  226. -    if (!(optExtract || optTest)) usage();
  227. -    BeginSession(&SessionPB);
  228. -
  229. -    do {
  230. -        if (fseek(tarfile, (long) block*512, SEEK_SET)) {
  231. -            fprintf(stderr, "Seek error- aborting\n"); exit(1);
  232. -        }
  233. -        got = fread(buffer, sizeof(byte), (size_t) 512, tarfile);
  234. -        if (!buffer[0]) break;
  235. -        if (got == 0) { fprintf(stderr, "Read error- aborting\n"); exit(1); }
  236. -        if (got == 512) {
  237. -            sscanf(buffer+0174, "%lo", &size);
  238. -
  239. -            fileBlocks = (size / 512);
  240. -            if (size % 512) fileBlocks++;
  241. -
  242. -            block += fileBlocks + 1;
  243. -            strcpy(filename, (char *) buffer); /* copy the filename for future
  244. -                                         reeference */
  245. -            if (optExtract) extractFile(filename, fileBlocks, size);
  246. -            else if (optTest) testFile(filename, fileBlocks, size);
  247. -
  248. -            buffer[0] = 0;
  249. -        }
  250. -    } while (got == 512);
  251. -    fclose(tarfile);
  252. -    EndSession(&SessionPB);
  253. -    return 0;
  254. -}
  255. -
  256. + END OF ARCHIVE
  257.