static void init(void) { /* Most file systems limit the size of filenames to 255 octets */ char *home_env, game_dir[256]; mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR; /* Get user HOME environment variable. Fail if we can't trust the * environment, like if the setuid bit is set on the executable. */ if ((home_env = secure_getenv("HOME")) != NULL) { strlcpy(game_dir, home_env, sizeof game_dir); } else { fprintf(stderr, "Environment variable $HOME does not exist, " "or it is not what you think it is.\n"); exit(EXIT_FAILURE); } char *dirs[] = { "/.local", "/share", "/tetris", NULL, }; int i; for (i = 0; dirs[i]; i++) { strlcat(game_dir, dirs[i], sizeof game_dir); if (try_mkdir(game_dir, mode) < 0) goto err_subdir; } /* open for writing in append mode ~/.local/share/tetris/logs */ strlcat(game_dir, "/logs", sizeof game_dir); if (freopen(game_dir, "a", stderr) == NULL) { fprintf(stderr, "Could not open: %s\n", game_dir); exit(EXIT_FAILURE); } srand(time(NULL)); /* Create game context */ if (blocks_init() > 0) { printf("Game successfully initialized\n"); printf("Appending logs to file: %s.\n", game_dir); } else { printf("Failed to initialize game\n"); exit(EXIT_FAILURE); } /* create ncurses display */ screen_init(); return; err_subdir: fprintf(stderr, "Unable to create sub directories. Cannot continue\n"); exit(EXIT_FAILURE); }
int get_xsockets (int *number, struct x_socket **sockets, int tcp_socket) { int dpy; struct x_socket *s; int n; int i; s = malloc (sizeof(*s) * 5); if (s == NULL) errx (1, "malloc: out of memory"); try_mkdir (X_UNIX_PATH); try_mkdir (X_PIPE_PATH); for(dpy = 4; dpy < 256; ++dpy) { char **path; int tmp = 0; n = 0; for (path = x_sockets; *path; ++path) { tmp = try_socket (&s[n], dpy, *path); if (tmp == -1) { if (errno != ENOTDIR && errno != ENOENT) err(1, "failed to open '%s'", *path); } else if (tmp == 1) { while(--n >= 0) { close (s[n].fd); free (s[n].pathname); } break; } else if (tmp == 0) ++n; } if (tmp == 1) continue; #ifdef MAY_HAVE_X11_PIPES for (path = x_pipes; *path; ++path) { tmp = try_pipe (&s[n], dpy, *path); if (tmp == -1) { if (errno != ENOTDIR && errno != ENOENT && errno != ENOSYS) err(1, "failed to open '%s'", *path); } else if (tmp == 1) { while (--n >= 0) { close (s[n].fd); free (s[n].pathname); } break; } else if (tmp == 0) ++n; } if (tmp == 1) continue; #endif if (tcp_socket) { tmp = try_tcp (&s[n], dpy); if (tmp == -1) err(1, "failed to open tcp stocket"); else if (tmp == 1) { while (--n >= 0) { close (s[n].fd); free (s[n].pathname); } break; } else if (tmp == 0) ++n; } break; } if (dpy == 256) errx (1, "no free x-servers"); for (i = 0; i < n; ++i) if (s[i].flags & LISTENP && listen (s[i].fd, SOMAXCONN) < 0) err (1, "listen %s", s[i].pathname ? s[i].pathname : "tcp"); *number = n; *sockets = s; return dpy; }