xref: /plan9/sys/src/cmd/gs/src/gp_mktmp.c (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1999 Aladdin Enterprises.  All rights reserved.
2 
3   This software is provided AS-IS with no warranty, either express or
4   implied.
5 
6   This software is distributed under license and may not be copied,
7   modified or distributed except as expressly authorized under the terms
8   of the license contained in the file LICENSE in this distribution.
9 
10   For more information about licensing, please refer to
11   http://www.ghostscript.com/licensing/. For information on
12   commercial licensing, go to http://www.artifex.com/licensing/ or
13   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
14   San Rafael, CA  94903, U.S.A., +1(415)492-9861.
15 */
16 
17 /* $Id: gp_mktmp.c,v 1.4 2002/02/21 22:24:52 giles Exp $ */
18 /* Replacement for missing mktemp */
19 #include "stat_.h"
20 #include "string_.h"
21 
22 /* This procedure simulates mktemp on platforms that don't provide it. */
23 char *
mktemp(char * fname)24 mktemp(char *fname)
25 {
26     struct stat fst;
27     int len = strlen(fname);
28     char *end = fname + len - 6;
29 
30     if (len < 6 || strcmp(end, "XXXXXX"))
31 	return (char *)0;	/* invalid  */
32     strcpy(end, "AA.AAA");
33 
34     while (stat(fname, &fst) == 0) {
35 	char *inc = fname + len - 1;
36 
37 	while (*inc == 'Z' || *inc == '.') {
38 	    if (inc == end)
39 		return (char *)0;	/* failure */
40 	    if (*inc == 'Z')
41 		*inc = 'A';
42 	    --inc;
43 	}
44 	++*inc;
45     }
46     return fname;
47 }
48