[comp.sys.mac.programmer] simple C Problem

Eliot.Henry@samba.acs.unc.edu (BBS Account) (10/09/90)

 I have a routine I wrote in pascal that reads and writes blocks from
a floppy drive. The problem is that when I converted it to C it doesn't
work. Yes, strange but true. It gives an error -50 every so often then
consistantly has trouble reading the next 65 or 66 sectors (there is a loop
that is writing to the entire disk) This is probably an embaressingly simple
problem. Any help would be greatly appreciated here is the code I have:


#include <stdio.h>
#include "DiskDvr.h"
 
 
 
main ()
 
 
{
 
 
 
OSErr			error;
ParamBlockRec	Param;
int				i,j,lb;
long int		buffersize=512;
char 			thebuffer[512];
 
 
/* This program will format an Apple Serial Hard Drive (SC20) */
/* Initialize Some Variables */
 
 
Param.ioParam.ioPosOffset=512;
Param.ioParam.ioPosMode=fsFromStart;
Param.ioParam.ioPermssn=fsRdPerm;
Param.ioParam.ioReqCount=buffersize;
Param.ioParam.ioBuffer=thebuffer;
Param.ioParam.ioRefNum=-5;
Param.ioParam.ioVRefNum=1;
 
printf ("\n");
 
error=PBRead(&Param,FALSE);
printf ("Read Error= %d\n",error);
printf ("Bytes Requested %d\n",Param.ioParam.ioReqCount);
printf ("Bytes Read %d \n",Param.ioParam.ioActCount);
 
/* Load the array with 0 bits (ascii character zero) */
for (i=1; i<=512; i++) 
	thebuffer[i]=0;
	
/* Write the 0 bits to the Drive */
 
printf ("Writing 0s....\n");
for (i=1; i<=800; i++)
{
	Param.ioParam.ioPosOffset=i*512;
	error=PBWrite (&Param,FALSE);
	if (error!=noErr) 
		printf ("Write Error:  %d\n",error);
	else
		printf ("Wrote sector: %d\n",i);
}
printf ("/n");
 
 
/* Verify the 0 bits */
 
printf("Verifying 0s...\n");
for (i=1; i<=800; i++)
{
	Param.ioParam.ioPosOffset=i*512;
	error=PBRead(&Param,FALSE);
	if (error!=noErr) 
		printf ("Read Error verifying sector %d\n",i);
	else
	{
		for (j=1; j<=512; j++);
			if (thebuffer[j]!=0) 
				printf ("Error in byte %d of sector %d\n",j,i);
			else
				printf (".");
	}
}
printf ("\n");
 
 
} /* END of the program */

Thanks in advance!

--

sean_parent.snarkmail_l_z@gateway.qm.apple.com (Sean Parent) (10/10/90)

In article <1268@beguine.UUCP> Eliot.Henry@samba.acs.unc.edu (BBS Account) 
writes:
>  I have a routine I wrote in pascal that reads and writes blocks from
> a floppy drive. The problem is that when I converted it to C it doesn't
> work. Yes, strange but true. It gives an error -50 every so often then
> consistantly has trouble reading the next 65 or 66 sectors (there is a 
loop
> that is writing to the entire disk) This is probably an embaressingly 
simple
> problem. Any help would be greatly appreciated here is the code I have:
> 
In article <1268@beguine.UUCP> Eliot.Henry@samba.acs.unc.edu (BBS Account) 
writes:
> 
> char                    thebuffer[512];
>  
In article <1268@beguine.UUCP> Eliot.Henry@samba.acs.unc.edu (BBS Account) 
writes:
> 
> /* Load the array with 0 bits (ascii character zero) */
> for (i=1; i<=512; i++) 
>         thebuffer[i]=0;

This is a commen problem with pascal programers converting to C. thebuffer 
is an array containing 512 items but is indexed form 0 to 511. The for 
loop here will write off the end of the array.


Sean Parent
"Quality unattainable in a reasonable amount of time."

phils@chaos.cs.brandeis.edu (Phil Shapiro) (10/12/90)

In article <10630@goofy.Apple.COM> sean_parent.snarkmail_l_z@gateway.qm.apple.com (Sean Parent) writes:

   In article <1268@beguine.UUCP> Eliot.Henry@samba.acs.unc.edu (BBS Account) 
   writes:
   > 
   > char                    thebuffer[512];
   >  
   In article <1268@beguine.UUCP> Eliot.Henry@samba.acs.unc.edu (BBS Account) 
   writes:
   > 
   > /* Load the array with 0 bits (ascii character zero) */
   > for (i=1; i<=512; i++) 
   >         thebuffer[i]=0;

   This is a commen problem with pascal programers converting to C. thebuffer 
   is an array containing 512 items but is indexed form 0 to 511. The for 
   loop here will write off the end of the array.

True.  But there is another, more insidious bug:

In article <1268@beguine.UUCP> Eliot.Henry@samba.acs.unc.edu (BBS Account) 
writes:
int        i,j,lb;

In article <1268@beguine.UUCP> Eliot.Henry@samba.acs.unc.edu (BBS Account) 
writes:
for (i=1; i<=800; i++)
{
    Param.ioParam.ioPosOffset=i*512;

This will produce an integer overflow (in THINK C, at least).  Even
though the result of "i*512" is stored in a long integer, the
calculation is performed using short (16 bit) integer.  Either declare
"i" as a long, or use 512L.

	-phil shapiro, symantec tech support
--
Phil Shapiro
phils@chaos.cs.brandeis.edu