Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
socket_unix_connect.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
20
21#include <string.h>
22#ifdef __unix__
23#include <sys/socket.h>
24#include <sys/types.h>
25#include <sys/un.h>
26#include <unistd.h>
27#endif
28
30 /* Pops path; returns fd (>0) or 0 */
31 Value pathv = pop_value(vm);
32 int fd = 0;
33#ifdef __unix__
34 if (pathv.type != VAL_STRING) {
35 fprintf(stderr, "Runtime type error: unix_connect expects (string path)\n");
37 push_value(vm, make_int(0));
38 break;
39 }
41 if (path) {
42 int s = socket(AF_UNIX, SOCK_STREAM, 0);
43 if (s >= 0) {
44 struct sockaddr_un addr;
45 memset(&addr, 0, sizeof(addr));
46 addr.sun_family = AF_UNIX;
47 size_t maxlen = sizeof(addr.sun_path) - 1;
48 strncpy(addr.sun_path, path, maxlen);
49 addr.sun_path[maxlen] = '\0';
50 if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) == 0) {
51 fd = s;
52 } else {
53 close(s);
54 }
55 }
56 free(path);
57 }
58#endif
60 push_value(vm, make_int(fd > 0 ? fd : 0));
61 break;
62}
@ OP_SOCK_UNIX_CONNECT
Definition bytecode.h:227
free(vals)
uint32_t s
Definition rol.c:31
Value pathv
Definition serial_open.c:91
int fd
Definition serial_open.c:92
free_value(pathv)
push_value(vm, make_int(fd > 0 ? fd :0))
Tagged union representing a Fun value.
Definition value.h:68
char * value_to_string_alloc(const Value *v)
Allocate a printable C string for a Value.
Definition value.c:641
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
@ VAL_STRING
Definition value.h:53
#define fprintf
Definition vm.c:200
Value path
Definition write_file.c:33