From owner-freebsd-hackers@FreeBSD.ORG Wed Jul 2 11:10:07 2003 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id ACF4F37B401 for ; Wed, 2 Jul 2003 11:10:07 -0700 (PDT) Received: from smtpout.mac.com (smtpout.mac.com [17.250.248.97]) by mx1.FreeBSD.org (Postfix) with ESMTP id 1CDF143FBD for ; Wed, 2 Jul 2003 11:10:07 -0700 (PDT) (envelope-from justin@mac.com) Received: from mac.com (smtpin08-en2 [10.13.10.153]) by smtpout.mac.com (Xserve/MantshX 2.0) with ESMTP id h62IA6ad025345 for ; Wed, 2 Jul 2003 11:10:06 -0700 (PDT) Received: from mac.com (12-210-49-211.client.attbi.com [12.210.49.211]) (authenticated bits=0) by mac.com (Xserve/smtpin08/MantshX 2.0) with ESMTP id h62IA40T012016 (version=TLSv1/SSLv3 cipher=DES-CBC3-SHA bits=168 verify=NO) for ; Wed, 2 Jul 2003 11:10:05 -0700 (PDT) Date: Wed, 2 Jul 2003 11:10:02 -0700 Content-Type: text/plain; charset=US-ASCII; format=flowed Mime-Version: 1.0 (Apple Message framework v552) From: "Justin C. Walker" To: freebsd-hackers@freebsd.org Content-Transfer-Encoding: 7bit In-Reply-To: <1560.216.120.158.65.1057168456.squirrel@www.mundomateo.com> Message-Id: <66D4CD3C-ACB8-11D7-A6BB-00306544D642@mac.com> X-Mailer: Apple Mail (2.552) Subject: Re: Can a pass-by-reference var be assigned to a local var? X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 02 Jul 2003 18:10:08 -0000 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 / \