Subject: Re: lseek() continued! DOH!
To: None <netbsd-help@NetBSD.ORG>
From: None <tooleym@douglas.bc.ca>
List: netbsd-help
Date: 02/04/1998 05:00:19
On Wed, 4 Feb 1998, Dave Huang wrote:

> Date: Wed, 4 Feb 1998 06:32:18 -0600 (CST)
> From: Dave Huang <khym@bga.com>
> To: tooleym@Douglas.BC.CA
> Subject: Re: lseek() continued! DOH!
> 
> On Wed, 4 Feb 1998 tooleym@douglas.bc.ca wrote:
> > I'm probably doing something wrong right? I'm sitting here tearing my hair
> > out over it! :)  Please show me something that will make me feel
> > sheepish.. 
> 
> It still sounds like you're treating the offset parameter and return
> value of lseek() as 32-bit values... they're 64-bit values. When you
> printf lseek's return value, you have to use a %qd, not %ld or %d. And
> make sure you #include <unistd.h>
> 
> Try compiling your program with -Wall and get rid of any warnings that
> show up... If you still can't get it to work, it'd help if you'd show
> some code :)

Okay here's some code--  -Wall doesn't help a whole lot. :)

    pos = 1L;
    if ((datafile=open("player.records",O_RDWR))>=0) {

    status=lseek(datafile, (off_t)(-(sizeof(struct record))), SEEK_END);
    printf("lseeked to %d\n", status);
    while(pos != -1L) {
        status=lseek(datafile, (off_t)(-(sizeof(struct record)*pos)), \
SEEK_END);
        if (status < 0) break;
        if ((status=read (datafile, &myrec, sizeof (struct record)))<0) {
          pos = -1L;
        } else {
          printf("status: %d\n", status);
          printf("name field:%s\n", myrec.name);
          if (!strncmp(myrec.name,who,strlen(who))) {
                printf("found an entry with name: %s\n",
myrec.name);
                printf("fiddling with entry..\n");
		strcpy(myrec.status, "clean");
               status=lseek(datafile,(off_t)(-( (sizeof(struct record)) *
pos),SEEK_END));
               write (datafile, &myrec, sizeof (record));
               pos = -1L;
          } else { pos += 1L; printf("pos:%d\n", pos);}
        }
     }
     close(f);



***********
God dat's messy and ugly..  but I just whipped it it up this morning.. :)
Also I cleaned it up for your viewing displeasure, so if there are
marginal errors, chalk it up to typos.

So--gist: Goes to end of file (most recent addition). If it finds
something it likes, it changes the name, rewrites it back to the same
place it found it. Sounds good.

Thanks again,

Marc Tooley