File: file.c

package info (click to toggle)
libast 0.7-9
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,732 kB
  • sloc: ansic: 13,136; sh: 10,332; makefile: 78
file content (93 lines) | stat: -rw-r--r-- 3,545 bytes parent folder | download | duplicates (6)
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
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright (C) 1997-2004, Michael Jennings
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/**
 * @file file.c
 * File manipulation routines.
 *
 * This file contains file-related functions.
 *
 * @author Michael Jennings <mej@eterm.org>
 * $Revision: 1.11 $
 * $Date: 2004/07/23 21:38:39 $
 */

static const char __attribute__((unused)) cvs_ident[] = "$Id: file.c,v 1.11 2004/07/23 21:38:39 mej Exp $";

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <libast_internal.h>

/**
 * Create and open a temporary file.
 *
 * This function creates a temporary file and returns a standard UNIX
 * file descriptor for the new file.  The file is created in $TMPDIR,
 * or $TMP, or /tmp if all else fails.  The libc function @c mkstemp()
 * does the actual file creation/opening.  The umask is set to 0077
 * just before the call, and the permissions are forceably changed to
 * 0600 immediately after.  If the @a len parameter is non-zero, up to
 * @a len characters of the path and filename of the newly-created
 * file are copied into the buffer pointed to by @a ftemplate.  The
 * file descriptor is then returned to the caller.
 *
 * @param ftemplate The initial portion of the filename.  If you want
 *                  the path info returned, make sure this buffer is
 *                  sufficiently large.
 * @param len       The maximum number of bytes @a ftemplate can
 *                  hold.
 * @return          The file descriptor for the new temp file.
 */
int
spiftool_temp_file(spif_charptr_t ftemplate, size_t len)
{
    spif_char_t buff[256];
    int fd;
    mode_t m;

    ASSERT_RVAL(!SPIF_PTR_ISNULL(ftemplate), SPIF_CAST_C(int) -1);
    ASSERT_RVAL(len > 0, SPIF_CAST_C(int) -1);
    if (getenv("TMPDIR")) {
        snprintf(SPIF_CAST_C(char *) buff, sizeof(buff),
                 "%s/%sXXXXXX", getenv("TMPDIR"), ftemplate);
    } else if (getenv("TMP")) {
        snprintf(SPIF_CAST_C(char *) buff, sizeof(buff),
                 "%s/%sXXXXXX", getenv("TMP"), ftemplate);
    } else {
        snprintf(SPIF_CAST_C(char *) buff, sizeof(buff),
                 "/tmp/%sXXXXXX", ftemplate);
    }
    m = umask(0077);
    fd = mkstemp(SPIF_CAST_C(char *) buff);
    umask(m);
    if ((fd < 0) || fchmod(fd, (S_IRUSR | S_IWUSR))) {
        return (-1);
    }

    if (len) {
        spiftool_safe_strncpy(ftemplate, buff, len);
    }
    return (fd);
}