main(int ac, char *av[]) { char *path = ""; ncio *nciop; char linebuf[128]; off_t offset; size_t extent; void *vp; int status = ENOERR; int verbose = 0; int flags = 0; int create = 0; off_t igeto = 0; size_t igetsz = 0; size_t initialsz = 0; int doUnlink = 0; size_t sizehint = NC_SIZEHINT_DEFAULT; { extern int optind; extern int opterr; extern char *optarg; int ch; opterr = 1; while ((ch = getopt(ac, av, "vwcnLSUo:i:I:s:")) != EOF) switch (ch) { case 'v': verbose = 1; break; case 'w': flags |= NC_WRITE; break; case 'c': create = 1; break; case 'n': create = 1; flags |= NC_NOCLOBBER; break; case 'L': flags |= NC_LOCK; break; case 'S': flags |= NC_SHARE; break; case 'U': doUnlink = 1; break; case 'o': igeto = argscale(optarg, "igeto"); break; case 'i': igetsz = argscale(optarg, "igetsz"); break; case 'I': initialsz = argscale(optarg, "initialsz"); break; case 's': sizehint = argscale(optarg, "sizehint"); break; case '?': usage(av[0]); break; } /* last arg, the file name, is required */ if(ac - optind <= 0) usage(av[0]) ; path = av[optind]; } if(!create) { status = ncio_open(path, flags, igeto, igetsz, &sizehint, &nciop, &vp); if(status != ENOERR) { fprintf(stderr, "ncio_open: %s: %s\n", path, strerror(status)); return(EXIT_FAILURE); } } else { status = ncio_create(path, flags, initialsz, igeto, igetsz, &sizehint, &nciop, &vp); if(status != ENOERR) { fprintf(stderr, "ncio_create: %s: %s\n", path, strerror(status)); return(EXIT_FAILURE); } } while(fgets(linebuf, sizeof(linebuf), stdin) != NULL) { offset = 0; extent = 0; if(*linebuf == '#') continue; /* comment */ if(sscanf(linebuf, "rel 0x%lx", &offset) == 1 || sscanf(linebuf, "rel %ld", &offset) == 1) { if(verbose) printf("- rel %8ld\n", offset); if(!riu_pop(offset, 0)) continue; status = nciop->rel(nciop, offset, 0); if(status) { fprintf(stderr, "- rel error: %s\n", strerror(status)); continue; } } else if(sscanf(linebuf, "relm 0x%lx", &offset) == 1 || sscanf(linebuf, "relm %ld", &offset) == 1) { if(verbose) printf("- relm %8ld\n", offset); if(!riu_pop(offset, 1)) continue; status = nciop->rel(nciop, offset, RGN_MODIFIED); if(status) { fprintf(stderr, "- relm %8ld error: %s\n", offset, strerror(status)); continue; } } else if(sscanf(linebuf, "get 0x%lx %ld", &offset, &extent) == 2 || sscanf(linebuf, "get %ld %ld", &offset, &extent) == 2) { if(verbose) printf("- get %10ld %8ld\n", offset, extent); status = nciop->get(nciop, offset, extent, 0, &vp); if(status) { fprintf(stderr, "- get error: %s\n", strerror(status)); continue; } riu_push(offset, extent, vp); } else if(sscanf(linebuf, "getw 0x%lx %ld", &offset, &extent) == 2 || sscanf(linebuf, "getw %ld %ld", &offset, &extent) == 2) { if(verbose) printf("- getw %10ld %8ld\n", offset, extent); status = nciop->get(nciop, offset, extent, RGN_WRITE, &vp); if(status) { fprintf(stderr, "- getw error: %s\n", strerror(status)); continue; } riu_push(offset, extent, vp); } else if(strchr(linebuf, 'q') != NULL) break; else printf("???\n"); } status = ncio_close(nciop, doUnlink); if(status != ENOERR) { fprintf(stderr, "ncio_close(%s): %s: %s\n", doUnlink ? "doUnlink" : "", path, strerror(status)); return(EXIT_FAILURE); } return(EXIT_SUCCESS); }
int nc__create_mp(const char * path, int ioflags, size_t initialsz, int basepe, size_t *chunksizehintp, int *ncid_ptr) { NC *ncp; int status; void *xp = NULL; int sizeof_off_t = 0; #if ALWAYS_NC_SHARE /* DEBUG */ fSet(ioflags, NC_SHARE); #endif ncp = new_NC(chunksizehintp); if(ncp == NULL) return NC_ENOMEM; #if defined(LOCKNUMREC) /* && _CRAYMPP */ if (status = NC_init_pe(ncp, basepe)) { return status; } #else /* * !_CRAYMPP, only pe 0 is valid */ if(basepe != 0) return NC_EINVAL; #endif assert(ncp->flags == 0); /* Apply default create format. */ if (default_create_format == NC_FORMAT_64BIT) ioflags |= NC_64BIT_OFFSET; if (fIsSet(ioflags, NC_64BIT_OFFSET)) { fSet(ncp->flags, NC_64BIT_OFFSET); sizeof_off_t = 8; } else { sizeof_off_t = 4; } assert(ncp->xsz == ncx_len_NC(ncp,sizeof_off_t)); status = ncio_create(path, ioflags, initialsz, 0, ncp->xsz, &ncp->chunk, &ncp->nciop, &xp); if(status != NC_NOERR) { /* translate error status */ if(status == EEXIST) status = NC_EEXIST; goto unwind_alloc; } fSet(ncp->flags, NC_CREAT); if(fIsSet(ncp->nciop->ioflags, NC_SHARE)) { /* * NC_SHARE implies sync up the number of records as well. * (File format version one.) * Note that other header changes are not shared * automatically. Some sort of IPC (external to this package) * would be used to trigger a call to nc_sync(). */ fSet(ncp->flags, NC_NSYNC); } status = ncx_put_NC(ncp, &xp, sizeof_off_t, ncp->xsz); if(status != NC_NOERR) goto unwind_ioc; add_to_NCList(ncp); if(chunksizehintp != NULL) *chunksizehintp = ncp->chunk; *ncid_ptr = ncp->nciop->fd; return NC_NOERR; unwind_ioc: (void) ncio_close(ncp->nciop, 1); /* N.B.: unlink */ ncp->nciop = NULL; /*FALLTHRU*/ unwind_alloc: free_NC(ncp); return status; }