1/* On some systems, mkdir ("foo/", 0700) fails because of the trailing
2   slash.  On those systems, this wrapper removes the trailing slash.
3
4   Copyright (C) 2001, 2003 Free Software Foundation, Inc.
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software Foundation,
18   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19#include <sys/cdefs.h>
20__RCSID("$NetBSD: mkdir.c,v 1.2 2016/05/17 14:00:09 christos Exp $");
21
22
23/* written by Jim Meyering */
24
25#ifdef HAVE_CONFIG_H
26# include <config.h>
27#endif
28
29/* Disable the definition of mkdir to rpl_mkdir (from config.h) in this
30   file.  Otherwise, we'd get conflicting prototypes for rpl_mkdir on
31   most systems.  */
32#undef mkdir
33
34#include <sys/types.h>
35#include <sys/stat.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39
40#include "dirname.h"
41#include "xalloc.h"
42
43/* This function is required at least for NetBSD 1.5.2.  */
44
45int
46rpl_mkdir (char const *dir, mode_t mode)
47{
48  int ret_val;
49  char *tmp_dir;
50  size_t len = strlen (dir);
51
52  if (len && dir[len - 1] == '/')
53    {
54      tmp_dir = xstrdup (dir);
55      strip_trailing_slashes (tmp_dir);
56    }
57  else
58    {
59      tmp_dir = (char *) dir;
60    }
61
62  ret_val = mkdir (tmp_dir, mode);
63
64  if (tmp_dir != dir)
65    free (tmp_dir);
66
67  return ret_val;
68}
69