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 # ifdef HAVE_SYS_PARAM_H
23 # include <sys/param.h>
40 #include CMTAR_ZLIB_HEADER
42 #include <libtar/compat.h>
53 puts("OOPS! Caught SIGSEGV, bailing out...");
68 struct gzStruct GZStruct
;
69 #if defined ( _MSC_VER) || defined(__WATCOMC__)
71 //Yogi: hack. this should work on windows where there is no O_ACCMODE defined
73 # define O_ACCMODE 0x0003
77 static int libtar_gzopen(void* call_data
, const char *pathname
,
78 int oflags
, mode_t mode
)
82 struct gzStruct
* gzf
= (struct gzStruct
*)call_data
;
84 switch (oflags
& O_ACCMODE
)
98 fd
= open(pathname
, oflags
, mode
);
104 #if defined(__BEOS__) && !defined(__ZETA__) /* no fchmod on BeOS...do pathname instead. */
105 if ((oflags
& O_CREAT
) && chmod(pathname
, mode
& 07777))
109 #elif !defined(_WIN32) || defined(__CYGWIN__)
110 if ((oflags
& O_CREAT
) && fchmod(fd
, mode
& 07777))
116 gzf
->GZFile
= gzdopen(fd
, gzoflags
);
126 static int libtar_gzclose(void* call_data
)
128 struct gzStruct
* gzf
= (struct gzStruct
*)call_data
;
129 return gzclose(gzf
->GZFile
);
132 static ssize_t
libtar_gzread(void* call_data
, void* buf
, size_t count
)
134 struct gzStruct
* gzf
= (struct gzStruct
*)call_data
;
135 return gzread(gzf
->GZFile
, buf
, (unsigned int)count
);
138 static ssize_t
libtar_gzwrite(void* call_data
, const void* buf
, size_t count
)
140 struct gzStruct
* gzf
= (struct gzStruct
*)call_data
;
141 return gzwrite(gzf
->GZFile
, (void*)buf
, (unsigned int)count
);
152 #endif /* HAVE_LIBZ */
156 create(char *tarfile
, char *rootdir
, libtar_list_t
*l
)
160 char buf
[TAR_MAXPATHLEN
];
163 if (tar_open(&t
, tarfile
,
165 (use_zlib
? &gztype
: NULL
),
169 O_WRONLY
| O_CREAT
, 0644,
170 (verbose
? TAR_VERBOSE
: 0)
171 | (use_gnu
? TAR_GNU
: 0)) == -1)
173 fprintf(stderr
, "tar_open(): %s\n", strerror(errno
));
177 libtar_listptr_reset(&lp
);
178 while (libtar_list_next(l
, &lp
) != 0)
180 pathname
= (char *)libtar_listptr_data(&lp
);
181 if (pathname
[0] != '/' && rootdir
!= NULL
)
182 snprintf(buf
, sizeof(buf
), "%s/%s", rootdir
, pathname
);
184 strlcpy(buf
, pathname
, sizeof(buf
));
185 if (tar_append_tree(t
, buf
, pathname
) != 0)
188 "tar_append_tree(\"%s\", \"%s\"): %s\n", buf
,
189 pathname
, strerror(errno
));
195 if (tar_append_eof(t
) != 0)
197 fprintf(stderr
, "tar_append_eof(): %s\n", strerror(errno
));
202 if (tar_close(t
) != 0)
204 fprintf(stderr
, "tar_close(): %s\n", strerror(errno
));
218 if (tar_open(&t
, tarfile
,
220 (use_zlib
? &gztype
: NULL
),
225 (verbose
? TAR_VERBOSE
: 0)
226 | (use_gnu
? TAR_GNU
: 0)) == -1)
228 fprintf(stderr
, "tar_open(): %s\n", strerror(errno
));
232 while ((i
= th_read(t
)) == 0)
238 if (TH_ISREG(t
) && tar_skip_regfile(t
) != 0)
240 fprintf(stderr
, "tar_skip_regfile(): %s\n",
247 printf("th_read() returned %d\n", i
);
248 printf("EOF mark encountered after %ld bytes\n",
251 ? gzseek((gzFile
) t
->fd
, 0, SEEK_CUR
)
254 lseek(t
->fd
, 0, SEEK_CUR
)
261 if (tar_close(t
) != 0)
263 fprintf(stderr
, "tar_close(): %s\n", strerror(errno
));
273 extract(char *tarfile
, char *rootdir
)
278 puts("opening tarfile...");
280 if (tar_open(&t
, tarfile
,
282 (use_zlib
? &gztype
: NULL
),
287 (verbose
? TAR_VERBOSE
: 0)
288 | (use_gnu
? TAR_GNU
: 0)) == -1)
290 fprintf(stderr
, "tar_open(): %s\n", strerror(errno
));
295 puts("extracting tarfile...");
297 if (tar_extract_all(t
, rootdir
) != 0)
299 fprintf(stderr
, "tar_extract_all(): %s\n", strerror(errno
));
304 puts("closing tarfile...");
306 if (tar_close(t
) != 0)
308 fprintf(stderr
, "tar_close(): %s\n", strerror(errno
));
319 printf("Usage: %s [-C rootdir] [-g] [-z] -x|-t filename.tar\n",
321 printf(" %s [-C rootdir] [-g] [-z] -c filename.tar ...\n",
328 #define MODE_CREATE 2
329 #define MODE_EXTRACT 3
332 main(int argc
, char *argv
[])
335 char *rootdir
= NULL
;
339 #if defined(_WIN32) && !defined(__CYGWIN__)
342 progname
= basename(argv
[0]);
344 #if !defined(_WIN32) || defined(__CYGWIN__)
346 while ((c
= getopt(argc
, argv
, "cC:gtvVxz")) != -1)
350 printf("libtar %s by Mark D. Roth <roth@uiuc.edu>\n",
354 rootdir
= strdup(optarg
);
381 #endif /* HAVE_LIBZ */
385 if (!mode
|| ((argc
- optind
) < (mode
== MODE_CREATE
? 2 : 1)))
388 printf("argc - optind == %d\tmode == %d\n", argc
- optind
,
401 signal(SIGSEGV
, segv_handler
);
407 return extract(argv
[optind
], rootdir
);
409 tarfile
= argv
[optind
];
410 l
= libtar_list_new(LIST_QUEUE
, NULL
);
411 for (c
= optind
+ 1; c
< argc
; c
++)
412 libtar_list_add(l
, argv
[c
]);
413 return create(tarfile
, rootdir
, l
);
415 return list(argv
[optind
]);