File type nomenclature in 170
Lassi Kortela 31 Jul 2020 11:10 UTC
SRFI 170 says:
#o140000 local domain socket
But this is for all sockets - not just local-domain ones but internet,
etc. This prints "socket":
----------------------------------------------------------------------
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <unistd.h>
#define PORT 23456
int main(void) {
struct sockaddr_in addr;
struct stat st;
int fd, is_socket;
if ((fd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
return 1;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
addr.sin_port = htons(PORT);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
return 1;
if (listen(fd, 10) == -1)
return 1;
if (fstat(fd, &st) == -1)
return 1;
is_socket = (st.st_mode & S_IFMT) == S_IFSOCK;
printf("%s\n", is_socket ? "socket" : "not a socket");
if (close(fd) == -1)
return 1;
return 0;
}
----------------------------------------------------------------------