示例#1
0
// read the IVT from eMMC
int read_block(char *ivt){
	int ret = 0;
	int d = open(MMC_DEVICE, O_RDONLY);
	if(d == -1) {
		return -1;
	}
	if((skip_blocks(d, ivt, 1, BLOCK_SIZE) != 0) || (safe_read(d, ivt, BLOCK_SIZE) != (int)BLOCK_SIZE)){
		ret = -1;
	}

	close(d);
	return ret;
}
示例#2
0
// write the IVT to eMMC
int write_block(char *ivt){
	int ret = 0;
	int d = open(MMC_DEVICE, O_WRONLY|O_DIRECT|O_SYNC);
	if(d == -1) {
		return -1;
	}
	if((skip_blocks(d, ivt, 1, BLOCK_SIZE) != 0) || (write(d, ivt, BLOCK_SIZE) != (int)BLOCK_SIZE)){
		ret = -1;
	}

	close(d);
	return ret;
}
示例#3
0
文件: dd.c 项目: Agochka/klibc
int main(int argc, char *argv[])
{
	struct stats stats;
	int ret;
	int rd_fd = 0, wr_fd = 1;

	progname = argv[0];

	ret = parse_options(argc, argv);
	if (ret)
		return ret;

	if (conv & (CONV_BLOCK | CONV_UNBLOCK)) {
		fprintf(stderr, "%s: block/unblock not implemented\n",
			progname);
		return 1;
	}

	in_buf = malloc(ibs);
	if (!in_buf) {
		perror("malloc ibs");
		return 1;
	}

	out_buf = malloc(obs);
	if (!out_buf) {
		perror("malloc obs");
		return 1;
	}

	/*
	 * Open the input file, if specified.
	 */
	if (OPT_IF->str) {
		rd_fd = open(OPT_IF->str, O_RDONLY);
		if (rd_fd == -1) {
			perror("open input file");
			return 1;
		}
	}

	/*
	 * Open the output file, if specified.
	 */
	if (OPT_OF->str) {
		int flags = O_WRONLY|O_CREAT;
		flags |= (conv & CONV_NOTRUNC) ? 0 : O_TRUNC;
		wr_fd = open(OPT_OF->str, flags, 0666);
		if (wr_fd == -1) {
			perror("open output file");
			close(rd_fd);
			return 1;
		}
	}

	/*
	 * Skip obs-sized blocks of output file.
	 */
	if (OPT_SEEK->str && skip_blocks(wr_fd, out_buf, seek, obs)) {
		close(rd_fd);
		close(wr_fd);
		return 1;
	}

	/*
	 * Skip ibs-sized blocks of input file.
	 */
	if (OPT_SKIP->str && skip_blocks(rd_fd, in_buf, skip, ibs)) {
		close(rd_fd);
		close(wr_fd);
		return 1;
	}

	memset(&stats, 0, sizeof(stats));

	/*
	 * Do the real work
	 */
	ret = dd(rd_fd, wr_fd, &stats);

	if (close(rd_fd) == -1)
		perror(OPT_IF->str ? OPT_IF->str : "stdin");
	if (close(wr_fd) == -1)
		perror(OPT_OF->str ? OPT_OF->str : "stdout");

	fprintf(stderr, "%u+%u records in\n", stats.in_full, stats.in_partial);
	fprintf(stderr, "%u+%u records out\n",
		stats.out_full, stats.out_partial);
	if (stats.truncated)
		fprintf(stderr, "%u truncated record%s\n",
			stats.truncated, stats.truncated == 1 ? "" : "s");

	/*
	 * ret will be -SIGINT if we got a SIGINT.  Raise
	 * the signal again to cause us to terminate with
	 * SIGINT status.
	 */
	if (ret == -SIGINT)
		raise(SIGINT);

	return ret;
}