off_t gt_file_size(const char *file) { struct stat sb; int fd; gt_assert(file); fd = gt_xopen(file, O_RDONLY, 0); gt_xfstat(fd, &sb); gt_xclose(fd); return sb.st_size; }
int gt_mmapandread(int argc, const char **argv, GtError *err) { int i, fd, parsed_args; void *map; struct stat sb; unsigned long long j; unsigned int byte = 0; gt_error_check(err); /* option parsing */ switch (parse_options(&parsed_args, argc, argv, err)) { case GT_OPTION_PARSER_OK: break; case GT_OPTION_PARSER_ERROR: return -1; case GT_OPTION_PARSER_REQUESTS_EXIT: return 0; } /* iterate over all files */ for (i = parsed_args; i < argc; i++) { /* open file */ fd = gt_xopen(argv[i], O_RDONLY, 0); /* get file statistics */ gt_xfstat(fd, &sb); if (sb.st_size == 0) printf("file \"%s\" is empty\n", argv[i]); else if (!(sb.st_mode & S_IFREG)) printf("\"%s\" is not a regular file\n", argv[i]); else { /* map file */ map = gt_xmmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); /* read file */ printf("reading file \"%s\"\n", argv[i]); j = 0; gt_progressbar_start(&j, (unsigned long long) sb.st_size); for (; j < (unsigned long long) sb.st_size; j++) byte |= (unsigned int) ((char*) map)[j]; gt_progressbar_stop(); /* unmap file */ gt_xmunmap(map, sb.st_size); } /* close file */ gt_xclose(fd); } if (!byte) printf("all read files contained only null characters\n"); return 0; }
static bool file_exists_and_is_regular_executable(const char *path) { bool is_exec = false; struct stat sb; FILE *file; if ((file = fopen(path, "r")) == NULL) return false; gt_xfstat(fileno(file), &sb); if (S_ISREG(sb.st_mode) && (sb.st_mode & S_IXUSR #ifndef _WIN32 || sb.st_mode & S_IXGRP || sb.st_mode & S_IXOTH #endif ) ) { is_exec = true; } gt_xfclose(file); return is_exec; }
off_t gt_file_estimate_size(const char *file) { off_t size; struct stat sb; GtFileMode gfm; int fd; gt_assert(file); fd = gt_xopen(file, O_RDONLY, 0); gt_xfstat(fd, &sb); gfm = gt_file_mode_determine(file); if (gfm == GT_FILE_MODE_UNCOMPRESSED) size = sb.st_size; else size = sb.st_size * 4; /* expected compression rate for sequence is 0.25 */ gt_xclose(fd); return size; }