/* * Main code */ static int seqchksum(opts_t* opts) { int retcode = 0; BAMit_t *bam_in = NULL; bam1_t *rec = NULL; /* * Open input BAM file */ bam_in = BAMit_open(opts->input_name, 'r', opts->input_fmt, 0, NULL); if (!bam_in) return 1; // Initialise results structure chksum_results_t *results = chksum_init_results(opts->hash); // Read and process each record in the input BAM while ( (rec = BAMit_next(bam_in)) ) { // ignore secondary and supplementary records if (!(rec->core.flag & (BAM_FSECONDARY | BAM_FSUPPLEMENTARY))) { if (seqchksum_processRecord(rec, opts->hash, results)) { retcode = 1; break; } } } hFILE *f = hdopen(fileno(stdout),"w"); if (!f) die("Can't open stdout"); chksum_print_results(f, results); if (hclose(f)) die("Can't close stdout"); // tidy up after us BAMit_free(bam_in); chksum_free_results(results); return retcode; }
static hFILE *hopen_fd_stdinout(const char *mode) { int fd = (strchr(mode, 'r') != NULL)? STDIN_FILENO : STDOUT_FILENO; #if defined HAVE_SETMODE && defined O_BINARY if (setmode(fd, O_BINARY) < 0) return NULL; #endif return hdopen(fd, mode); }
htsFile * sam_popen(char *cmd) { htsFile *fp = (htsFile*)calloc(1, sizeof(htsFile)); int fid, fid2; assert(fp); popen_fd = popen(cmd, "r"); //Global fid = fileno(popen_fd); fid2 = dup(fid); //otherwise, the file descriptor is closed by zlib and pclose() won't work!! if(popen_fd == NULL) return 0; if(fp == NULL) return 0; hFILE *hfile = hdopen(fid2, "r"); //does this exist? if(hfile == NULL) return 0; fp->is_be = ed_is_big(); BGZF *gzfp = bgzf_hopen(hfile, "r"); fp->fp.voidp = ks_init(gzfp); fp->format.format = sam; return(fp); }
static hFILE *hopen_fd_stdinout(const char *mode) { int fd = (strchr(mode, 'r') != NULL)? STDIN_FILENO : STDOUT_FILENO; // TODO Set binary mode (for Windows) return hdopen(fd, mode); }
static htsFile *dup_stdout(const char *mode) { int fd = dup(STDOUT_FILENO); hFILE *hfp = (fd >= 0)? hdopen(fd, mode) : NULL; return hfp? hts_hopen(hfp, "-", mode) : NULL; }
/* * Find all disks through /sys/block. */ static char *list_disks(DIR* blk, unsigned int* flags) { struct dirent *d; while ((d = readdir(blk))) { (*flags) = 0; if (d->d_name[1] == 'd' && (d->d_name[0] == 'h' || d->d_name[0] == 's')) { char buf[NAME_MAX+1], lnk[NAME_MAX+1], *ptr; FILE *fp; int ret; fp = hdopen(SYS_BLK "/%s/removable", d->d_name); if (0 == (long)fp || -1 == (long)fp) { if (-1 == (long)fp) goto empty; /* error */ continue; /* no entry `removable' */ } ret = getc(fp); fclose(fp); if (ret != '0') (*flags) |= DISK_REMOVABLE; if (d->d_name[0] == 'h') { if ((*flags) & DISK_REMOVABLE) continue; /* not a hard disk */ (*flags) |= DISK_IS_IDE; if ((ret = flush_cache_ext(d->d_name))) { if (ret < 0) goto empty; (*flags) |= DISK_EXTFLUSH; } break; /* old IDE disk not managed by kernel, out here */ } ret = snprintf(buf, sizeof(buf), SYS_BLK "/%s/device", d->d_name); if ((ret >= (int)sizeof(buf)) || (ret < 0)) goto empty; /* error */ ret = readlink(buf, lnk, sizeof(lnk)); if (ret >= (int)sizeof(lnk)) goto empty; /* error */ if (ret < 0) { if (errno != ENOENT) goto empty; /* error */ continue; /* no entry `device' */ } lnk[ret] = '\0'; ptr = basename(lnk); if (!ptr || !*ptr) continue; /* should not happen */ fp = hdopen(SYS_CLASS "/%s/manage_start_stop", ptr); if (0 == (long)fp || -1 == (long)fp) { if (-1 == (long)fp) goto empty; /* error */ } else { ret = getc(fp); fclose(fp); if (ret != '0') { (*flags) |= DISK_MANAGED; continue; } } fp = hdopen(SYS_BLK "/%s/device/vendor", d->d_name); if (0 == (long)fp || -1 == (long)fp) { if (-1 == (long)fp) goto empty; /* error */ continue; /* no entry `device/vendor' */ } ptr = fgets(buf, sizeof(buf), fp); fclose(fp); if (ptr == (char*)0) continue; /* should not happen */ ptr = strstrip(buf); if (*ptr == '\0') continue; /* should not happen */ if (strncmp(buf, "ATA", sizeof(buf)) == 0) { if ((*flags) & DISK_REMOVABLE) continue; /* not a hard disk */ (*flags) |= (DISK_IS_IDE|DISK_IS_SATA); if ((ret = flush_cache_ext(d->d_name))) { if (ret < 0) goto empty; (*flags) |= DISK_EXTFLUSH; } break; /* new SATA disk to shutdown, out here */ } if (((*flags) & DISK_REMOVABLE) == 0) continue; /* Seems to be a real SCSI disk */ if ((ret = flush_cache_ext(d->d_name))) { if (ret < 0) goto empty; (*flags) |= DISK_EXTFLUSH; } break; /* Removable disk like USB stick to shutdown */ } } if (d == (struct dirent*)0) goto empty; return d->d_name; empty: return (char*)0; }
/* * Check IDE/(S)ATA hard disk identity for * the FLUSH CACHE EXT bit set. */ static int flush_cache_ext(const char *device) { #ifndef WIN_IDENTIFY #define WIN_IDENTIFY 0xEC #endif unsigned char args[4+IDBYTES]; unsigned short *id = (unsigned short*)(&args[4]); char buf[NAME_MAX+1], *ptr; int fd = -1, ret = 0; FILE *fp; fp = hdopen(SYS_BLK "/%s/size", device); if (0 == (long)fp || -1 == (long)fp) { if (-1 == (long)fp) return -1; /* error */ goto out; /* no entry `size' */ } ptr = fgets(buf, sizeof(buf), fp); fclose(fp); if (ptr == (char*)0) goto out; /* should not happen */ ptr = strstrip(buf); if (*ptr == '\0') goto out; /* should not happen */ if ((size_t)atoll(buf) < (1<<28)) goto out; /* small disk */ ret = snprintf(buf, sizeof(buf), DEV_BASE "/%s", device); if ((ret >= (int)sizeof(buf)) || (ret < 0)) return -1; /* error */ if ((fd = open(buf, O_RDONLY|O_NONBLOCK)) < 0) goto out; memset(&args[0], 0, sizeof(args)); args[0] = WIN_IDENTIFY; args[3] = 1; if (ioctl(fd, HDIO_DRIVE_CMD, &args)) goto out; #ifdef WORDS_BIGENDIAN # if 0 { const unsigned short *end = id + IDBYTES/2; const unsigned short *from = id; unsigned short *to = id; while (from < end) *to++ = bswap_16(*from++); } # else id[83] = bswap_16(id[83]); # endif #endif if ((id[83] & MASK_EXT) == TEST_EXT) ret = 1; out: if (fd >= 0) close(fd); return ret; }