read(stream) and write(stream) under Cygwin work as text functions instead of binary ones - expand LF to CRLF ? How to override this?
I've added "|O_BINARY" to freshclam/manager.c[472]: if((fd = open(file, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644)) == -1) {
and now freshclam.exe works right.
O_BINARY isn't a portable flag, so I posted a patch for ClamAV-0.65 a few days ago[*] that used fdopen with mode "wb" instead. For ClamAV-0.66 and ClamAV-devel, the last hunk of the patch failed, so see the attached patch instead. I've tested it on Linux, but would like to know if it works on Windows. According to MS library docs it ought to work, but I'm not sure about the Cygwin runtime library. Maybe you could test it under Cygwin?
[*] (2004-02-06, Subject: Re: Fresclam not updating on Win32 because of MD5 Verification error)
Index: freshclam/manager.c =================================================================== RCS file: /cvsroot/clamav/clamav-devel/freshclam/manager.c,v retrieving revision 1.18 diff -u -r1.18 manager.c --- freshclam/manager.c 9 Feb 2004 01:07:36 -0000 1.18 +++ freshclam/manager.c 11 Feb 2004 16:19:15 -0000 @@ -447,6 +447,7 @@ { char cmd[512], buffer[FILEBUFF], *ch; int bread, fd, i, rot = 0; + FILE *fp; char *remotename = NULL, *authorization = NULL; const char *rotation = "|/-\\"; @@ -474,6 +475,12 @@ perror("open"); return -1; } + if((fp = fdopen(fd, "wb")) == NULL) { + mprintf("@Can't open new file %s to write\n", file); + perror("fdopen"); + close(fd); + return -1; + } #ifdef NO_SNPRINTF sprintf(cmd, "GET %s/%s HTTP/1.1\r\n" @@ -497,11 +504,13 @@ if ((bread = recv(socketfd, buffer, FILEBUFF, 0)) == -1) { mprintf("@Error while reading database from %s\n", hostname); + fclose(fp); return -1; } if ((strstr(buffer, "HTTP/1.1 404")) != NULL) { mprintf("@%s not found on remote server\n", dbfile); + fclose(fp); return -1; } @@ -517,9 +526,9 @@ i++; } - write(fd, ch, bread - i); + fwrite(ch, 1, bread - i, fp); while((bread = read(socketfd, buffer, FILEBUFF))) { - write(fd, buffer, bread); + fwrite(buffer, 1, bread, fp); mprintf("Downloading %s [%c]\r", dbfile, rotation[rot]); fflush(stdout); /* rot = ++rot % 4; -> operation on `rot' may be undefined (why ?) */ @@ -528,7 +537,7 @@ } mprintf("Downloading %s [*]\n", dbfile); - close(fd); + fclose(fp); return 0; }