delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2026/09/04/04:09:13

DMARC-Filter: OpenDMARC Filter v1.4.2 delorie.com 68489A8F2724423
Authentication-Results: delorie.com; dmarc=pass (p=none dis=none) header.from=cygwin.com
Authentication-Results: delorie.com; spf=pass smtp.mailfrom=cygwin.com
DKIM-Filter: OpenDKIM Filter v2.11.0 delorie.com 68489A8F2724423
Authentication-Results: delorie.com;
dkim=pass (1024-bit key, unprotected) header.d=cygwin.com header.i=@cygwin.com header.a=rsa-sha256 header.s=default header.b=B4qRkB91
X-Recipient: archive-cygwin AT delorie DOT com
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E5AE84BB3BB6
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com;
s=default; t=1788509348;
bh=egJMQz/W9dTKUYd7En2jHjIA+aVa3XXzEoFdqs8StiI=;
h=Resent-From:Resent-Date:Resent-To:Subject:Date:Cc:To:List-Id:
List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe:
From:Reply-To:From;
b=B4qRkB91URCZd9Ch8HwhBDN7278VwdtclNBgbdsZogXJHFizVvbUJfFMNG+Qqlf9p
aHNY8d4HIPq8+kCP1xg2SwOeGQYc+v86FqLn4R1/ypxDE6aus3MeNlgVqlFbb06FAB
VUL6WLVKsgAPNoBCTUC08ySbR7enBz53Pdp5GPzM=
X-Original-To: cygwin AT cygwin DOT com
Delivered-To: cygwin AT cygwin DOT com
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 732494BB1C3A
Resent-From: root <root AT sourceware DOT org>
Resent-Date: Fri, 4 Sep 2026 08:08:22 +0000
Resent-Message-ID: <app8diA8++mNLuTB AT sourceware DOT org>
Resent-To: cygwin AT cygwin DOT com
X-Original-To: spam AT localhost
Delivered-To: spam AT localhost
DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C73E14BA2E2D
ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C73E14BA2E2D
ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1788109191; cv=none;
b=g5srsQ84Cn80XGdrxx+c5FtqOgmBRc5qVL2ID5Wj/+LBR8oLi459xTGgDTKr+NadBRdsO7OzWOAx6pFS0QnNPynVaR9FUG4N/TzEwvYUl88VueJ+ib+NXXfBdWpLCsQqvCHIREaO3dD9BGG0igep2zFQY/tfj26gfiauKs50tiA=
ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key;
t=1788109191; c=relaxed/simple;
bh=KZqphPrk727RB7c2wR5DZpSEMXCrqTF2jpiiFAg4C3s=;
h=From:Mime-Version:Subject:Message-Id:Date:To;
b=KsbaksRNopGJZcF8aoI496RAZzmB/8LYLMvEO1EyKpjHB31aLCLaYWLZx2Tg5whDDUrjJ/2WkREFMtYd2/IelYdE0tW7NWBpHSAIdqtVatM7ic9YGPm5L8Z9ngsE6OZRS0FE/VvsXHIN6ipEflxqIxA8qClTxALGkRHcq1YxPrU=
ARC-Authentication-Results: i=1; sourceware.org
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C73E14BA2E2D
Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.700.81.1.8\))
Subject: diff, malloc() and/or read() questions
Message-Id: <3B21BC8B-7713-45F8-905F-6A109A908FD3@Denis-Excoffier.org>
Date: Sun, 30 Aug 2026 18:59:34 +0200
Cc: Denis Excoffier <cygwin AT Denis-Excoffier DOT org>
To: cygwin AT cygwin DOT com
X-Mailer: Apple Mail (2.3826.700.81.1.8)
X-BeenThere: cygwin AT cygwin DOT com
X-Mailman-Version: 2.1.30
List-Id: General Cygwin discussions and problem reports <cygwin.cygwin.com>
List-Archive: <https://cygwin.com/pipermail/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-request AT cygwin DOT com?subject=help>
List-Subscribe: <https://cygwin.com/mailman/listinfo/cygwin>,
<mailto:cygwin-request AT cygwin DOT com?subject=subscribe>
From: Denis Excoffier via Cygwin <cygwin AT cygwin DOT com>
Reply-To: Denis Excoffier <cygwin AT Denis-Excoffier DOT org>
Sender: "Cygwin" <cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com>

