File: fdset.c

package info (click to toggle)
ndtpd 3.1.2-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,524 kB
  • ctags: 690
  • sloc: ansic: 8,565; sh: 7,275; perl: 655; makefile: 175
file content (154 lines) | stat: -rw-r--r-- 2,710 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * Copyright (c) 2001  Takashi UMENO
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <sys/types.h>
#include <sys/socket.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

/*
 * FreeBSD uses bzero() in FD_ZERO().
 */
#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
#include <string.h>
#if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H)
#include <memory.h>
#endif /* not STDC_HEADERS and HAVE_MEMORY_H */
#else /* not STDC_HEADERS and not HAVE_STRING_H */
#include <strings.h>
#endif /* not STDC_HEADERS and not HAVE_STRING_H */

#ifdef TIME_WITH_SYS_TIME
#include <sys/time.h>
#include <time.h>
#else /* not TIME_WITH_SYS_TIME */
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#else /* not HAVE_SYS_TIME_H */
#include <time.h>
#endif /* not HAVE_SYS_TIME_H */
#endif /* not TIME_WITH_SYS_TIME */

#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#else
#include <sys/file.h>
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifndef SHUT_RD
#define SHUT_RD 0
#endif
#ifndef SHUT_WR
#define SHUT_WR 1
#endif
#ifndef SHUT_RDWR
#define SHUT_RDWR 2
#endif

#include "fdset.h"

/*
 * Initialize socket descriptors.
 */
void
initialize_fdset(fdset, max_fd)
    fd_set *fdset;
    int *max_fd;
{
    FD_ZERO(fdset);
    *max_fd = -1;
}

/*
 * Finalize socket descriptors.
 */
void
finalize_fdset(fdset, max_fd)
    fd_set *fdset;
    int *max_fd;
{
    shutdown_fdset(fdset, max_fd, SHUT_RDWR);
    close_fdset(fdset, max_fd);
}

/*
 * Add file to socket discritors.
 */
void
add_fd_to_fdset(fdset, max_fd, fd)
    fd_set *fdset;
    int *max_fd;
    int fd;
{
    FD_SET(fd, fdset);

    if (fd > *max_fd)
	*max_fd = fd;
}

/*
 * Shutdown multi sockets 
 */
int
shutdown_fdset(fdset, max_fd, how)
    fd_set *fdset;
    int *max_fd;
    int how;
{
    int i;
    
    for (i = 0; i <= *max_fd; i++) {
	if (FD_ISSET(i, fdset)) {
	    shutdown(i, how);
	    FD_CLR(i, fdset);
	}
    }
    return 0;
}

/*
 * Close multi sockets 
 */
int
close_fdset(fdset, max_fd)
    fd_set *fdset;
    int *max_fd;
{
    int i;

    for (i = 0; i <= *max_fd; i++) {
	if (FD_ISSET(i, fdset)) {
	    close(i);
	    FD_CLR(i, fdset);
	}
    }
    *max_fd = -1;
    return 0;
}