summaryrefslogtreecommitdiff
path: root/do/socket
blob: 08daa88d0c511d9aec931261322338474bebc104 (plain)
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
#ifdef HAS_SOCKET
int
do_socket(stab, arglast)
STAB *stab;
int *arglast;
{
    register STR **st = stack->ary_array;
    register int sp = arglast[1];
    register STIO *stio;
    int domain, type, protocol, fd;

    if (!stab) {
	errno = EBADF;
	return FALSE;
    }

    stio = stab_io(stab);
    if (!stio)
	stio = stab_io(stab) = stio_new();
    else if (stio->ifp)
	do_close(stab,FALSE);

    domain = (int)str_gnum(st[++sp]);
    type = (int)str_gnum(st[++sp]);
    protocol = (int)str_gnum(st[++sp]);
    TAINT_PROPER("socket");
    fd = socket(domain,type,protocol);
    if (fd < 0)
	return FALSE;
    stio->ifp = fdopen(fd, "r");	/* stdio gets confused about sockets */
    stio->ofp = fdopen(fd, "w");
    stio->type = 's';
    if (!stio->ifp || !stio->ofp) {
	if (stio->ifp) fclose(stio->ifp);
	if (stio->ofp) fclose(stio->ofp);
	if (!stio->ifp && !stio->ofp) close(fd);
	return FALSE;
    }

    return TRUE;
}