From 7ed2aa04eb87e659ee6d136737b62df2394af5c3 Mon Sep 17 00:00:00 2001 From: kientzle Date: Sat, 4 Jun 2005 22:19:25 +0000 Subject: Fix one error in the example usage of the archive_write API and fill in a few missing details. The example code here is now a complete, functioning example program. --- lib/libarchive/archive_write.3 | 79 +++++++++++++++++++++++++++--------------- 1 file changed, 52 insertions(+), 27 deletions(-) (limited to 'lib') diff --git a/lib/libarchive/archive_write.3 b/lib/libarchive/archive_write.3 index 73b433b..2086424 100644 --- a/lib/libarchive/archive_write.3 +++ b/lib/libarchive/archive_write.3 @@ -272,8 +272,50 @@ and .Xr close 2 system calls. .Bd -literal -offset indent +#include +#include +#include +#include +#include +#include + +struct mydata { + const char *name; + int fd; +}; + +int +myopen(struct archive *a, void *client_data) +{ + struct mydata *mydata = client_data; + + mydata->fd = open(mydata->name, O_WRONLY | O_CREAT, 0644); + if (mydata->fd >= 0) + return (ARCHIVE_OK); + else + return (ARCHIVE_FATAL); +} + +ssize_t +mywrite(struct archive *a, void *client_data, void *buff, size_t n) +{ + struct mydata *mydata = client_data; + + return (write(mydata->fd, buff, n)); +} + +int +myclose(struct archive *a, void *client_data) +{ + struct mydata *mydata = client_data; + + if (mydata->fd > 0) + close(mydata->fd); + return (0); +} + void -write_archive(const char **filename) +write_archive(const char *outname, const char **filename) { struct mydata *mydata = malloc(sizeof(struct mydata)); struct archive *a; @@ -281,9 +323,10 @@ write_archive(const char **filename) struct stat st; char buff[8192]; int len; + int fd; a = archive_write_new(); - mydata->name = name; + mydata->name = outname; archive_write_set_compression_gzip(a); archive_write_set_format_ustar(a); archive_write_open(a, mydata, myopen, mywrite, myclose); @@ -295,7 +338,7 @@ write_archive(const char **filename) archive_write_header(a, entry); fd = open(*filename, O_RDONLY); len = read(fd, buff, sizeof(buff)); - while ( len >= 0 ) { + while ( len > 0 ) { archive_write_data(a, buff, len); len = read(fd, buff, sizeof(buff)); } @@ -305,31 +348,13 @@ write_archive(const char **filename) archive_write_finish(a); } -int -myopen(struct archive *a, void *client_data) +int main(int argc, const char **argv) { - struct mydata *mydata = client_data; - - mydata->fd = open(mydata->name, O_WRONLY | O_CREAT, 0644); - return (mydata->fd >= 0); -} - -ssize_t -mywrite(struct archive *a, void *client_data, void *buff, size_t n) -{ - struct mydata *mydata = client_data; - - return (write(mydata->fd, buff, n)); -} - -int -myclose(struct archive *a, void *client_data) -{ - struct mydata *mydata = client_data; - - if (mydata->fd > 0) - close(mydata->fd); - return (0); + const char *outname; + argv++; + outname = argv++; + write_archive(outname, argv); + return 0; } .Ed .Sh RETURN VALUES -- cgit v1.1