Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 12 Aug 2001 12:32:56 +0100
From:      Ian Dowse <iedowse@maths.tcd.ie>
To:        Igor Shmukler <shmukler@mail.ru>
Cc:        freebsd-fs@freebsd.org
Subject:   Re: another FFS question 
Message-ID:   <200108121233.aa95748@salmon.maths.tcd.ie>
In-Reply-To: Your message of "Fri, 10 Aug 2001 18:31:22 EDT." <00bb01c121ec$307d48c0$7b02a8c0@tp600e> 

next in thread | previous in thread | raw e-mail | index | archive | help
In message <00bb01c121ec$307d48c0$7b02a8c0@tp600e>, Igor Shmukler writes:
>However dump of showed that 17th (starting count from 1st partition =
>sector) and following two sectors
>are 0x00 filled.

You obviously didn't compile this with compiler warnings turned on
(gcc -Wall) or you would have spotted the bugs yourself:

fs.c: In function `main':
fs.c:20: warning: assignment makes pointer from integer without a cast
fs.c:24: warning: implicit declaration of function `pread'
fs.c:29: warning: control reaches end of non-void function

`man open' will tell you that open() returns an int, not a FILE *.

`man pread' will tell you that `#include <unistd.h>' is required
to get the correct prototype for pread(). This is the omission that
actually stops the program from working as intended, because the
`offset' argument to pread() is a 64-bit type, but without a
prototype the compiler assumes an int (usually 32-bit).

The functions open(), pread() and malloc() are documented to return
values that indicate whether or not the operation was successful.
If you ignore these errors, the program just carries on blindly,
so any results may be meaningless - what happens if you accidentally
mistype the device path pased to open() for example? There are
useful functions such as perror() available to help print meaningful
error messages, e.g.:

	fd = open(path, O_RDONLY);
	if (fd < 0) {
		perror(path);
		exit(1);
	}

	sbbuf = malloc(SBSIZE);
	if (sbbuf == NULL) {
		perror("malloc");
		exit(1);
	}

	nread = pread(fd, sbbuf, SBSIZE, SBOFF);
	if (nread != SBSIZE) {
		if (nread < 0)
			perror("pread");
		else
			fprintf(stderr, "SBSIZE at SBOFF: short read\n");
		exit(1);
	}

See also `man err' for some handy but less portable error printing
functions.

Ian

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-fs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200108121233.aa95748>