moth/packages/printf/src/printf.c

101 lines
2.0 KiB
C

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
void
record(char *buf) {
char *p;
char *ip = getenv("TCPREMOTEIP");
fprintf(stderr, "%s: ", ip);
for (p = buf; *p; p += 1) {
if (isprint(*p)) {
fputc(*p, stderr);
} else {
fprintf(stderr, "%%%02x", *p);
}
}
fputc('\n', stderr);
}
/* Storage space for tokens */
char token[5][100];
/* Make this global so the stack isn't gigantic */
char global_fmt[8000] = {0};
int
main(int argc, char *argv[], char *env[])
{
char *t0 = token[0];
int t1[100];
char *fmt = global_fmt;
char *datacomp = "welcome datacomp";
int token4_flag = 0;
int i;
/* Read in tokens */
{
FILE *tf = fdopen(3, "r");
if (! tf) {
fprintf(stderr, "No tokens on fd3\n");
return 1;
}
for (i = 0; i < 5; i += 1) {
char *p = fgets(token[i], sizeof(token[i]), tf);
if (! p) {
fprintf(stderr, "Cannot read token %d\n", i);
return 1;
}
/* Replace newline with null */
for (; *p && (*p != '\n'); p += 1);
*p = 0;
}
}
/* Make stderr buffer until lines */
setlinebuf(stderr);
/* So the compiler won't complain about unused variables */
i = datacomp[0] ^ t0[0];
/* Token 0 just hangs out on the stack */
/* Set up token 1 (%c%c%c%c...) */
for (i = 0; '\0' != token[1][i]; i += 1) {
t1[i] = token[1][i];
}
t1[i-1] = '\n';
/* Stick token 2 into the environment */
for (i = 0; env[i]; i += 1);
env[i-1] = token[2];
/* token 3 is pretty much a gimmie */
/* token 4 will only be printed if you set token4_flag to non-zero */
if (NULL == fgets(global_fmt, sizeof(global_fmt), stdin)) {
return 0;
}
record(fmt);
printf(fmt,
"Welcome to the printf category.\n",
"There are multiple tokens hiding here.\n",
"Good luck!\n",
token[3],
"token4_flag (@ ", &token4_flag, "): ", token4_flag, "\n");
if (token4_flag) {
printf("%s\n", token[4]);
}
return 0;
}