🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.msdos.programmer
Article #1517

Re: BC++2.0 lseek problem(ALSO:lseek() question)

#1517
From: Mark Alexander
Date: Tue, 15 Sep 1992 18:16
48 lines
2134 bytes
In article <1992Sep12.235241.23213@eng.ufl.edu> zzang@stat.ufl.edu (Zhuo Zang) writes:

> Hello, the following program using lseek will give different results
> in BC++2.0 than that of in UNIX.  the BC++2.0 will print the last
> character twice.

I tried this program with BCC++ for OS/2, BC++ for MS-DOS, and GCC on a
unix box.  All produced the same result.  This assumes that the first line
of the file foo.c is the following:

/* use bcc -DMSDOS foo.c to compile in BC++i

The program seeks to file offset 3, then reads 20 bytes from the file
into a buffer.  These 20 bytes, which the program prints, should be
the following:

use bcc -DMSDOS foo.

There are several dangers and harmless mistakes in this program, though.

First, the buffer is declared as follows:

>char buf[BUFSIZ];

The assumption here is that the buffer is greater than 20 bytes long.
If BUFSIZ is <= 20, bad things could happen.  BUFSIZ is usually 512,
but you can't count on it.

Secondly, the attempt to zero-terminate the string didn't work; it
just converted '\0' to a (char *), and assigned it to sp:

>  sp = '\0';

This should have been:

   buf[20] = '\0';

or

   sp[20] = '\0';

since sp was set to point to buf[0].

However, the program worked without this, because buf[] was declared
as a global variable, and hence was initially zero-filled when the
program was started.  If you had used the buffer again, or had declared
the buffer as a local variable, the program would have failed, because
the buffer would probably not be zero-filled.
Technical Details
Message-ID: <1992Sep15.181611.27033@genghis.borland.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!haven.umd.edu!darwin.sura.net!spool.mu.edu!umn.edu!csus.edu!borland.com!alexande
References: <1992Sep12.235241.23213@eng.ufl.edu>

Show Full Headers
Complete Article Headers
Newsgroups: comp.os.msdos.programmer,comp.lang.c
Path: arc1-ca.pugleaf.net!sparky!uunet!haven.umd.edu!darwin.sura.net!spool.mu.edu!umn.edu!csus.edu!borland.com!alexande
From: alexande@genghis.borland.com (Mark Alexander)
Subject: Re: BC++2.0 lseek problem(ALSO:lseek() question)
Message-ID: <1992Sep15.181611.27033@genghis.borland.com>
Originator: alexande@genghis.borland.com
Sender: news@borland.com (News Admin)
Organization: Borland International
References: <1992Sep12.235241.23213@eng.ufl.edu>
Distribution: usa
Date: Tue, 15 Sep 1992 18:16:11 GMT
Lines: 48