Imported Upstream version 3.3.3
[debian/amanda] / perl / amglue / filehandles.swg
1 /*
2  * Copyright (c) 2008-2012 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17  *
18  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
20  */
21
22 %typemap(in) int fd {
23     IO *io = NULL;
24     PerlIO *pio = NULL;
25     int fd = -1;
26
27     if (SvIOK($input)) {
28         /* plain old integer */
29         $1 = SvIV($input);
30     } else {
31         /* try extracting as filehandle */
32         
33         /* note: sv_2io may call die() */
34         io = sv_2io($input);
35         if (io) {
36             pio = IoIFP(io);
37         }
38         if (pio) {
39             fd = PerlIO_fileno(pio);
40         }
41         if (fd >= 0) {
42             $1 = fd;
43         } else {
44             SWIG_exception(SWIG_TypeError, "Expected integer file descriptor "
45                 "or file handle for argument $argnum");
46         }
47     }
48 }