[comp.lang.c] Why does mkstemp

edh@ux.acs.umn.edu (Eric D. Hendrickson) (11/03/90)

I need to read from stdin, determine if the incoming file is postscript and
if not, run it through enscript.  I check the first 2 characters of stdin
for "%!", and then I need to rewind and send the file on to the next step.
I am trying to get mkstemp to create a temporary file for me to hold stdin,
so I can rewind it w/o worrying whether stdin came from a pipe or not, but
it core dumps upon hitting the line containing the call to mkstemp.  Can
anybody see whats wrong here?

		Thanks very much for any help,
			Eric
--
/----------"Oh carrots are divine, you get a dozen for dime, its maaaagic."--
|Eric (the "Mentat-Philosopher") Hendrickson	  Academic Computing Services
|edh@ux.acs.umn.edu	   The game is afoot!	      University of Minnesota
\-"What does 'masochist' and 'amnesia' mean?   Beats me, I don't remember."--
==============================================================================
#define	ENSCRIPT	"/usr/local/bin/enscript"
#define	FPR		"/usr/local/bin/fpr"
#define	PR		"/usr/bin/pr"
#define	MAGICSIZE	2

prefilter()
{
	char	cmdbuf[BUFSIZ];
	char	mybuf[BUFSIZ];
	char	magic[MAGICSIZE];
	char	*template = ".psoptfXXXXXX";
	int	c, cnt, magiccnt, tmp;
	struct	stat	sbuf;

/* !!! if PostScript file or Format is not one of (f,l,p,r) then return !!! */

	if ((magiccnt = read(fileno(stdin), magic, MAGICSIZE)) < 0) {
		log("bad magic number, EOF");
		return(0);
	}
	sprintf(cmdbuf, "%s -gq -p -", ENSCRIPT);
	(void) fstat(fileno(stdin), &sbuf);
	if ((sbuf.st_mode & S_IFMT) != S_IFREG) { /* pipe! */
/* THE FOLLOWING LINE CORE DUMPS */
		if ((tmp = mkstemp(template)) < 0) {
			log("creating tmp file");
		}
		write(tmp, magic, magiccnt);
		while ((cnt = read(fileno(stdin), mybuf, sizeof mybuf)) > 0) {
			if (write(tmp, mybuf, cnt) != cnt) {
				log("copying tmp file");
			}
		}
		if (cnt < 0) {
			log("copying tmp file");
		}
		close(tmp);
		if (freopen(template, "r", stdin) == NULL) {
			log("opening tmp file");
		}
		unlink(template);
	}
	lseek(0, 0L, 0);
	rewind(stdin);
	if (strncmp(magic, "%!", 2) == 0) { /* is file postscript? */
		while ((c = getc(stdin)) != EOF) {
			putchar(c); 
		}
		return(0);
	}
	switch (*format) {
	    case 'f':
	    case 'l':
		break;
	    case 'p':
		sprintf(cmdbuf, "%s | %s", PR, cmdbuf);
		break;
	    case 'r':
		sprintf(cmdbuf, "%s | %s", FPR, cmdbuf);
		break;
	    default:
		log("Cannot process format %c files", format);
		return(0);
	}
	if (validate_zopts(cmdbuf, zopts)) {
		fatal("Unknown Z options detected");
		return(2);
	}
	system(cmdbuf);
}
-- 
/----------"Oh carrots are divine, you get a dozen for dime, its maaaagic."--
|Eric (the "Mentat-Philosopher") Hendrickson	  Academic Computing Services
|edh@ux.acs.umn.edu	   The game is afoot!	      University of Minnesota
\-"What does 'masochist' and 'amnesia' mean?   Beats me, I don't remember."--

cc100aa@prism.gatech.EDU (Ray Spalding) (11/08/90)

In article <2626@ux.acs.umn.edu> edh@ux.acs.umn.edu (Eric D. Hendrickson) writes:
[...]
>	char	*template = ".psoptfXXXXXX";
[...]
>/* THE FOLLOWING LINE CORE DUMPS */
>		if ((tmp = mkstemp(template)) < 0) {
[...]

String constants may not be writable.  Try:
	char	template[] = ".psoptfXXXXXX";
-- 
Ray Spalding, Technical Services, Office of Information Technology
Georgia Institute of Technology, Atlanta Georgia, 30332-0715
uucp:     ...!{allegra,amd,hplabs,ut-ngp}!gatech!prism!cc100aa
Internet: cc100aa@prism.gatech.edu