tool.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. static __dead void usage(void);
  32. static int mkfwcf(int, const char *, int);
  33. static int unfwcf(int, const char *);
  34. #ifndef SMALL
  35. static int refwcf(int, int, int);
  36. #endif
  37. static int fsopen(const char *, int, int);
  38. int
  39. main(int argc, char *argv[])
  40. {
  41. int c;
  42. int mode = 0, doempty = 0;
  43. int ifd, ofd;
  44. #ifdef SMALL
  45. int calg = -1;
  46. #else
  47. int calg = 0;
  48. const char *infile = NULL, *outfile = NULL;
  49. #endif
  50. const char *dfile = NULL;
  51. const char *file_root = NULL;
  52. fwcf_compressor *cl;
  53. #ifdef SMALL
  54. while ((c = getopt(argc, argv, "D:delMUZ")) != -1)
  55. #else
  56. while ((c = getopt(argc, argv, "C:cD:dei:lMo:RUZ")) != -1)
  57. #endif
  58. switch (c) {
  59. #ifndef SMALL
  60. case 'C':
  61. if (!(calg = strtonum(optarg, 1, 255, NULL))
  62. && !(calg = compressor_getbyname(optarg)))
  63. usage();
  64. break;
  65. case 'c':
  66. calg = -1;
  67. break;
  68. #endif
  69. case 'D':
  70. if (doempty)
  71. usage();
  72. dfile = optarg;
  73. break;
  74. case 'd':
  75. mode = (mode == 5 || mode == 6) ? 6 : 3;
  76. break;
  77. case 'e':
  78. if (dfile != NULL)
  79. usage();
  80. doempty = 1;
  81. break;
  82. #ifndef SMALL
  83. case 'i':
  84. infile = optarg;
  85. break;
  86. #endif
  87. case 'l':
  88. return (list_compressors());
  89. case 'M':
  90. mode = 1;
  91. break;
  92. #ifndef SMALL
  93. case 'o':
  94. outfile = optarg;
  95. break;
  96. case 'R':
  97. mode = 4;
  98. break;
  99. #endif
  100. case 'U':
  101. mode = 2;
  102. break;
  103. case 'Z':
  104. mode = (mode == 3) ? 6 : 5;
  105. break;
  106. default:
  107. usage();
  108. }
  109. argc -= optind;
  110. argv += optind;
  111. switch (mode) {
  112. case 1:
  113. if (argc != ((dfile == NULL) ? (1 - doempty) : 0))
  114. usage();
  115. break;
  116. case 2:
  117. if (argc != ((dfile == NULL) ? 1 : 0))
  118. usage();
  119. break;
  120. #ifndef SMALL
  121. case 3:
  122. case 4:
  123. if (argc || doempty || (dfile != NULL))
  124. usage();
  125. break;
  126. #endif
  127. case 5:
  128. case 6:
  129. if ((dfile != NULL) || doempty
  130. #ifndef SMALL
  131. || infile || outfile
  132. #endif
  133. )
  134. usage();
  135. break;
  136. default:
  137. usage();
  138. }
  139. if (argc)
  140. file_root = *argv;
  141. if (mode == 5 || mode == 6) {
  142. ifd = fsopen(argc-- > 0 ? *argv++ : NULL,
  143. O_RDONLY, STDIN_FILENO);
  144. ofd = fsopen(argc-- > 0 ? *argv++ : NULL,
  145. O_WRONLY | O_CREAT | O_TRUNC, STDOUT_FILENO);
  146. if (argc > 0)
  147. usage();
  148. #ifndef SMALL
  149. if (calg == 0)
  150. /* force host tool to compress even without -c */
  151. calg = -1;
  152. #endif
  153. goto get_calg;
  154. }
  155. #ifdef SMALL
  156. ifd = STDIN_FILENO;
  157. ofd = STDOUT_FILENO;
  158. #else
  159. ifd = fsopen(infile, O_RDONLY, STDIN_FILENO);
  160. ofd = fsopen(outfile, O_WRONLY | O_CREAT | O_TRUNC, STDOUT_FILENO);
  161. #endif
  162. if (mode == 2 && dfile != NULL) {
  163. char *data;
  164. size_t sz;
  165. int dfd;
  166. if ((data = fwcf_unpack(ifd, &sz)) == NULL)
  167. return (1);
  168. if ((dfd = fsopen(dfile, O_WRONLY | O_CREAT | O_TRUNC,
  169. STDOUT_FILENO)) < 0)
  170. err(1, "open %s", dfile);
  171. write_aszdata(dfd, data, sz);
  172. close(dfd);
  173. free(data);
  174. return (0);
  175. }
  176. if ((mode == 2) || (mode == 3))
  177. return (unfwcf(ifd, (mode == 3) ? NULL : file_root));
  178. get_calg:
  179. if (calg == -1) {
  180. if ((cl = compress_enumerate()) != NULL)
  181. for (calg = 1; calg < 257; ++calg)
  182. if (cl[calg & 0xFF].name != NULL)
  183. break;
  184. if ((cl == NULL) || (calg == 257))
  185. errx(1, "no compression algorithms found");
  186. calg &= 0xFF;
  187. }
  188. if (mode == 5 || mode == 6)
  189. return (minilzop(ifd, ofd, calg, (mode == 6)));
  190. #ifndef SMALL
  191. if (mode == 4)
  192. return (refwcf(ifd, ofd, calg));
  193. #endif
  194. if (dfile != NULL) {
  195. char *udata, *data;
  196. size_t sz, isz;
  197. int dfd;
  198. if ((dfd = fsopen(dfile, O_RDONLY, STDIN_FILENO)) < 0)
  199. err(1, "open %s", dfile);
  200. read_aszdata(dfd, &udata, &isz);
  201. close(dfd);
  202. data = fwcf_pack(udata, isz, calg, &sz);
  203. isz = write(ofd, data, sz);
  204. free(data);
  205. return (isz == sz ? 0 : 1);
  206. }
  207. return (mkfwcf(ofd, doempty ? NULL : file_root, calg));
  208. }
  209. static __dead void
  210. usage(void)
  211. {
  212. extern const char *__progname;
  213. fprintf(stderr, "Usage:"
  214. #ifdef SMALL
  215. " %s -M { -D <file> | -e | <directory> }"
  216. "\n %s -U { -D <file> | <directory> }"
  217. "\n %s -Z[d] [<infile> [<outfile>]]"
  218. "\n %s -l\n", __progname, __progname, __progname, __progname);
  219. #else
  220. " %s -M [-c | -C <compressor>] [-o <file>]"
  221. "\n { -D <file> | -e | <directory> }"
  222. "\n %s [-i <file>] -U { -D <file> | <directory> }"
  223. "\n %s [-i <file>] -d"
  224. "\n %s -R [-c | -C <compressor>] [-i <infile>] [-o <outfile>]"
  225. "\n %s -Z[d] [-c | -C <compressor>] [<infile> [<outfile>]]"
  226. "\n %s -l\n",
  227. __progname, __progname, __progname, __progname, __progname,
  228. __progname);
  229. #endif
  230. exit(1);
  231. }
  232. static int
  233. mkfwcf(int fd, const char *dir, int algo)
  234. {
  235. size_t sz;
  236. char *data;
  237. data = fwcf_packm(dir, algo, &sz);
  238. return ((size_t)write(fd, data, sz) == sz ? 0 : 1);
  239. }
  240. static int
  241. unfwcf(int fd, const char *dir)
  242. {
  243. char *udata;
  244. if ((udata = fwcf_unpack(fd, NULL))) {
  245. #ifndef SMALL
  246. if (dir == NULL)
  247. ft_dump(udata);
  248. else
  249. #endif
  250. ft_creatm(udata, dir);
  251. }
  252. return (udata != NULL ? 0 : 1);
  253. }
  254. #ifndef SMALL
  255. static int
  256. refwcf(int ifd, int ofd, int algo)
  257. {
  258. char *udata, *data;
  259. size_t sz, isz;
  260. if ((udata = fwcf_unpack(ifd, &isz)) == NULL)
  261. return (1);
  262. data = fwcf_pack(udata, isz, algo, &sz);
  263. return ((size_t)write(ofd, data, sz) == sz ? 0 : 1);
  264. }
  265. #endif
  266. static int
  267. fsopen(const char *fn, int mode, int altfd)
  268. {
  269. return (((fn == NULL) || (*fn == '\0') ||
  270. ((fn[0] == '-') && (fn[1] == '\0'))) ? altfd :
  271. open(fn, mode, 0666));
  272. }