tests: using 64-bit offsets
[libha.git] / src / main_unp.c
blobd46c8f5e9da2d6a9d67799928f98246a5006e9b5
1 /***********************************************************************
2 * This file is part of HA, a general purpose file archiver.
3 * Copyright (C) 1995 Harri Hirvola
4 * Modified by Ketmar // Invisible Vector
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 ***********************************************************************/
20 #include <fcntl.h>
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
30 #include "libhaunp.h"
31 #include "pbar.h"
34 static int fdi = -1;
35 static int fdo = -1;
36 static uint64_t rdcur = 0, rdtotal = 0;
38 #ifdef NDEBUG
39 # define OUTBUF_SIZE (1024*1024)
40 #else
41 # define OUTBUF_SIZE 1
42 #endif
43 static uint8_t *wrbuf;
46 static int dot_count = -1;
49 static int bread (void *buf, int buf_len, void *udata) {
50 int res = read(fdi, buf, buf_len);
51 if (res >= 0) {
52 if (pbar_dot_count(rdcur, rdtotal) != dot_count) {
53 dot_count = pbar_dot_count(rdcur, rdtotal);
54 pbar_draw(rdcur, rdtotal);
56 rdcur += res;
58 return res;
62 int main (int argc, char *argv[]) {
63 haunp_t hup;
64 int res = 0;
65 #ifndef NDEBUG
66 if (argc != 3) {
67 argc = 3;
68 argv[1] = "egatiles.dd2.haz";
69 argv[2] = "z01";
71 #endif
72 if (argc != 3) {
73 fprintf(stderr, "usage: %s infile outfile\n", argv[0]);
74 return 1;
76 fdi = open(argv[1], O_RDONLY|O_CLOEXEC);
77 if (fdi < 0) {
78 fprintf(stderr, "FATAL: can't open file: '%s'\n", argv[1]);
79 return 1;
81 fdo = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0640);
82 if (fdo < 0) {
83 fprintf(stderr, "FATAL: can't create file: '%s'\n", argv[2]);
84 return 1;
86 rdtotal = lseek(fdi, 0, SEEK_END);
87 lseek(fdi, 0, SEEK_SET);
88 hup = haunp_open_io(bread, NULL);
89 wrbuf = malloc(OUTBUF_SIZE);
90 printf("output buffer size: %d\n", OUTBUF_SIZE);
91 for (;;) {
92 int rd = haunp_read(hup, wrbuf, OUTBUF_SIZE);
93 if (rd <= 0) {
94 pbar_clear();
95 if (rd < 0) fprintf(stdout, "READ ERROR!\n");
96 res = (rd < 0 ? -1 : 0);
97 break;
99 if (write(fdo, wrbuf, rd) != rd) {
100 pbar_clear();
101 fprintf(stdout, "WRITE ERROR!\n");
102 res = -1;
103 break;
106 free(wrbuf);
107 pbar_clear();
108 haunp_close(hup);
109 close(fdi);
110 close(fdo);
111 if (res != 0) unlink(argv[2]);
112 return res;