annotate src/win32/untar.c @ 7431:643cbc9a6035

[gaim-migrate @ 8036] This is good enough for CVS. This is new logging. It centers around the highly modular "GaimLogLogger," which controls how to write the log. Currently I only have the plain text logger. I wrote the beginning of an XML logger, but decided I didn't think it was that great an idea. Plugins can implement loggers themselves, so you can have, like, an SQL logger or something. The default logger writes to a file unique to the conversation, and they're saved on disk in a heirarchical fashion: ~/.gaim/logs/aim/seanegn/robflynn-date.log would be a conversation I had with Rob on date. What doesn't work: System logging The search button in the log viewer. Oh, chats probably don't log either, I didn't test. You can only log in plain text right now. Obviously, it's not done yet. But you can play around with it, and give it some love. I'll get back to it tomorrow after school, maybe. committer: Tailor Script <tailor@pidgin.im>
author Sean Egan <seanegan@gmail.com>
date Wed, 05 Nov 2003 06:15:49 +0000
parents 59ffe137176d
children 0f7452b1f777
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5005
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
1 /* untar.c */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
2
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
3 /*#define VERSION "1.4"*/
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
4
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
5 /* DESCRIPTION:
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
6 * Untar extracts files from an uncompressed tar archive, or one which
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
7 * has been compressed with gzip. Usually such archives will have file
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
8 * names that end with ".tar" or ".tgz" respectively, although untar
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
9 * doesn't depend on any naming conventions. For a summary of the
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
10 * command-line options, run untar with no arguments.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
11 *
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
12 * HOW TO COMPILE:
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
13 * Untar doesn't require any special libraries or compile-time flags.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
14 * A simple "cc untar.c -o untar" (or the local equivalent) is
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
15 * sufficient. Even "make untar" works, without needing a Makefile.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
16 * For Microsoft Visual C++, the command is "cl /D_WEAK_POSIX untar.c"
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
17 * (for 32 bit compilers) or "cl /F 1400 untar.c" (for 16-bit).
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
18 *
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
19 * IF YOU SEE COMPILER WARNINGS, THAT'S NORMAL; you can ignore them.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
20 * Most of the warnings could be eliminated by adding #include <string.h>
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
21 * but that isn't portable -- some systems require <strings.h> and
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
22 * <malloc.h>, for example. Because <string.h> isn't quite portable,
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
23 * and isn't really necessary in the context of this program, it isn't
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
24 * included.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
25 *
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
26 * PORTABILITY:
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
27 * Untar only requires the <stdio.h> header. It uses old-style function
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
28 * definitions. It opens all files in binary mode. Taken together,
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
29 * this means that untar should compile & run on just about anything.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
30 *
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
31 * If your system supports the POSIX chmod(2), utime(2), link(2), and
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
32 * symlink(2) calls, then you may wish to compile with -D_POSIX_SOURCE,
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
33 * which will enable untar to use those system calls to restore the
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
34 * timestamp and permissions of the extracted files, and restore links.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
35 * (For Linux, _POSIX_SOURCE is always defined.)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
36 *
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
37 * For systems which support some POSIX features but not enough to support
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
38 * -D_POSIX_SOURCE, you might be able to use -D_WEAK_POSIX. This allows
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
39 * untar to restore time stamps and file permissions, but not links.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
40 * This should work for Microsoft systems, and hopefully others as well.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
41 *
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
42 * AUTHOR & COPYRIGHT INFO:
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
43 * Written by Steve Kirkendall, kirkenda@cs.pdx.edu
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
44 * Placed in public domain, 6 October 1995
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
45 *
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
46 * Portions derived from inflate.c -- Not copyrighted 1992 by Mark Adler
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
47 * version c10p1, 10 January 1993
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
48 *
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
49 * Altered by Herman Bloggs <hermanator12002@yahoo.com>
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
50 * April 4, 2003
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
51 * Changes: Stripped out gz compression code, added better interface for
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
52 * untar.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
53 */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
54 #include <windows.h>
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
55 #include <stdio.h>
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
56 #include <io.h>
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
57 #include <string.h>
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
58 #include <stdlib.h>
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
59 #ifndef SEEK_SET
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
60 # define SEEK_SET 0
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
61 #endif
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
62
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
63 #ifdef _WEAK_POSIX
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
64 # ifndef _POSIX_SOURCE
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
65 # define _POSIX_SOURCE
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
66 # endif
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
67 #endif
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
68
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
69 #ifdef _POSIX_SOURCE
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
70 # include <sys/types.h>
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
71 # include <sys/stat.h>
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
72 # include <sys/utime.h>
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
73 # ifdef _WEAK_POSIX
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
74 # define mode_t int
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
75 # else
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
76 # include <unistd.h>
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
77 # endif
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
78 #endif
6425
26b739bc9f1a [gaim-migrate @ 6933]
Herman Bloggs <hermanator12002@yahoo.com>
parents: 5005
diff changeset
79 #include "debug.h"
5005
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
80 #include "untar.h"
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
81
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
82 #define mkdir(a,b) _mkdir((a))
7242
59ffe137176d [gaim-migrate @ 7817]
Herman Bloggs <hermanator12002@yahoo.com>
parents: 6425
diff changeset
83 #define untar_error( error, args... ) gaim_debug(GAIM_DEBUG_ERROR, "untar", error, ## args )
59ffe137176d [gaim-migrate @ 7817]
Herman Bloggs <hermanator12002@yahoo.com>
parents: 6425
diff changeset
84 #define untar_warning( warning, args... ) gaim_debug(GAIM_DEBUG_WARNING, "untar", warning, ## args )
6425
26b739bc9f1a [gaim-migrate @ 6933]
Herman Bloggs <hermanator12002@yahoo.com>
parents: 5005
diff changeset
85 #define untar_verbose( args... ) gaim_debug(GAIM_DEBUG_INFO, "untar", ## args )
26b739bc9f1a [gaim-migrate @ 6933]
Herman Bloggs <hermanator12002@yahoo.com>
parents: 5005
diff changeset
86
5005
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
87 #define WSIZE 32768 /* size of decompression buffer */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
88 #define TSIZE 512 /* size of a "tape" block */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
89 #define CR 13 /* carriage-return character */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
90 #define LF 10 /* line-feed character */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
91
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
92 typedef unsigned char Uchar_t;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
93 typedef unsigned short Ushort_t;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
94 typedef unsigned long Ulong_t;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
95
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
96 typedef struct
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
97 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
98 char filename[100]; /* 0 name of next file */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
99 char mode[8]; /* 100 Permissions and type (octal digits) */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
100 char owner[8]; /* 108 Owner ID (ignored) */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
101 char group[8]; /* 116 Group ID (ignored) */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
102 char size[12]; /* 124 Bytes in file (octal digits) */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
103 char mtime[12]; /* 136 Modification time stamp (octal digits)*/
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
104 char checksum[8]; /* 148 Header checksum (ignored) */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
105 char type; /* 156 File type (see below) */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
106 char linkto[100]; /* 157 Linked-to name */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
107 char brand[8]; /* 257 Identifies tar version (ignored) */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
108 char ownername[32]; /* 265 Name of owner (ignored) */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
109 char groupname[32]; /* 297 Name of group (ignored) */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
110 char devmajor[8]; /* 329 Device major number (ignored) */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
111 char defminor[8]; /* 337 Device minor number (ignored) */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
112 char prefix[155]; /* 345 Prefix of name (optional) */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
113 char RESERVED[12]; /* 500 Pad header size to 512 bytes */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
114 } tar_t;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
115 #define ISREGULAR(hdr) ((hdr).type < '1' || (hdr).type > '6')
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
116
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
117 Uchar_t slide[WSIZE];
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
118
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
119 static const char *inname = NULL; /* name of input archive */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
120 static FILE *infp = NULL; /* input byte stream */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
121 static FILE *outfp = NULL; /* output stream, for file currently being extracted */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
122 static Ulong_t outsize = 0; /* number of bytes remainin in file currently being extracted */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
123 static char **only = NULL; /* array of filenames to extract/list */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
124 static int nonlys = 0; /* number of filenames in "only" array; 0=extract all */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
125 static int didabs = 0; /* were any filenames affected by the absence of -p? */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
126
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
127 static untar_opt untarops = 0; /* Untar options */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
128
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
129 /* Options checked during untar process */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
130 #define LISTING (untarops & UNTAR_LISTING) /* 1 if listing, 0 if extracting */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
131 #define QUIET (untarops & UNTAR_QUIET) /* 1 to write nothing to stdout, 0 for normal chatter */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
132 #define VERBOSE (untarops & UNTAR_VERBOSE) /* 1 to write extra information to stdout */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
133 #define FORCE (untarops & UNTAR_FORCE) /* 1 to overwrite existing files, 0 to skip them */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
134 #define ABSPATH (untarops & UNTAR_ABSPATH) /* 1 to allow leading '/', 0 to strip leading '/' */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
135 #define CONVERT (untarops & UNTAR_CONVERT) /* 1 to convert newlines, 0 to leave unchanged */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
136
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
137 /*----------------------------------------------------------------------------*/
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
138
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
139 /* create a file for writing. If necessary, create the directories leading up
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
140 * to that file as well.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
141 */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
142 static FILE *createpath(name)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
143 char *name; /* pathname of file to create */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
144 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
145 FILE *fp;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
146 int i;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
147
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
148 /* if we aren't allowed to overwrite and this file exists, return NULL */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
149 if (!FORCE && access(name, 0) == 0)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
150 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
151 untar_warning("%s: exists, will not overwrite without \"FORCE option\"\n", name);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
152 return NULL;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
153 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
154
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
155 /* first try creating it the easy way */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
156 fp = fopen(name, CONVERT ? "w" : "wb");
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
157 if (fp)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
158 return fp;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
159
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
160 /* Else try making all of its directories, and then try creating
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
161 * the file again.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
162 */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
163 for (i = 0; name[i]; i++)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
164 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
165 /* If this is a slash, then temporarily replace the '/'
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
166 * with a '\0' and do a mkdir() on the resulting string.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
167 * Ignore errors for now.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
168 */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
169 if (name[i] == '/')
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
170 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
171 name[i] = '\0';
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
172 (void)mkdir(name, 0777);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
173 name[i] = '/';
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
174 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
175 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
176 fp = fopen(name, CONVERT ? "w" : "wb");
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
177 if (!fp)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
178 untar_error("Error opening: %s\n", name);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
179 return fp;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
180 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
181
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
182 /* Create a link, or copy a file. If the file is copied (not linked) then
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
183 * give a warning.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
184 */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
185 static void linkorcopy(src, dst, sym)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
186 char *src; /* name of existing source file */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
187 char *dst; /* name of new destination file */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
188 int sym; /* use symlink instead of link */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
189 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
190 FILE *fpsrc;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
191 FILE *fpdst;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
192 int c;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
193
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
194 /* Open the source file. We do this first to make sure it exists */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
195 fpsrc = fopen(src, "rb");
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
196 if (!fpsrc)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
197 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
198 untar_error("Error opening: %s\n", src);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
199 return;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
200 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
201
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
202 /* Create the destination file. On POSIX systems, this is just to
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
203 * make sure the directory path exists.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
204 */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
205 fpdst = createpath(dst);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
206 if (!fpdst)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
207 /* error message already given */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
208 return;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
209
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
210 #ifdef _POSIX_SOURCE
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
211 # ifndef _WEAK_POSIX
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
212 /* first try to link it over, instead of copying */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
213 fclose(fpdst);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
214 unlink(dst);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
215 if (sym)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
216 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
217 if (symlink(src, dst))
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
218 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
219 perror(dst);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
220 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
221 fclose(fpsrc);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
222 return;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
223 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
224 if (!link(src, dst))
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
225 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
226 /* This story had a happy ending */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
227 fclose(fpsrc);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
228 return;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
229 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
230
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
231 /* Dang. Reopen the destination again */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
232 fpdst = fopen(dst, "wb");
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
233 /* This *can't* fail */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
234
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
235 # endif /* _WEAK_POSIX */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
236 #endif /* _POSIX_SOURCE */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
237
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
238 /* Copy characters */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
239 while ((c = getc(fpsrc)) != EOF)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
240 putc(c, fpdst);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
241
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
242 /* Close the files */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
243 fclose(fpsrc);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
244 fclose(fpdst);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
245
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
246 /* Give a warning */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
247 untar_warning("%s: copy instead of link\n", dst);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
248 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
249
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
250 /* This calls fwrite(), possibly after converting CR-LF to LF */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
251 static void cvtwrite(blk, size, fp)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
252 Uchar_t *blk; /* the block to be written */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
253 Ulong_t size; /* number of characters to be written */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
254 FILE *fp; /* file to write to */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
255 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
256 int i, j;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
257 static Uchar_t mod[TSIZE];
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
258
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
259 if (CONVERT)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
260 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
261 for (i = j = 0; i < size; i++)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
262 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
263 /* convert LF to local newline convention */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
264 if (blk[i] == LF)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
265 mod[j++] = '\n';
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
266 /* If CR-LF pair, then delete the CR */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
267 else if (blk[i] == CR && (i+1 >= size || blk[i+1] == LF))
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
268 ;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
269 /* other characters copied literally */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
270 else
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
271 mod[j++] = blk[i];
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
272 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
273 size = j;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
274 blk = mod;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
275 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
276
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
277 fwrite(blk, (size_t)size, sizeof(Uchar_t), fp);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
278 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
279
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
280
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
281 /* Compute the checksum of a tar header block, and return it as a long int.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
282 * The checksum can be computed using either POSIX rules (unsigned bytes)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
283 * or Sun rules (signed bytes).
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
284 */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
285 static long checksum(tblk, sunny)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
286 tar_t *tblk; /* buffer containing the tar header block */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
287 int sunny; /* Boolean: Sun-style checksums? (else POSIX) */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
288 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
289 long sum;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
290 char *scan;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
291
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
292 /* compute the sum of the first 148 bytes -- everything up to but not
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
293 * including the checksum field itself.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
294 */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
295 sum = 0L;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
296 for (scan = (char *)tblk; scan < tblk->checksum; scan++)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
297 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
298 sum += (*scan) & 0xff;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
299 if (sunny && (*scan & 0x80) != 0)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
300 sum -= 256;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
301 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
302
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
303 /* for the 8 bytes of the checksum field, add blanks to the sum */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
304 sum += ' ' * sizeof tblk->checksum;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
305 scan += sizeof tblk->checksum;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
306
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
307 /* finish counting the sum of the rest of the block */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
308 for (; scan < (char *)tblk + sizeof *tblk; scan++)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
309 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
310 sum += (*scan) & 0xff;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
311 if (sunny && (*scan & 0x80) != 0)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
312 sum -= 256;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
313 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
314
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
315 return sum;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
316 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
317
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
318
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
319
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
320 /* list files in an archive, and optionally extract them as well */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
321 static int untar_block(Uchar_t *blk) {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
322 static char nbuf[256];/* storage space for prefix+name, combined */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
323 static char *name,*n2;/* prefix and name, combined */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
324 static int first = 1;/* Boolean: first block of archive? */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
325 long sum; /* checksum for this block */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
326 int i;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
327 tar_t tblk[1];
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
328
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
329 #ifdef _POSIX_SOURCE
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
330 static mode_t mode; /* file permissions */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
331 static struct utimbuf timestamp; /* file timestamp */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
332 #endif
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
333
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
334 /* make a local copy of the block, and treat it as a tar header */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
335 tblk[0] = *(tar_t *)blk;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
336
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
337 /* process each type of tape block differently */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
338 if (outsize > TSIZE)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
339 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
340 /* data block, but not the last one */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
341 if (outfp)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
342 cvtwrite(blk, (Ulong_t)TSIZE, outfp);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
343 outsize -= TSIZE;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
344 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
345 else if (outsize > 0)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
346 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
347 /* last data block of current file */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
348 if (outfp)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
349 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
350 cvtwrite(blk, outsize, outfp);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
351 fclose(outfp);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
352 outfp = NULL;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
353 #ifdef _POSIX_SOURCE
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
354 utime(nbuf, &timestamp);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
355 chmod(nbuf, mode);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
356 #endif
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
357 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
358 outsize = 0;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
359 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
360 else if ((tblk)->filename[0] == '\0')
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
361 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
362 /* end-of-archive marker */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
363 if (didabs)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
364 untar_warning("Removed leading slashes because \"ABSPATH option\" wasn't given.\n");
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
365 return 1;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
366 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
367 else
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
368 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
369 /* file header */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
370
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
371 /* half-assed verification -- does it look like header? */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
372 if ((tblk)->filename[99] != '\0'
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
373 || ((tblk)->size[0] < '0'
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
374 && (tblk)->size[0] != ' ')
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
375 || (tblk)->size[0] > '9')
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
376 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
377 if (first)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
378 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
379 untar_error("%s: not a valid tar file\n", inname);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
380 return 0;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
381 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
382 else
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
383 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
384 untar_error("Garbage detected; preceding file may be damaged\n");
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
385 return 0;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
386 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
387 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
388
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
389 /* combine prefix and filename */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
390 memset(nbuf, 0, sizeof nbuf);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
391 name = nbuf;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
392 if ((tblk)->prefix[0])
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
393 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
394 strncpy(name, (tblk)->prefix, sizeof (tblk)->prefix);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
395 strcat(name, "/");
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
396 strncat(name + strlen(name), (tblk)->filename,
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
397 sizeof (tblk)->filename);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
398 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
399 else
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
400 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
401 strncpy(name, (tblk)->filename,
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
402 sizeof (tblk)->filename);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
403 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
404
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
405 /* Convert any backslashes to forward slashes, and guard
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
406 * against doubled-up slashes. (Some DOS versions of "tar"
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
407 * get this wrong.) Also strip off leading slashes.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
408 */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
409 if (!ABSPATH && (*name == '/' || *name == '\\'))
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
410 didabs = 1;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
411 for (n2 = nbuf; *name; name++)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
412 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
413 if (*name == '\\')
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
414 *name = '/';
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
415 if (*name != '/'
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
416 || (ABSPATH && n2 == nbuf)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
417 || (n2 != nbuf && n2[-1] != '/'))
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
418 *n2++ = *name;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
419 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
420 if (n2 == nbuf)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
421 *n2++ = '/';
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
422 *n2 = '\0';
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
423
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
424 /* verify the checksum */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
425 for (sum = 0L, i = 0; i < sizeof((tblk)->checksum); i++)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
426 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
427 if ((tblk)->checksum[i] >= '0'
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
428 && (tblk)->checksum[i] <= '7')
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
429 sum = sum * 8 + (tblk)->checksum[i] - '0';
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
430 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
431 if (sum != checksum(tblk, 0) && sum != checksum(tblk, 1))
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
432 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
433 if (!first)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
434 untar_error("Garbage detected; preceding file may be damaged\n");
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
435 untar_error("%s: header has bad checksum for %s\n", inname, nbuf);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
436 return 0;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
437 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
438
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
439 /* From this point on, we don't care whether this is the first
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
440 * block or not. Might as well reset the "first" flag now.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
441 */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
442 first = 0;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
443
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
444 /* if last character of name is '/' then assume directory */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
445 if (*nbuf && nbuf[strlen(nbuf) - 1] == '/')
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
446 (tblk)->type = '5';
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
447
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
448 /* convert file size */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
449 for (outsize = 0L, i = 0; i < sizeof((tblk)->size); i++)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
450 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
451 if ((tblk)->size[i] >= '0' && (tblk)->size[i] <= '7')
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
452 outsize = outsize * 8 + (tblk)->size[i] - '0';
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
453 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
454
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
455 #ifdef _POSIX_SOURCE
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
456 /* convert file timestamp */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
457 for (timestamp.modtime=0L, i=0; i < sizeof((tblk)->mtime); i++)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
458 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
459 if ((tblk)->mtime[i] >= '0' && (tblk)->mtime[i] <= '7')
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
460 timestamp.modtime = timestamp.modtime * 8
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
461 + (tblk)->mtime[i] - '0';
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
462 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
463 timestamp.actime = timestamp.modtime;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
464
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
465 /* convert file permissions */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
466 for (mode = i = 0; i < sizeof((tblk)->mode); i++)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
467 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
468 if ((tblk)->mode[i] >= '0' && (tblk)->mode[i] <= '7')
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
469 mode = mode * 8 + (tblk)->mode[i] - '0';
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
470 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
471 #endif
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
472
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
473 /* If we have an "only" list, and this file isn't in it,
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
474 * then skip it.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
475 */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
476 if (nonlys > 0)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
477 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
478 for (i = 0;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
479 i < nonlys
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
480 && strcmp(only[i], nbuf)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
481 && (strncmp(only[i], nbuf, strlen(only[i]))
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
482 || nbuf[strlen(only[i])] != '/');
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
483 i++)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
484 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
485 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
486 if (i >= nonlys)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
487 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
488 outfp = NULL;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
489 return 1;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
490 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
491 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
492
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
493 /* list the file */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
494 if (VERBOSE)
6425
26b739bc9f1a [gaim-migrate @ 6933]
Herman Bloggs <hermanator12002@yahoo.com>
parents: 5005
diff changeset
495 untar_verbose("%c %s",
5005
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
496 ISREGULAR(*tblk) ? '-' : ("hlcbdp"[(tblk)->type - '1']),
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
497 nbuf);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
498 else if (!QUIET)
6425
26b739bc9f1a [gaim-migrate @ 6933]
Herman Bloggs <hermanator12002@yahoo.com>
parents: 5005
diff changeset
499 untar_verbose("%s\n", nbuf);
5005
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
500
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
501 /* if link, then do the link-or-copy thing */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
502 if (tblk->type == '1' || tblk->type == '2')
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
503 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
504 if (VERBOSE)
6425
26b739bc9f1a [gaim-migrate @ 6933]
Herman Bloggs <hermanator12002@yahoo.com>
parents: 5005
diff changeset
505 untar_verbose(" -> %s\n", tblk->linkto);
5005
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
506 if (!LISTING)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
507 linkorcopy(tblk->linkto, nbuf, tblk->type == '2');
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
508 outsize = 0L;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
509 return 1;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
510 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
511
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
512 /* If directory, then make a weak attempt to create it.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
513 * Ideally we would do the "create path" thing, but that
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
514 * seems like more trouble than it's worth since traditional
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
515 * tar archives don't contain directories anyway.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
516 */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
517 if (tblk->type == '5')
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
518 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
519 if (LISTING)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
520 n2 = " directory";
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
521 #ifdef _POSIX_SOURCE
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
522 else if (mkdir(nbuf, mode) == 0)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
523 #else
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
524 else if (mkdir(nbuf, 0755) == 0)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
525 #endif
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
526 n2 = " created";
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
527 else
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
528 n2 = " ignored";
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
529 if (VERBOSE)
6425
26b739bc9f1a [gaim-migrate @ 6933]
Herman Bloggs <hermanator12002@yahoo.com>
parents: 5005
diff changeset
530 untar_verbose("%s\n", n2);
5005
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
531 return 1;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
532 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
533
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
534 /* if not a regular file, then skip it */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
535 if (!ISREGULAR(*tblk))
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
536 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
537 if (VERBOSE)
6425
26b739bc9f1a [gaim-migrate @ 6933]
Herman Bloggs <hermanator12002@yahoo.com>
parents: 5005
diff changeset
538 untar_verbose(" ignored\n");
5005
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
539 outsize = 0L;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
540 return 1;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
541 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
542
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
543 /* print file statistics */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
544 if (VERBOSE)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
545 {
6425
26b739bc9f1a [gaim-migrate @ 6933]
Herman Bloggs <hermanator12002@yahoo.com>
parents: 5005
diff changeset
546 untar_verbose(" (%ld byte%s, %ld tape block%s)\n",
5005
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
547 outsize,
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
548 outsize == 1 ? "" : "s",
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
549 (outsize + TSIZE - 1) / TSIZE,
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
550 (outsize > 0 && outsize <= TSIZE) ? "" : "s");
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
551 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
552
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
553 /* if extracting, then try to create the file */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
554 if (!LISTING)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
555 outfp = createpath(nbuf);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
556 else
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
557 outfp = NULL;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
558
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
559 /* if file is 0 bytes long, then we're done already! */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
560 if (outsize == 0 && outfp)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
561 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
562 fclose(outfp);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
563 #ifdef _POSIX_SOURCE
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
564 utime(nbuf, &timestamp);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
565 chmod(nbuf, mode);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
566 #endif
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
567 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
568 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
569 return 1;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
570 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
571
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
572 /* Process an archive file. This involves reading the blocks one at a time
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
573 * and passing them to a untar() function.
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
574 */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
575 int untar(const char *filename, const char* destdir, untar_opt options) {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
576 int ret=1;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
577 char curdir[_MAX_PATH];
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
578 untarops = options;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
579 /* open the archive */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
580 inname = filename;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
581 infp = fopen(filename, "rb");
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
582 if (!infp)
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
583 {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
584 untar_error("Error opening: %s\n", filename);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
585 return 0;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
586 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
587
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
588 /* Set current directory */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
589 if(!GetCurrentDirectory(_MAX_PATH, curdir)) {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
590 untar_error("Could not get current directory (error %d).\n", GetLastError());
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
591 fclose(infp);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
592 return 0;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
593 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
594 if(!SetCurrentDirectory(destdir)) {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
595 untar_error("Could not set current directory to (error %d): %s\n", GetLastError(), destdir);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
596 fclose(infp);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
597 return 0;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
598 } else {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
599 /* UNCOMPRESSED */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
600 /* send each block to the untar_block() function */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
601 while (fread(slide, 1, TSIZE, infp) == TSIZE) {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
602 if(!untar_block(slide)) {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
603 untar_error("untar failure: %s\n", filename);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
604 fclose(infp);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
605 ret=0;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
606 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
607 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
608 if (outsize > 0 && ret) {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
609 untar_warning("Last file might be truncated!\n");
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
610 fclose(outfp);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
611 outfp = NULL;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
612 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
613 if(!SetCurrentDirectory(curdir)) {
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
614 untar_error("Could not set current dir back to original (error %d).\n", GetLastError());
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
615 ret=0;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
616 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
617 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
618
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
619 /* close the archive file. */
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
620 fclose(infp);
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
621
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
622 return ret;
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
623 }
a1f9725f4816 [gaim-migrate @ 5340]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
624