1 /* $MirOS: contrib/hosted/fwcf/tool.c,v 1.7 2007/03/09 22:35:13 tg Exp $ */
4 * Copyright (c) 2006, 2007
5 * Thorsten Glaser <tg@mirbsd.de>
7 * Provided that these terms and disclaimer and all copyright notices
8 * are retained or reproduced in an accompanying document, permission
9 * is granted to deal in this work without restriction, including un-
10 * limited rights to use, publicly perform, distribute, sell, modify,
11 * merge, give away, or sublicence.
13 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
14 * the utmost extent permitted by applicable law, neither express nor
15 * implied; without malicious intent or gross negligence. In no event
16 * may a licensor, author or contributor be held liable for indirect,
17 * direct, other damage, loss, or other issues arising in any way out
18 * of dealing in the work, even if advised of the possibility of such
19 * damage or existence of a defect, except proven that it results out
20 * of said person's immediate fault when using the work as intended.
23 #include <sys/param.h>
35 static __dead
void usage(void);
36 static int mkfwcf(int, const char *, int);
37 static int unfwcf(int, const char *);
39 static int refwcf(int, int, int);
41 static int fsopen(const char *, int, int);
44 main(int argc
, char *argv
[])
47 int mode
= 0, doempty
= 0;
53 const char *infile
= NULL
, *outfile
= NULL
;
55 const char *dfile
= NULL
;
56 const char *file_root
= NULL
;
60 while ((c
= getopt(argc
, argv
, "D:delMUZ")) != -1)
62 while ((c
= getopt(argc
, argv
, "C:cD:dei:lMo:RUZ")) != -1)
67 if (!(calg
= strtonum(optarg
, 1, 255, NULL
))
68 && !(calg
= compressor_getbyname(optarg
)))
81 mode
= (mode
== 5 || mode
== 6) ? 6 : 3;
94 return (list_compressors());
110 mode
= (mode
== 3) ? 6 : 5;
120 if (argc
!= ((dfile
== NULL
) ? (1 - doempty
) : 0))
124 if (argc
!= ((dfile
== NULL
) ? 1 : 0))
130 if (argc
|| doempty
|| (dfile
!= NULL
))
136 if ((dfile
!= NULL
) || doempty
149 if (mode
== 5 || mode
== 6) {
150 ifd
= fsopen(argc
-- > 0 ? *argv
++ : NULL
,
151 O_RDONLY
, STDIN_FILENO
);
152 ofd
= fsopen(argc
-- > 0 ? *argv
++ : NULL
,
153 O_WRONLY
| O_CREAT
| O_TRUNC
, STDOUT_FILENO
);
158 /* force host tool to compress even without -c */
168 ifd
= fsopen(infile
, O_RDONLY
, STDIN_FILENO
);
169 ofd
= fsopen(outfile
, O_WRONLY
| O_CREAT
| O_TRUNC
, STDOUT_FILENO
);
172 if (mode
== 2 && dfile
!= NULL
) {
177 if ((data
= fwcf_unpack(ifd
, &sz
)) == NULL
)
179 if ((dfd
= fsopen(dfile
, O_WRONLY
| O_CREAT
| O_TRUNC
,
181 err(1, "open %s", dfile
);
182 write_aszdata(dfd
, data
, sz
);
188 if ((mode
== 2) || (mode
== 3))
189 return (unfwcf(ifd
, (mode
== 3) ? NULL
: file_root
));
193 if ((cl
= compress_enumerate()) != NULL
)
194 for (calg
= 1; calg
< 257; ++calg
)
195 if (cl
[calg
& 0xFF].name
!= NULL
)
198 if ((cl
== NULL
) || (calg
== 257))
199 errx(1, "no compression algorithms found");
203 if (mode
== 5 || mode
== 6)
204 return (minilzop(ifd
, ofd
, calg
, (mode
== 6)));
208 return (refwcf(ifd
, ofd
, calg
));
216 if ((dfd
= fsopen(dfile
, O_RDONLY
, STDIN_FILENO
)) < 0)
217 err(1, "open %s", dfile
);
218 read_aszdata(dfd
, &udata
, &isz
);
220 data
= fwcf_pack(udata
, isz
, calg
, &sz
);
221 isz
= write(ofd
, data
, sz
);
223 return (isz
== sz
? 0 : 1);
226 return (mkfwcf(ofd
, doempty
? NULL
: file_root
, calg
));
232 extern const char *__progname
;
234 fprintf(stderr
, "Usage:"
236 " %s -M { -D <file> | -e | <directory> }"
237 "\n %s -U { -D <file> | <directory> }"
238 "\n %s -Z[d] [<infile> [<outfile>]]"
239 "\n %s -l\n", __progname
, __progname
, __progname
, __progname
);
241 " %s -M [-c | -C <compressor>] [-o <file>]"
242 "\n { -D <file> | -e | <directory> }"
243 "\n %s [-i <file>] -U { -D <file> | <directory> }"
244 "\n %s [-i <file>] -d"
245 "\n %s -R [-c | -C <compressor>] [-i <infile>] [-o <outfile>]"
246 "\n %s -Z[d] [-c | -C <compressor>] [<infile> [<outfile>]]"
248 __progname
, __progname
, __progname
, __progname
, __progname
,
255 mkfwcf(int fd
, const char *dir
, int algo
)
260 data
= fwcf_packm(dir
, algo
, &sz
);
261 return ((size_t)write(fd
, data
, sz
) == sz
? 0 : 1);
265 unfwcf(int fd
, const char *dir
)
269 if ((udata
= fwcf_unpack(fd
, NULL
))) {
275 ft_creatm(udata
, dir
);
277 return (udata
!= NULL
? 0 : 1);
282 refwcf(int ifd
, int ofd
, int algo
)
287 if ((udata
= fwcf_unpack(ifd
, &isz
)) == NULL
)
289 data
= fwcf_pack(udata
, isz
, algo
, &sz
);
290 return ((size_t)write(ofd
, data
, sz
) == sz
? 0 : 1);
295 fsopen(const char *fn
, int mode
, int altfd
)
297 return (((fn
== NULL
) || (*fn
== '\0') ||
298 ((fn
[0] == '-') && (fn
[1] == '\0'))) ? altfd
:
299 open(fn
, mode
, 0666));