tool.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* $MirOS: contrib/hosted/fwcf/tool.c,v 1.7 2007/03/09 22:35:13 tg Exp $ */
  2. /*-
  3. * Copyright (c) 2006, 2007
  4. * Thorsten Glaser <tg@mirbsd.de>
  5. *
  6. * Provided that these terms and disclaimer and all copyright notices
  7. * are retained or reproduced in an accompanying document, permission
  8. * is granted to deal in this work without restriction, including un-
  9. * limited rights to use, publicly perform, distribute, sell, modify,
  10. * merge, give away, or sublicence.
  11. *
  12. * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
  13. * the utmost extent permitted by applicable law, neither express nor
  14. * implied; without malicious intent or gross negligence. In no event
  15. * may a licensor, author or contributor be held liable for indirect,
  16. * direct, other damage, loss, or other issues arising in any way out
  17. * of dealing in the work, even if advised of the possibility of such
  18. * damage or existence of a defect, except proven that it results out
  19. * of said person's immediate fault when using the work as intended.
  20. */
  21. #include <sys/param.h>
  22. #include <err.h>
  23. #include <fcntl.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include "defs.h"
  28. #include "compress.h"
  29. #include "minilzop.h"
  30. #include "pack.h"
  31. __RCSID("$MirOS: contrib/hosted/fwcf/tool.c,v 1.7 2007/03/09 22:35:13 tg Exp $");
  32. static __dead void usage(void);
  33. static int mkfwcf(int, const char *, int);
  34. static int unfwcf(int, const char *);
  35. #ifndef SMALL
  36. static int refwcf(int, int, int);
  37. #endif
  38. static int fsopen(const char *, int, int);
  39. int
  40. main(int argc, char *argv[])
  41. {
  42. int c;
  43. int mode = 0, doempty = 0;
  44. int ifd, ofd;
  45. #ifdef SMALL
  46. int calg = -1;
  47. #else
  48. int calg = 0;
  49. const char *infile = NULL, *outfile = NULL;
  50. #endif
  51. const char *dfile = NULL;
  52. const char *file_root = NULL;
  53. fwcf_compressor *cl;
  54. #ifdef SMALL
  55. while ((c = getopt(argc, argv, "D:delMUZ")) != -1)
  56. #else
  57. while ((c = getopt(argc, argv, "C:cD:dei:lMo:RUZ")) != -1)
  58. #endif
  59. switch (c) {
  60. #ifndef SMALL
  61. case 'C':
  62. if (!(calg = strtonum(optarg, 1, 255, NULL))
  63. && !(calg = compressor_getbyname(optarg)))
  64. usage();
  65. break;
  66. case 'c':
  67. calg = -1;
  68. break;
  69. #endif
  70. case 'D':
  71. if (doempty)
  72. usage();
  73. dfile = optarg;
  74. break;
  75. case 'd':
  76. mode = (mode == 5 || mode == 6) ? 6 : 3;
  77. break;
  78. case 'e':
  79. if (dfile != NULL)
  80. usage();
  81. doempty = 1;
  82. break;
  83. #ifndef SMALL
  84. case 'i':
  85. infile = optarg;
  86. break;
  87. #endif
  88. case 'l':
  89. return (list_compressors());
  90. case 'M':
  91. mode = 1;
  92. break;
  93. #ifndef SMALL
  94. case 'o':
  95. outfile = optarg;
  96. break;
  97. case 'R':
  98. mode = 4;
  99. break;
  100. #endif
  101. case 'U':
  102. mode = 2;
  103. break;
  104. case 'Z':
  105. mode = (mode == 3) ? 6 : 5;
  106. break;
  107. default:
  108. usage();
  109. }
  110. argc -= optind;
  111. argv += optind;
  112. switch (mode) {
  113. case 1:
  114. if (argc != ((dfile == NULL) ? (1 - doempty) : 0))
  115. usage();
  116. break;
  117. case 2:
  118. if (argc != ((dfile == NULL) ? 1 : 0))
  119. usage();
  120. break;
  121. #ifndef SMALL
  122. case 3:
  123. case 4:
  124. if (argc || doempty || (dfile != NULL))
  125. usage();
  126. break;
  127. #endif
  128. case 5:
  129. case 6:
  130. if ((dfile != NULL) || doempty
  131. #ifndef SMALL
  132. || infile || outfile
  133. #endif
  134. )
  135. usage();
  136. break;
  137. default:
  138. usage();
  139. }
  140. if (argc)
  141. file_root = *argv;
  142. if (mode == 5 || mode == 6) {
  143. ifd = fsopen(argc-- > 0 ? *argv++ : NULL,
  144. O_RDONLY, STDIN_FILENO);
  145. ofd = fsopen(argc-- > 0 ? *argv++ : NULL,
  146. O_WRONLY | O_CREAT | O_TRUNC, STDOUT_FILENO);
  147. if (argc > 0)
  148. usage();
  149. #ifndef SMALL
  150. if (calg == 0)
  151. /* force host tool to compress even without -c */
  152. calg = -1;
  153. #endif
  154. goto get_calg;
  155. }
  156. #ifdef SMALL
  157. ifd = STDIN_FILENO;
  158. ofd = STDOUT_FILENO;
  159. #else
  160. ifd = fsopen(infile, O_RDONLY, STDIN_FILENO);
  161. ofd = fsopen(outfile, O_WRONLY | O_CREAT | O_TRUNC, STDOUT_FILENO);
  162. #endif
  163. if (mode == 2 && dfile != NULL) {
  164. char *data;
  165. size_t sz;
  166. int dfd;
  167. if ((data = fwcf_unpack(ifd, &sz)) == NULL)
  168. return (1);
  169. if ((dfd = fsopen(dfile, O_WRONLY | O_CREAT | O_TRUNC,
  170. STDOUT_FILENO)) < 0)
  171. err(1, "open %s", dfile);
  172. write_aszdata(dfd, data, sz);
  173. close(dfd);
  174. free(data);
  175. return (0);
  176. }
  177. if ((mode == 2) || (mode == 3))
  178. return (unfwcf(ifd, (mode == 3) ? NULL : file_root));
  179. get_calg:
  180. if (calg == -1) {
  181. if ((cl = compress_enumerate()) != NULL)
  182. for (calg = 1; calg < 257; ++calg)
  183. if (cl[calg & 0xFF].name != NULL)
  184. break;
  185. if ((cl == NULL) || (calg == 257))
  186. errx(1, "no compression algorithms found");
  187. calg &= 0xFF;
  188. }
  189. if (mode == 5 || mode == 6)
  190. return (minilzop(ifd, ofd, calg, (mode == 6)));
  191. #ifndef SMALL
  192. if (mode == 4)
  193. return (refwcf(ifd, ofd, calg));
  194. #endif
  195. if (dfile != NULL) {
  196. char *udata, *data;
  197. size_t sz, isz;
  198. int dfd;
  199. if ((dfd = fsopen(dfile, O_RDONLY, STDIN_FILENO)) < 0)
  200. err(1, "open %s", dfile);
  201. read_aszdata(dfd, &udata, &isz);
  202. close(dfd);
  203. data = fwcf_pack(udata, isz, calg, &sz);
  204. isz = write(ofd, data, sz);
  205. free(data);
  206. return (isz == sz ? 0 : 1);
  207. }
  208. return (mkfwcf(ofd, doempty ? NULL : file_root, calg));
  209. }
  210. static __dead void
  211. usage(void)
  212. {
  213. extern const char *__progname;
  214. fprintf(stderr, "Usage:"
  215. #ifdef SMALL
  216. " %s -M { -D <file> | -e | <directory> }"
  217. "\n %s -U { -D <file> | <directory> }"
  218. "\n %s -Z[d] [<infile> [<outfile>]]"
  219. "\n %s -l\n", __progname, __progname, __progname, __progname);
  220. #else
  221. " %s -M [-c | -C <compressor>] [-o <file>]"
  222. "\n { -D <file> | -e | <directory> }"
  223. "\n %s [-i <file>] -U { -D <file> | <directory> }"
  224. "\n %s [-i <file>] -d"
  225. "\n %s -R [-c | -C <compressor>] [-i <infile>] [-o <outfile>]"
  226. "\n %s -Z[d] [-c | -C <compressor>] [<infile> [<outfile>]]"
  227. "\n %s -l\n",
  228. __progname, __progname, __progname, __progname, __progname,
  229. __progname);
  230. #endif
  231. exit(1);
  232. }
  233. static int
  234. mkfwcf(int fd, const char *dir, int algo)
  235. {
  236. size_t sz;
  237. char *data;
  238. data = fwcf_packm(dir, algo, &sz);
  239. return ((size_t)write(fd, data, sz) == sz ? 0 : 1);
  240. }
  241. static int
  242. unfwcf(int fd, const char *dir)
  243. {
  244. char *udata;
  245. if ((udata = fwcf_unpack(fd, NULL))) {
  246. #ifndef SMALL
  247. if (dir == NULL)
  248. ft_dump(udata);
  249. else
  250. #endif
  251. ft_creatm(udata, dir);
  252. }
  253. return (udata != NULL ? 0 : 1);
  254. }
  255. #ifndef SMALL
  256. static int
  257. refwcf(int ifd, int ofd, int algo)
  258. {
  259. char *udata, *data;
  260. size_t sz, isz;
  261. if ((udata = fwcf_unpack(ifd, &isz)) == NULL)
  262. return (1);
  263. data = fwcf_pack(udata, isz, algo, &sz);
  264. return ((size_t)write(ofd, data, sz) == sz ? 0 : 1);
  265. }
  266. #endif
  267. static int
  268. fsopen(const char *fn, int mode, int altfd)
  269. {
  270. return (((fn == NULL) || (*fn == '\0') ||
  271. ((fn[0] == '-') && (fn[1] == '\0'))) ? altfd :
  272. open(fn, mode, 0666));
  273. }