/* it's a shame gnu top won't show argv[0] as process COMMAND */

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



int
main(int argc, char **argv)
{
	unsigned long delay = 1000000;
	char *msg;
	int proclen;
	int msglen, offset;
	int i;

	if (argc < 2) {
		fprintf(stderr, "Usage: %s MESSAGE [DELAY IN MS]\n", argv[0]);
		return EXIT_SUCCESS;
	}

	proclen = strlen(argv[0]);

	msglen = strlen(argv[1]);
	msg = malloc(msglen + 1);
	memcpy(msg, argv[1], msglen + 1);

	if (argc >= 3) {
		delay = atoi(argv[2]) * 1000;
	}

	for (i = 1; i < argc; i++) {
		int l = strlen(argv[i]);
		memset(argv[i], 0, l);
	}

	offset = 0;
	while (1) {
		memset(argv[0], ' ', proclen);
		if (offset < proclen) {
			strncpy(argv[0] + proclen - offset - 1,
					msg, offset + 1);
		} else {
			strncpy(argv[0], msg + offset - proclen, proclen);
		}
		usleep(delay);

		offset++;
		if (offset == msglen + proclen) {
			offset = 0;
		}
	}

	free(msg);

	return 0;
}
