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