Blame src/stdio/fscanf.c

nsz 086c6a
#include <stdlib.h>
nsz a95e16
#include <stdio.h>
nsz a95e16
#include <string.h>
nsz a95e16
#include <errno.h>
nsz a95e16
#include <limits.h>
nsz a95e16
#include <unistd.h>
nsz a95e16
#include "test.h"
nsz a95e16
nsz 462b4f
#define T(f, x, m) do { \
nsz a95e16
	r = (f); \
nsz a95e16
	if (r != (x)) \
nsz a95e16
		error("%s failed (got %d, expected %d, errno \"%s\") (%s)\n", \
nsz a95e16
			#f, r, x, errno ? strerror(errno) : "", m); \
nsz a95e16
	errno = 0; \
nsz a95e16
} while (0)
nsz a95e16
nsz 462b4f
#define S(s, x, m) do { \
nsz 462b4f
	if (strcmp(s, x) != 0) \
nsz 462b4f
		error("got [%s] want [%s] (%s)\n", s, x, m); \
nsz 462b4f
} while(0)
nsz 086c6a
nsz 462b4f
int main(void)
nsz 462b4f
{
nsz 462b4f
	int r, x, y;
nsz a95e16
	char a[100], b[100];
nsz a95e16
	int p[2];
nsz a95e16
	FILE *f;
nsz a95e16
nsz 462b4f
	T(pipe(p), 0, "open pipe");
nsz 462b4f
	T(!(f = fdopen(p[0], "rb")), 0, "fdopen pipe");
nsz a95e16
	if (!f) {
nsz a95e16
		close(p[0]);
nsz a95e16
		close(p[1]);
nsz 462b4f
		return test_status;
nsz a95e16
	}
nsz a95e16
nsz 462b4f
	T(write(p[1], "hello, world\n", 13), 13, "write to pipe");
nsz 462b4f
	T(fscanf(f, "%s %[own]", a, b), 2, "");
nsz a95e16
	S(a, "hello,", "wrong result for %s");
nsz a95e16
	S(b, "wo", "wrong result for %[own]");
nsz 462b4f
	T(fgetc(f), 'r', "fgetc 'r'");
nsz a95e16
nsz 462b4f
	T(write(p[1], " 0x12 0x34", 10), 10, "");
nsz 462b4f
	T(fscanf(f, "ld %5i%2i", &x, &y), 1, "");
nsz 462b4f
	T(x, 0x12, "");
nsz 462b4f
	T(fgetc(f), '3', "fgetc '3'");
nsz a95e16
nsz a95e16
	fclose(f);
nsz a95e16
	close(p[1]);
nsz 462b4f
	return test_status;
nsz a95e16
}