Пример #1
0
Файл: ln.c Проект: jarn0x/Escape
int main(int argc,const char *argv[]) {
	char *oldp,*newp;

	int res = ca_parse(argc,argv,CA_NO_FREE,"=s =s",&oldp,&newp);
	if(res < 0) {
		printe("Invalid arguments: %s",ca_error(res));
		usage(argv[0]);
	}
	if(ca_hasHelp())
		usage(argv[0]);

	if(link(oldp,newp) < 0)
		error("Unable to create the link '%s' to '%s'",newp,oldp);
	return EXIT_SUCCESS;
}
Пример #2
0
int main(int argc,const char *argv[]) {
	int rec = false;
	const char **args;

	int res = ca_parse(argc,argv,0,"r",&rec,&rec);
	if(res < 0) {
		printe("Invalid arguments: %s",ca_error(res));
		usage(argv[0]);
	}
	if(ca_hasHelp() || ca_getFreeCount() == 0)
		usage(argv[0]);

	args = ca_getFree();
	while(*args) {
		removeRec(*args,rec);
		args++;
	}
	return EXIT_SUCCESS;
}
Пример #3
0
int main(int argc,const char *argv[]) {
	const char **args;

	int res = ca_parse(argc,argv,0,"");
	if(res < 0) {
		printe("Invalid arguments: %s",ca_error(res));
		usage(argv[0]);
	}
	if(ca_hasHelp())
		usage(argv[0]);

	args = ca_getFree();
	while(*args) {
		if(mkdir(*args) < 0)
			printe("Unable to create directory '%s'",*args);
		args++;
	}
	return EXIT_SUCCESS;
}
Пример #4
0
int main(int argc,const char *argv[]) {
	const char **args;
	FILE *in = stdin;
	int c,line,n = 5;

	/* parse args */
	int res = ca_parse(argc,argv,CA_MAX1_FREE,"n=d",&n);
	if(res < 0) {
		printe("Invalid arguments: %s",ca_error(res));
		usage(argv[0]);
	}
	if(ca_hasHelp())
		usage(argv[0]);

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

	/* read the first n lines*/
	line = 0;
	while(!ferror(stdout) && line < n && (c = fgetc(in)) != EOF) {
		putchar(c);
		if(c == '\n')
			line++;
	}
	if(ferror(in))
		error("Read failed");
	if(ferror(stdout))
		error("Write failed");

	/* clean up */
	if(args[0])
		fclose(in);
	return EXIT_SUCCESS;
}
Пример #5
0
int main(int argc,const char **argv) {
	int first = 1,last = -1;
	char *fields = NULL;
	char *delim = (char*)"\t";
	const char **args;

	int res = ca_parse(argc,argv,0,"f=s* d=s",&fields,&delim);
	if(res < 0) {
		printe("Invalid arguments: %s",ca_error(res));
		usage(argv[0]);
	}
	if(ca_hasHelp())
		usage(argv[0]);

	parseFields(fields,&first,&last);

	args = ca_getFree();
	if(args[0] == NULL)
		handleFile(stdin,delim,first,last);
	else {
		FILE *f;
		while(*args) {
			if(isdir(*args)) {
				printe("'%s' is a directory!",*args);
				continue;
			}
			f = fopen(*args,"r");
			if(!f) {
				printe("Unable to open '%s'",*args);
				continue;
			}
			handleFile(f,delim,first,last);
			fclose(f);
			args++;
		}
	}
	return EXIT_SUCCESS;
}
Пример #6
0
int main(int argc,const char *argv[]) {
	char c;
	const char **args;
	size_t i,argCount;
	size_t freeArgs;
	FILE *file = stdin;
	const char *filename = NULL;

	/* parse args */
	int res = ca_parse(argc,argv,0,"a=s",&filename);
	if(res < 0) {
		printe("Invalid arguments: %s",ca_error(res));
		usage(argv[0]);
	}
	if(ca_hasHelp())
		usage(argv[0]);

	/* open file? */
	if(filename != NULL) {
		file = fopen(filename,"r");
		if(file == NULL)
			error("Unable to open file '%s'",filename);
	}

	/* read arguments */
	argCount = 0;
	while((c = fgetc(file)) != EOF) {
		if(isspace(c)) {
			appendArg();
			argCount++;
			argPos = 0;
		}
		else {
			if(argPos < MAX_ARG_LEN)
				argBuf[argPos++] = c;
			else
				error("Argument too long");
		}
	}
	if(ferror(file))
		error("Read failed");
	if(filename != NULL)
		fclose(file);
	/* append last one */
	if(argPos > 0) {
		appendArg();
		argCount++;
	}

	/* build args-array and copy arguments into it */
	freeArgs = ca_getFreeCount();
	if(freeArgs == 0)
		freeArgs = 1;
	args = (const char**)malloc(sizeof(char*) * (argCount + freeArgs + 1));
	if(!args)
		error("Not enough mem");
	i = 0;
	if(ca_getFreeCount() == 0)
		args[i++] = "echo";
	else {
		for(i = 0; i < freeArgs; i++)
			args[i] = ca_getFree()[i];
	}
	for(; first != NULL; i++) {
		args[i] = first->arg;
		first = first->next;
	}
	args[i] = NULL;
	/* exchange us with requested command */
	execvp(args[0],args);
	return EXIT_SUCCESS;
}
Пример #7
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;
}
Пример #8
0
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;
}
Пример #9
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;
}