示例#1
0
文件: dump.c 项目: jarn0x/Escape
int main(int argc,const char *argv[]) {
    FILE *in = stdin;
    uint base = 16;
    char format = OUT_FORMAT_HEX;
    int i,count = -1;
    const char **args;

    int res = ca_parse(argc,argv,CA_MAX1_FREE,"n=d f=c",&count,&format);
    if(res < 0) {
        printe("Invalid arguments: %s",ca_error(res));
        usage(argv[0]);
    }
    if(ca_hasHelp())
        usage(argv[0]);

    /* TODO perhaps cmdargs should provide a possibility to restrict the values of an option */
    /* like 'arg=[ohd]' */
    if(format != OUT_FORMAT_DEC && format != OUT_FORMAT_HEX &&
            format != OUT_FORMAT_OCT)
        usage(argv[0]);

    switch(format) {
    case OUT_FORMAT_DEC:
        base = 10;
        break;
    case OUT_FORMAT_HEX:
        base = 16;
        break;
    case OUT_FORMAT_OCT:
        base = 8;
        break;
    }

    args = ca_getFree();
    if(args[0]) {
        in = fopen(args[0],"r");
        if(!in)
            error("Unable to open '%s'",args[0]);
    }

    ulong shname;
    uchar *shmem;
    if(sharebuf(fileno(in),BUF_SIZE,(void**)&shmem,&shname,0) < 0) {
        if(shmem == NULL)
            error("Unable to mmap buffer");
    }

    i = 0;
    while(!ferror(stdout) && (count < 0 || count > 0)) {
        size_t x,c;
        c = count >= 0 ? MIN(count,BUF_SIZE) : BUF_SIZE;
        c = fread(shmem,sizeof(char),c,in);
        if(c == 0)
            break;

        for(x = 0; x < c; x++, i++) {
            if(i % base == 0) {
                if(i > 0)
                    printAscii(base,i);
                printf("%08x: ",i);
            }

            if(isprint(shmem[x]) && shmem[x] < 0x80 && !isspace(shmem[x]))
                ascii[i % base] = shmem[x];
            else
                ascii[i % base] = NPRINT_CHAR;
            switch(format) {
            case OUT_FORMAT_DEC:
                printf("%03d ",shmem[x]);
                break;
            case OUT_FORMAT_HEX:
                printf("%02x ",shmem[x]);
                break;
            case OUT_FORMAT_OCT:
                printf("%03o ",shmem[x]);
                break;
            }
        }

        if(count >= 0)
            count -= c;
    }
    if(ferror(in))
        error("Read failed");
    if(ferror(stdout))
        error("Write failed");

    printAscii(base,i);

    destroybuf(shmem,shname);
    if(args[0])
        fclose(in);
    return EXIT_SUCCESS;
}
示例#2
0
文件: dd.c 项目: jarn0x/Escape
int main(int argc,const char *argv[]) {
	size_t bs = 4096;
	size_t count = 0;
	ullong total = 0;
	char *inFile = NULL;
	char *outFile = NULL;
	FILE *in = stdin;
	FILE *out = stdout;

	int res = ca_parse(argc,argv,CA_NO_DASHES | CA_NO_FREE | CA_REQ_EQ,
			"if=s of=s bs=k count=k",&inFile,&outFile,&bs,&count);
	if(res < 0) {
		printe("Invalid arguments: %s",ca_error(res));
		usage(argv[0]);
	}
	if(ca_hasHelp())
		usage(argv[0]);

	if(signal(SIGINT,interrupted) == SIG_ERR)
		error("Unable to set sig-handler for SIGINT");

	if(inFile) {
		in = fopen(inFile,"r");
		if(in == NULL)
			error("Unable to open '%s'",inFile);
	}
	if(outFile) {
		out = fopen(outFile,"w");
		if(out == NULL)
			error("Unable to open '%s'",outFile);
	}

	uint64_t start = rdtsc(), end;
	{
		ulong shname;
		uchar *shmem;
		if(sharebuf(fileno(in),bs,(void**)&shmem,&shname,0) < 0) {
			if(shmem == NULL)
				error("Unable to mmap buffer");
		}

		size_t result;
		ullong limit = (ullong)count * bs;
		while(run && (!count || total < limit)) {
			if((result = fread(shmem,1,bs,in)) == 0)
				break;
			if(fwrite(shmem,1,bs,out) == 0)
				break;
			total += result;
		}

		if(ferror(in))
			error("Read failed");
		if(ferror(out))
			error("Write failed");
		destroybuf(shmem,shname);
	}
	end = rdtsc();

	uint64_t usecs = tsctotime(end - start);
	printf("%zu records in\n",count);
	printf("%zu records out\n",count);
	printf("%Lu bytes (%.1lf MB) copied, %.1lf s, %.1lf MB/s\n",
		total,total / 1000000.0,usecs / 1000000.0,total / (double)usecs);

	if(inFile)
		fclose(in);
	if(outFile)
		fclose(out);
	return EXIT_SUCCESS;
}
示例#3
0
int main(int argc,const char *argv[]) {
	size_t bs = 4096;
	size_t count = 0;
	ullong total = 0;
	char *inFile = NULL;
	char *outFile = NULL;
	int infd = STDIN_FILENO;
	int outfd = STDOUT_FILENO;

	int res = ca_parse(argc,argv,CA_NO_DASHES | CA_NO_FREE | CA_REQ_EQ,
			"if=s of=s bs=k count=k",&inFile,&outFile,&bs,&count);
	if(res < 0) {
		printe("Invalid arguments: %s",ca_error(res));
		usage(argv[0]);
	}
	if(ca_hasHelp())
		usage(argv[0]);

	if(signal(SIGINT,interrupted) == SIG_ERR)
		error("Unable to set sig-handler for SIGINT");

	if(inFile) {
		infd = open(inFile,O_RDONLY);
		if(infd < 0)
			error("Unable to open '%s'",inFile);
	}
	if(outFile) {
		outfd = open(outFile,O_WRONLY);
		if(outfd < 0)
			error("Unable to open '%s'",outFile);
	}

	uint64_t start = rdtsc(), end;
	{
		int shmfd;
		uchar *shmem;
		if((shmfd = sharebuf(infd,bs,(void**)&shmem,0)) < 0) {
			if(shmem == NULL)
				error("Unable to mmap buffer");
		}
		if(shmem) {
			if(delegate(outfd,shmfd,O_RDONLY,DEL_ARG_SHFILE) < 0) {}
		}

		ssize_t result;
		ullong limit = (ullong)count * bs;
		while(run && (!count || total < limit)) {
			if((result = read(infd,shmem,bs)) <= 0) {
				if(result < 0)
					error("Read failed");
				break;
			}

			if(write(outfd,shmem,result) < 0)
				error("Write failed");

			total += result;
		}

		destroybuf(shmem,shmfd);
	}
	end = rdtsc();

	uint64_t usecs = tsctotime(end - start);
	printf("%zu records in\n",count);
	printf("%zu records out\n",count);
	printf("%Lu bytes (%.1lf MB) copied, %.1lf s, %.1lf MB/s\n",
		total,total / 1000000.0,usecs / 1000000.0,total / (double)usecs);

	if(inFile)
		close(infd);
	if(outFile)
		close(outfd);
	return EXIT_SUCCESS;
}