1 #if !defined(lint) && !defined(DOS)
2 static char rcsid
[] = "$Id: tempfile.c 770 2007-10-24 00:23:09Z hubert@u.washington.edu $";
6 * ========================================================================
7 * Copyright 2006-2007 University of Washington
8 * Copyright 2013-2016 Eduardo Chappa
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * ========================================================================
19 #include "../pith/headers.h"
20 #include "../pith/tempfile.h"
24 * Return the name of a file in the same directory as filename.
25 * Same as temp_nam except it figures out a name in the same directory.
26 * It also returns the name of the directory in ret_dir if ret_dir is
27 * not NULL. That has to be freed by caller. If return is not NULL the
28 * empty file has been created.
31 tempfile_in_same_dir(char *filename
, char *prefix
, char **ret_dir
)
34 #define MAXPATH 1000 /* Longest file path we can deal with */
38 char *ret_file
= NULL
;
43 if((lc
= last_cmpnt(filename
)) != NULL
){
46 to_copy
= (lc
- filename
> 1) ? (lc
- filename
- 1) : 1;
47 strncpy(dir
, filename
, MIN(to_copy
, sizeof(dir
)-1));
48 dir
[MIN(to_copy
, sizeof(dir
)-1)] = '\0';
59 /* temp_nam creates ret_file */
60 ret_file
= temp_nam(dirp
, prefix
);
63 * If temp_nam can't write in dirp it puts the file in a temp directory
64 * anyway. We don't want that to happen to us.
66 if(dirp
&& ret_file
&& !in_dir(dirp
, ret_file
)){
68 fs_give((void **)&ret_file
); /* sets it to NULL */
71 if(ret_file
&& ret_dir
&& dirp
)
72 *ret_dir
= cpystr(dirp
);
80 * Returns non-zero if dir is a prefix of path.
81 * zero if dir is not a prefix of path, or if dir is empty.
84 in_dir(char *dir
, char *path
)
86 return(*dir
? !strncmp(dir
, path
, strlen(dir
)) : 0);