annotate nt/minitar.c @ 2583:6a89ac689a9a

[xemacs-hg @ 2005-02-13 22:51:33 by viteno] Update xemacs_extra_name.
author viteno
date Sun, 13 Feb 2005 22:51:33 +0000
parents f5967016f79a
children b694dfd2f40e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
1
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
2 /* Minitar: extract .tar.gz files on Win32 platforms.
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
3 Uses zlib for decompression.
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
4
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
5 This is very simple-minded, it ignores checksums, and any type of file
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
6 that is not a plain file or a directory. Nonetheless it is useful.
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
7
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
8 Author: Charles G. Waldman (cgw@pgt.com), Aug 4 1998
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
9
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
10 This file is placed in the public domain; you can
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
11 do whatever you like with it. There is NO WARRANTY.
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
12 If it breaks, you get to keep both pieces */
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
13
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
14
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
15 #include <stdio.h>
812
f846c2ef930d [xemacs-hg @ 2002-04-13 20:31:27 by jhar]
jhar
parents: 495
diff changeset
16 #include <stdlib.h>
438
84b14dcb0985 Import from CVS: tag r21-2-27
cvs
parents: 434
diff changeset
17 #include <errno.h>
464
5aa1854ad537 Import from CVS: tag r21-2-47
cvs
parents: 438
diff changeset
18 #include <string.h>
5aa1854ad537 Import from CVS: tag r21-2-47
cvs
parents: 438
diff changeset
19 #include <io.h>
812
f846c2ef930d [xemacs-hg @ 2002-04-13 20:31:27 by jhar]
jhar
parents: 495
diff changeset
20 #ifdef WIN32_NATIVE
f846c2ef930d [xemacs-hg @ 2002-04-13 20:31:27 by jhar]
jhar
parents: 495
diff changeset
21 # include <direct.h> /* For mkdir */
f846c2ef930d [xemacs-hg @ 2002-04-13 20:31:27 by jhar]
jhar
parents: 495
diff changeset
22 #endif
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
23
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
24 #include <zlib.h>
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
25
1782
f5967016f79a [xemacs-hg @ 2003-11-06 22:53:31 by adrian]
adrian
parents: 1529
diff changeset
26 static void
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
27 Usage (char *name)
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
28 {
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
29 fprintf (stderr, "Usage: %s file.tar.gz [base-dir]\n", name);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
30 fprintf (stderr, "\tExtracts the contents compressed tar file to base-dir\n");
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
31 exit (-1);
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
32 }
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
33
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
34
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
35 #define BLOCKSIZE 512
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
36 #define MAXNAMELEN 1024
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
37
464
5aa1854ad537 Import from CVS: tag r21-2-47
cvs
parents: 438
diff changeset
38 static int
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
39 octal (char *str)
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
40 {
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
41 int ret = -1;
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
42 sscanf (str, "%o", &ret);
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
43 return ret;
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
44 }
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
45
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
46 /* this is like mkdir -p, except if there is no trailing slash,
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
47 the final component is assumed to be a file, rather than a
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
48 path component, so it is not created as a directory */
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
49
464
5aa1854ad537 Import from CVS: tag r21-2-47
cvs
parents: 438
diff changeset
50 static int
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
51 makepath (char *path)
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
52 {
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
53 char tmp[MAXNAMELEN];
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
54 char *cp;
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
55
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
56 for (cp=path; cp; cp = (char*)strchr (cp+1, '/'))
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
57 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
58 if (!*cp)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
59 break;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
60 if (*cp != '/')
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
61 continue;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
62 strncpy (tmp, path, cp-path);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
63 tmp[cp-path] = '\0';
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
64 if (strlen (tmp) == 0)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
65 continue;
464
5aa1854ad537 Import from CVS: tag r21-2-47
cvs
parents: 438
diff changeset
66 #ifdef WIN32_NATIVE
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
67 if (mkdir (tmp))
464
5aa1854ad537 Import from CVS: tag r21-2-47
cvs
parents: 438
diff changeset
68 #else
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
69 if (mkdir (tmp, 0777))
464
5aa1854ad537 Import from CVS: tag r21-2-47
cvs
parents: 438
diff changeset
70 #endif
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
71 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
72 if (errno == EEXIST)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
73 continue;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
74 else
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
75 return -1;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
76 }
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
77 }
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
78 return 0;
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
79 }
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
80
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
81
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
82
464
5aa1854ad537 Import from CVS: tag r21-2-47
cvs
parents: 438
diff changeset
83 int
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
84 main (int argc, char **argv)
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
85 {
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
86 char fullname[MAXNAMELEN];
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
87 char *basedir = ".";
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
88 char *tarfile;
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
89 int size;
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
90 char osize[13];
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
91 char name[101];
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
92 char magic[7];
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
93 char type;
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
94
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
95 gzFile *infile = (gzFile*)0;
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
96 FILE *outfile = (FILE*)0;
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
97
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
98 char block[BLOCKSIZE];
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
99 int nbytes, nread, nwritten;
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
100
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
101 int in_block = 0;
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
102 int directory = 0;
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
103
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
104 if (argc < 2 || argc > 3)
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
105 Usage (argv[0]);
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
106
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
107 tarfile = argv[1];
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
108 if (argc==3)
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
109 basedir = argv[2];
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
110
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
111 if (! (infile = gzopen (tarfile, "rb")))
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
112 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
113 fprintf (stderr, "Cannot open %s\n", tarfile);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
114 exit (-2);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
115 }
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
116
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
117 while (1)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
118 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
119 nread = gzread (infile, block, 512);
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
120
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
121 if (!in_block && nread == 0)
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
122 break;
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
123
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
124 if (nread != BLOCKSIZE)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
125 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
126 fprintf (stderr, "Error: incomplete block read. Exiting.\n");
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
127 exit (-2);
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
128 }
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
129
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
130 if (!in_block)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
131 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
132 if (block[0]=='\0') /* We're done */
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
133 break;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
134
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
135 strncpy (magic, block+257, 6);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
136 magic[6] = '\0';
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
137 if (strcmp (magic, "ustar "))
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
138 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
139 fprintf (stderr,
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
140 "Error: incorrect magic number in tar header. Exiting\n");
1529
d4c017f833e2 [xemacs-hg @ 2003-06-15 13:25:46 by adrian]
adrian
parents: 812
diff changeset
141 exit (-2);
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
142 }
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
143
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
144 strncpy (name, block, 100);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
145 name[100] = '\0';
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
146 sprintf (fullname, "%s/%s", basedir, name);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
147 printf ("%s\n", fullname);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
148 type = block[156];
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
149
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
150 switch (type)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
151 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
152 case '0':
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
153 case '\0':
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
154 directory = 0;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
155 break;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
156 case '5':
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
157 directory = 1;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
158 break;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
159 default:
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
160 fprintf (stderr, "Error: unknown type flag %c. Exiting.\n", type);
1529
d4c017f833e2 [xemacs-hg @ 2003-06-15 13:25:46 by adrian]
adrian
parents: 812
diff changeset
161 exit (-2);
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
162 break;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
163 }
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
164
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
165 if (directory)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
166 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
167 in_block = 0;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
168
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
169 /* makepath will ignore the final path component, so make sure
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
170 dirnames have a trailing slash */
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
171
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
172 if (fullname[strlen (fullname)-1] != '/')
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
173 strcat (fullname, "/");
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
174 if (makepath (fullname))
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
175 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
176 fprintf (stderr, "Error: cannot create directory %s. Exiting.\n",
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
177 fullname);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
178 exit (-2);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
179 }
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
180 continue;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
181 }
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
182 else
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
183 { /*file */
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
184 in_block = 1;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
185 if (outfile)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
186 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
187 if (fclose (outfile))
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
188 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
189 fprintf (stderr, "Error: cannot close file %s. Exiting.\n",
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
190 fullname);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
191 exit (-2);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
192 }
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
193 outfile = (FILE*)0;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
194 }
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
195
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
196 if (!(outfile = fopen (fullname, "wb")))
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
197 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
198 /*try creating the directory, maybe it's not there */
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
199 if (makepath (fullname))
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
200 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
201 fprintf (stderr, "Error: cannot create file %s. Exiting.\n",
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
202 fullname);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
203 exit (-2);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
204 }
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
205 /* now try again to open the file */
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
206 if (!(outfile = fopen (fullname, "wb")))
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
207 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
208 fprintf (stderr, "Error: cannot create file %s. Exiting.\n",
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
209 fullname);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
210 exit (-2);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
211 }
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
212 }
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
213
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
214 strncpy (osize, block+124, 12);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
215 osize[12] = '\0';
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
216 size = octal (osize);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
217 if (size<0)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
218 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
219 fprintf (stderr, "Error: invalid size in tar header. Exiting.\n");
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
220 exit (-2);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
221 }
1529
d4c017f833e2 [xemacs-hg @ 2003-06-15 13:25:46 by adrian]
adrian
parents: 812
diff changeset
222 if (size==0) /* file of size 0 is done */
d4c017f833e2 [xemacs-hg @ 2003-06-15 13:25:46 by adrian]
adrian
parents: 812
diff changeset
223 in_block = 0;
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
224 }
1782
f5967016f79a [xemacs-hg @ 2003-11-06 22:53:31 by adrian]
adrian
parents: 1529
diff changeset
225 }
f5967016f79a [xemacs-hg @ 2003-11-06 22:53:31 by adrian]
adrian
parents: 1529
diff changeset
226 else
f5967016f79a [xemacs-hg @ 2003-11-06 22:53:31 by adrian]
adrian
parents: 1529
diff changeset
227 { /* write or continue writing file contents */
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
228 nbytes = size>512? 512:size;
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
229
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
230 nwritten = fwrite (block, 1, nbytes, outfile);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
231 if (nwritten != nbytes)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
232 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
233 fprintf (stderr, "Error: only wrote %d bytes to file %s. Exiting.\n",
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
234 nwritten, fullname);
1529
d4c017f833e2 [xemacs-hg @ 2003-06-15 13:25:46 by adrian]
adrian
parents: 812
diff changeset
235 exit (-2);
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
236 }
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
237 size -= nbytes;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
238 if (size==0)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
239 in_block = 0;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
240 }
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
241 }
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
242 return 0;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
243 }
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
244
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
245
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
246