File: mktemp.c

package info (click to toggle)
apache2 2.2.16-6%2Bsqueeze12
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 38,424 kB
  • ctags: 20,767
  • sloc: ansic: 227,630; sh: 20,790; perl: 2,191; makefile: 1,244; awk: 1,133; pascal: 517; lex: 191; python: 136; yacc: 100; xml: 36
file content (64 lines) | stat: -rw-r--r-- 2,228 bytes parent folder | download | duplicates (3)
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
/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "apr_private.h"
#include "apr_file_io.h" /* prototype of apr_mkstemp() */
#include "apr_strings.h" /* prototype of apr_mkstemp() */
#include "apr_arch_file_io.h" /* prototype of apr_mkstemp() */
#include "apr_portable.h" /* for apr_os_file_put() */
#include "apr_arch_inherit.h"

#include <stdlib.h> /* for mkstemp() - Single Unix */

APR_DECLARE(apr_status_t) apr_file_mktemp(apr_file_t **fp, char *template, apr_int32_t flags, apr_pool_t *p)
{
    int fd;
    apr_status_t rv;

    flags = (!flags) ? APR_CREATE | APR_READ | APR_WRITE |  
                       APR_DELONCLOSE : flags & ~APR_EXCL;

    fd = mkstemp(template);
    if (fd == -1) {
        return errno;
    }
    /* We need to reopen the file to get rid of the o_excl flag.
     * Otherwise file locking will not allow the file to be shared.
     */
    close(fd);
    if ((rv = apr_file_open(fp, template, flags|APR_FILE_NOCLEANUP,
                            APR_UREAD | APR_UWRITE, p)) == APR_SUCCESS) {


	if (!(flags & APR_FILE_NOCLEANUP)) {
            int flags;

            if ((flags = fcntl((*fp)->filedes, F_GETFD)) == -1)
                return errno;

            flags |= FD_CLOEXEC;
            if (fcntl((*fp)->filedes, F_SETFD, flags) == -1)
                return errno;

	    apr_pool_cleanup_register((*fp)->pool, (void *)(*fp),
				      apr_unix_file_cleanup,
				      apr_unix_child_file_cleanup);
	}
    }

    return rv;
}