| 28 | } |
| 29 | |
| 30 | int main(int argc, char* argv[]) { |
| 31 | long bufsize; |
| 32 | |
| 33 | if (argc != 2) { |
| 34 | fputs("Need 1 argument\n", stderr); |
| 35 | return (EXIT_FAILURE); |
| 36 | } |
| 37 | |
| 38 | unsigned char *source = NULL; |
| 39 | FILE *fp = fopen(argv[1], "rb"); |
| 40 | if (fp != NULL) { |
| 41 | /* Go to the end of the file. */ |
| 42 | if (fseek(fp, 0L, SEEK_END) == 0) { |
| 43 | /* Get the size of the file. */ |
| 44 | bufsize = ftell(fp); |
| 45 | if (bufsize == -1) { fputs("Couldn't get size\n", stderr); return (EXIT_FAILURE); } |
| 46 | |
| 47 | /* Allocate our buffer to that size. */ |
| 48 | source = malloc(sizeof(char) * (bufsize + 1)); |
| 49 | if (source == NULL) { fputs("Couldn't allocate\n", stderr); return (EXIT_FAILURE); } |
| 50 | |
| 51 | /* Go back to the start of the file. */ |
| 52 | if (fseek(fp, 0L, SEEK_SET) == -1) { fputs("Couldn't seek\n", stderr); return (EXIT_FAILURE); } |
| 53 | |
| 54 | /* Read the entire file into memory. */ |
| 55 | size_t newLen = fread(source, sizeof(char), bufsize, fp); |
| 56 | if (newLen == 0) { |
| 57 | fputs("Error reading file\n", stderr); |
| 58 | //return (EXIT_FAILURE); |
| 59 | } else { |
| 60 | source[++newLen] = '\0'; /* Just to be safe. */ |
| 61 | } |
| 62 | } else { |
| 63 | fputs("Couldn't seek to end\n", stderr); |
| 64 | return (EXIT_FAILURE); |
| 65 | } |
| 66 | fclose(fp); |
| 67 | } else { |
| 68 | fputs("Couldn't open\n", stderr); |
| 69 | return (EXIT_FAILURE); |
| 70 | } |
| 71 | |
| 72 | printf("%u\n", (uint32_t) adler32(source, bufsize)); |
| 73 | |
| 74 | free(source); /* Don't forget to call free() later! */ |
| 75 | |
| 76 | return (EXIT_SUCCESS); |
| 77 | } |