[net.unix] How do you check for an exact match with a scanf format?

ado@elsie.UUCP (Arthur David Olson) (08/22/85)

Consider the C language source for a program named "isdecimal", which is to
print "1" if given one argument that's a decimal number or print "0" otherwise:

	#include <stdio.h>

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

		result = argc == 2 && sscanf(argv[1], "%d", &d) == 1;
		printf("%d\n", result);
		return !result;
	}

Now the above program does the right thing in most cases; however, if you do a
	isdecimal 1a
the output you get is
	1
which isn't right in my book.

The best way I've been able to come up with to get "isdecimal" to do the right
thing is:

	#include <stdio.h>

	main(argc, argv)
	int	argc;
	char *	argv[];
	{
		int	result;
		int	d;
		char	c;

		c = '\0';
		result = argc == 2 && sscanf(argv[1], "%d%c", &d, &c) == 1 &&
			c == '\0';
		printf("%d\n", result);
		return !result;
	}

which seems a kludge to me.

If you have a better, portable (to 4.1bsd especially) way of doing the above
ilk of checking, I'd appreciate hearing from you BY MAIL.
--
UNIX is an AT&T Bell Laboratories trademark.
--
	UUCP: ..decvax!seismo!elsie!ado    ARPA: elsie!ado@seismo.ARPA
	DEC, VAX and Elsie are Digital Equipment and Borden trademarks