2 ** Copyright 1998-2003 University of Illinois Board of Trustees
3 ** Copyright 1998-2003 Mark D. Roth
4 ** All rights reserved.
6 ** libtar.c - demo driver program for libtar
8 ** Mark D. Roth <roth@uiuc.edu>
9 ** Campus Information Technologies and Educational Services
10 ** University of Illinois at Urbana-Champaign
12 #include <libtar/config.h>
13 #include <libtar/libtar.h>
18 #if defined(_WIN32) && !defined(__CYGWIN__)
19 #include <libtar/compat.h>
22 #include <sys/param.h>
38 #include CMTAR_ZLIB_HEADER
40 #include <libtar/compat.h>
51 puts("OOPS! Caught SIGSEGV, bailing out...");
66 struct gzStruct GZStruct
;
67 #if defined ( _MSC_VER) || defined(__WATCOMC__)
69 //Yogi: hack. this should work on windows where there is no O_ACCMODE defined
71 # define O_ACCMODE 0x0003
75 static int libtar_gzopen(void* call_data
, const char *pathname
,
76 int oflags
, mode_t mode
)
80 struct gzStruct
* gzf
= (struct gzStruct
*)call_data
;
82 switch (oflags
& O_ACCMODE
)
96 fd
= open(pathname
, oflags
, mode
);
102 #if defined(__BEOS__) && !defined(__ZETA__) /* no fchmod on BeOS...do pathname instead. */
103 if ((oflags
& O_CREAT
) && chmod(pathname
, mode
& 07777))
107 #elif !defined(_WIN32) || defined(__CYGWIN__)
108 if ((oflags
& O_CREAT
) && fchmod(fd
, mode
& 07777))
114 gzf
->GZFile
= gzdopen(fd
, gzoflags
);
124 static int libtar_gzclose(void* call_data
)
126 struct gzStruct
* gzf
= (struct gzStruct
*)call_data
;
127 return gzclose(gzf
->GZFile
);
130 static ssize_t
libtar_gzread(void* call_data
, void* buf
, size_t count
)
132 struct gzStruct
* gzf
= (struct gzStruct
*)call_data
;
133 return gzread(gzf
->GZFile
, buf
, (unsigned int)count
);
136 static ssize_t
libtar_gzwrite(void* call_data
, const void* buf
, size_t count
)
138 struct gzStruct
* gzf
= (struct gzStruct
*)call_data
;
139 return gzwrite(gzf
->GZFile
, (void*)buf
, (unsigned int)count
);
150 #endif /* HAVE_LIBZ */
154 create(char *tarfile
, char *rootdir
, libtar_list_t
*l
)
158 char buf
[TAR_MAXPATHLEN
];
161 if (tar_open(&t
, tarfile
,
163 (use_zlib
? &gztype
: NULL
),
167 O_WRONLY
| O_CREAT
, 0644,
168 (verbose
? TAR_VERBOSE
: 0)
169 | (use_gnu
? TAR_GNU
: 0)) == -1)
171 fprintf(stderr
, "tar_open(): %s\n", strerror(errno
));
175 libtar_listptr_reset(&lp
);
176 while (libtar_list_next(l
, &lp
) != 0)
178 pathname
= (char *)libtar_listptr_data(&lp
);
179 if (pathname
[0] != '/' && rootdir
!= NULL
)
180 snprintf(buf
, sizeof(buf
), "%s/%s", rootdir
, pathname
);
182 strlcpy(buf
, pathname
, sizeof(buf
));
183 if (tar_append_tree(t
, buf
, pathname
) != 0)
186 "tar_append_tree(\"%s\", \"%s\"): %s\n", buf
,
187 pathname
, strerror(errno
));
193 if (tar_append_eof(t
) != 0)
195 fprintf(stderr
, "tar_append_eof(): %s\n", strerror(errno
));
200 if (tar_close(t
) != 0)
202 fprintf(stderr
, "tar_close(): %s\n", strerror(errno
));
216 if (tar_open(&t
, tarfile
,
218 (use_zlib
? &gztype
: NULL
),
223 (verbose
? TAR_VERBOSE
: 0)
224 | (use_gnu
? TAR_GNU
: 0)) == -1)
226 fprintf(stderr
, "tar_open(): %s\n", strerror(errno
));
230 while ((i
= th_read(t
)) == 0)
236 if (TH_ISREG(t
) && tar_skip_regfile(t
) != 0)
238 fprintf(stderr
, "tar_skip_regfile(): %s\n",
245 printf("th_read() returned %d\n", i
);
246 printf("EOF mark encountered after %ld bytes\n",
249 ? gzseek((gzFile
) t
->fd
, 0, SEEK_CUR
)
252 lseek(t
->fd
, 0, SEEK_CUR
)
259 if (tar_close(t
) != 0)
261 fprintf(stderr
, "tar_close(): %s\n", strerror(errno
));
271 extract(char *tarfile
, char *rootdir
)
276 puts("opening tarfile...");
278 if (tar_open(&t
, tarfile
,
280 (use_zlib
? &gztype
: NULL
),
285 (verbose
? TAR_VERBOSE
: 0)
286 | (use_gnu
? TAR_GNU
: 0)) == -1)
288 fprintf(stderr
, "tar_open(): %s\n", strerror(errno
));
293 puts("extracting tarfile...");
295 if (tar_extract_all(t
, rootdir
) != 0)
297 fprintf(stderr
, "tar_extract_all(): %s\n", strerror(errno
));
302 puts("closing tarfile...");
304 if (tar_close(t
) != 0)
306 fprintf(stderr
, "tar_close(): %s\n", strerror(errno
));
317 printf("Usage: %s [-C rootdir] [-g] [-z] -x|-t filename.tar\n",
319 printf(" %s [-C rootdir] [-g] [-z] -c filename.tar ...\n",
326 #define MODE_CREATE 2
327 #define MODE_EXTRACT 3
330 main(int argc
, char *argv
[])
333 char *rootdir
= NULL
;
337 #if defined(_WIN32) && !defined(__CYGWIN__)
340 progname
= basename(argv
[0]);
342 #if !defined(_WIN32) || defined(__CYGWIN__)
344 while ((c
= getopt(argc
, argv
, "cC:gtvVxz")) != -1)
348 printf("libtar %s by Mark D. Roth <roth@uiuc.edu>\n",
352 rootdir
= strdup(optarg
);
379 #endif /* HAVE_LIBZ */
383 if (!mode
|| ((argc
- optind
) < (mode
== MODE_CREATE
? 2 : 1)))
386 printf("argc - optind == %d\tmode == %d\n", argc
- optind
,
399 signal(SIGSEGV
, segv_handler
);
405 return extract(argv
[optind
], rootdir
);
407 tarfile
= argv
[optind
];
408 l
= libtar_list_new(LIST_QUEUE
, NULL
);
409 for (c
= optind
+ 1; c
< argc
; c
++)
410 libtar_list_add(l
, argv
[c
]);
411 return create(tarfile
, rootdir
, l
);
413 return list(argv
[optind
]);