annotate nt/minitar.c @ 1574:c58ffeded728

[xemacs-hg @ 2003-07-16 21:52:20 by youngs] Update xemacs_extra_name.
author youngs
date Wed, 16 Jul 2003 21:52:20 +0000
parents d4c017f833e2
children f5967016f79a
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
464
5aa1854ad537 Import from CVS: tag r21-2-47
cvs
parents: 438
diff changeset
26 static int
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);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
32 return 0;
327
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
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
36 #define BLOCKSIZE 512
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
37 #define MAXNAMELEN 1024
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
38
464
5aa1854ad537 Import from CVS: tag r21-2-47
cvs
parents: 438
diff changeset
39 static int
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
40 octal (char *str)
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
41 {
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
42 int ret = -1;
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
43 sscanf (str, "%o", &ret);
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
44 return ret;
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
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
47 /* this is like mkdir -p, except if there is no trailing slash,
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
48 the final component is assumed to be a file, rather than a
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
49 path component, so it is not created as a directory */
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
50
464
5aa1854ad537 Import from CVS: tag r21-2-47
cvs
parents: 438
diff changeset
51 static int
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
52 makepath (char *path)
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
53 {
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
54 char tmp[MAXNAMELEN];
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
55 char *cp;
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
56
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
57 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
58 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
59 if (!*cp)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
60 break;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
61 if (*cp != '/')
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
62 continue;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
63 strncpy (tmp, path, cp-path);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
64 tmp[cp-path] = '\0';
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
65 if (strlen (tmp) == 0)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
66 continue;
464
5aa1854ad537 Import from CVS: tag r21-2-47
cvs
parents: 438
diff changeset
67 #ifdef WIN32_NATIVE
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
68 if (mkdir (tmp))
464
5aa1854ad537 Import from CVS: tag r21-2-47
cvs
parents: 438
diff changeset
69 #else
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
70 if (mkdir (tmp, 0777))
464
5aa1854ad537 Import from CVS: tag r21-2-47
cvs
parents: 438
diff changeset
71 #endif
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
72 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
73 if (errno == EEXIST)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
74 continue;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
75 else
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
76 return -1;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
77 }
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
78 }
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
79 return 0;
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
80 }
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
81
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
82
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
83
464
5aa1854ad537 Import from CVS: tag r21-2-47
cvs
parents: 438
diff changeset
84 int
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
85 main (int argc, char **argv)
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
86 {
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
87 char fullname[MAXNAMELEN];
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
88 char *basedir = ".";
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
89 char *tarfile;
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
90 int size;
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
91 char osize[13];
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
92 char name[101];
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
93 char magic[7];
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
94 char type;
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
95
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
96 gzFile *infile = (gzFile*)0;
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
97 FILE *outfile = (FILE*)0;
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
98
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
99 char block[BLOCKSIZE];
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
100 int nbytes, nread, nwritten;
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
101
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
102 int in_block = 0;
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
103 int directory = 0;
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
104
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
105 if (argc < 2 || argc > 3)
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
106 Usage (argv[0]);
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
107
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
108 tarfile = argv[1];
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
109 if (argc==3)
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
110 basedir = argv[2];
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
111
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
112 if (! (infile = gzopen (tarfile, "rb")))
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
113 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
114 fprintf (stderr, "Cannot open %s\n", tarfile);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
115 exit (-2);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
116 }
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
117
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
118 while (1)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
119 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
120 nread = gzread (infile, block, 512);
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
121
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
122 if (!in_block && nread == 0)
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
123 break;
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
124
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
125 if (nread != BLOCKSIZE)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
126 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
127 fprintf (stderr, "Error: incomplete block read. Exiting.\n");
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
128 exit (-2);
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
129 }
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
130
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
131 if (!in_block)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
132 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
133 if (block[0]=='\0') /* We're done */
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
134 break;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
135
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
136 strncpy (magic, block+257, 6);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
137 magic[6] = '\0';
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
138 if (strcmp (magic, "ustar "))
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
139 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
140 fprintf (stderr,
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
141 "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
142 exit (-2);
495
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
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
145 strncpy (name, block, 100);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
146 name[100] = '\0';
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
147 sprintf (fullname, "%s/%s", basedir, name);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
148 printf ("%s\n", fullname);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
149 type = block[156];
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
150
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
151 switch (type)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
152 {
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 case '\0':
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
155 directory = 0;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
156 break;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
157 case '5':
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
158 directory = 1;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
159 break;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
160 default:
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
161 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
162 exit (-2);
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
163 break;
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
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
166 if (directory)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
167 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
168 in_block = 0;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
169
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
170 /* 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
171 dirnames have a trailing slash */
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
172
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
173 if (fullname[strlen (fullname)-1] != '/')
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
174 strcat (fullname, "/");
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
175 if (makepath (fullname))
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
176 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
177 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
178 fullname);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
179 exit (-2);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
180 }
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
181 continue;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
182 }
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
183 else
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
184 { /*file */
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
185 in_block = 1;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
186 if (outfile)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
187 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
188 if (fclose (outfile))
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
189 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
190 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
191 fullname);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
192 exit (-2);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
193 }
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
194 outfile = (FILE*)0;
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
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
197 if (!(outfile = fopen (fullname, "wb")))
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
198 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
199 /*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
200 if (makepath (fullname))
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
201 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
202 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
203 fullname);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
204 exit (-2);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
205 }
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
206 /* now try again to open the file */
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
207 if (!(outfile = fopen (fullname, "wb")))
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
208 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
209 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
210 fullname);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
211 exit (-2);
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
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
215 strncpy (osize, block+124, 12);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
216 osize[12] = '\0';
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
217 size = octal (osize);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
218 if (size<0)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
219 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
220 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
221 exit (-2);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
222 }
1529
d4c017f833e2 [xemacs-hg @ 2003-06-15 13:25:46 by adrian]
adrian
parents: 812
diff changeset
223 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
224 in_block = 0;
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
225 }
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
226 } else { /* write or continue writing file contents */
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
227 nbytes = size>512? 512:size;
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
228
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
229 nwritten = fwrite (block, 1, nbytes, outfile);
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
230 if (nwritten != nbytes)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
231 {
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
232 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
233 nwritten, fullname);
1529
d4c017f833e2 [xemacs-hg @ 2003-06-15 13:25:46 by adrian]
adrian
parents: 812
diff changeset
234 exit (-2);
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
235 }
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
236 size -= nbytes;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
237 if (size==0)
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
238 in_block = 0;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
239 }
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
240 }
495
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
241 return 0;
02f7a782086f [xemacs-hg @ 2001-05-01 09:36:44 by ben]
ben
parents: 464
diff changeset
242 }
327
03446687b7cc Import from CVS: tag r21-0-61
cvs
parents:
diff changeset
243
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