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
))
91 gzf
= gzdopen(fd
, gzoflags
);
98 /* This is a bad thing to do on big-endian lp64 systems, where the
99 size and placement of integers is different than pointers.
100 However, to fix the problem 4 wrapper functions would be needed and
101 an extra bit of data associating GZF with the wrapper functions. */
105 tartype_t gztype
= { (openfunc_t
) gzopen_frontend
, (closefunc_t
) gzclose
,
106 (readfunc_t
) gzread
, (writefunc_t
) gzwrite
109 #endif /* HAVE_LIBZ */
113 create(char *tarfile
, char *rootdir
, libtar_list_t
*l
)
117 char buf
[MAXPATHLEN
];
120 if (tar_open(&t
, tarfile
,
122 (use_zlib
? &gztype
: NULL
),
126 O_WRONLY
| O_CREAT
, 0644,
127 (verbose
? TAR_VERBOSE
: 0)
128 | (use_gnu
? TAR_GNU
: 0)) == -1)
130 fprintf(stderr
, "tar_open(): %s\n", strerror(errno
));
134 libtar_listptr_reset(&lp
);
135 while (libtar_list_next(l
, &lp
) != 0)
137 pathname
= (char *)libtar_listptr_data(&lp
);
138 if (pathname
[0] != '/' && rootdir
!= NULL
)
139 snprintf(buf
, sizeof(buf
), "%s/%s", rootdir
, pathname
);
141 strlcpy(buf
, pathname
, sizeof(buf
));
142 if (tar_append_tree(t
, buf
, pathname
) != 0)
145 "tar_append_tree(\"%s\", \"%s\"): %s\n", buf
,
146 pathname
, strerror(errno
));
152 if (tar_append_eof(t
) != 0)
154 fprintf(stderr
, "tar_append_eof(): %s\n", strerror(errno
));
159 if (tar_close(t
) != 0)
161 fprintf(stderr
, "tar_close(): %s\n", strerror(errno
));
175 if (tar_open(&t
, tarfile
,
177 (use_zlib
? &gztype
: NULL
),
182 (verbose
? TAR_VERBOSE
: 0)
183 | (use_gnu
? TAR_GNU
: 0)) == -1)
185 fprintf(stderr
, "tar_open(): %s\n", strerror(errno
));
189 while ((i
= th_read(t
)) == 0)
195 if (TH_ISREG(t
) && tar_skip_regfile(t
) != 0)
197 fprintf(stderr
, "tar_skip_regfile(): %s\n",
204 printf("th_read() returned %d\n", i
);
205 printf("EOF mark encountered after %ld bytes\n",
208 ? gzseek((gzFile
) t
->fd
, 0, SEEK_CUR
)
211 lseek(t
->fd
, 0, SEEK_CUR
)
218 if (tar_close(t
) != 0)
220 fprintf(stderr
, "tar_close(): %s\n", strerror(errno
));
229 extract(char *tarfile
, char *rootdir
)
234 puts("opening tarfile...");
236 if (tar_open(&t
, tarfile
,
238 (use_zlib
? &gztype
: NULL
),
243 (verbose
? TAR_VERBOSE
: 0)
244 | (use_gnu
? TAR_GNU
: 0)) == -1)
246 fprintf(stderr
, "tar_open(): %s\n", strerror(errno
));
251 puts("extracting tarfile...");
253 if (tar_extract_all(t
, rootdir
) != 0)
255 fprintf(stderr
, "tar_extract_all(): %s\n", strerror(errno
));
260 puts("closing tarfile...");
262 if (tar_close(t
) != 0)
264 fprintf(stderr
, "tar_close(): %s\n", strerror(errno
));
275 printf("Usage: %s [-C rootdir] [-g] [-z] -x|-t filename.tar\n",
277 printf(" %s [-C rootdir] [-g] [-z] -c filename.tar ...\n",
284 #define MODE_CREATE 2
285 #define MODE_EXTRACT 3
288 main(int argc
, char *argv
[])
290 char *tarfile
= NULL
;
291 char *rootdir
= NULL
;
296 progname
= basename(argv
[0]);
298 while ((c
= getopt(argc
, argv
, "cC:gtvVxz")) != -1)
302 printf("libtar %s by Mark D. Roth <roth@uiuc.edu>\n",
306 rootdir
= strdup(optarg
);
333 #endif /* HAVE_LIBZ */
338 if (!mode
|| ((argc
- optind
) < (mode
== MODE_CREATE
? 2 : 1)))
341 printf("argc - optind == %d\tmode == %d\n", argc
- optind
,
348 signal(SIGSEGV
, segv_handler
);
354 return extract(argv
[optind
], rootdir
);
356 tarfile
= argv
[optind
];
357 l
= libtar_list_new(LIST_QUEUE
, NULL
);
358 for (c
= optind
+ 1; c
< argc
; c
++)
359 libtar_list_add(l
, argv
[c
]);
360 return create(tarfile
, rootdir
, l
);
362 return list(argv
[optind
]);