Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 2 Jul 2003 11:10:02 -0700
From:      "Justin C. Walker" <justin@mac.com>
To:        freebsd-hackers@freebsd.org
Subject:   Re: Can a pass-by-reference var be assigned to a local var?
Message-ID:  <66D4CD3C-ACB8-11D7-A6BB-00306544D642@mac.com>
In-Reply-To: <1560.216.120.158.65.1057168456.squirrel@www.mundomateo.com>

next in thread | previous in thread | raw e-mail | index | archive | help
I think you're missing something.

This is "structure assignment" in action.  The variable 'sbp' is a 
*statb, and the assignment assigns a structure, sb2, to *sbp, which is 
itself a structure, not a pointer.

Regards,

Justin

On Wednesday, July 2, 2003, at 10:54 AM, Matthew Hagerty wrote:

> Greetings,
>
> I was looking over the code for the tail command and found something 
> that
> seems wrong.  Below is the abbreviated code that highlights my concern.
> Basically, sb is defined in main() and passed to forward() (and 
> reverse())
> by reference.  Then in forward() sb2 is defined, and finally sb is set 
> to
> sb2.
>
> Two things come to mind that don't seem right: 1. What happens to sb 
> when
> forward() returns?  It was pointing to sb2 which was a local var of
> forward()...  and 2. The assignment *sbp = sb2 happens in a loop and 
> could
> be executed more than once, which seems to make the comparisons just 
> prior
> to the assignment usless 2 or more loop iterations.
>
> Am I missing something?  It seems to me that instead of *sbp = sb2, it
> should be done with memcpy() or something?  Any insight would be 
> greatly
> appreciated.
>
> Thanks,
> Matthew
>
>
> In tail.c:
>
> int
> main(argc, argv)
>   int argc;
>   char *argv[];
> {
>   struct stat sb;
> .
> .
>   if ((fp = fopen(fname, "r")) == NULL ||
>     fstat(fileno(fp), &sb)) {
> .
> .
>   if (rflag)
>     reverse(fp, style, off, &sb);
>   else
>     forward(fp, style, off, &sb);
> .
> .
> }
>
> Then in forward.c:
>
> void
> forward(fp, style, off, sbp)
>   FILE *fp;
>   enum STYLE style;
>   off_t off;
>   struct stat *sbp;
> {
>   int ch, n, kq = -1;
>   int action = USE_SLEEP;
>   struct kevent ev[2];
>   struct stat sb2;
>   struct timespec ts;
> .
> .
>     if (Fflag && fileno(fp) != STDIN_FILENO) {
>       while (stat(fname, &sb2) != 0)
>       /* file was rotated, wait until it reappears */
>         (void)sleep(1);
>       if (sb2.st_ino != sbp->st_ino ||
>         sb2.st_dev != sbp->st_dev ||
>         sb2.st_rdev != sbp->st_rdev ||
>         sb2.st_nlink == 0) {
>         fp = freopen(fname, "r", fp);
>         if (fp == NULL) {
>           ierr();
>           return;
>         } else {
>           *sbp = sb2;
>           action = ADD_EVENTS;
>         }
>       }
>     }
> .
> .
> }
>
> _______________________________________________
> freebsd-hackers@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to 
> "freebsd-hackers-unsubscribe@freebsd.org"
>
>
--
/~\ The ASCII           Justin C. Walker, Curmudgeon-at-Large
\ / Ribbon Campaign
  X  Help cure HTML Email
/ \



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?66D4CD3C-ACB8-11D7-A6BB-00306544D642>