summaryrefslogtreecommitdiffstats
path: root/arch/um/kernel/tempfile.c
blob: b1674bc1395db1fa028e19b66f6072c564966a4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
 * Licensed under the GPL
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/param.h>
#include "init.h"

/* Modified from create_mem_file and start_debugger */
static char *tempdir = NULL;

static void __init find_tempdir(void)
{
	char *dirs[] = { "TMP", "TEMP", "TMPDIR", NULL };
	int i;
	char *dir = NULL;

	if(tempdir != NULL) return;	/* We've already been called */
	for(i = 0; dirs[i]; i++){
		dir = getenv(dirs[i]);
		if((dir != NULL) && (*dir != '\0'))
			break;
	}
	if((dir == NULL) || (*dir == '\0')) 
		dir = "/tmp";

	tempdir = malloc(strlen(dir) + 2);
	if(tempdir == NULL){
		fprintf(stderr, "Failed to malloc tempdir, "
			"errno = %d\n", errno);
		return;
	}
	strcpy(tempdir, dir);
	strcat(tempdir, "/");
}

int make_tempfile(const char *template, char **out_tempname, int do_unlink)
{
	char tempname[MAXPATHLEN];
	int fd;

	find_tempdir();
	if (*template != '/')
		strcpy(tempname, tempdir);
	else
		*tempname = 0;
	strcat(tempname, template);
	fd = mkstemp(tempname);
	if(fd < 0){
		fprintf(stderr, "open - cannot create %s: %s\n", tempname, 
			strerror(errno));
		return -1;
	}
	if(do_unlink && (unlink(tempname) < 0)){
		perror("unlink");
		return -1;
	}
	if(out_tempname){
		*out_tempname = strdup(tempname);
		if(*out_tempname == NULL){
			perror("strdup");
			return -1;
		}
	}
	return(fd);
}

/*
 * Overrides for Emacs so that we follow Linus's tabbing style.
 * Emacs will notice this stuff at the end of the file and automatically
 * adjust the settings for this buffer only.  This must remain at the end
 * of the file.
 * ---------------------------------------------------------------------------
 * Local variables:
 * c-file-style: "linux"
 * End:
 */
OpenPOWER on IntegriCloud