Prevent funny output when giving a directory name on the command line.

Before:
	$ dc ..
	miyu% dc ..
	dc: 02 unimplemented
	dc: 0 unimplemented
	dc: 0 unimplemented
	dc: input base must be a number between 2 and 16 (inclusive)
	dc: stack empty
	dc: stack empty
	dc: 'h' (0150) unimplemented
	dc: stack empty
	dc: 'u' (0165) unimplemented
	...
	** get heart attack suspecting major FS corruption **

After:
	$ dc ..
	Cannot use directory as input!
This commit is contained in:
hubertf 2003-10-13 21:52:40 +00:00
parent 9be5d4cbe9
commit 83f7cd41fd

23
gnu/dist/bc/dc/dc.c vendored
View File

@ -41,6 +41,9 @@
# endif
#endif
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include "dc.h"
#include "dc-proto.h"
@ -107,10 +110,22 @@ try_file(const char *filename)
if (strcmp(filename, "-") == 0) {
input = stdin;
} else if ( !(input=fopen(filename, "r")) ) {
fprintf(stderr, "Could not open file ");
perror(filename);
exit(EXIT_FAILURE);
} else {
struct stat sb;
if (stat(filename, &sb) < 0) {
fprintf(stderr, "Cannot stat %s: %s\n",
filename, strerror(errno));
exit(EXIT_FAILURE);
}
if (S_ISDIR(sb.st_mode)) {
fprintf(stderr, "Cannot use directory as input!\n");
exit(EXIT_FAILURE);
}
if ( !(input=fopen(filename, "r")) ) {
fprintf(stderr, "Could not open file ");
perror(filename);
exit(EXIT_FAILURE);
}
}
if (dc_evalfile(input))
exit(EXIT_FAILURE);