[ create a new paste ] login | about

Link: http://codepad.org/lloLZ1FW    [ raw code | fork ]

C, pasted on Apr 21:
#include <ctype.h>
#include <string.h>
#include <stdio.h>

int
forallchar(const char *s0, int (*p)(int))
{
	const char	*s;

	for (s = s0; *s != '\0'; s ++) {
		if (!(*p)((int)*s)) {
			return 0;
		}
	}
	return 1;
}

void
string_to_base64array(const char *p, short *a, int len)
{
	int	i;
	short	c, n;

	for (i = 0; i < len; i ++) {
		a[i] = 0;
	}
	for (; *p != '\0'; p ++) {
		c = 0;
		for (i = 0; i < len; i ++) {
			n = a[i] * 10 + c;
			a[i] = n % 64;
			c = n / 64;
		}
		n = a[0] + (*p - '0');
		a[0] = n % 64;
		c = n / 64;
		for (i = 1; i < len; i ++) {
			n = a[i] + c;
			a[i] = n % 64;
			c = n / 64;
		}
	}
}

void
print_as_octal(const char *str)
{
	int	ok;
	const char	*prefix = "";

	switch (str[0]) {
	case '+':
		prefix = "+";
		str ++;
		break;
	case '-':
		prefix = "-";
		str ++;
		break;
	}
	if (*str != '\0' && forallchar(str, isdigit)) {
		int	length = strlen(str);
		short	buf[length];
		int	i;

		string_to_base64array(str, buf, length);
		printf("%s", prefix);
		i = length - 1;
		while (i > 0 && buf[i] == 0) {
			i --;
		}
		printf("%o", buf[i]);
		while (i > 0) {
			i --;
			printf("%02o", buf[i]);
		}
	} else {
		printf("*");
	}
	printf("\n");
}

int
main(int argc, char *argv[])
{
	int	pos;

	for (pos = 1; pos < argc; pos ++) {
		print_as_octal(argv[pos]);
	}
	return 0;
}


Output:
No errors or program output.


Create a new paste based on this one


Comments: