home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / a / bin / fileutil.12 / fileutil / fileutils-3.12 / src / mknod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-07  |  5.0 KB  |  205 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 <config.h>
  31. #include <stdio.h>
  32. #include <getopt.h>
  33. #include <sys/types.h>
  34. #include "system.h"
  35. #include "modechange.h"
  36. #include "version.h"
  37.  
  38. void error ();
  39.  
  40. static void usage ();
  41.  
  42. /* The name this program was run with. */
  43. char *program_name;
  44.  
  45. /* If non-zero, display usage information and exit.  */
  46. static int show_help;
  47.  
  48. /* If non-zero, print the version on standard output and exit.  */
  49. static int show_version;
  50.  
  51. static struct option const longopts[] =
  52. {
  53.   {"mode", required_argument, NULL, 'm'},
  54.   {"help", no_argument, &show_help, 1},
  55.   {"version", no_argument, &show_version, 1},
  56.   {NULL, 0, NULL, 0}
  57. };
  58.  
  59. void
  60. main (argc, argv)
  61.      int argc;
  62.      char **argv;
  63. {
  64.   unsigned short newmode;
  65.   struct mode_change *change;
  66.   char *symbolic_mode;
  67.   int optc;
  68.  
  69.   program_name = argv[0];
  70.   symbolic_mode = NULL;
  71.  
  72.   while ((optc = getopt_long (argc, argv, "m:", longopts, (int *) 0)) != EOF)
  73.     {
  74.       switch (optc)
  75.     {
  76.     case 0:
  77.       break;
  78.     case 'm':
  79.       symbolic_mode = optarg;
  80.       break;
  81.     default:
  82.       usage (1);
  83.     }
  84.     }
  85.  
  86.   if (show_version)
  87.     {
  88.       printf ("%s\n", version_string);
  89.       exit (0);
  90.     }
  91.  
  92.   if (show_help)
  93.     usage (0);
  94.  
  95.   newmode = 0666 & ~umask (0);
  96.   if (symbolic_mode)
  97.     {
  98.       change = mode_compile (symbolic_mode, 0);
  99.       if (change == MODE_INVALID)
  100.     error (1, 0, "invalid mode");
  101.       else if (change == MODE_MEMORY_EXHAUSTED)
  102.     error (1, 0, "virtual memory exhausted");
  103.       newmode = mode_adjust (newmode, change);
  104.     }
  105.  
  106.   if (argc - optind != 2 && argc - optind != 4)
  107.     {
  108.       const char *msg;
  109.       if (argc - optind < 2)
  110.     msg = "too few arguments";
  111.       else if (argc - optind > 4)
  112.     msg = "too many arguments";
  113.       else
  114.     msg = "wrong number of arguments";
  115.       error (0, 0, msg);
  116.       usage (1);
  117.     }
  118.  
  119.   /* Only check the first character, to allow mnemonic usage like
  120.      `mknod /dev/rst0 character 18 0'. */
  121.  
  122.   switch (argv[optind + 1][0])
  123.     {
  124.     case 'b':            /* `block' or `buffered' */
  125. #ifndef S_IFBLK
  126.       error (4, 0, "block special files not supported");
  127. #else
  128.       if (argc - optind != 4)
  129.     {
  130.       error (0, 0, "\
  131. when creating block special files, major and minor device\n\
  132. numbers must be specified");
  133.       usage (1);
  134.     }
  135.       if (mknod (argv[optind], newmode | S_IFBLK,
  136.          makedev (atoi (argv[optind + 2]), atoi (argv[optind + 3]))))
  137.     error (1, errno, "%s", argv[optind]);
  138. #endif
  139.       break;
  140.  
  141.     case 'c':            /* `character' */
  142.     case 'u':            /* `unbuffered' */
  143. #ifndef S_IFCHR
  144.       error (4, 0, "character special files not supported");
  145. #else
  146.       if (argc - optind != 4)
  147.     {
  148.       error (0, 0, "\
  149. when creating character special files, major and minor device\n\
  150. numbers must be specified");
  151.       usage (1);
  152.     }
  153.       if (mknod (argv[optind], newmode | S_IFCHR,
  154.          makedev (atoi (argv[optind + 2]), atoi (argv[optind + 3]))))
  155.     error (1, errno, "%s", argv[optind]);
  156. #endif
  157.       break;
  158.  
  159.     case 'p':            /* `pipe' */
  160. #ifndef S_ISFIFO
  161.       error (4, 0, "fifo files not supported");
  162. #else
  163.       if (argc - optind != 2)
  164.     {
  165.       error (0, 0, "\
  166. major and minor device numbers may not be specified for fifo files");
  167.       usage (1);
  168.     }
  169.       if (mkfifo (argv[optind], newmode))
  170.     error (1, errno, "%s", argv[optind]);
  171. #endif
  172.       break;
  173.  
  174.     default:
  175.       usage (1);
  176.     }
  177.  
  178.   exit (0);
  179. }
  180.  
  181. static void
  182. usage (status)
  183.      int status;
  184. {
  185.   if (status != 0)
  186.     fprintf (stderr, "Try `%s --help' for more information.\n",
  187.          program_name);
  188.   else
  189.     {
  190.       printf ("Usage: %s [OPTION]... PATH TYPE [MAJOR MINOR]\n", program_name);
  191.       printf ("\
  192. \n\
  193.   -m, --mode=MODE   set permission mode (as in chmod), not 0666 - umask\n\
  194.       --help        display this help and exit\n\
  195.       --version     output version information and exit\n\
  196. \n\
  197. MAJOR MINOR are forbidden for TYPE p, mandatory otherwise.  TYPE may be:\n\
  198. \n\
  199.   b      create a block (buffered) special file\n\
  200.   c, u   create a character (unbuffered) special file   \n\
  201.   p      create a FIFO\n");
  202.     }
  203.   exit (status);
  204. }
  205.