hio/bin/untar.c

65 lines
1.1 KiB
C
Raw Normal View History

2022-11-01 11:31:04 +00:00
#include <hio-tar.h>
#include <hio-utl.h>
#include <stdio.h>
int main (int argc, char* argv[])
{
hio_t* hio;
2022-12-13 14:21:19 +00:00
hio_tar_t* tar;
2022-11-29 16:23:53 +00:00
FILE* fp;
2022-11-01 11:31:04 +00:00
if (argc != 2)
{
fprintf (stderr, "Usage: %s <filename>\n", hio_get_base_name_bcstr(argv[0]));
return -1;
}
hio = hio_open(HIO_NULL, 0, HIO_NULL, HIO_FEATURE_ALL, 512, HIO_NULL);
if (!hio)
{
fprintf (stderr, "Error: unable to open hio\n");
return -1;
}
2022-12-13 14:21:19 +00:00
tar = hio_tar_open(hio, 0);
if (!tar)
2022-11-29 16:23:53 +00:00
{
2022-12-13 14:21:19 +00:00
fprintf (stderr, "Error: unable to open tar\n");
2022-11-29 16:23:53 +00:00
hio_close (hio);
return -1;
}
fp = fopen(argv[1], "r");
if (!fp)
{
fprintf (stderr, "Error: unable to open file %s\n", argv[1]);
2022-12-13 14:21:19 +00:00
hio_tar_close (tar);
2022-11-29 16:23:53 +00:00
hio_close (hio);
return -1;
}
while (!feof(fp) && !ferror(fp))
{
int n;
2022-12-13 14:21:19 +00:00
char buf[4096]; /* TODO: use a different buffer size???*/
2022-11-29 16:23:53 +00:00
n = fread(buf, 1, sizeof(buf), fp);
if (n > 0)
{
2022-12-13 14:21:19 +00:00
if (hio_tar_xfeed(tar, buf, n) <= -1)
2022-11-29 16:23:53 +00:00
{
2022-12-13 14:21:19 +00:00
fprintf (stderr, "Error: tar error - %s\n", hio_geterrbmsg(hio));
2022-11-29 16:23:53 +00:00
break;
}
}
}
2022-12-13 14:21:19 +00:00
hio_tar_endxfeed (tar); /* indicate the end of input */
2022-11-01 11:31:04 +00:00
2022-11-29 16:23:53 +00:00
fclose (fp);
2022-12-13 14:21:19 +00:00
hio_tar_close (tar);
2022-11-01 11:31:04 +00:00
hio_close (hio);
return 0;
}