summaryrefslogtreecommitdiff
path: root/lib/creat.c
blob: 96a3d661a05503689e634339f8520ddadabc9e67 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* Create a file.
   Copyright (C) 2007-2021 Free Software Foundation, Inc.

   This file is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation; either version 2.1 of the
   License, or (at your option) any later version.

   This file 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 Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

/* If the user's config.h happens to include <fcntl.h>, let it include only
   the system's <fcntl.h> here, so that orig_creat doesn't recurse to
   rpl_creat.  */
#define __need_system_fcntl_h
#include <config.h>

/* Get the original definition of creat.  It might be defined as a macro.  */
#include <fcntl.h>
#include <sys/types.h>
#undef __need_system_fcntl_h

static int
orig_creat (const char *filename, mode_t mode)
{
#if defined _WIN32 && !defined __CYGWIN__
  return _creat (filename, mode);
#else
  return creat (filename, mode);
#endif
}

/* Specification.  */
/* Write "fcntl.h" here, not <fcntl.h>, otherwise OSF/1 5.1 DTK cc eliminates
   this include because of the preliminary #include <fcntl.h> above.  */
#include "fcntl.h"

#include <errno.h>
#include <string.h>
#include <sys/types.h>

int
creat (const char *filename, mode_t mode)
{
#if OPEN_TRAILING_SLASH_BUG
  /* Fail if the filename ends in a slash,
     as POSIX says such a filename must name a directory
     <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>:
       "A pathname that contains at least one non-<slash> character and that
        ends with one or more trailing <slash> characters shall not be resolved
        successfully unless the last pathname component before the trailing
        <slash> characters names an existing directory"
     creat() is defined as being equivalent to open() with flags
     O_CREAT | O_TRUNC | O_WRONLY.  Therefore:
     If the named file already exists as a directory, then creat() must fail
     with errno = EISDIR.
     If the named file does not exist or does not name a directory, then
     creat() must fail since creat() cannot create directories.  */
  {
    size_t len = strlen (filename);
    if (len > 0 && filename[len - 1] == '/')
      {
        errno = EISDIR;
        return -1;
      }
  }
#endif

#if defined _WIN32 && !defined __CYGWIN__
  /* Remap the 'x' bits to the 'r' bits.  */
  mode = (mode & ~0111) | ((mode & 0111) << 2);
#endif

  return orig_creat (filename, mode);
}