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>
48 puts("OOPS! Caught SIGSEGV, bailing out...");
60 gzopen_frontend(char *pathname
, int oflags
, int mode
)
66 switch (oflags
& O_ACCMODE
)
80 fd
= open(pathname
, oflags
, mode
);
84 if ((oflags
& O_CREAT
) && fchmod(fd
, mode
))
87 gzf
= gzdopen(fd
, gzoflags
);
97 tartype_t gztype
= { (openfunc_t
) gzopen_frontend
, (closefunc_t
) gzclose
,
98 (readfunc_t
) gzread
, (writefunc_t
) gzwrite
101 #endif /* HAVE_LIBZ */
105 create(char *tarfile
, char *rootdir
, libtar_list_t
*l
)
109 char buf
[MAXPATHLEN
];
112 if (tar_open(&t
, tarfile
,
114 (use_zlib
? &gztype
: NULL
),
118 O_WRONLY
| O_CREAT
, 0644,
119 (verbose
? TAR_VERBOSE
: 0)
120 | (use_gnu
? TAR_GNU
: 0)) == -1)
122 fprintf(stderr
, "tar_open(): %s\n", strerror(errno
));
126 libtar_listptr_reset(&lp
);
127 while (libtar_list_next(l
, &lp
) != 0)
129 pathname
= (char *)libtar_listptr_data(&lp
);
130 if (pathname
[0] != '/' && rootdir
!= NULL
)
131 snprintf(buf
, sizeof(buf
), "%s/%s", rootdir
, pathname
);
133 strlcpy(buf
, pathname
, sizeof(buf
));
134 if (tar_append_tree(t
, buf
, pathname
) != 0)
137 "tar_append_tree(\"%s\", \"%s\"): %s\n", buf
,
138 pathname
, strerror(errno
));
144 if (tar_append_eof(t
) != 0)
146 fprintf(stderr
, "tar_append_eof(): %s\n", strerror(errno
));
151 if (tar_close(t
) != 0)
153 fprintf(stderr
, "tar_close(): %s\n", strerror(errno
));
167 if (tar_open(&t
, tarfile
,
169 (use_zlib
? &gztype
: NULL
),
174 (verbose
? TAR_VERBOSE
: 0)
175 | (use_gnu
? TAR_GNU
: 0)) == -1)
177 fprintf(stderr
, "tar_open(): %s\n", strerror(errno
));
181 while ((i
= th_read(t
)) == 0)
187 if (TH_ISREG(t
) && tar_skip_regfile(t
) != 0)
189 fprintf(stderr
, "tar_skip_regfile(): %s\n",
196 printf("th_read() returned %d\n", i
);
197 printf("EOF mark encountered after %ld bytes\n",
200 ? gzseek((gzFile
) t
->fd
, 0, SEEK_CUR
)
203 lseek(t
->fd
, 0, SEEK_CUR
)
210 if (tar_close(t
) != 0)
212 fprintf(stderr
, "tar_close(): %s\n", strerror(errno
));
221 extract(char *tarfile
, char *rootdir
)
226 puts("opening tarfile...");
228 if (tar_open(&t
, tarfile
,
230 (use_zlib
? &gztype
: NULL
),
235 (verbose
? TAR_VERBOSE
: 0)
236 | (use_gnu
? TAR_GNU
: 0)) == -1)
238 fprintf(stderr
, "tar_open(): %s\n", strerror(errno
));
243 puts("extracting tarfile...");
245 if (tar_extract_all(t
, rootdir
) != 0)
247 fprintf(stderr
, "tar_extract_all(): %s\n", strerror(errno
));
252 puts("closing tarfile...");
254 if (tar_close(t
) != 0)
256 fprintf(stderr
, "tar_close(): %s\n", strerror(errno
));
267 printf("Usage: %s [-C rootdir] [-g] [-z] -x|-t filename.tar\n",
269 printf(" %s [-C rootdir] [-g] [-z] -c filename.tar ...\n",
276 #define MODE_CREATE 2
277 #define MODE_EXTRACT 3
280 main(int argc
, char *argv
[])
282 char *tarfile
= NULL
;
283 char *rootdir
= NULL
;
288 progname
= basename(argv
[0]);
290 while ((c
= getopt(argc
, argv
, "cC:gtvVxz")) != -1)
294 printf("libtar %s by Mark D. Roth <roth@uiuc.edu>\n",
298 rootdir
= strdup(optarg
);
325 #endif /* HAVE_LIBZ */
330 if (!mode
|| ((argc
- optind
) < (mode
== MODE_CREATE
? 2 : 1)))
333 printf("argc - optind == %d\tmode == %d\n", argc
- optind
,
340 signal(SIGSEGV
, segv_handler
);
346 return extract(argv
[optind
], rootdir
);
348 tarfile
= argv
[optind
];
349 l
= libtar_list_new(LIST_QUEUE
, NULL
);
350 for (c
= optind
+ 1; c
< argc
; c
++)
351 libtar_list_add(l
, argv
[c
]);
352 return create(tarfile
, rootdir
, l
);
354 return list(argv
[optind
]);