
#include <stdio.h>
#include <stdarg.h>

static char teststr[] = "this is a test\r\n";

static int my_printf(const char *msg, ...)
{
	va_list args;

	va_start(args, msg);
	vfprintf(msg, args, stdout);
	va_end(args);
}

static int test_write(const char *msg)
{
	int ptr;

	for (ptr = 0; msg[ptr] != 0; ptr++);

	fwrite(msg, 1, ptr, stdout);
}

static int my_printf1(const char *msg, ...)
{
	va_list args;
	char buff[1024];

	va_start(args, msg);
	snprintf(buff, sizeof(buff), msg, args);
	va_end(args);

	test_write(buff);
}

int main(int argc, char **argv, char **envp)
{
	int i;

	printf("-----------\n");
	printf("hello world (4)\n");
	printf("-----------\n");

	fwrite(teststr, 1, sizeof(teststr), stdout);

	test_write(teststr);

	my_printf1("test printf 1, argc %d, argv %p\n", argc, argv);
	my_printf("test my printf\n");

	printf("argc is %d\n", argc);
	printf("argv is %p\n", argv);
	printf("envp is %p\n", envp);

	printf("\n");

	printf("argv 0 is %s\n", argv[0]);

	printf("\n");

	for (i = 0; i < argc; i++)
		printf("arg %d/%d: %s\n", i, argc, argv[i]);

	for (i = 0; envp[i] != NULL; i++) 
		printf("env %d: %s\n", i, envp[i]);

	return 0;
}
