Since Macosx Lion fread does not read file with length > 2G (int size, 2'147'483'648 bytes). It worked for years with macosx snow leopard.
I wrote a program to test it :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *fin = NULL, *fout = NULL;
char *ptr = NULL;
size_t len;
fpos_t flen;
if (!(fin = fopen(argv[1], "rb")))
{
printf("The input file: %s could not be opened\n", argv[1]);
return -1;
}
if ((fout = fopen(argv[2], "rb")))
{
printf("The output file %s already exist\n", argv[2]);
fclose(fin);
return -1;
}
if (!(fout = fopen(argv[2],"wb")))
{
printf("Cannot write on output file %s\n", argv[2]);
fclose(fin);
return -1;
}
fseek(fin, 0, SEEK_END);
fgetpos(fin, &flen);
len = flen;
printf("Input file length : %zd\n", len);
fseek(fin, 0, SEEK_SET);
if (!(ptr = malloc(len)))
{
printf("Canot allocate %zd bytes\n", len);
fclose(fin);
fclose(fout);
return -1;
}
if (fread(ptr, sizeof(char), len, fin) != len)
{
printf("Cannot read file\n");
fclose(fin);
fclose(fout);
free(ptr);
return -1;
}
fclose(fin);
if (fwrite(ptr, sizeof(char), len, fout) != len)
{
printf("Cannot write file\n");
fclose(fout);
free(ptr);
return -1;
}
free(ptr);
fclose(fout);
return 1;
}
just run :
- ./pgm inputfile outputfile
- openssl sha inputfile
- openssl sha outputfile
There is no error. The length of the 2 files are the same. The two fingerprints are not the same. (The pointer is well allocated and write in the outputfile) Its only with fread, not fwrite.
i don't understand the problem.
I just see this program (i don't know if apple use this one on Lion) and r variable is defined as int. http://www.opensource.apple.com/source/Libc/Libc-186/stdio.subproj/fread.c
Thanks for answers