Plan 9 from Bell Labs’s /usr/web/sources/patch/sorry/qfrom/qfrom.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


/*
 * quote from lines without messing with character encoding.
 *	(might rather just undo the character encoding and use sed.)
 */

#include <u.h>
#include <libc.h>
#include <bio.h>

void
qfrom(int fd)
{
	int l;
	char *s;
	Biobuf b, bo;

	if(Binit(&b, fd, OREAD) == -1 ||
	    Binit(&bo, 1, OWRITE) == -1)
		sysfatal("Binit: %r");

	while(s = Brdstr(&b, '\n', 0)){
		l = Blinelen(&b);
		if(l >= 5 && memcmp(s, "From ", 5) == 0)
			Bputc(&bo, ' ');
		Bwrite(&bo, s, l);
		free(s);
	}
	Bterm(&b);
	Bterm(&bo);
}

void
usage(void)
{
	fprint(2, "usage: qfrom [files...]\n");
	exits("usage");
}

void
main(int argc, char **argv)
{
	int fd;

	ARGBEGIN{
	default:
		usage();
	}ARGEND

	if(*argv == 0){
		qfrom(0);
		exits(nil);
	}
	for(; *argv; argv++){
		fd = open(*argv, OREAD);
		if(fd < 0)
			sysfatal("open: %r");
		qfrom(fd);
		close(fd);
	}
	exits(nil);
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.