diff options
author | kientzle <kientzle@FreeBSD.org> | 2005-06-04 22:19:25 +0000 |
---|---|---|
committer | kientzle <kientzle@FreeBSD.org> | 2005-06-04 22:19:25 +0000 |
commit | 7ed2aa04eb87e659ee6d136737b62df2394af5c3 (patch) | |
tree | d4653055375754287d8b77ed38043a5893580300 /lib | |
parent | bbde8d81978bf136ee11066b3a217c2237566ce6 (diff) | |
download | FreeBSD-src-7ed2aa04eb87e659ee6d136737b62df2394af5c3.zip FreeBSD-src-7ed2aa04eb87e659ee6d136737b62df2394af5c3.tar.gz |
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.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libarchive/archive_write.3 | 79 |
1 files changed, 52 insertions, 27 deletions
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 <sys/stat.h> +#include <archive.h> +#include <archive_entry.h> +#include <fcntl.h> +#include <stdlib.h> +#include <unistd.h> + +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 |