summaryrefslogtreecommitdiffstats
path: root/Utilities/cmlibarchive/contrib/untar.c
diff options
context:
space:
mode:
authorDavid Cole <david.cole@kitware.com>2012-01-09 19:10:01 (GMT)
committerCMake Topic Stage <kwrobot@kitware.com>2012-01-09 19:10:01 (GMT)
commit5a5c32e1f22cae3cdba3e5be142ee84b9e4a8d4d (patch)
tree7ac1ebd509d98ed93d796ce3bf0be1778a20dfe5 /Utilities/cmlibarchive/contrib/untar.c
parente1c07a4342b719b982ffa099d930d2e193591114 (diff)
parent6c611c6b94ef54cbfdc81a1f049032f13a0ef024 (diff)
downloadCMake-5a5c32e1f22cae3cdba3e5be142ee84b9e4a8d4d.zip
CMake-5a5c32e1f22cae3cdba3e5be142ee84b9e4a8d4d.tar.gz
CMake-5a5c32e1f22cae3cdba3e5be142ee84b9e4a8d4d.tar.bz2
Merge topic 'update-libarchive'
6c611c6 libarchive: Restore CMake 2.6.3 as minimum version 2f5b677 libarchive: Update README-CMake.txt for new snapshot 156cb3b Merge branch 'libarchive-upstream' into update-libarchive fd42bf1 libarchive: Set .gitattributes to allow trailing whitespace 4f4fe6e libarchive 3.0.2-r4051 (reduced) 65b6e19 libarchive: Avoid bogus conversion warning from PGI compiler 9ccaeb1 libarchive: Suppress PathScale compiler warnings 2309438 libarchive: Rename isoent_rr_move_dir parameter isoent => curent b6ca96e libarchive: Include linux/types.h before linux/fiemap.h f293b73 libarchive: Define _XOPEN_SOURCE=500 on HP-UX 6781a09 libarchive: Cleanup after ZLIB_WINAPI check f15d757 libarchive: Remove hard-coded build configuration 3a9f449 libarchive: Use Apple copyfile.h API only if available 6af6b96 libarchive: Do not use MNT_NOATIME if not defined 02d5e40 libarchive: Check for 'struct statvfs' member 'f_iosize' 8b7ee30 libarchive: Do not use ST_NOATIME if not defined ...
Diffstat (limited to 'Utilities/cmlibarchive/contrib/untar.c')
-rw-r--r--Utilities/cmlibarchive/contrib/untar.c225
1 files changed, 0 insertions, 225 deletions
diff --git a/Utilities/cmlibarchive/contrib/untar.c b/Utilities/cmlibarchive/contrib/untar.c
deleted file mode 100644
index 0ea6455..0000000
--- a/Utilities/cmlibarchive/contrib/untar.c
+++ /dev/null
@@ -1,225 +0,0 @@
-/*
- * "untar" is an extremely simple tar extractor:
- * * A single C source file, so it should be easy to compile
- * and run on any system with a C compiler.
- * * Extremely portable standard C. The only non-ANSI function
- * used is mkdir().
- * * Reads basic ustar tar archives.
- * * Does not require libarchive or any other special library.
- *
- * To compile: cc -o untar untar.c
- *
- * Usage: untar <archive>
- *
- * In particular, this program should be sufficient to extract the
- * distribution for libarchive, allowing people to bootstrap
- * libarchive on systems that do not already have a tar program.
- *
- * To unpack libarchive-x.y.z.tar.gz:
- * * gunzip libarchive-x.y.z.tar.gz
- * * untar libarchive-x.y.z.tar
- *
- * Written by Tim Kientzle, March 2009.
- *
- * Released into the public domain.
- */
-
-/* These are all highly standard and portable headers. */
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-/* This is for mkdir(); this may need to be changed for some platforms. */
-#include <sys/stat.h> /* For mkdir() */
-
-/* Parse an octal number, ignoring leading and trailing nonsense. */
-static int
-parseoct(const char *p, size_t n)
-{
- int i = 0;
-
- while (*p < '0' || *p > '7') {
- ++p;
- --n;
- }
- while (*p >= '0' && *p <= '7' && n > 0) {
- i *= 8;
- i += *p - '0';
- ++p;
- --n;
- }
- return (i);
-}
-
-/* Returns true if this is 512 zero bytes. */
-static int
-is_end_of_archive(const char *p)
-{
- int n;
- for (n = 511; n >= 0; --n)
- if (p[n] != '\0')
- return (0);
- return (1);
-}
-
-/* Create a directory, including parent directories as necessary. */
-static void
-create_dir(char *pathname, int mode)
-{
- char *p;
- int r;
-
- /* Strip trailing '/' */
- if (pathname[strlen(pathname) - 1] == '/')
- pathname[strlen(pathname) - 1] = '\0';
-
- /* Try creating the directory. */
- r = mkdir(pathname, mode);
-
- if (r != 0) {
- /* On failure, try creating parent directory. */
- p = strrchr(pathname, '/');
- if (p != NULL) {
- *p = '\0';
- create_dir(pathname, 0755);
- *p = '/';
- r = mkdir(pathname, mode);
- }
- }
- if (r != 0)
- fprintf(stderr, "Could not create directory %s\n", pathname);
-}
-
-/* Create a file, including parent directory as necessary. */
-static FILE *
-create_file(char *pathname, int mode)
-{
- FILE *f;
- f = fopen(pathname, "w+");
- if (f == NULL) {
- /* Try creating parent dir and then creating file. */
- char *p = strrchr(pathname, '/');
- if (p != NULL) {
- *p = '\0';
- create_dir(pathname, 0755);
- *p = '/';
- f = fopen(pathname, "w+");
- }
- }
- return (f);
-}
-
-/* Verify the tar checksum. */
-static int
-verify_checksum(const char *p)
-{
- int n, u = 0;
- for (n = 0; n < 512; ++n) {
- if (n < 148 || n > 155)
- /* Standard tar checksum adds unsigned bytes. */
- u += ((unsigned char *)p)[n];
- else
- u += 0x20;
-
- }
- return (u == parseoct(p + 148, 8));
-}
-
-/* Extract a tar archive. */
-static void
-untar(FILE *a, const char *path)
-{
- char buff[512];
- FILE *f = NULL;
- size_t bytes_read;
- int filesize;
-
- printf("Extracting from %s\n", path);
- for (;;) {
- bytes_read = fread(buff, 1, 512, a);
- if (bytes_read < 512) {
- fprintf(stderr,
- "Short read on %s: expected 512, got %d\n",
- path, bytes_read);
- return;
- }
- if (is_end_of_archive(buff)) {
- printf("End of %s\n", path);
- return;
- }
- if (!verify_checksum(buff)) {
- fprintf(stderr, "Checksum failure\n");
- return;
- }
- filesize = parseoct(buff + 124, 12);
- switch (buff[156]) {
- case '1':
- printf(" Ignoring hardlink %s\n", buff);
- break;
- case '2':
- printf(" Ignoring symlink %s\n", buff);
- break;
- case '3':
- printf(" Ignoring character device %s\n", buff);
- break;
- case '4':
- printf(" Ignoring block device %s\n", buff);
- break;
- case '5':
- printf(" Extracting dir %s\n", buff);
- create_dir(buff, parseoct(buff + 100, 8));
- filesize = 0;
- break;
- case '6':
- printf(" Ignoring FIFO %s\n", buff);
- break;
- default:
- printf(" Extracting file %s\n", buff);
- f = create_file(buff, parseoct(buff + 100, 8));
- break;
- }
- while (filesize > 0) {
- bytes_read = fread(buff, 1, 512, a);
- if (bytes_read < 512) {
- fprintf(stderr,
- "Short read on %s: Expected 512, got %d\n",
- path, bytes_read);
- return;
- }
- if (filesize < 512)
- bytes_read = filesize;
- if (f != NULL) {
- if (fwrite(buff, 1, bytes_read, f)
- != bytes_read)
- {
- fprintf(stderr, "Failed write\n");
- fclose(f);
- f = NULL;
- }
- }
- filesize -= bytes_read;
- }
- if (f != NULL) {
- fclose(f);
- f = NULL;
- }
- }
-}
-
-int
-main(int argc, char **argv)
-{
- FILE *a;
-
- ++argv; /* Skip program name */
- for ( ;*argv != NULL; ++argv) {
- a = fopen(*argv, "r");
- if (a == NULL)
- fprintf(stderr, "Unable to open %s\n", *argv);
- else {
- untar(a, *argv);
- fclose(a);
- }
- }
- return (0);
-}