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
19 #include <sys/param.h>
49 puts("OOPS! Caught SIGSEGV, bailing out...");
61 gzopen_frontend(char *pathname
, int oflags
, int mode
)
67 switch (oflags
& O_ACCMODE
)
81 fd
= open(pathname
, oflags
, mode
);
85 if ((oflags
& O_CREAT
) && fchmod(fd
, mode
))
88 gzf
= gzdopen(fd
, gzoflags
);
95 /* This is a bad thing to do on big-endian lp64 systems, where the
96 size and placement of integers is different than pointers.
97 However, to fix the problem 4 wrapper functions would be needed and
98 an extra bit of data associating GZF with the wrapper functions. */
102 tartype_t gztype
= { (openfunc_t
) gzopen_frontend
, (closefunc_t
) gzclose
,
103 (readfunc_t
) gzread
, (writefunc_t
) gzwrite
106 #endif /* HAVE_LIBZ */
110 create(char *tarfile
, char *rootdir
, libtar_list_t
*l
)
114 char buf
[MAXPATHLEN
];
117 if (tar_open(&t
, tarfile
,
119 (use_zlib
? &gztype
: NULL
),
123 O_WRONLY
| O_CREAT
, 0644,
124 (verbose
? TAR_VERBOSE
: 0)
125 | (use_gnu
? TAR_GNU
: 0)) == -1)
127 fprintf(stderr
, "tar_open(): %s\n", strerror(errno
));
131 libtar_listptr_reset(&lp
);
132 while (libtar_list_next(l
, &lp
) != 0)
134 pathname
= (char *)libtar_listptr_data(&lp
);
135 if (pathname
[0] != '/' && rootdir
!= NULL
)
136 snprintf(buf
, sizeof(buf
), "%s/%s", rootdir
, pathname
);
138 strlcpy(buf
, pathname
, sizeof(buf
));
139 if (tar_append_tree(t
, buf
, pathname
) != 0)
142 "tar_append_tree(\"%s\", \"%s\"): %s\n", buf
,
143 pathname
, strerror(errno
));
149 if (tar_append_eof(t
) != 0)
151 fprintf(stderr
, "tar_append_eof(): %s\n", strerror(errno
));
156 if (tar_close(t
) != 0)
158 fprintf(stderr
, "tar_close(): %s\n", strerror(errno
));
172 if (tar_open(&t
, tarfile
,
174 (use_zlib
? &gztype
: NULL
),
179 (verbose
? TAR_VERBOSE
: 0)
180 | (use_gnu
? TAR_GNU
: 0)) == -1)
182 fprintf(stderr
, "tar_open(): %s\n", strerror(errno
));
186 while ((i
= th_read(t
)) == 0)
192 if (TH_ISREG(t
) && tar_skip_regfile(t
) != 0)
194 fprintf(stderr
, "tar_skip_regfile(): %s\n",
201 printf("th_read() returned %d\n", i
);
202 printf("EOF mark encountered after %ld bytes\n",
205 ? gzseek((gzFile
) t
->fd
, 0, SEEK_CUR
)
208 lseek(t
->fd
, 0, SEEK_CUR
)
215 if (tar_close(t
) != 0)
217 fprintf(stderr
, "tar_close(): %s\n", strerror(errno
));
226 extract(char *tarfile
, char *rootdir
)
231 puts("opening tarfile...");
233 if (tar_open(&t
, tarfile
,
235 (use_zlib
? &gztype
: NULL
),
240 (verbose
? TAR_VERBOSE
: 0)
241 | (use_gnu
? TAR_GNU
: 0)) == -1)
243 fprintf(stderr
, "tar_open(): %s\n", strerror(errno
));
248 puts("extracting tarfile...");
250 if (tar_extract_all(t
, rootdir
) != 0)
252 fprintf(stderr
, "tar_extract_all(): %s\n", strerror(errno
));
257 puts("closing tarfile...");
259 if (tar_close(t
) != 0)
261 fprintf(stderr
, "tar_close(): %s\n", strerror(errno
));
272 printf("Usage: %s [-C rootdir] [-g] [-z] -x|-t filename.tar\n",
274 printf(" %s [-C rootdir] [-g] [-z] -c filename.tar ...\n",
281 #define MODE_CREATE 2
282 #define MODE_EXTRACT 3
285 main(int argc
, char *argv
[])
287 char *tarfile
= NULL
;
288 char *rootdir
= NULL
;
293 progname
= basename(argv
[0]);
295 while ((c
= getopt(argc
, argv
, "cC:gtvVxz")) != -1)
299 printf("libtar %s by Mark D. Roth <roth@uiuc.edu>\n",
303 rootdir
= strdup(optarg
);
330 #endif /* HAVE_LIBZ */
335 if (!mode
|| ((argc
- optind
) < (mode
== MODE_CREATE
? 2 : 1)))
338 printf("argc - optind == %d\tmode == %d\n", argc
- optind
,
345 signal(SIGSEGV
, segv_handler
);
351 return extract(argv
[optind
], rootdir
);
353 tarfile
= argv
[optind
];
354 l
= libtar_list_new(LIST_QUEUE
, NULL
);
355 for (c
= optind
+ 1; c
< argc
; c
++)
356 libtar_list_add(l
, argv
[c
]);
357 return create(tarfile
, rootdir
, l
);
359 return list(argv
[optind
]);