added COPYING and 'modified by'
[libha.git] / src / main_unp.c
blobd16b76542347d5fa3e888f66b291e5125c4001cb
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"
33 static int fdi = -1;
34 static int fdo = -1;
35 static int rdcur = 0, rdtotal = 0;
37 #ifndef _NDEBUG
38 # define OUTBUF_SIZE (1024*1024)
39 #else
40 # define OUTBUF_SIZE 1
41 #endif
42 static uint8_t *wrbuf;
45 static int bread (void *buf, int buf_len, void *udata) {
46 int res = read(fdi, buf, buf_len);
47 if (res >= 0) {
48 rdcur += res;
49 fprintf(stdout, "\r[%d/%d] %3d%%", rdcur, rdtotal, (int)((uint64_t)100*rdcur/rdtotal));
50 fflush(stdout);
52 return res;
56 int main (int argc, char *argv[]) {
57 haunp_t hup;
58 int res = 0;
59 #ifndef _NDEBUG
60 if (argc != 3) {
61 argc = 3;
62 argv[1] = "egatiles.dd2.haz";
63 argv[2] = "z01";
65 #endif
66 if (argc != 3) {
67 fprintf(stderr, "usage: %s infile outfile\n", argv[0]);
68 return 1;
70 fdi = open(argv[1], O_RDONLY|O_CLOEXEC);
71 if (fdi < 0) {
72 fprintf(stderr, "FATAL: can't open file: '%s'\n", argv[1]);
73 return 1;
75 fdo = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0640);
76 if (fdo < 0) {
77 fprintf(stderr, "FATAL: can't create file: '%s'\n", argv[2]);
78 return 1;
80 rdtotal = lseek(fdi, 0, SEEK_END);
81 lseek(fdi, 0, SEEK_SET);
82 hup = haunp_open_io(bread, NULL);
83 wrbuf = malloc(OUTBUF_SIZE);
84 for (;;) {
85 int rd = haunp_read(hup, wrbuf, sizeof(wrbuf));
86 if (rd <= 0) {
87 if (rd < 0) fprintf(stdout, "\nREAD ERROR!");
88 res = (rd < 0 ? -1 : 0);
89 break;
91 if (write(fdo, wrbuf, rd) != rd) {
92 fprintf(stdout, "\nWRITE ERROR!");
93 res = -1;
94 break;
97 free(wrbuf);
98 fprintf(stdout, "\n");
99 haunp_close(hup);
100 close(fdi);
101 close(fdo);
102 if (res != 0) unlink(argv[2]);
103 return res;