home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / fileutils-3.6 / src / mknod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-14  |  3.9 KB  |  167 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. #include "version.h"
  36.  
  37. void error ();
  38.  
  39. static void usage ();
  40.  
  41. /* The name this program was run with. */
  42. char *program_name;
  43.  
  44. /* If non-zero, display usage information and exit.  */
  45. static int flag_help;
  46.  
  47. /* If non-zero, print the version on standard error.  */
  48. static int flag_version;
  49.  
  50. static struct option const longopts[] =
  51. {
  52.   {"mode", required_argument, NULL, 'm'},
  53.   {"help", no_argument, &flag_help, 1},
  54.   {"version", no_argument, &flag_version, 1},
  55.   {NULL, 0, NULL, 0}
  56. };
  57.  
  58. void
  59. main (argc, argv)
  60.      int argc;
  61.      char **argv;
  62. {
  63.   unsigned short newmode;
  64.   struct mode_change *change;
  65.   char *symbolic_mode;
  66.   int optc;
  67.  
  68.   program_name = argv[0];
  69.   symbolic_mode = NULL;
  70.  
  71.   while ((optc = getopt_long (argc, argv, "m:", longopts, (int *) 0)) != EOF)
  72.     {
  73.       switch (optc)
  74.     {
  75.     case 0:
  76.       break;
  77.     case 'm':
  78.       symbolic_mode = optarg;
  79.       break;
  80.     default:
  81.       usage ();
  82.     }
  83.     }
  84.  
  85.   if (flag_version)
  86.     {
  87.       fprintf (stderr, "%s\n", version_string);
  88.       exit (0);
  89.     }
  90.  
  91.   if (flag_help)
  92.     usage ();
  93.  
  94.   newmode = 0666 & ~umask (0);
  95.   if (symbolic_mode)
  96.     {
  97.       change = mode_compile (symbolic_mode, 0);
  98.       if (change == MODE_INVALID)
  99.     error (1, 0, "invalid mode");
  100.       else if (change == MODE_MEMORY_EXHAUSTED)
  101.     error (1, 0, "virtual memory exhausted");
  102.       newmode = mode_adjust (newmode, change);
  103.     }
  104.  
  105.   if (argc - optind != 2 && argc - optind != 4)
  106.     usage ();
  107.  
  108.   /* Only check the first character, to allow mnemonic usage like
  109.      `mknod /dev/rst0 character 18 0'. */
  110.  
  111.   switch (argv[optind + 1][0])
  112.     {
  113.     case 'b':            /* `block' or `buffered' */
  114. #ifndef S_IFBLK
  115.       error (4, 0, "block special files not supported");
  116. #else
  117.       if (argc - optind != 4)
  118.     usage ();
  119.       if (mknod (argv[optind], newmode | S_IFBLK,
  120.          makedev (atoi (argv[optind + 2]), atoi (argv[optind + 3]))))
  121.     error (1, errno, "%s", argv[optind]);
  122. #endif
  123.       break;
  124.  
  125.     case 'c':            /* `character' */
  126.     case 'u':            /* `unbuffered' */
  127. #ifndef S_IFCHR
  128.       error (4, 0, "character special files not supported");
  129. #else
  130.       if (argc - optind != 4)
  131.     usage ();
  132.       if (mknod (argv[optind], newmode | S_IFCHR,
  133.          makedev (atoi (argv[optind + 2]), atoi (argv[optind + 3]))))
  134.     error (1, errno, "%s", argv[optind]);
  135. #endif
  136.       break;
  137.  
  138.     case 'p':            /* `pipe' */
  139. #ifndef S_ISFIFO
  140.       error (4, 0, "fifo files not supported");
  141. #else
  142.       if (argc - optind != 2)
  143.     usage ();
  144.       if (mkfifo (argv[optind], newmode))
  145.     error (1, errno, "%s", argv[optind]);
  146. #endif
  147.       break;
  148.  
  149.     default:
  150.       usage ();
  151.     }
  152.  
  153.   exit (0);
  154. }
  155.  
  156. static void
  157. usage ()
  158. {
  159.   fprintf (stderr, "\
  160. Usage: %s [options] path {bcu} major minor\n\
  161.        %s [options] path p\n\
  162. Options:\n\
  163.        [-m mode] [--mode=mode] [--help] [--version]\n",
  164.        program_name, program_name);
  165.   exit (1);
  166. }
  167.