ft_packm.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* $MirOS: contrib/hosted/fwcf/ft_packm.c,v 1.4 2006/09/23 23:21:04 tg Exp $ */
  2. /*-
  3. * Copyright (c) 2006
  4. * Thorsten Glaser <tg@mirbsd.de>
  5. *
  6. * Licensee is hereby permitted to deal in this work without restric-
  7. * tion, including unlimited rights to use, publicly perform, modify,
  8. * merge, distribute, sell, give away or sublicence, provided all co-
  9. * pyright notices above, these terms and the disclaimer are retained
  10. * in all redistributions or reproduced in accompanying documentation
  11. * or other materials provided with binary redistributions.
  12. *
  13. * Licensor offers the work "AS IS" and WITHOUT WARRANTY of any kind,
  14. * express, or implied, to the maximum extent permitted by applicable
  15. * law, without malicious intent or gross negligence; in no event may
  16. * licensor, an author or contributor be held liable for any indirect
  17. * or other damage, or direct damage except proven a consequence of a
  18. * direct error of said person and intended use of this work, loss or
  19. * other issues arising in any way out of its use, even if advised of
  20. * the possibility of such damage or existence of a defect.
  21. */
  22. #include <sys/param.h>
  23. #include <err.h>
  24. #include <errno.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include "defs.h"
  28. #include "fts_subs.h"
  29. #include "pack.h"
  30. __RCSID("$MirOS: contrib/hosted/fwcf/ft_packm.c,v 1.4 2006/09/23 23:21:04 tg Exp $");
  31. char *
  32. ft_packm(void)
  33. {
  34. ftsf_entry e;
  35. int i;
  36. char *rv = NULL, *rv2, *afile;
  37. size_t len = 1 + sizeof (size_t), k, pos = sizeof (size_t);
  38. while ((i = ftsf_next(&e)) > 0) {
  39. //ftsf_debugent(&e);
  40. if ((e.etype != FTSF_FILE) && (e.etype != FTSF_SYMLINK) &&
  41. (e.etype != FTSF_DIR))
  42. continue;
  43. if ((afile = ft_pack(&e)) == NULL)
  44. errx(1, "cannot pack %s/%s", ftsf_prefix, e.pathname);
  45. len += (k = *(size_t *)afile - sizeof (size_t));
  46. if ((rv2 = realloc(rv, len)) == NULL) {
  47. i = errno;
  48. free(rv);
  49. errno = i;
  50. err(1, "malloc");
  51. }
  52. rv = rv2;
  53. memcpy(rv + pos, afile + sizeof (size_t), k);
  54. pos += k;
  55. free(afile);
  56. }
  57. if (i < 0)
  58. errx(1, "ft_packm failed in fts");
  59. if (rv == NULL)
  60. if ((rv = malloc(len)) == NULL)
  61. err(1, "malloc");
  62. rv[len - 1] = '\0';
  63. *(size_t *)rv = len;
  64. return (rv);
  65. }