File: Conn.c

package info (click to toggle)
pconsole 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 636 kB
  • sloc: ansic: 920; sh: 305; makefile: 64
file content (127 lines) | stat: -rw-r--r-- 2,582 bytes parent folder | download | duplicates (2)
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
/*
    pconsole WJ101
    Copyright (C) 2001  Walter de Jong <walter@heiho.net>

    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 of the License, 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.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
/*
	Conn.c	WJ101
*/

#include "config.h"
#include "Conn.h"
#include "cstring.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

Conn *AllConns = NULL;

Conn *new_Conn(void) {
Conn *c;

	if ((c = (Conn *)malloc(sizeof(Conn))) == NULL)
		return NULL;
	memset(c, 0, sizeof(Conn));
	return c;
}

void destroy_Conn(Conn *c) {
	if (c == NULL)
		return;

	if (c->fd > 0)
		close(c->fd);
	if (c->dev != NULL)
		free(c->dev);
	if (c->hostname != NULL)
		free(c->hostname);
	free(c);
}

Conn *prepend_Conn(Conn *c) { return (Conn *)prepend_List(&AllConns, c); }
Conn *add_Conn(Conn *c) { return (Conn *)add_List(&AllConns, c); }
void remove_Conn(Conn *c) { remove_List(&AllConns, c); }

Conn *find_Conn_by_fd(int fd) {
Conn *c;

	for(c = AllConns; c != NULL; c = c->next)
		if (c->fd == fd)
			return c;
	return NULL;
}

Conn *find_Conn_by_dev(char *name) {
Conn *c;

	if (name == NULL)
		return NULL;

	for(c = AllConns; c != NULL; c = c->next)
		if (c->dev != NULL && !strcmp(c->dev, name))
			return c;
	return NULL;
}

Conn *find_Conn_by_hostname(char *name) {
Conn *c;

	if (name == NULL)
		return NULL;

	for(c = AllConns; c != NULL; c = c->next)
		if (c->hostname != NULL && !strcmp(c->hostname, name))
			return c;
	return NULL;
}

#ifdef HAVE_ST_RDEV
Conn *find_Conn_by_rdev(dev_t rdev) {
Conn *c;

	for(c = AllConns; c != NULL; c = c->next)
		if (c->rdev == rdev)
			return c;
	return NULL;
}
#endif

int write_Conn(Conn *c, void *buf, size_t len) {
ssize_t n;

	if (c == NULL) {
		/* ignore (not a hard error) */
		return 0;
	}
	if (c->fd <= 0) {
		/* ignore (not a hard error) */
		return 0;
	}

	while (len > 0) {
		n = write(c->fd, buf, len);
		if (n == -1) {
			/* write() error (silent) */
			return -1;
		}
		len -= n;
		buf += n;
	}
	return 0;
}

/* EOB */