static int create_server(int port, t_fd **fds) { int fd; t_sin sin; t_proto *p; if (!(p = getprotobyname(PROTONAME))) return (msg_error(EPROTONAME, 1)); if ((fd = socket(PF_INET, SOCK_STREAM, 0)) == -1) return (msg_error(ESOCKET, 1)); sin.sin_family = AF_INET; sin.sin_port = htons(port); sin.sin_addr.s_addr = INADDR_ANY; if (bind(fd, (struct sockaddr*)&sin, sizeof(sin)) == -1) return (msg_error(EBINDING, 1)); if (listen(fd, SOMAXCONN) == -1) return (msg_error(ELISTEN, 1)); if (push_fd(fds, fd, FD_SERVER, handle_server)) return (EXIT_FAILURE); return (EXIT_SUCCESS); }
void __cdecl output_and_cleanup(int code) { FILE *f; string line; if (code == 0) { /* output result if any */ if ((f = fopen(getval("STDOUT"), "r")) != NULL) { if (redirect_stdout >= 0) { int fds[3]; fds[0] = 0; fds[1] = redirect_stdout; fds[2] = 2; push_fd(fds); } while((line = read_line(f)) != NULL) { fputs(line, stdout); free (line); } fclose(f); if (redirect_stdout >= 0) { pop_fd(); redirect_stdout = -1; } } } if (test_file('d', tmpdir)) rec_rmdir(tmpdir); { int count = 0; /* Give more than one chance to remove the files */ while ((count++ < 20) && test_file('d', tmpdir)) { Sleep(250); rec_rmdir(tmpdir); } } }