Exemple #1
0
int main(int argc, char **argv)
{
	size_t param[2] = { };
	size_t size, prev_size = 0, new_size = 0;
	char cmd;
	int fd, i, rc;

	if (argc < 3)
		goto usage;

	fd = open(argv[1], O_RDWR);
	if (fd < 0) {
		perror("open");
		return 1;
	}

	cmd = tolower(argv[2][0]);
	argc -= 3;
	argv += 3;

	for (i = 0; i < argc; i++) {
		char *endp;
		param[i] = strtoul(argv[i], &endp, 0);
		if (endp == argv[i] || *endp != '\0')
			goto usage;
	}

	switch (cmd) {
	case 's':
		if (!argc) {
			if (ioctl(fd, FIOC_GET_SIZE, &size)) {
				perror("ioctl");
				return 1;
			}
			printf("%zu\n", size);
		} else {
			size = param[0];
			if (ioctl(fd, FIOC_SET_SIZE, &size)) {
				perror("ioctl");
				return 1;
			}
		}
		return 0;

	case 'r':
	case 'w':
		rc = do_rw(fd, cmd == 'r', param[0], param[1],
			   &prev_size, &new_size);
		if (rc < 0)
			return 1;
		fprintf(stderr, "transferred %d bytes (%zu -> %zu)\n",
			rc, prev_size, new_size);
		return 0;
	}

 usage:
	fprintf(stderr, usage);
	return 1;
}
int main(int argc, char **argv)
{
	ssize_t size;
	int fd;

	if (argc < 5) {
		fprintf(stderr,
			"%s DEVICE SIZE ITERATIONS USE_MMAP\n", argv[0]);
		exit(EXIT_FAILURE);
	}
	filename = argv[1];
	size = strtol(argv[2], (char **)NULL, 10);
	iterations = strtol(argv[3], (char **)NULL, 10);
	use_mmap = strtol(argv[4], (char **)NULL, 10);

	setbuf(stdout, NULL);
	setbuf(stderr, NULL);

        if ((fd = open(filename, O_RDWR | O_SYNC)) < 0) {
		perror("open");
		exit(EXIT_FAILURE);
        }
	if (!size) {
		if (ioctl(fd, FCHAR_IOCGSIZE, &size) < 0) {
			perror("ioctl");
			exit(EXIT_FAILURE);
		}
	}
	dup2(fd, fileno(stdout));

	if (use_mmap)
		do_mmap(size);
	else
		do_rw(size);
	close(fd);

	return 0;
}