forked from robertapengelly/ar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
append.c
153 lines (97 loc) · 3.28 KB
/
append.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/******************************************************************************
* @file append.c
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ar.h"
#include "lib.h"
#include "report.h"
void append (FILE *ofp, char *fname) {
unsigned char aout_magic[2];
FILE *tfp;
struct ar_header header;
char temp[17];
char *p, *name = fname, *contents;
long bytes, len, read;
int need_newline = 0;
int valid = 0;
if ((tfp = fopen (fname, "r+b")) == NULL) {
report_at (program_name, 0, REPORT_ERROR, "failed to open %s", fname);
return;
}
if (fread (aout_magic, 2, 1, tfp) != 1) {
fclose (tfp);
report_at (program_name, 0, REPORT_ERROR, "failed whilst reading %s", fname);
return;
}
valid = ((aout_magic[0] == 0x07 && aout_magic[1] == 0x01) || (aout_magic[0] == 0x4C && aout_magic[1] == 0x01) || (aout_magic[0] == 0x64 && aout_magic[1] == 0x86));
if (!valid) {
fclose (tfp);
report_at (program_name, 0, REPORT_ERROR, "%s is not a valid a.out or coff object", fname);
return;
}
memset (temp, 0x20, 16);
temp[0] = '0';
if ((p = strrchr (fname, '/'))) {
name = (p + 1);
}
len = strlen (name);
if (len > 16) {
len = 16;
}
memcpy (header.name, name, len);
while (len < 16) {
header.name[len++] = 0x20;
}
memcpy (header.mtime, temp, 12);
memcpy (header.owner, temp, 6);
memcpy (header.group, temp, 6);
memcpy (header.mode, temp, 8);
fseek (tfp, 0, SEEK_END);
bytes = ftell (tfp);
len = sprintf (temp, "%ld", bytes);
temp[len] = 0x20;
memcpy (header.size, temp, 10);
header.endsig[0] = 0x60;
header.endsig[1] = 0x0A;
need_newline = (bytes % 2);
if (fwrite (&header, sizeof (header), 1, ofp) != 1) {
fclose (tfp);
report_at (program_name, 0, REPORT_ERROR, "failed whilst writing header");
return;
}
contents = xmalloc (512);
fseek (tfp, 0, SEEK_SET);
for (;;) {
if (bytes == 0 || feof (tfp)) {
break;
} else if (bytes >= 512) {
read = 512;
} else {
read = bytes;
}
if (fread (contents, read, 1, tfp) != 1) {
free (contents);
fclose (tfp);
report_at (program_name, 0, REPORT_ERROR, "failed whilst reading %s", fname);
return;
}
bytes -= read;
if (fwrite (contents, read, 1, ofp) != 1) {
free (contents);
fclose (tfp);
report_at (program_name, 0, REPORT_ERROR, "failed whilst writing %s to archive", fname);
return;
}
}
free (contents);
if (need_newline) {
temp[0] = 0x0A;
if (fwrite (temp, 1, 1, ofp) != 1) {
fclose (tfp);
exit (EXIT_FAILURE);
}
}
fclose (tfp);
}