#include #include #include #include #include #include #include void is_socket(int fd) { struct stat buf; memset(&buf, 0, sizeof(buf)); int status = fstat(fd, &buf); if (status < 0) { printf("fstat() failed with %d\n", errno); return; } if (S_ISSOCK(buf.st_mode)) printf("fd %d is a socket\n", fd); else printf("fd %d is not a socket\n", fd); } int main() { int status = 0; int fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { printf("socket() failed with %d\n", errno); return 1; } is_socket(fd); int fd2 = 13; status = dup2(fd, fd2); if (status < 0) { printf("dup2() failed with %d\n", errno); return 1; } is_socket(fd2); return 0; }