forked from robertapengelly/ar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ar.c
129 lines (78 loc) · 2.92 KB
/
ar.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/******************************************************************************
* @file ar.c
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ar.h"
#include "lib.h"
#include "report.h"
#include "stdint.h"
struct ar_state *state = 0;
const char *program_name = 0;
FILE *arfp = NULL;
int main (int argc, char **argv) {
char ar_magic[8];
long i;
if (argc && *argv) {
char *p;
program_name = *argv;
if ((p = strrchr (program_name, '/'))) {
program_name = (p + 1);
}
}
state = xmalloc (sizeof (*state));
parse_args (&argc, &argv, 1);
if (state->append || state->replace) {
if ((arfp = fopen (state->outfile, "r+b")) == NULL) {
if ((arfp = fopen (state->outfile, "w+b")) == NULL) {
report_at (program_name, 0, REPORT_ERROR, "failed to create %s", state->outfile);
return EXIT_FAILURE;
}
if (fwrite ("!<arch>\n", 8, 1, arfp) != 1) {
fclose (arfp);
report_at (program_name, 0, REPORT_ERROR, "failed whilst writing to %s", state->outfile);
return EXIT_FAILURE;
}
}
fclose (arfp);
}
if ((arfp = fopen (state->outfile, "r+b")) == NULL) {
report_at (program_name, 0, REPORT_ERROR, "failed to open %s", state->outfile);
return EXIT_FAILURE;
}
if (fread (ar_magic, 8, 1, arfp) != 1) {
fclose (arfp);
report_at (program_name, 0, REPORT_ERROR, "failed whilst reading %s", state->outfile);
return EXIT_FAILURE;
}
if (memcmp ("!<arch>\n", ar_magic, 8)) {
fclose (arfp);
report_at (program_name, 0, REPORT_ERROR, "%s is not a valid archive file", state->outfile);
return EXIT_FAILURE;
}
for (i = 0; i < state->nb_files; ++i) {
if (state->append) {
fseek (arfp, 0, SEEK_END);
append (arfp, state->files[i]);
} else if (state->replace) {
fseek (arfp, 8, SEEK_SET);
replace (state->files[i]);
} else if (state->del) {
fseek (arfp, 8, SEEK_SET);
delete (state->files[i]);
} else if (state->extract) {
fseek (arfp, 8, SEEK_SET);
extract (state->files[0]);
}
}
if (state->display) {
fseek (arfp, 8, SEEK_SET);
display ();
} else if (state->ranlib) {
fseek (arfp, 8, SEEK_SET);
ranlib ();
}
fclose (arfp);
return EXIT_SUCCESS;
}