home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / utility / misc / fileutil.lha / fileutils-3.3 / src / mkdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-02  |  3.0 KB  |  124 lines

  1. /* mkdir -- make directories
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Options:
  19.    -p, --path        Ensure that the given path(s) exist:
  20.             Make any missing parent directories for each argument.
  21.             Parent dirs default to umask modified by `u+wx'.
  22.             Do not consider an argument directory that already
  23.             exists to be an error.
  24.    -m, --mode=mode    Set the mode of created directories to `mode', which is
  25.             symbolic as in chmod and uses the umask as a point of
  26.             departure.
  27.  
  28.    David MacKenzie <djm@ai.mit.edu>  */
  29.  
  30. #include <stdio.h>
  31. #include <getopt.h>
  32. #include <sys/types.h>
  33. #include "system.h"
  34. #include "modechange.h"
  35.  
  36. int make_path ();
  37. void error ();
  38. void strip_trailing_slashes ();
  39. void usage ();
  40.  
  41. /* If nonzero, ensure that a path exists.  */
  42. int path_mode;
  43.  
  44. /* The name this program was run with. */
  45. char *program_name;
  46.  
  47. struct option longopts[] =
  48. {
  49.   {"mode", 1, NULL, 'm'},
  50.   {"path", 0, &path_mode, 1},
  51.   {NULL, 0, NULL, 0}
  52. };
  53.  
  54. void
  55. main (argc, argv)
  56.      int argc;
  57.      char **argv;
  58. {
  59.   unsigned int newmode;
  60.   unsigned int parent_mode;
  61.   char *symbolic_mode = NULL;
  62.   int errors = 0;
  63.   int optc;
  64.  
  65.   program_name = argv[0];
  66.   path_mode = 0;
  67.  
  68.   while ((optc = getopt_long (argc, argv, "pm:", longopts, (int *) 0)) != EOF)
  69.     {
  70.       switch (optc)
  71.     {
  72.     case 0:            /* Long option. */
  73.       break;
  74.     case 'p':
  75.       path_mode = 1;
  76.       break;
  77.     case 'm':
  78.       symbolic_mode = optarg;
  79.       break;
  80.     default:
  81.       usage ();
  82.     }
  83.     }
  84.  
  85.   if (optind == argc)
  86.     usage ();
  87.   
  88.   newmode = 0777 & ~umask (0);
  89.   parent_mode = newmode | 0300;    /* u+wx */
  90.   if (symbolic_mode)
  91.     {
  92.       struct mode_change *change = mode_compile (symbolic_mode, 0);
  93.       if (change == MODE_INVALID)
  94.     error (1, 0, "invalid mode `%s'", symbolic_mode);
  95.       else if (change == MODE_MEMORY_EXHAUSTED)
  96.     error (1, 0, "virtual memory exhausted");
  97.       newmode = mode_adjust (newmode, change);
  98.     }
  99.  
  100.   for (; optind < argc; ++optind)
  101.     {
  102.       strip_trailing_slashes (argv[optind]);
  103.       if (path_mode)
  104.     errors |= make_path (argv[optind], newmode, parent_mode, -1, -1, NULL);
  105.       else if (mkdir (argv[optind], newmode))
  106.     {
  107.       error (0, errno, "cannot make directory `%s'", argv[optind]);
  108.       errors = 1;
  109.     }
  110.     }
  111.  
  112.   exit (errors);
  113. }
  114.  
  115. void
  116. usage ()
  117. {
  118.   fprintf (stderr, "\
  119. Usage: %s [-p] [-m mode] [--path] [--mode=mode] dir...\n",
  120.        program_name);
  121.   exit (1);
  122. }
  123.  
  124.