Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
2.2 KiB

  1. //
  2. // In this file are routines to perform operations on archive files.
  3. // They do this by determining the type of the archive and calling a
  4. // corresponding type-specific routine.
  5. //
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <tar.h>
  9. #include <string.h>
  10. #include "buf.h"
  11. #include "psxarc.h"
  12. #include "tarhead.h"
  13. #include "cpio.h"
  14. #include "links.h"
  15. //
  16. // These should be in an include file.
  17. //
  18. extern void CpioList(), CpioRead(), CpioWrite();
  19. extern void TarList(), TarRead(), TarWrite();
  20. extern void cpio_itoa();
  21. void
  22. ListArchive(PBUF pb)
  23. {
  24. PCPIO_HEAD cp;
  25. PTAR_HEAD tp;
  26. InitLinkList();
  27. bfill(pb);
  28. cp = (PCPIO_HEAD)pb->data;
  29. if (0 == strncmp(cp->c_magic, MAGIC, strlen(MAGIC))) {
  30. CpioList(pb);
  31. return;
  32. }
  33. tp = (PTAR_HEAD)pb->data;
  34. if (0 == strncmp(tp->s.magic, TMAGIC, strlen(TMAGIC))) {
  35. TarList(pb);
  36. return;
  37. }
  38. fprintf(stderr, "%s: unknown archive type\n", progname);
  39. exit(4);
  40. }
  41. void
  42. WriteArchive(PBUF pb, int format, char **files, int count)
  43. {
  44. InitLinkList();
  45. if (FORMAT_CPIO == format) {
  46. int len, i;
  47. CPIO_HEAD h;
  48. CpioWrite(pb, files, count);
  49. // write the trailer.
  50. (void)strncpy(h.c_magic, MAGIC, strlen(MAGIC));
  51. cpio_itoa(C_ISREG, h.c_mode, sizeof(h.c_mode));
  52. cpio_itoa(sizeof(LASTFILENAME), h.c_namesize,
  53. sizeof(h.c_namesize));
  54. cpio_itoa(0, h.c_filesize, sizeof(h.c_namesize));
  55. for (i = 0; i < sizeof(h); ++i) {
  56. bputc(pb, ((char *)&h)[i]);
  57. }
  58. // "sizeof" so we get the nul, too
  59. len = sizeof(LASTFILENAME);
  60. for (i = 0; i < len; ++i) {
  61. bputc(pb, LASTFILENAME[i]);
  62. }
  63. bflush(pb);
  64. return;
  65. }
  66. if (FORMAT_TAR == format) {
  67. TarWrite(pb, files, count);
  68. // a tar archive is terminated by two blocks of zeroes.
  69. memset(pb->data, 0, sizeof(pb->data));
  70. bflush(pb);
  71. bflush(pb);
  72. return;
  73. }
  74. // shouldn't get here.
  75. exit(10);
  76. }
  77. void
  78. ReadArchive(PBUF pb)
  79. {
  80. PCPIO_HEAD cp;
  81. PTAR_HEAD tp;
  82. InitLinkList();
  83. bfill(pb);
  84. cp = (PCPIO_HEAD)pb->data;
  85. if (0 == strncmp(cp->c_magic, MAGIC, strlen(MAGIC))) {
  86. CpioRead(pb);
  87. return;
  88. }
  89. tp = (PTAR_HEAD)pb->data;
  90. if (0 == strncmp(tp->s.magic, TMAGIC, strlen(TMAGIC))) {
  91. TarRead(pb);
  92. return;
  93. }
  94. fprintf(stderr, "%s: unknown archive type\n", progname);
  95. exit(4);
  96. }