-
Notifications
You must be signed in to change notification settings - Fork 46
58 lines (48 loc) · 1.36 KB
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
/********************************/
/* Program Name: checksum.c */
/* Author:Akkharaw Jom. */
/* Date: 2/9/2021 */
/********************************/
/* Description: */
/* Validation Checks: */
/* Enhancements: */
/********************************/
#include "stdio.h"
#include "stdlib.h"
#include <sys/types.h>
#include <unistd.h>
#define max_int (255)
#define byte unsigned char
int main (int argc, char * argv[], char ** envp) {
int count = 10;
int sum = 0;
int quotient;
int remainder;
byte checksum;
byte complement;
byte buffer[10];
read(STDIN_FILENO, &buffer, 10);
for (int x = 0; x < count; x++) {
printf("%d ", buffer[x]);
printf("\n");
}
for (int x = 0; x < count; x++) {
if (x == 5) {
checksum = buffer[x];
buffer[x] = 0;
}
sum += buffer[x];
}
quotient = sum / (max_int + 1);
remainder = sum % (max_int + 1);
sum = quotient + remainder;
complement = max_int - sum;
/* the following is the prototype for the read system call */
/* int read(int fildes, void *buf, size_t nbyte); */
fprintf(stdout, "Stored Checksum: %d, Computed Checksum: %d\n", checksum, complement);
if (checksum != complement ) {
fprintf(stderr, "Error Detected!\n");
return 1;
}
return 0;
}