Add pclose, 'fix' GC of files closed where fclose fails

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Add pclose, 'fix' GC of files closed where fclose fails

Michael Panetta

The attached patch adds a function io.pclose() and file:pclose() to the liolib which returns the return code of the called program in io.popen().  This is to facilitate scripting of external programs where you need to know if they worked or not based on the exit code passed to the shell.  

This patch also modifies pushresult to allow passing arbitrary return values back to LUA.   The way I modified it, 'should' not break anything (all numbers including 0 are resolved as 'true' and nil/false are false) but someone more knowledgeable may wish to confirm this.

It also fixes what I perceve as a bug in the io_pclose() and io_fclose() functions where the file pointer is only set to NULL if the close operation succeeds.  According to the man pages for fclose(), after fclose() has been called on a FILE *, no operation must ever again be performed on that FILE *, or undefined behaviour will result.  I believe setting the FILE * to NULL is suppsed to prevent any further garbage collection in the __gc() metamethod, so it still needs to be done even if fclose() fails, else fclose() will get called on the same FILE * twice, with an unknown result.

Please look at this patch and tell me if I have made any grave errors, as this is my first attempt at patching one of the core libraries of LUA, and the intent is to add functionality and not change it (if possible).

Thanks
Mike Panetta
<a href="cid:bHVhLTUuMS1wY2xvc2UtcHVzaHJlc3VsdC5wYXRjaA==@wegener.com">lua-5.1-pclose-pushresult.patch

lua-5.1-pclose-pushresult.patch (5K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Add pclose, 'fix' GC of files closed where fclose fails

David Given
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Michael Panetta wrote:
[...]
> Please look at this patch and tell me if I have made any grave errors, as
> this is my first attempt at patching one of the core libraries of LUA, and
> the intent is to add functionality and not change it (if possible).

You should be aware that it's not strictly legal to call fclose() on a
FILE* that was previously opened with popen(), or pclose() on one that
was opened with fopen(). It happens to work on a lot of platforms, but
only by chance.

- --
+- David Given --McQ-+ "Preacher, don't the Bible have some pretty
|  [hidden email]    | specific things to say about killing?" "Quite
| ([hidden email]) | specific. It is, however, somewhat fuzzier on the
+- www.cowlark.com --+ subject of kneecaps." --- Firefly, _War Stories_
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEV3Ilf9E0noFvlzgRAhrRAKCcpSrg6kIx2tXbQvs/9kDvuaMvjQCfZaxA
CEGWNzG/2Muf4Nb6zTBzpak=
=yYO+
-----END PGP SIGNATURE-----
Reply | Threaded
Open this post in threaded view
|

Re: Add pclose, 'fix' GC of files closed where fclose fails

Michael Panetta
Yeah I know.  I called pclose explicitly in my pclose.   I believe the check function will do the right think if its a standard file *.

Thanks,
Mike Panetta

Lua list <[hidden email]> on Tuesday, May 02, 2006 at 10:52 AM +0000 wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Michael Panetta wrote:
[...]
> Please look at this patch and tell me if I have made any grave errors, as
> this is my first attempt at patching one of the core libraries of LUA, and
> the intent is to add functionality and not change it (if possible).

You should be aware that it's not strictly legal to call fclose() on a
FILE* that was previously opened with popen(), or pclose() on one that
was opened with fopen(). It happens to work on a lot of platforms, but
only by chance.

- --
+- David Given --McQ-+ "Preacher, don't the Bible have some pretty
|  [hidden email]    | specific things to say about killing?" "Quite
| ([hidden email]) | specific. It is, however, somewhat fuzzier on the
+- www.cowlark.com --+ subject of kneecaps." --- Firefly, _War Stories_
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEV3Ilf9E0noFvlzgRAhrRAKCcpSrg6kIx2tXbQvs/9kDvuaMvjQCfZaxA
CEGWNzG/2Muf4Nb6zTBzpak=
=yYO+
-----END PGP SIGNATURE-----

Reply | Threaded
Open this post in threaded view
|

Re: Add pclose, 'fix' GC of files closed where fclose fails

Michael Panetta
Fixed the patch to make pclose actually return the correct value.  Previously it wasnt using the WEXITSTATUS macro so it was returning the return value of the program * 256.

<a href="cid:bHVhLTUuMS1wY2xvc2UtcHVzaHJlc3VsdC5wYXRjaA==@wegener.com">lua-5.1-pclose-pushresult.patch

Mike Panetta


lua-5.1-pclose-pushresult.patch (5K) Download Attachment