xref: /petsc/src/sys/classes/viewer/impls/socket/mex-scripts/sclose.c (revision ed0a315dc690bca58568ab127a9f322718ff62b7)
1b75c6efcSBarry Smith /*
2b75c6efcSBarry Smith 
3b75c6efcSBarry Smith         Written by Barry Smith, bsmith@mcs.anl.gov 4/14/92
4b75c6efcSBarry Smith         Updated by Richard Katz, katz@ldeo.columbia.edu 9/28/03
5b75c6efcSBarry Smith */
6b75c6efcSBarry Smith 
7b75c6efcSBarry Smith #include <petscsys.h>
8b75c6efcSBarry Smith #include <../src/sys/classes/viewer/impls/socket/socket.h>
9b75c6efcSBarry Smith 
10b75c6efcSBarry Smith #include <errno.h>
11b75c6efcSBarry Smith #include <ctype.h>
12b75c6efcSBarry Smith #if defined(PETSC_HAVE_MACHINE_ENDIAN_H)
13b75c6efcSBarry Smith   #include <machine/endian.h>
14b75c6efcSBarry Smith #endif
15b75c6efcSBarry Smith #if defined(PETSC_HAVE_UNISTD_H)
16b75c6efcSBarry Smith   #include <unistd.h>
17b75c6efcSBarry Smith #endif
18b75c6efcSBarry Smith #if defined(PETSC_HAVE_SYS_SOCKET_H)
19b75c6efcSBarry Smith   #include <sys/socket.h>
20b75c6efcSBarry Smith #endif
21b75c6efcSBarry Smith #if defined(PETSC_HAVE_SYS_WAIT_H)
22b75c6efcSBarry Smith   #include <sys/wait.h>
23b75c6efcSBarry Smith #endif
24b75c6efcSBarry Smith #if defined(PETSC_HAVE_NETINET_IN_H)
25b75c6efcSBarry Smith   #include <netinet/in.h>
26b75c6efcSBarry Smith #endif
27b75c6efcSBarry Smith #if defined(PETSC_HAVE_NETDB_H)
28b75c6efcSBarry Smith   #include <netdb.h>
29b75c6efcSBarry Smith #endif
30b75c6efcSBarry Smith #if defined(PETSC_HAVE_FCNTL_H)
31b75c6efcSBarry Smith   #include <fcntl.h>
32b75c6efcSBarry Smith #endif
33b75c6efcSBarry Smith #if defined(PETSC_HAVE_IO_H)
34b75c6efcSBarry Smith   #include <io.h>
35b75c6efcSBarry Smith #endif
36b75c6efcSBarry Smith 
37b75c6efcSBarry Smith #if defined(PETSC_NEED_CLOSE_PROTO)
38b75c6efcSBarry Smith PETSC_EXTERN int close(int);
39b75c6efcSBarry Smith #endif
40b75c6efcSBarry Smith 
41b75c6efcSBarry Smith #include <mex.h>
42b75c6efcSBarry Smith #define PETSC_MEX_ERROR(a) \
43b75c6efcSBarry Smith   { \
44b75c6efcSBarry Smith     mexErrMsgTxt(a); \
45b75c6efcSBarry Smith     return; \
46b75c6efcSBarry Smith   }
47b75c6efcSBarry Smith typedef struct {
48b75c6efcSBarry Smith   int onoff;
49b75c6efcSBarry Smith   int time;
50b75c6efcSBarry Smith } Linger;
51*4ee01570SBarry Smith 
mexFunction(int nlhs,mxArray * plhs[],int nrhs,const mxArray * prhs[])52b75c6efcSBarry Smith PETSC_EXTERN void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
53b75c6efcSBarry Smith {
54b75c6efcSBarry Smith   int    t = 0;
55b75c6efcSBarry Smith   Linger linger;
56b75c6efcSBarry Smith 
57b75c6efcSBarry Smith   linger.onoff = 1;
58b75c6efcSBarry Smith   linger.time  = 0;
59b75c6efcSBarry Smith 
60b75c6efcSBarry Smith   if (!nrhs) PETSC_MEX_ERROR("Needs one argument, the port");
61b75c6efcSBarry Smith   t = (int)*mxGetPr(prhs[0]);
62b75c6efcSBarry Smith 
63b75c6efcSBarry Smith   if (setsockopt(t, SOL_SOCKET, SO_LINGER, (char *)&linger, sizeof(Linger))) PETSC_MEX_ERROR("Setting linger");
64b75c6efcSBarry Smith   if (close(t)) PETSC_MEX_ERROR("closing socket");
65b75c6efcSBarry Smith   return;
66b75c6efcSBarry Smith }
67b75c6efcSBarry Smith 
main(int argc,char ** argv)68b75c6efcSBarry Smith int main(int argc, char **argv)
69b75c6efcSBarry Smith {
70b75c6efcSBarry Smith   return 0;
71b75c6efcSBarry Smith }
72