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
|
#include <stdio.h>
#include <fcntl.h>
#define USAGE "USAGE: checkum -[vhcs] infile outfile\n\t-v\tverbose\n\
\t-h\thelp\n\t-c\tcheck checksum\n\t-s\tprint the ipl size"
static int verbose = 0;
static int verify = 0;
static int size = 0;
typedef int word_t;
#define WORDSIZE (sizeof(word_t))
main(argc, argv)
int argc;
char **argv;
{
char *infile;
char *outfile;
int infd;
int outfd;
word_t checksum = 0;
int nbytes;
word_t buf;
int i = 1;
int filesize = 0;
while (*argv[i] == '-') {
switch (*(argv[i]+1)) {
case 'v':
verbose++;
break;
case 'c':
verify++;
puts ("Sorry, unimplemented for now");
exit(1);
break;
case 's':
size++;
break;
case 'h':
puts (USAGE);
exit(0);
default:
printf ("\"%s\", Illegal option\n", argv[i]);
puts (USAGE);
exit(1);
}
i++;
}
infile = *(argv + i);
outfile = *(argv + i+1);
/* see it there were file names on the command line */
if (infile == 0x0) {
puts("Didn't specify an input file name");
exit(1);
}
if (outfile == 0x0) {
puts("Didn't specify an output file name");
exit(1);
}
/* try to open the files */
infd = open(infile, O_RDONLY);
if (infd == -1) {
printf("Couldn't open %s\n", infile);
exit(1);
}
outfd = open(outfile, O_WRONLY|O_CREAT|O_TRUNC);
if (outfd == -1) {
printf("Couldn't open %s\n", outfile);
exit(1);
}
if (verbose > 2)
putchar('\n');
/* calculate the checksum */
while ((nbytes = read(infd, &buf, WORDSIZE)) == WORDSIZE) {
if (verbose > 2)
putchar('.');
checksum+= buf;
filesize+= WORDSIZE;
if (write(outfd, &buf, WORDSIZE) != WORDSIZE) {
puts("Couldn't write");
}
if (verbose > 3)
putchar('+');
}
if (verbose > 2)
putchar('\n');
/* write the last byte read */
if (nbytes > 0) {
write(outfd, &buf, nbytes);
checksum+= buf; /* calculate the last word */
filesize+= nbytes;
}
/* write the checksum */
buf = -checksum;
write(outfd, &buf, WORDSIZE);
filesize+= WORDSIZE; /* checksum increase the size */
if (verbose > 0)
printf("The calculated checksum is:\n\t0x%x,\n\t%u\n", -checksum, -checksum);
/* calculate the extra 2K here */
buf = 0;
while ((filesize % 2048) !=0) {
filesize+=WORDSIZE;
write(outfd, &buf, WORDSIZE);
}
if (size > 0) {
printf ("%u is the new file size\n", filesize);
}
close(outfd);
close(infd);
exit(0);
}
#if 0
/* Calculate a simple checksum and concatenate it to the end of BUF. */
void
compute_and_concatenate_checksum (word *buf, size_t bufsize_in_words)
{
size_t i;
word sum;
sum = buf[0]
for (i = 1; i < bufsize_in_words; i++)
sum += buf[i];
buf[bufsize_in_words] = -sum;
}
/* Calculate a simple checksum and verify it. NOTE: bufsize_in_words should
include the checksum, i.e., it should be one larger than when the
checksum was calculated using compute_and_concatenate_checksum! */
int
compute_and_and_verify_checksum (word *buf, size_t bufsize_in_words)
{
size_t i;
word sum;
sum = buf[0];
for (i = 1; i < bufsize_in_words; i++)
sum += buf[i];
if (sum != 0)
return ERROR;
return SUCCESS;
}
#endif
|