A C Program a Day

2019-10-03 - Move to trash

trash-cli is good, but it can be a bit slow (probably because it's written in Python :/). So, here's a small trash program (it might have bugs though!). This only puts files in the trash; later I'll write a trash restorer.
Today's procedure is ltoa, for converting numbers to strings.


#include "procs-2019-10-03.c"

#include <fcntl.h>
#include <sys/stat.h>

int main(int argc, char **argv) {
	extern int rename(const char *old, const char *new);
	extern char *getenv(const char *name);
	int i;
	char *home, *pwd;
    long homelen = 0, pwdlen = 0;
	long brkbytes = 0;
	void *brk = sbrk(0);
	char *p;
	home = getenv("HOME");
	pwd = getenv("PWD");
	for (p = home; *p; p++)
	for (p = pwd; *p; p++)
	if (argc < 2) {
		const char err[] = "No files supplied.\n";
		write(stderr, err, sizeof err - 1);
		return exitfailure;

	for (i = 1; i < argc; i++) {
		long filelen = 0;
		long outlen;
		long whencelen;
	    long bytes;
	    long c;
		char *out, *whence;
		char *inp, *outp;
		char *homep;
		char *in = argv[i];
		if (in[0] == '/' && in[1] == '\0') {
			const char err[] = "Will not trash /.\n";
			write(stderr, err, sizeof err - 1);
			return exitfailure;

		for (inp = in; *inp; inp++)
		outlen = filelen + 32;
		whencelen = outlen + 7 /* strlen("-whence") */;
		bytes = outlen + whencelen; 
		if (bytes > brkbytes)
			sbrk(bytes - brkbytes);
		out = brk, whence = (char*)brk + outlen;
	    outp = out;
		homep = home;
		if (*inp == '/') inp--;
		while (*inp != '/' && inp > in) inp--;
		while (*homep)
			*outp++ = *homep++;
		for (c = 0; c < 7 /* strlen("/.trash") */; c++)
			*outp++ = "/.trash"[c];
		if (*inp != '/')
			*outp++ = '/';
		while (*inp)
			*outp++ = *inp++;
		if (outp[-1] == '/') 
		*outp = 0;

			long id = 0; /* how many times has a file with this name been trashed? */
			struct stat st;
			while (stat(out, &st) == 0) {
				ltoa(++id, outp);
		inp = out;
		outp = whence;
	    while (*inp)
			*outp++ = *inp++;
		*outp++ = '-';
		*outp++ = 'w';
		*outp++ = 'h';
		*outp++ = 'e';
		*outp++ = 'n';
		*outp++ = 'c';
		*outp++ = 'e';
		*outp = 0;
			int whenced = creat(whence, 0666);
			if (whenced == -1) {
				char err[] = "Could not create origin file. Do you have write permission to ~/.trash?";
				write(stderr, err, sizeof err - 1);
				return exitfailure;
			if (in[0] != '/') {
				write(whenced, pwd, (size_t)pwdlen);
				write(whenced, "/", 1);
			write(whenced, in, (size_t)filelen);

		if (rename(in, out) != 0) {
			char err[] = "Could not move file to trash.";
			write(stderr, err, sizeof err - 1);
			return exitfailure;
	return 0;

This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means. In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. For more information, please refer to <http://unlicense.org/>