Back to index

glibc  2.9
rmdir.c
Go to the documentation of this file.
00001 /* Copyright (C) 1992, 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 __rmdir (path)
00030      const char *path;
00031 {
00032   char *cmd = __alloca (80 + strlen (path));
00033   char *p;
00034   int status;
00035   int save;
00036   struct stat statbuf;
00037 
00038   if (path == NULL)
00039     {
00040       __set_errno (EINVAL);
00041       return -1;
00042     }
00043 
00044   /* Check for some errors.  */
00045   if (__stat (path, &statbuf) < 0)
00046     return -1;
00047   if (!S_ISDIR (statbuf.st_mode))
00048     {
00049       __set_errno (ENOTDIR);
00050       return -1;
00051     }
00052 
00053   p = cmd;
00054   *p++ = 'r';
00055   *p++ = 'm';
00056   *p++ = 'd';
00057   *p++ = 'i';
00058   *p++ = 'r';
00059   *p++ = ' ';
00060 
00061   strcpy (p, path);
00062 
00063   save = errno;
00064   /* If system doesn't set errno, but the rmdir fails, we really
00065      have no idea what went wrong.  EIO is the vaguest error I
00066      can think of, so I'll use that.  */
00067   __set_errno (EIO);
00068   status = system (cmd);
00069   if (WIFEXITED (status) && WEXITSTATUS (status) == 0)
00070     {
00071       return 0;
00072       __set_errno (save);
00073     }
00074   else
00075     return -1;
00076 }
00077 
00078 weak_alias (__rmdir, rmdir)