Hello,
I have two files F1 and F2 (4.5Gb each) that i try to compare using cmp and
diff. They can be created with a Makefile as follows:

all: F1 F2
	-cmp -lb F1 F2
	-diff F1 F2
F1: l1 M100 G1
	#cat M100 l1 M100 M100 G1 G1 l1 G1 G1 > $@
	cat M100 M100 l1 M100 M100 M100 G1 G1 l1 G1 G1 > $@
F2: l2 M100 G1
	#cat M100 l2 M100 M100 G1 G1 l2 G1 G1 > $@
	cat M100 M100 l2 M100 M100 M100 G1 G1 l2 G1 G1 > $@
l1:
	echo "abc" > $@
l2:
	echo "xyz" > $@
G1: M100
	cat $+ $+ $+ $+ $+ $+ $+ $+ $+ $+ > $@
M100: M50
	cat $+ $+ > $@
M50: M5
	cat $+ $+ $+ $+ $+ $+ $+ $+ $+ $+ > $@
M5: k500
	cat $+ $+ $+ $+ $+ $+ $+ $+ $+ $+ > $@
k500: k50
	cat $+ $+ $+ $+ $+ $+ $+ $+ $+ $+ > $@
k50: k5
	cat $+ $+ $+ $+ $+ $+ $+ $+ $+ $+ > $@
k5:
	echo `seq 1001 2000` > $@
clean:
	-rm -f F1 F2 l1 l2
	-rm -f G1 M100 M50 M5 k500 k50 k5

Output is:

% make
cmp -lb F1 F2
 200000001 141 a    170 x
 200000002 142 b    171 y
 200000003 143 c    172 z
2500000005 141 a    170 x
2500000006 142 b    171 y
2500000007 143 c    172 z
diff F1 F2
40001c40001
< abc
---
> xyz
%

That is: cmp is correct but diff is missing the second set of differences.

After a look into the block_read() function in lib/cmpbuf.c in diffutils
source code (3.12), i come back with the simple.c program below:

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
  int fd;
  if ((fd = open(argv[1], O_RDONLY)) == -1) {
    perror("open failed");
    return(1);
  };
  struct stat s;
  if (fstat(fd, &s) == -1) {
    perror("stat failed");
    return(1);
  };
  size_t toread = s.st_size;
  size_t bufsize = toread;
  char *buf;
  if ((buf = malloc(bufsize)) == nullptr) {
    perror("malloc failed");
    return(1);
  };
  while (1) {
    size_t nread;
    if ((nread = read(fd, buf, toread)) > 0) {
      printf("read %zd bytes\n", nread);
      toread -= nread;
    } else {
      if (nread == -1) {
        perror("read failed");
      } else {
        printf("read() reached EOF\n");
      };
      break;
    };
  };
  close(fd);
  //sleep(1000);
  return(0);
};


When run on F1, the program displays:
% ./simple F1
read 205032712 bytes
read() reached EOF
%

and the last 2^32 bytes of the file have disappeared.

My analysis:
ssize_t read(int fildes, void *buf, size_t nbyte) seems to kill the
bits of nbyte that are over 32. Hence the premature EOF in this case.
Also malloc() seems strange because (with an appropriate sleep() near
the end) the Windows TaskManager shows 200Mb rather than 4Gb.

Any ideas?

I'm using cygwin1.dll from 3.7.0-0.579.g61b4866baf41.x86_64 as reported
by uname -a.

Regards,
Denis Excoffier.

















































































































-- 
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2026   by DJ Delorie     Updated Sep 20 2026