On Thu, Nov 10, 2011 at 11:10:34PM -0600, Abel Abraham Camarillo Ojeda wrote: > On Thu, Nov 10, 2011 at 9:26 PM, Michael W. Bombardieri <m...@ii.net> wrote: > > Hi tech, > > > > I have a diff for the patch(1) tool which copies > > one included in NetBSD two years ago... > > > > Source: NetBSD src/usr.bin/patch/inp.c revision 1.23 > > Purpose: Don't bother mmap'ing an empty file > > > > Comments/OK? > > > > - Michael > > > > > > Index: inp.c > > =================================================================== > > RCS file: /usr/src/cvs/src/usr.bin/patch/inp.c,v > > retrieving revision 1.35 > > diff -u -r1.35 inp.c > > --- inp.c ?? ?? ?? 27 Oct 2009 23:59:41 -0000 ?? ?? ??1.35 > > +++ inp.c ?? ?? ?? 11 Nov 2011 03:12:10 -0000 > > @@ -243,12 +243,16 @@ > > ?? ?? ?? ??if ((ifd = open(filename, O_RDONLY)) < 0) > > ?? ?? ?? ?? ?? ?? ?? ??pfatal("can't open file %s", filename); > > > > - ?? ?? ?? i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0); > > - ?? ?? ?? if (i_womp == MAP_FAILED) { > > - ?? ?? ?? ?? ?? ?? ?? perror("mmap failed"); > > + ?? ?? ?? if (i_size == 0) > > ?? ?? ?? ?? ?? ?? ?? ??i_womp = NULL; > > - ?? ?? ?? ?? ?? ?? ?? close(ifd); > > - ?? ?? ?? ?? ?? ?? ?? return false; > > + ?? ?? ?? else { > > + ?? ?? ?? ?? ?? ?? ?? i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, > > ifd, 0); > > + ?? ?? ?? ?? ?? ?? ?? if (i_womp == MAP_FAILED) { > > + ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? perror("mmap failed"); > > + ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? i_womp = NULL; > > + ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? close(ifd); > > + ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? return false; > > + ?? ?? ?? ?? ?? ?? ?? } > > ?? ?? ?? ??} > > > > ?? ?? ?? ??close(ifd); > > > > > > if "if (i_size == 0) {...}" returns, there's still need for an else block?... > > Just asking...
Thanks for your comment. >From what I can see, plan_a() should return true in any case when we get an empty file, so it should be fairly safe to return directly here as you mentioned. I have tested the following diff on i386; it appears to produce the same behaviour as my previous diff. If this works for you, I'm happy to go with the simpler diff rather than blindly following the NetBSD code. - Michael Index: inp.c =================================================================== RCS file: /usr/src/cvs/src/usr.bin/patch/inp.c,v retrieving revision 1.35 diff -u -r1.35 inp.c --- inp.c 27 Oct 2009 23:59:41 -0000 1.35 +++ inp.c 11 Nov 2011 05:27:41 -0000 @@ -243,6 +243,8 @@ if ((ifd = open(filename, O_RDONLY)) < 0) pfatal("can't open file %s", filename); + if (i_size == 0) + return true; i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0); if (i_womp == MAP_FAILED) { perror("mmap failed");