Back to index

glibc  2.9
mkdir.c
Go to the documentation of this file.
00001 /* Copyright (C) 1992, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <stddef.h>
00021 #include <sys/stat.h>
00022 #include <sys/types.h>
00023 #include <stdlib.h>
00024 #include <sys/wait.h>
00025 #include <string.h>
00026 
00027 /* Create a directory named PATH with protections MODE.  */
00028 int
00029 __mkdir (path, mode)
00030      const char *path;
00031      mode_t mode;
00032 {
00033   char *cmd = __alloca (80 + strlen (path));
00034   char *p;
00035   int status;
00036   mode_t mask;
00037   int save;
00038   struct stat statbuf;
00039 
00040   if (path == NULL)
00041     {
00042       __set_errno (EINVAL);
00043       return -1;
00044     }
00045 
00046   /* Check for some errors.  */
00047   if (__stat (path, &statbuf) < 0)
00048     {
00049       if (errno != ENOENT)
00050        return -1;
00051       /* There is no file by that name.  Good.  */
00052     }
00053   else
00054     {
00055       __set_errno (EEXIST);
00056       return -1;
00057     }
00058 
00059   /* Race condition, but how else to do it?  */
00060   mask = __umask (0777);
00061   (void) __umask (mask);
00062 
00063   p = cmd;
00064   *p++ = 'm';
00065   *p++ = 'k';
00066   *p++ = 'd';
00067   *p++ = 'i';
00068   *p++ = 'r';
00069   *p++ = ' ';
00070 
00071   mode &= ~mask;
00072   *p++ = '-';
00073   *p++ = 'm';
00074   *p++ = ' ';
00075   *p++ = ((mode & 07000) >> 9) + '0';
00076   *p++ = ((mode & 0700) >> 6) + '0';
00077   *p++ = ((mode & 070) >> 3) + '0';
00078   *p++ = ((mode & 07)) + '0';
00079   *p++ = ' ';
00080 
00081   strcpy (p, path);
00082 
00083   save = errno;
00084   /* If system doesn't set errno, but the mkdir fails, we really
00085      have no idea what went wrong.  EIO is the vaguest error I
00086      can think of, so I'll use that.  */
00087   __set_errno (EIO);
00088   status = system (cmd);
00089   if (WIFEXITED (status) && WEXITSTATUS (status) == 0)
00090     {
00091       __set_errno (save);
00092       return 0;
00093     }
00094   else
00095     return -1;
00096 }
00097 
00098 weak_alias (__mkdir, mkdir)