spicylord / clutils (public) (License: GPLv3) (since 2019-07-04) (hash sha1)
An assortment of new and rewritten preexisting tools written by me.

/clp.c (193c45d55d89e8b0954c1cbe277edfd83b9fa33b) (3763 bytes) (mode 100644) (type blob)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define maxtofrom 10
/* ocountflag means that it will only count
 how many entries there are then quit */

long from=0, to=0, end=0;
int inflag=0, outflag=0, ocountflag=0, delflag=0, commaflag=0;
char tester[100000]; /* if you have a line longer than this then you have a problem. */
char *filename;

void prepcomms(char *where)
{
	int fromcount=0, tocount=0;
	char cfrom[maxtofrom+8];
	char cto[maxtofrom+8];
	for (int j=0; where[j] && j<=(maxtofrom*2+8); j++) {
		if (where[j]==',') { commaflag=1; continue; }
		switch (where[j]) {
			case 'i': inflag = 1;
		        	  break;
			case 'p': outflag = 1; 
			          break;
			case 'd': delflag = 1;; 
			          break;
			case 'c': ocountflag = 1;
			          break;
		}
		if (!commaflag) cfrom[fromcount++] = where[j];
		else cto[tocount++] = where[j];
	}
	if (commaflag) {
		from = atol(cfrom);
		to = atol(cto);
	} else {
		to = (from=atol(cfrom));
	}
	if (!from && commaflag && to) {
		fputs("clip: must have a number before comma\n", stderr);
		free(filename);
		exit(1);
	}
	if (from > to) {
		fputs("clip: first number must be less than second\n", stderr);
		free(filename);
		exit(1);
	}
}

void cookargs(int c, char *v[])
{
	if (c>1) {
		for (int i=1; v[i]; i++) {
			if (v[i][0]=='-' && v[i][1] && v[i][1]=='f' && v[i+1]) {
				filename = v[i+1];
				i++; /* makes cursor skip file name */
			}
			else prepcomms(v[i]);
		}
		if (!inflag && !outflag && !delflag && !ocountflag) {
			fputs("usage: clip [-f filename] [justcount|in|out #,#|delete #,#]\n", stderr);
			free(filename);
			exit(1);
		}
	} else {
		fputs("usage: clip [-f filename] [count|in|print #,#|delete #,#]\n", stderr);
		free(filename);
		exit(1);
	}
}

int main(int argc, char *argv[])
{
	filename = malloc(3000);
	sprintf(filename, "%s/%s", getenv("HOME"), ".clipboard");
	char delim[31] = "------------------------------\n";
	FILE *filep;
	long counter = 0;
	cookargs(argc, argv); /* can change file name based on what comes after "-f" */

	if ((filep=fopen(filename, "a+"))==NULL) {
		fprintf(stderr, "clip: cannot open %s", filename);
		free(filename);
		exit(1);
	}
	if (inflag) {
		char inbuf[1000000];
		fread(inbuf, sizeof(inbuf), 1, stdin);
		fprintf(filep, "%s%s", delim, inbuf);
		fclose(filep);
		free(filename);
		exit(1);
	}

	while (fgets(tester, sizeof(tester), filep))
		if (memcmp(tester, delim, sizeof(delim))==0)
			end++;

	rewind(filep);
	if (ocountflag) {
		printf("%ld\n", end);
		goto ending; /* stops program after outputting count */
	}

	if (end==0) {
		fputs("nothing to act on\n", stderr);
		fclose(filep);
		free(filename);
		exit(1);
	}
	if (to>end) {
		fputs("clip: end point too big\n", stderr);
		fclose(filep);
		free(filename);
		exit(1);
	}
	if (!from && !to && commaflag) { from=1; to=end; }
	if (!from && !to && !commaflag) { from = end; to = end; }
	if (delflag) {
		FILE *tdelfilep;
		int delimflag=0;
		if ((tdelfilep=fopen(".clipboard~~~", "w+"))==NULL) {
			fputs("clip: cannot create temporary file\n", stderr);
			fclose(filep);
			free(filename);
			exit(1);
		}
		while (counter <= end && fgets(tester, sizeof(tester), filep)) {
			if (memcmp(tester, delim, sizeof(delim))==0) {
				counter++;
				delimflag=1;
				continue;
			}
			if (counter < from || counter > to) {
				fprintf(tdelfilep, "%s%s", delimflag?delim:"", tester);
				delimflag=0;
			}
		}
		rename(".clipboard~~~", filename);
		goto ending;
	}

	if (outflag) {
		while (counter <= end  && fgets(tester, sizeof(tester), filep)) {
			if (memcmp(tester, delim, sizeof(delim))==0) {
				counter++;
				continue;
			}
			if (counter >= from && counter <= to) {
				fputs(tester, stdout);
			}
		}
	}
ending:
	free(filename);
 	fclose(filep);
	return 0;
}


Mode Type Size Ref File
100644 blob 1488 4f3466dbd6e7f10f771ec95842644cf40bb1cdda COPYING
100755 blob 730 8baf42812ace55be14435771a91991cf1935c1db INSTALL
100644 blob 1224 cc37ebb68b792ee29b9f99111c7ee88e5ffa7723 README
100644 blob 3763 193c45d55d89e8b0954c1cbe277edfd83b9fa33b clp.c
100644 blob 142 66ddd77911db11fab37139f10ccace21a21ea7e8 echo.c
100644 blob 404 b285e6453e6bf738ef8cc2ce7df9564695d1a612 nl.l
100644 blob 1278 5d13cc09ac4024170ba3ca102c657f10e2d0fac5 sc.c
100644 blob 341 0a5de084da1b736602af29a9175341ac9322ffcd spng.c
100644 blob 513 1ad1125d57e0a255ed9218defa5b14d9c7962b0a tee.c
100644 blob 1444 d3316c60317a88cbe4f9c4230cec56022348dca6 wc.c
100644 blob 130 17ddde74c448540143c760e7cacd359c3d6ca85e yes.c
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/spicylord/clutils

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/spicylord/clutils

Clone this repository using git:
git clone git://git.rocketgit.com/user/spicylord/clutils

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main