summaryrefslogtreecommitdiff
path: root/common/gdm-common-unknown-origin.c
blob: 3bc8a0581ee865c0500939c3513ad326dfb4dba8 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * (c) 2000 Eazel, Inc.
 * (c) 2001,2002 George Lebl
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <locale.h>
#include <fcntl.h>
#include <sys/types.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>

#include "gdm-common.h"

/* Like fopen with "w" */
FILE *
gdm_safe_fopen_w (const char *file,
                  mode_t      perm)
{
        int fd;
        FILE *ret;
        VE_IGNORE_EINTR (g_unlink (file));
        do {
                int flags;

                errno = 0;
                flags = O_EXCL | O_CREAT | O_TRUNC | O_WRONLY;
#ifdef O_NOCTTY
                flags |= O_NOCTTY;
#endif
#ifdef O_NOFOLLOW
                flags |= O_NOFOLLOW;
#endif

                fd = g_open (file, flags, perm);
        } while (errno == EINTR);

        if (fd < 0) {
                return NULL;
        }

        ret = fdopen (fd, "w");
        return ret;
}