int bu_file_delete(const char *path) { int fd = 0; int ret = 0; int retry = 0; struct stat sb; /* reject empty, special, or non-existent paths */ if (!path || BU_STR_EQUAL(path, "") || BU_STR_EQUAL(path, ".") || BU_STR_EQUAL(path, "..") || !bu_file_exists(path, &fd)) { return 0; } do { if (retry++) { /* second pass, try to force deletion by changing file * permissions (similar to rm -f). */ if (fstat(fd, &sb) == -1) { break; } bu_fchmod(fd, (sb.st_mode|S_IRWXU)); } ret = (remove(path) == 0) ? 0 : 1; } while (ret == 0 && retry < 2); close(fd); /* all boils down to whether the file still exists, not whether * remove thinks it succeeded. */ if (bu_file_exists(path, &fd)) { /* failure */ if (retry > 1) { /* restore original file permission */ bu_fchmod(fd, sb.st_mode); } close(fd); return 0; } else { /* deleted */ return 1; } }
int get_args(int argc, char **argv) { int c; while ((c = bu_getopt(argc, argv, "chiF:s:w:n:")) != -1) { switch (c) { case 'c': crunch = 1; break; case 'h': /* high-res */ screen_height = screen_width = 1024; break; case 'i': inverse = 1; break; case 'F': framebuffer = bu_optarg; break; case 's': /* square size */ screen_height = screen_width = atoi(bu_optarg); break; case 'w': screen_width = atoi(bu_optarg); break; case 'n': screen_height = atoi(bu_optarg); break; default: /* '?' */ return 0; } } if (bu_optind >= argc) { if (isatty(fileno(stdout))) return 0; file_name = "-"; outfp = stdout; } else { file_name = argv[bu_optind]; if ((outfp = fopen(file_name, "wb")) == NULL) { fprintf(stderr, "fb-pix: cannot open \"%s\" for writing\n", file_name); return 0; } (void)bu_fchmod(fileno(outfp), 0444); } if (argc > ++bu_optind) fprintf(stderr, "fb-pix: excess argument(s) ignored\n"); return 1; /* OK */ }
int ged_saveview(struct ged *gedp, int argc, const char *argv[]) { struct ged_display_list *gdlp; struct ged_display_list *next_gdlp; int i; FILE *fp; char *base; int c; char rtcmd[255] = {'r', 't', 0}; char outlog[255] = {0}; char outpix[255] = {0}; char inputg[255] = {0}; static const char *usage = "[-e] [-i] [-l] [-o] filename [args]"; GED_CHECK_DATABASE_OPEN(gedp, GED_ERROR); GED_CHECK_VIEW(gedp, GED_ERROR); GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR); /* initialize result */ bu_vls_trunc(gedp->ged_result_str, 0); /* must be wanting help */ if (argc == 1) { bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage); return GED_HELP; } bu_optind = 1; while ((c = bu_getopt(argc, (char * const *)argv, "e:i:l:o:")) != -1) { switch (c) { case 'e': snprintf(rtcmd, 255, "%s", bu_optarg); break; case 'l': snprintf(outlog, 255, "%s", bu_optarg); break; case 'o': snprintf(outpix, 255, "%s", bu_optarg); break; case 'i': snprintf(inputg, 255, "%s", bu_optarg); break; default: { bu_vls_printf(gedp->ged_result_str, "Option '%c' unknown\n", c); bu_vls_printf(gedp->ged_result_str, "help saveview"); return GED_ERROR; } } } argc -= bu_optind-1; argv += bu_optind-1; if (argc < 2) { bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage); return GED_ERROR; } if ((fp = fopen(argv[1], "a")) == NULL) { perror(argv[1]); return GED_ERROR; } (void)bu_fchmod(fileno(fp), 0755); /* executable */ if (!gedp->ged_wdbp->dbip->dbi_filename) { bu_log("Error: geometry file is not specified\n"); fclose(fp); return GED_ERROR; } if (!bu_file_exists(gedp->ged_wdbp->dbip->dbi_filename, NULL)) { bu_log("Error: %s does not exist\n", gedp->ged_wdbp->dbip->dbi_filename); fclose(fp); return GED_ERROR; } base = basename_without_suffix(argv[1], ".sh"); if (outpix[0] == '\0') { snprintf(outpix, 255, "%s.pix", base); } if (outlog[0] == '\0') { snprintf(outlog, 255, "%s.log", base); } /* Do not specify -v option to rt; batch jobs must print everything. -Mike */ fprintf(fp, "#!/bin/sh\n%s -M ", rtcmd); if (gedp->ged_gvp->gv_perspective > 0) fprintf(fp, "-p%g ", gedp->ged_gvp->gv_perspective); for (i = 2; i < argc; i++) fprintf(fp, "%s ", argv[i]); if (bu_strncmp(rtcmd, "nirt", 4) != 0) fprintf(fp, "\\\n -o %s\\\n $*\\\n", outpix); if (inputg[0] == '\0') { snprintf(inputg, 255, "%s", gedp->ged_wdbp->dbip->dbi_filename); } fprintf(fp, " '%s'\\\n ", inputg); gdlp = BU_LIST_NEXT(ged_display_list, gedp->ged_gdp->gd_headDisplay); while (BU_LIST_NOT_HEAD(gdlp, gedp->ged_gdp->gd_headDisplay)) { next_gdlp = BU_LIST_PNEXT(ged_display_list, gdlp); fprintf(fp, "'%s' ", bu_vls_addr(&gdlp->gdl_path)); gdlp = next_gdlp; } fprintf(fp, "\\\n 2>> %s\\\n", outlog); fprintf(fp, " <<EOF\n"); { vect_t eye_model; _ged_rt_set_eye_model(gedp, eye_model); _ged_rt_write(gedp, fp, eye_model); } fprintf(fp, "\nEOF\n"); (void)fclose(fp); return GED_OK; }