/** * process commandline arguments */ void get_args ( int argc, char **argv ) { const char *short_opts = "hp:vd:1:2:3:z"; const struct option long_opts[] = { { "help", no_argument, NULL, 'h' }, { "port", required_argument, NULL, 'p' }, { "verbose", no_argument, NULL, 'v' }, { "device", required_argument, NULL, 'd' }, { "button-one", required_argument, NULL, '1' }, { "button-two", required_argument, NULL, '2' }, { "button-three", required_argument, NULL, '3' }, { "daemon", no_argument, NULL, 'z' }, { NULL, 0, NULL, 0 } }; int c; while ( ( c = getopt_long( argc, argv, short_opts, long_opts, NULL )) != -1 ) { switch (c) { case 'h': usage(); exit(0); break; case 'p': sub_name = optarg; break; case 'v': verbose = 1; break; case 'd': device = optarg; break; case '1': parse_map( 0, optarg ); break; case '2': parse_map( 1, optarg ); break; case '3': parse_map( 2, optarg ); break; case 'z': daemonize = 1; break; } } }
map_t *init_map(char *fp) { map_t *map = malloc(sizeof(map_t)); /* Read raw map */ char **raw_map = read_map(fp, &(map->height), &(map->width)); if(raw_map != NULL) { map->block = malloc(sizeof(block_t*)*map->height); map->redraw_block = NULL; map->animate_unit = NULL; map->unit = NULL; map->player = NULL; map->num_players = 0; map->num_units = 0; map->camera = 0; map->block = malloc(sizeof(block_t*)*map->height); int i; int j; for(i = 0; i < map->height; i++) { map->block[i] = malloc(sizeof(block_t)*map->width); for(j = 0; j < map->width; j++) { map->block[i][j].status = UNASSIGNED; } } parse_map(map, raw_map); free(raw_map); return map; } else { free(map); return NULL; } }
t_env *init_data(char *file) { int fd; int error; t_env *env; error = 0; fd = open(file, O_RDONLY); if (alloc_data(&env)) error = ft_error_retint("Cannot allocate memory for env struct!\n", 1); if (fd == -1) error = ft_error_retint("File not found\n", 1); if (!error) { if (!(error = parse_header(env, fd))) ft_printf("xSize: %u, ySize: %u, blockSize: %u\n", env->map_x + 1, env->map_y + 1, env->block_size); env->player->dir_x = DIR_X; env->player->dir_y = DIR_Y; env->player->speed_move = SPEED_MOVE; env->player->speed_rotate = SPEED_ROTATE; env->camera->plane_x = CAM_X; env->camera->plane_y = CAM_Y; error = error ? error : parse_map(env, fd); } close(fd); return (error ? NULL : env); }
void select_level(void) { t_uint i; t_lmap *map; int x; x = 0; i = 0; if (g_arka.map_count == 0) return ; map = g_arka.map_list; while (i != g_arka.menu.selection) { i++; map = map->next; } g_arka.current_map = map; if (g_arka.map) x = 1; free_map(g_arka.map); g_arka.map = parse_map(map->path); recreate(); g_arka.players[0]->life = 3; g_arka.players[0]->score = 0; resume_game(); }
static void parse_game( pr_state_t* parser ) { match_tag( parser, "game" ); pr_node_t* node = (pr_node_t*)malloc( sizeof( pr_node_t ) ); if ( node == NULL ) longjmp( parser->env, PR_OUT_OF_MEMORY ); node->count = 0; parser->node = node; *parser->prev = node; parser->prev = &node->next; parse_map( parser, 0, 0 ); }
int main(int argc, char *argv[]) { uint8_t *dat = NULL; uint32_t sz, ocnt, area; int alt, idx = 0, i, type = 0; const quest_dat_hdr_t *ptrs[2][18] = { { 0 } }; const quest_dat_hdr_t *hdr; /* Parse the command line... */ parse_command_line(argc, argv); /* See if we got a .qst file a .dat file. */ type = is_qst(filename, version); if(type == 1) { dat = read_qst(filename, &sz, version); } else if(!type) { dat = read_dat(filename, &sz, compressed); } if(!dat) { printf("Confused by earlier errors, bailing out.\n"); return -1; } parse_quest_objects(dat, sz, &ocnt, ptrs); printf("Found %d objects\n", (int)ocnt); for(i = 0; i < 18; ++i) { if((hdr = ptrs[1][i])) { /* XXXX: Ugly! */ sz = LE32(hdr->size); area = LE32(hdr->area); alt = 0; if((episode == 3 && area > 5) || (episode == 2 && area > 15)) alt = 1; if(parse_map((map_enemy_t *)(hdr->data), sz / sizeof(map_enemy_t), episode, alt, &idx, (int)area)) { printf("Cannot parse map!\n"); return -4; } } } return 0; }
int parse(char *av, t_env *world) { int i; i = 0; if (parse_dim(av, world) != 0) return (-1); world->map = malloc(sizeof(int *) * (MYSIZE + 1)); while (i <= MYSIZE) { world->map[i] = malloc(sizeof(int) * (MXSIZE + 1)); i++; } if (world->map == NULL) return (-1); parse_map(av, world); return (0); }
static void parse_map( pr_state_t* parser, int skip, int isrom ) { pr_node_t dummy; pr_node_t* node; unsigned hash; const char* key; unsigned keylen; if ( skip ) { node = &dummy; dummy.count = 0; } else node = parser->node; match( parser, LX_LPAREN ); while ( parser->lexer.token != LX_RPAREN ) { if ( parser->lexer.token != LX_TAG ) longjmp( parser->env, PR_UNEXPECTED_TOKEN ); key = parser->lexer.start; keylen = parser->lexer.len; hash = djb2( key, keylen ); match_any( parser ); switch ( hash ) { case 0x0b88a693U: /* rom */ parse_map( parser, skip, 1 ); break; default: parse_value( parser, key, keylen, node, isrom ); break; } } match_any( parser ); }
int run_epikong() { t_map map; char *path; if ((path = init_screen()) == NULL) return (EXIT_FAILURE); if (start_music() == EXIT_FAILURE) return (EXIT_FAILURE); if (start_parsing(path, &map) == EXIT_FAILURE) return (EXIT_FAILURE); if (get_monster(&map) == EXIT_FAILURE) return (EXIT_FAILURE); if (get_hero(&map) == EXIT_FAILURE) return (EXIT_FAILURE); if (parse_map(&map) == EXIT_FAILURE) return (EXIT_FAILURE); return (EXIT_SUCCESS); }
int loadImage(char *imageName, int flags) { FILE *in; char *dot; char map_name[100]; if((in=fopen(imageName,"rb"))!=NULL) { size_t got = fread( mem, 1, 32768, in ); if (ferror(in)) { perror("Error while reading the rom image"); return -1; } fclose(in); if (flags&DMAP) { /* Try to find the map file */ areas = NULL; sorted_globals = NULL; if ((dot=strrchr( imageName, '.'))!=NULL) { *dot = '\0'; strcpy( map_name, imageName ); strcat( map_name, ".map"); if (!parse_map( map_name, &areas )) { sort_globals( &sorted_globals, areas ); } *dot = '.'; } if (sorted_globals==NULL) { printf("Warning: no map file loaded.\n"); } } return 0; } else { printf("Couldn't load image %s.\n", imageName); return -1; } }
int main(int argc, char *argv[]) { int c; unsigned long flags = CLONE_NEWUSER | CLONE_NEWNS; char ttyname0[256], ttyname1[256], ttyname2[256]; int status; int ret; int pid; char *default_args[] = {"/bin/sh", NULL}; char buf[1]; int pipe1[2], // child tells parent it has unshared pipe2[2]; // parent tells child it is mapped and may proceed memset(ttyname0, '\0', sizeof(ttyname0)); memset(ttyname1, '\0', sizeof(ttyname1)); memset(ttyname2, '\0', sizeof(ttyname2)); if (isatty(0)) { ret = readlink("/proc/self/fd/0", ttyname0, sizeof(ttyname0)); if (ret < 0) { perror("unable to open stdin."); exit(1); } ret = readlink("/proc/self/fd/1", ttyname1, sizeof(ttyname1)); if (ret < 0) { printf("Warning: unable to open stdout, continuing."); memset(ttyname1, '\0', sizeof(ttyname1)); } ret = readlink("/proc/self/fd/2", ttyname2, sizeof(ttyname2)); if (ret < 0) { printf("Warning: unable to open stderr, continuing."); memset(ttyname2, '\0', sizeof(ttyname2)); } } lxc_list_init(&active_map); while ((c = getopt(argc, argv, "m:h")) != EOF) { switch (c) { case 'm': if (parse_map(optarg)) usage(argv[0]); break; case 'h': default: usage(argv[0]); } }; if (lxc_list_empty(&active_map)) { if (find_default_map()) { fprintf(stderr, "You have no allocated subuids or subgids\n"); exit(1); } } argv = &argv[optind]; argc = argc - optind; if (argc < 1) { argv = default_args; argc = 1; } if (pipe(pipe1) < 0 || pipe(pipe2) < 0) { perror("pipe"); exit(1); } if ((pid = fork()) == 0) { // Child. close(pipe1[0]); close(pipe2[1]); opentty(ttyname0, 0); opentty(ttyname1, 1); opentty(ttyname2, 2); ret = unshare(flags); if (ret < 0) { perror("unshare"); return 1; } buf[0] = '1'; if (write(pipe1[1], buf, 1) < 1) { perror("write pipe"); exit(1); } if (read(pipe2[0], buf, 1) < 1) { perror("read pipe"); exit(1); } if (buf[0] != '1') { fprintf(stderr, "parent had an error, child exiting\n"); exit(1); } close(pipe1[1]); close(pipe2[0]); return do_child((void*)argv); } close(pipe1[1]); close(pipe2[0]); if (read(pipe1[0], buf, 1) < 1) { perror("read pipe"); exit(1); } buf[0] = '1'; if (lxc_map_ids(&active_map, pid)) { fprintf(stderr, "error mapping child\n"); ret = 0; } if (write(pipe2[1], buf, 1) < 0) { perror("write to pipe"); exit(1); } if ((ret = waitpid(pid, &status, __WALL)) < 0) { printf("waitpid() returns %d, errno %d\n", ret, errno); exit(1); } exit(WEXITSTATUS(status)); }
int main(int argc, char *argv[]) { int c, pid, ret, status; char buf[1]; int pipe_fds1[2], /* child tells parent it has unshared */ pipe_fds2[2]; /* parent tells child it is mapped and may proceed */ unsigned long flags = CLONE_NEWUSER | CLONE_NEWNS; char ttyname0[256] = {0}, ttyname1[256] = {0}, ttyname2[256] = {0}; char *default_args[] = {"/bin/sh", NULL}; lxc_log_fd = STDERR_FILENO; if (isatty(STDIN_FILENO)) { ret = readlink("/proc/self/fd/0", ttyname0, sizeof(ttyname0)); if (ret < 0) { CMD_SYSERROR("Failed to open stdin"); _exit(EXIT_FAILURE); } ret = readlink("/proc/self/fd/1", ttyname1, sizeof(ttyname1)); if (ret < 0) { CMD_SYSINFO("Failed to open stdout. Continuing"); ttyname1[0] = '\0'; } ret = readlink("/proc/self/fd/2", ttyname2, sizeof(ttyname2)); if (ret < 0) { CMD_SYSINFO("Failed to open stderr. Continuing"); ttyname2[0] = '\0'; } } lxc_list_init(&active_map); while ((c = getopt(argc, argv, "m:h")) != EOF) { switch (c) { case 'm': ret = parse_map(optarg); if (ret < 0) { usage(argv[0]); _exit(EXIT_FAILURE); } break; case 'h': usage(argv[0]); _exit(EXIT_SUCCESS); default: usage(argv[0]); _exit(EXIT_FAILURE); } }; if (lxc_list_empty(&active_map)) { ret = find_default_map(); if (ret < 0) { fprintf(stderr, "Failed to find subuid or subgid allocation\n"); _exit(EXIT_FAILURE); } } argv = &argv[optind]; argc = argc - optind; if (argc < 1) argv = default_args; ret = pipe2(pipe_fds1, O_CLOEXEC); if (ret < 0) { CMD_SYSERROR("Failed to open new pipe"); _exit(EXIT_FAILURE); } ret = pipe2(pipe_fds2, O_CLOEXEC); if (ret < 0) { CMD_SYSERROR("Failed to open new pipe"); close(pipe_fds1[0]); close(pipe_fds1[1]); _exit(EXIT_FAILURE); } pid = fork(); if (pid < 0) { close(pipe_fds1[0]); close(pipe_fds1[1]); close(pipe_fds2[0]); close(pipe_fds2[1]); _exit(EXIT_FAILURE); } if (pid == 0) { close(pipe_fds1[0]); close(pipe_fds2[1]); opentty(ttyname0, STDIN_FILENO); opentty(ttyname1, STDOUT_FILENO); opentty(ttyname2, STDERR_FILENO); ret = unshare(flags); if (ret < 0) { CMD_SYSERROR("Failed to unshare mount and user namespace"); close(pipe_fds1[1]); close(pipe_fds2[0]); _exit(EXIT_FAILURE); } buf[0] = '1'; ret = lxc_write_nointr(pipe_fds1[1], buf, 1); if (ret != 1) { CMD_SYSERROR("Failed to write to pipe file descriptor %d", pipe_fds1[1]); close(pipe_fds1[1]); close(pipe_fds2[0]); _exit(EXIT_FAILURE); } ret = lxc_read_nointr(pipe_fds2[0], buf, 1); if (ret != 1) { CMD_SYSERROR("Failed to read from pipe file descriptor %d", pipe_fds2[0]); close(pipe_fds1[1]); close(pipe_fds2[0]); _exit(EXIT_FAILURE); } close(pipe_fds1[1]); close(pipe_fds2[0]); if (buf[0] != '1') { fprintf(stderr, "Received unexpected value from parent process\n"); _exit(EXIT_FAILURE); } ret = do_child((void *)argv); if (ret < 0) _exit(EXIT_FAILURE); _exit(EXIT_SUCCESS); } close(pipe_fds1[1]); close(pipe_fds2[0]); ret = lxc_read_nointr(pipe_fds1[0], buf, 1); if (ret <= 0) CMD_SYSERROR("Failed to read from pipe file descriptor %d", pipe_fds1[0]); buf[0] = '1'; ret = lxc_map_ids(&active_map, pid); if (ret < 0) fprintf(stderr, "Failed to write id mapping for child process\n"); ret = lxc_write_nointr(pipe_fds2[1], buf, 1); if (ret < 0) { CMD_SYSERROR("Failed to write to pipe file descriptor %d", pipe_fds2[1]); _exit(EXIT_FAILURE); } ret = waitpid(pid, &status, __WALL); if (ret < 0) { CMD_SYSERROR("Failed to wait on child process"); _exit(EXIT_FAILURE); } _exit(WEXITSTATUS(status)); }
int main(int argc,char** argv) { double value; bool odom_laser_flag=false; string meas,meas_id; ifstream log_file; // input file stream for log file istringstream log_stream; // stream to parse log values mat laser_meas = zeros<mat>(n_laser_meas,1); // vector for laser measure mat odom_meas = zeros<mat>(n_odom_meas,1); // vector for odom measure mat map = parse_map(map_file_name); // map in matrix form mat particles = initialize_particles(map); // particles as (x,y,p) mat previous_pose = zeros<mat>(3,1); log_file.open(log_file_name.c_str()); if(!log_file.is_open()){ cout<<"log file not open\n"; return 1; } // cout << particles << endl; for(int j=0;j<=log_length ;j++) { getline(log_file,meas); log_stream.str(meas + " "); log_stream >> meas_id; if(odom_laser_flag == false){ if(meas_id == "O"){ odom_laser_flag = true; for(int i=0;i<n_odom_meas;i++){ log_stream >> value; odom_meas(i,0) = value; } if(j==1){ previous_pose(0,0) = odom_meas(0,0); previous_pose(1,0) = odom_meas(1,0); previous_pose(2,0) = odom_meas(2,0); } particles=motion_model(particles,odom_meas,previous_pose); previous_pose(0,0) = odom_meas(0,0); previous_pose(1,0) = odom_meas(1,0); previous_pose(2,0) = odom_meas(2,0); } } if(odom_laser_flag == true) { if(meas_id == "L"){ odom_laser_flag = false; for(int i=0;i<n_laser_meas;i++){ log_stream >> value; laser_meas(i,0) = value; } odom_meas(0,0) = laser_meas(0,0); odom_meas(1,0) = laser_meas(1,0); odom_meas(2,0) = laser_meas(2,0); particles=motion_model(particles,odom_meas,previous_pose); particles=sensor_model(map,particles,laser_meas,j); previous_pose(0,0) = laser_meas(0,0); previous_pose(1,0) = laser_meas(1,0); previous_pose(2,0) = laser_meas(2,0); particles = resample(particles,j); } to_cvmat(map,particles,j); }
static void parse_clrmamepro( pr_state_t* parser ) { match_tag( parser, "clrmamepro" ); parse_map( parser, 1, 0 ); }
void parse_args(int argc, char *argv[]) { int r; static struct option longOptions[] = { {"receive-cmd", required_argument, 0, 'v'}, {"send-cmd", required_argument, 0, 's'}, {"imap", required_argument, 0, 'I' }, {"omap", required_argument, 0, 'O' }, {"emap", required_argument, 0, 'E' }, {"escape", required_argument, 0, 'e'}, {"echo", no_argument, 0, 'c'}, {"noinit", no_argument, 0, 'i'}, {"noreset", no_argument, 0, 'r'}, {"nolock", no_argument, 0, 'l'}, {"flow", required_argument, 0, 'f'}, {"baud", required_argument, 0, 'b'}, {"parity", required_argument, 0, 'y'}, {"databits", required_argument, 0, 'd'}, {"stopbits", required_argument, 0, 'p'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; r = 0; while (1) { int optionIndex = 0; int c; int map; /* no default error messages printed. */ opterr = 0; c = getopt_long(argc, argv, "hirlcv:s:r:e:f:b:y:d:p:", longOptions, &optionIndex); if (c < 0) break; switch (c) { case 's': strncpy(opts.send_cmd, optarg, sizeof(opts.send_cmd)); opts.send_cmd[sizeof(opts.send_cmd) - 1] = '\0'; break; case 'v': strncpy(opts.receive_cmd, optarg, sizeof(opts.receive_cmd)); opts.receive_cmd[sizeof(opts.receive_cmd) - 1] = '\0'; break; case 'I': map = parse_map(optarg); if (map >= 0) opts.imap = map; else { fprintf(stderr, "Invalid --imap\n"); r = -1; } break; case 'O': map = parse_map(optarg); if (map >= 0) opts.omap = map; else { fprintf(stderr, "Invalid --omap\n"); r = -1; } break; case 'E': map = parse_map(optarg); if (map >= 0) opts.emap = map; else { fprintf(stderr, "Invalid --emap\n"); r = -1; } break; case 'c': opts.lecho = 1; break; case 'i': opts.noinit = 1; break; case 'r': opts.noreset = 1; break; case 'l': #if defined (UUCP_LOCK_DIR) || defined (USE_FLOCK) opts.nolock = 1; #endif break; case 'e': opts.escape = CKEY(optarg[0]); break; case 'f': switch (optarg[0]) { case 'X': case 'x': opts.flow = FC_XONXOFF; break; case 'H': case 'h': opts.flow = FC_RTSCTS; break; case 'N': case 'n': opts.flow = FC_NONE; break; default: fprintf(stderr, "Invalid --flow: %c\n", optarg[0]); r = -1; break; } break; case 'b': opts.baud = atoi(optarg); if ( opts.baud == 0 || ! term_baud_ok(opts.baud) ) { fprintf(stderr, "Invalid --baud: %d\n", opts.baud); r = -1; } break; case 'y': switch (optarg[0]) { case 'e': opts.parity = P_EVEN; break; case 'o': opts.parity = P_ODD; break; case 'n': opts.parity = P_NONE; break; default: fprintf(stderr, "Invalid --parity: %c\n", optarg[0]); r = -1; break; } break; case 'd': switch (optarg[0]) { case '5': opts.databits = 5; break; case '6': opts.databits = 6; break; case '7': opts.databits = 7; break; case '8': opts.databits = 8; break; default: fprintf(stderr, "Invalid --databits: %c\n", optarg[0]); r = -1; break; } break; case 'p': switch (optarg[0]) { case '1': opts.stopbits = 1; break; case '2': opts.stopbits = 2; break; default: fprintf(stderr, "Invalid --stopbits: %c\n", optarg[0]); r = -1; break; } break; case 'h': show_usage(argv[0]); exit(EXIT_SUCCESS); case '?': default: fprintf(stderr, "Unrecognized option(s)\n"); r = -1; break; } if ( r < 0 ) { fprintf(stderr, "Run with '--help'.\n"); exit(EXIT_FAILURE); } } /* while */ if ( (argc - optind) < 1) { fprintf(stderr, "No port given\n"); fprintf(stderr, "Run with '--help'.\n"); exit(EXIT_FAILURE); } strncpy(opts.port, argv[optind], sizeof(opts.port) - 1); opts.port[sizeof(opts.port) - 1] = '\0'; #ifndef NO_HELP printf("picocom v%s\n", VERSION_STR); printf("\n"); printf("port is : %s\n", opts.port); printf("flowcontrol : %s\n", flow_str[opts.flow]); printf("baudrate is : %d\n", opts.baud); printf("parity is : %s\n", parity_str[opts.parity]); printf("databits are : %d\n", opts.databits); printf("stopbits are : %d\n", opts.stopbits); printf("escape is : C-%c\n", KEYC(opts.escape)); printf("local echo is : %s\n", opts.lecho ? "yes" : "no"); printf("noinit is : %s\n", opts.noinit ? "yes" : "no"); printf("noreset is : %s\n", opts.noreset ? "yes" : "no"); #if defined (UUCP_LOCK_DIR) || defined (USE_FLOCK) printf("nolock is : %s\n", opts.nolock ? "yes" : "no"); #endif printf("send_cmd is : %s\n", (opts.send_cmd[0] == '\0') ? "disabled" : opts.send_cmd); printf("receive_cmd is : %s\n", (opts.receive_cmd[0] == '\0') ? "disabled" : opts.receive_cmd); printf("imap is : "); print_map(opts.imap); printf("omap is : "); print_map(opts.omap); printf("emap is : "); print_map(opts.emap); printf("\n"); #endif /* of NO_HELP */ }
int main(int argc, char *argv[]) { struct stat sb; char dbname[SMTPD_MAXPATHLEN]; char *opts; char *conf; int ch; DBTYPE dbtype = DB_HASH; char *p; mode_t omode; log_init(1); mode = strcmp(__progname, "newaliases") ? P_MAKEMAP : P_NEWALIASES; conf = CONF_FILE; type = T_PLAIN; opts = "ho:t:d:"; if (mode == P_NEWALIASES) opts = "f:h"; while ((ch = getopt(argc, argv, opts)) != -1) { switch (ch) { case 'd': if (strcmp(optarg, "hash") == 0) dbtype = DB_HASH; else if (strcmp(optarg, "btree") == 0) dbtype = DB_BTREE; else if (strcmp(optarg, "dbm") == 0) dbtype = DB_RECNO; else errx(1, "unsupported DB type '%s'", optarg); break; case 'f': conf = optarg; break; case 'o': oflag = optarg; break; case 't': if (strcmp(optarg, "aliases") == 0) type = T_ALIASES; else if (strcmp(optarg, "set") == 0) type = T_SET; else errx(1, "unsupported type '%s'", optarg); break; default: usage(); } } argc -= optind; argv += optind; /* sendmail-compat makemap ... re-execute using proper interface */ if (argc == 2) { if (oflag) usage(); p = strstr(argv[1], ".db"); if (p == NULL || strcmp(p, ".db") != 0) { if (! bsnprintf(dbname, sizeof dbname, "%s.db", argv[1])) errx(1, "database name too long"); } else { if (strlcpy(dbname, argv[1], sizeof dbname) >= sizeof dbname) errx(1, "database name too long"); } execlp("makemap", "makemap", "-d", argv[0], "-o", dbname, "-", NULL); err(1, "execlp"); } if (mode == P_NEWALIASES) { if (geteuid()) errx(1, "need root privileges"); if (argc != 0) usage(); type = T_ALIASES; source = conf_aliases(conf); } else { if (argc != 1) usage(); source = argv[0]; } if (oflag == NULL && asprintf(&oflag, "%s.db", source) == -1) err(1, "asprintf"); if (strcmp(source, "-") != 0) if (stat(source, &sb) == -1) err(1, "stat: %s", source); if (! bsnprintf(dbname, sizeof(dbname), "%s.XXXXXXXXXXX", oflag)) errx(1, "path too long"); omode = umask(7077); if (mkstemp(dbname) == -1) err(1, "mkstemp"); umask(omode); db = dbopen(dbname, O_EXLOCK|O_RDWR|O_SYNC, 0644, dbtype, NULL); if (db == NULL) { warn("dbopen: %s", dbname); goto bad; } if (strcmp(source, "-") != 0) if (fchmod(db->fd(db), sb.st_mode) == -1 || fchown(db->fd(db), sb.st_uid, sb.st_gid) == -1) { warn("couldn't carry ownership and perms to %s", dbname); goto bad; } if (! parse_map(source)) goto bad; if (db->close(db) == -1) { warn("dbclose: %s", dbname); goto bad; } if (rename(dbname, oflag) == -1) { warn("rename"); goto bad; } if (mode == P_NEWALIASES) printf("%s: %d aliases\n", source, dbputs); else if (dbputs == 0) warnx("warning: empty map created: %s", oflag); return 0; bad: unlink(dbname); return 1; }
/* Functions */ int main(int argc, char *argv[] ) { FILE *rules, *map; /* Pointers to rules/map file streams */ Grid board; /* Store info about the game board */ Ship ships[MAX_SHIPS]; /* Store info about each ship */ int **answer; /* Store the solution of the game */ unsigned int numShips = 0; /* Store the total numberof ships */ unsigned int xGuess, yGuess;/* Store the player's current guess */ /* Temp Variables */ int status = 0; /* exit status */ int i = 0, j = 0; /* loop indeces */ /* There must be at least three parameters */ if(argc < 3) { return params_missing(); } /* Save rules file into "rules", it's OK if "standard.rules" DNE * Rules file must be readable unless it is standard.rules */ if((rules = fopen(argv[1], "r")) == NULL) { if(!strcmp(argv[1], "standard.rules")) { fclose(rules); /* Looking for "standard.rules" but file DNE so create it * with the default contents */ rules = fopen(argv[1], "w"); fprintf(rules, "8 8\n5\n5\n4\n3\n2\n1\n\n"); fclose(rules); rules = fopen(argv[1], "r"); } else { return rules_missing(); } fclose(rules); } /* Save map file into "map" * Map file must be readable */ if((map = fopen(argv[2], "r")) == NULL ) { fclose(map); return maps_missing(); } /* Save and check rules file * Rules must conform to specification */ if((status = parse_rules(rules, ships, &board, &numShips))) { return status; } /* Initialize the answer array */ answer = (int**)malloc(board.width * sizeof(int*)); for(i = 0; i < board.width; i++) { answer[i] = (int*)malloc(board.height * sizeof(int)); for(j = 0; j < board.height; j++) { answer[i][j] = 1; } } /* Save and check map file * Map must conform to specification */ if((status = parse_map(map, ships, &board, &numShips))) { return status; } /* Populate the solution */ if((status = place_ships(ships, numShips, answer))) { return status; } /* Interaction Loop */ for(;;) { display_board(board.width, board.height, answer); display_prompt(); switch(get_prompt(&xGuess, &yGuess)) { case 0: make_guess(&xGuess, &yGuess, &board, answer); break; case 1: printf("Bad guess\n"); break; case -1: return bad_guess(); } } return 0; }