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

  1. /* mknod -- make special files
  2.    Copyright (C) 1990, 1991 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. /* Usage: mknod [-m mode] [--mode=mode] path {bcu} major minor
  19.         make a block or character device node
  20.           mknod [-m mode] [--mode=mode] path p
  21.         make a FIFO (named pipe)
  22.  
  23.    Options:
  24.    -m, --mode=mode    Set the mode of created nodes 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. void error ();
  37. void strip_trailing_slashes ();
  38. void usage ();
  39.  
  40. /* The name this program was run with. */
  41. char *program_name;
  42.  
  43. struct option longopts[] =
  44. {
  45.   {"mode", 1, NULL, 'm'},
  46.   {NULL, 0, NULL, 0}
  47. };
  48.  
  49. void
  50. main (argc, argv)
  51.      int argc;
  52.      char **argv;
  53. {
  54.   unsigned short newmode;
  55.   struct mode_change *change;
  56.   char *symbolic_mode;
  57.   int optc;
  58.  
  59.   program_name = argv[0];
  60.   symbolic_mode = NULL;
  61.  
  62.   while ((optc = getopt_long (argc, argv, "m:", longopts, (int *) 0)) != EOF)
  63.     {
  64.       switch (optc)
  65.     {
  66.     case 'm':
  67.       symbolic_mode = optarg;
  68.       break;
  69.     default:
  70.       usage ();
  71.     }
  72.     }
  73.  
  74.   newmode = 0666 & ~umask (0);
  75.   if (symbolic_mode)
  76.     {
  77.       change = mode_compile (symbolic_mode, 0);
  78.       if (change == MODE_INVALID)
  79.     error (1, 0, "invalid mode");
  80.       else if (change == MODE_MEMORY_EXHAUSTED)
  81.     error (1, 0, "virtual memory exhausted");
  82.       newmode = mode_adjust (newmode, change);
  83.     }
  84.  
  85.   if (argc - optind != 2 && argc - optind != 4)
  86.     usage ();
  87.  
  88.   strip_trailing_slashes (argv[optind]);
  89.  
  90.   /* Only check the first character, to allow mnemonic usage like
  91.      `mknod /dev/rst0 character 18 0'. */
  92.  
  93.   switch (argv[optind + 1][0])
  94.     {
  95.     case 'b':            /* `block' or `buffered' */
  96. #ifndef S_IFBLK
  97.       error (4, 0, "block special files not supported");
  98. #else
  99.       if (argc - optind != 4)
  100.     usage ();
  101.       if (mknod (argv[optind], newmode | S_IFBLK,
  102.          makedev (atoi (argv[optind + 2]), atoi (argv[optind + 3]))))
  103.     error (1, errno, "%s", argv[optind]);
  104. #endif
  105.       break;
  106.  
  107.     case 'c':            /* `character' */
  108.     case 'u':            /* `unbuffered' */
  109. #ifndef S_IFCHR
  110.       error (4, 0, "character special files not supported");
  111. #else
  112.       if (argc - optind != 4)
  113.     usage ();
  114.       if (mknod (argv[optind], newmode | S_IFCHR,
  115.          makedev (atoi (argv[optind + 2]), atoi (argv[optind + 3]))))
  116.     error (1, errno, "%s", argv[optind]);
  117. #endif
  118.       break;
  119.  
  120.     case 'p':            /* `pipe' */
  121. #ifndef S_ISFIFO
  122.       error (4, 0, "fifo files not supported");
  123. #else
  124.       if (argc - optind != 2)
  125.     usage ();
  126.       if (mkfifo (argv[optind], newmode))
  127.     error (1, errno, "%s", argv[optind]);
  128. #endif
  129.       break;
  130.  
  131.     default:
  132.       usage ();
  133.     }
  134.  
  135.   exit (0);
  136. }
  137.  
  138. void
  139. usage ()
  140. {
  141.   fprintf (stderr, "\
  142. Usage: %s [-m mode] [--mode=mode] path {bcu} major minor\n\
  143.        %s [-m mode] [--mode=mode] path p\n",
  144.        program_name, program_name);
  145.   exit (1);
  146. }
  147.