home *** CD-ROM | disk | FTP | other *** search
- /* mkdir -- make directories
- Copyright (C) 1990 Free Software Foundation, Inc.
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 1, or (at your option)
- any later version.
-
- This program 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 General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
-
- /*
- * MS-DOS port (c) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet
- *
- * To this port, the same copying conditions apply as to the
- * original release.
- *
- * IMPORTANT:
- * This file is not identical to the original GNU release!
- * You should have received this code as patch to the official
- * GNU release.
- *
- * MORE IMPORTANT:
- * This port comes with ABSOLUTELY NO WARRANTY.
- *
- * $Header: e:/gnu/fileutil/RCS/mkdir.c'v 1.3.0.2 90/06/29 00:46:57 tho Stable $
- */
-
- /* Usage: mkdir [-p] [-m mode] [+path] [+mode mode] dir...
-
- Options:
- -p, +path Ensure that the given path(s) exist:
- Make any missing parent directories for each argument.
- Parent dirs default to umask modified by `u+wx'.
- Do not consider an argument directory that already
- exists to be an error.
- -m, +mode mode Set the mode of created directories to `mode', which is
- symbolic as in chmod and uses the umask as a point of
- departure.
-
- David MacKenzie <djm@ai.mit.edu> */
-
- #include <stdio.h>
- #include <sys/types.h>
- #include "system.h"
- #include "getopt.h"
-
- #ifdef MSDOS
- #include <direct.h>
- extern void main (int argc, char **argv);
- extern void error (int status, int errnum, char *message, ...);
- static int make_path (char *path, unsigned short mode,\
- unsigned short parent_mode);
- static void strip_trailing_slashes (char *path);
- static void usage (void);
- #else /* not MSDOS */
- #include "modechange.h"
- #endif /* not MSDOS */
-
- #ifdef STDC_HEADERS
- #include <errno.h>
- #include <stdlib.h>
- #else
- extern int errno;
- #endif
-
- int make_path ();
- void error ();
- void strip_trailing_slashes ();
- void usage ();
-
- /* If nonzero, ensure that a path exists. */
- int path_mode;
-
- /* The name this program was run with. */
- char *program_name;
-
- struct option longopts[] =
- {
- #ifndef MSDOS
- {"mode", 1, NULL, 'm'},
- #endif /* not MSDOS */
- {"path", 0, &path_mode, 1},
- {NULL, 0, NULL, 0}
- };
-
- void
- main (argc, argv)
- int argc;
- char **argv;
- {
- unsigned short newmode;
- unsigned short parent_mode;
- #ifndef MSDOS
- struct modechange *change;
- #endif /* not MSDOS */
- char *symbolic_mode;
- int errors = 0;
- int optc;
- int ind;
-
- program_name = argv[0];
- #ifdef MSDOS
- strlwr (program_name);
- #endif /* MSDOS */
- path_mode = 0;
- symbolic_mode = NULL;
-
- #ifdef MSDOS
- while ((optc = getopt_long (argc, argv, "p", longopts, &ind)) != EOF)
- #else /* not MSDOS */
- while ((optc = getopt_long (argc, argv, "pm:", longopts, &ind)) != EOF)
- #endif /* not MSDOS */
- {
- if (optc == 0 && longopts[ind].flag == 0)
- optc = longopts[ind].val;
- switch (optc)
- {
- case 0: /* Long option. */
- break;
- case 'p':
- path_mode = 1;
- break;
- #ifndef MSDOS
- case 'm':
- symbolic_mode = optarg;
- break;
- default:
- #endif /* not MSDOS */
- usage ();
- }
- }
-
- if (optind == argc)
- usage ();
-
- #ifndef MSDOS
- newmode = 0777 & ~umask (0);
- parent_mode = newmode | 0300; /* u+wx */
- if (symbolic_mode)
- {
- change = mode_compile (symbolic_mode, MODE_MASK_EQUALS | MODE_MASK_PLUS);
- if (change == MODE_INVALID)
- error (1, 0, "invalid mode");
- else if (change == MODE_MEMORY_EXHAUSTED)
- error (1, 0, "virtual memory exhausted");
- newmode = mode_adjust (newmode, change);
- }
- #endif /* not MSDOS */
-
- for (; optind < argc; ++optind)
- {
- #ifdef MSDOS
- strlwr (argv[optind]);
- #endif /* MSDOS */
- strip_trailing_slashes (argv[optind]);
- if (path_mode)
- errors |= make_path (argv[optind], newmode, parent_mode);
- #ifdef MSDOS
- else if (mkdir (argv[optind]) == -1)
- #else /* not MSDOS */
- else if (mkdir (argv[optind], newmode))
- #endif /* not MSDOS */
- {
- error (0, errno, "cannot make directory `%s'", argv[optind]);
- errors = 1;
- }
- }
-
- exit (errors);
- }
-
- /* Make sure directory `path' and all leading directories exist,
- and give it permission mode `mode'.
- If any leading directories are created, give them permission
- mode `parent_mode'.
- Return 0 if successful, 1 if errors occur. */
-
- int
- make_path (path, mode, parent_mode)
- char *path;
- unsigned short mode;
- unsigned short parent_mode;
- {
- char *slash;
- struct stat stats;
-
- if (stat (path, &stats))
- {
- slash = path;
- while (*slash == '/')
- slash++;
- while (slash = index (slash, '/'))
- {
- *slash = 0;
- if (stat (path, &stats))
- {
- #ifdef MSDOS
- if (mkdir (path) == -1)
- #else /* not MSDOS */
- if (mkdir (path, parent_mode))
- #endif /* not MSDOS */
- {
- error (0, errno, "cannot make directory `%s'", path);
- return 1;
- }
- }
- else if ((stats.st_mode & S_IFMT) != S_IFDIR)
- {
- error (0, 0, "`%s' is not a directory", path);
- return 1;
- }
- *slash++ = '/';
- }
-
- #ifdef MSDOS
- if (mkdir (path) == -1)
- #else /* not MSDOS */
- if (mkdir (path, mode))
- #endif /* not MSDOS */
- {
- error (0, errno, "cannot make directory `%s'", path);
- return 1;
- }
- }
- else if ((stats.st_mode & S_IFMT) != S_IFDIR)
- {
- error (0, 0, "`%s' is not a directory", path);
- return 1;
- }
- #ifndef MSDOS
- else if (chmod (path, mode))
- {
- error (0, errno, "cannot change mode of `%s'", path);
- return 1;
- }
- #endif /* not MSDOS */
- return 0;
- }
-
- /* Remove trailing slashes from PATH; they cause some system calls to fail. */
-
- void
- strip_trailing_slashes (path)
- char *path;
- {
- int last;
-
- last = strlen (path) - 1;
- while (last > 0 && path[last] == '/')
- path[last--] = '\0';
- }
-
- void
- usage ()
- {
-
- #ifdef MSDOS
- fprintf (stderr, "Usage: %s [-p] [+path] dir...\n", program_name);
- #else /* not MSDOS */
- fprintf (stderr, "\
- Usage: %s [-p] [-m mode] [+path] [+mode mode] dir...\n",
- program_name);
- #endif /* not MSDOS */
- exit (1);
- }
-