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.

124 lines
2.5 KiB

  1. /*++
  2. psxarc - a program to do minimal minipulation and extraction of POSIX-type
  3. tar and cpio archives. Certainly not as good as real tar and cpio.
  4. --*/
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <string.h>
  9. #include "getopt.h"
  10. #include "buf.h"
  11. #include "psxarc.h"
  12. char *progname = "psxarc";
  13. char *pchArchive;
  14. PBUF pbArchive;
  15. int fRead, fWrite; // what to do; neither == list archive
  16. int fVerbose; // to be, or not to be
  17. static void
  18. usage(void)
  19. {
  20. fprintf(stderr,
  21. "usage: %s [-hrv] [-f archive]\n", progname);
  22. fprintf(stderr,
  23. "\t%s -w [-f archive] [-x format] files\n", progname);
  24. }
  25. int
  26. main(int argc, char **argv)
  27. {
  28. int c;
  29. char *pchOpts = "hf:rvwx:";
  30. int format = FORMAT_DEFAULT; // to write tar or cpio?
  31. // parse options
  32. while (-1 != (c = getopt(argc, argv, pchOpts))) {
  33. switch (c) {
  34. case 'f':
  35. pchArchive = optarg;
  36. break;
  37. case 'h':
  38. usage();
  39. fprintf(stderr, "-h:\t help\n");
  40. fprintf(stderr, "-r:\t read archive file\n");
  41. fprintf(stderr, "-w:\t write archive file\n");
  42. fprintf(stderr, "-f:\t specify archive file, default stdio\n");
  43. fprintf(stderr, "-v:\t be verbose\n");
  44. fprintf(stderr, "-x:\t use format, tar or cpio\n");
  45. return 0;
  46. case 'r':
  47. ++fRead;
  48. break;
  49. case 'w':
  50. ++fWrite;
  51. break;
  52. case 'v':
  53. ++fVerbose;
  54. break;
  55. case 'x':
  56. if (0 == strcmp(optarg, "tar")) {
  57. format = FORMAT_TAR;
  58. } else if (0 == strcmp(optarg, "cpio")) {
  59. format = FORMAT_CPIO;
  60. } else {
  61. fprintf(stderr, "%s: unknown format %s\n",
  62. progname, optarg);
  63. return 4;
  64. }
  65. break;
  66. case BADCH:
  67. default:
  68. usage();
  69. return 1;
  70. }
  71. }
  72. if (fRead && fWrite) {
  73. fprintf(stderr, "%s: -r excludes -w\n", progname);
  74. return 1;
  75. }
  76. if (NULL != pchArchive) {
  77. int mode;
  78. if (fWrite) {
  79. // write to archive file instead of stdout
  80. mode = O_WRONLY | O_CREAT;
  81. } else {
  82. // either -r (read) or list
  83. mode = O_RDONLY;
  84. }
  85. pbArchive = bopen(pchArchive, mode);
  86. } else {
  87. if (fRead) {
  88. pbArchive = bfdopen(fileno(stdin), O_RDONLY);
  89. } else if (fWrite) {
  90. pbArchive = bfdopen(fileno(stdout), O_WRONLY);
  91. }
  92. }
  93. if (!fRead && !fWrite) {
  94. // list the archive
  95. ListArchive(pbArchive);
  96. return 0;
  97. }
  98. if (fRead) {
  99. ReadArchive(pbArchive);
  100. return 0;
  101. }
  102. if (optind == argc) {
  103. usage();
  104. return 1;
  105. }
  106. if (FORMAT_DEFAULT == format) {
  107. fprintf(stderr, "%s: warning: using tar format\n", progname);
  108. format = FORMAT_TAR;
  109. }
  110. WriteArchive(pbArchive, format, &argv[optind], argc - optind);
  111. return 0;
  112. }