* SMIME: Change the default signature digest from sha1 to sha-256,
[alpine.git] / pith / tempfile.c
blobd5e3545a0b6b1473c2f0712618d730241bf2186b
1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: tempfile.c 770 2007-10-24 00:23:09Z hubert@u.washington.edu $";
3 #endif
5 /*
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.
30 char *
31 tempfile_in_same_dir(char *filename, char *prefix, char **ret_dir)
33 #ifndef MAXPATH
34 #define MAXPATH 1000 /* Longest file path we can deal with */
35 #endif
36 char dir[MAXPATH+1];
37 char *dirp = NULL;
38 char *ret_file = NULL;
40 if(filename){
41 char *lc;
43 if((lc = last_cmpnt(filename)) != NULL){
44 int to_copy;
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';
50 else{
51 dir[0] = '.';
52 dir[1] = '\0';
55 dirp = dir;
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)){
67 our_unlink(ret_file);
68 fs_give((void **)&ret_file); /* sets it to NULL */
71 if(ret_file && ret_dir && dirp)
72 *ret_dir = cpystr(dirp);
75 return(ret_file);
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.
83 int
84 in_dir(char *dir, char *path)
86 return(*dir ? !strncmp(dir, path, strlen(dir)) : 0);