home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / gnu / fileutils-3.12-src.lha / fileutils-3.12 / src / mknod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-12  |  5.0 KB  |  204 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. 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 (1);
  82.     }
  83.     }
  84.  
  85.   if (show_version)
  86.     {
  87.       printf ("%s\n", version_string);
  88.       exit (0);
  89.     }
  90.  
  91.   if (show_help)
  92.     usage (0);
  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.     {
  107.       const char *msg;
  108.       if (argc - optind < 2)
  109.     msg = "too few arguments";
  110.       else if (argc - optind > 4)
  111.     msg = "too many arguments";
  112.       else
  113.     msg = "wrong number of arguments";
  114.       error (0, 0, msg);
  115.       usage (1);
  116.     }
  117.  
  118.   /* Only check the first character, to allow mnemonic usage like
  119.      `mknod /dev/rst0 character 18 0'. */
  120.  
  121.   switch (argv[optind + 1][0])
  122.     {
  123.     case 'b':            /* `block' or `buffered' */
  124. #ifndef S_IFBLK
  125.       error (4, 0, "block special files not supported");
  126. #else
  127.       if (argc - optind != 4)
  128.     {
  129.       error (0, 0, "\
  130. when creating block special files, major and minor device\n\
  131. numbers must be specified");
  132.       usage (1);
  133.     }
  134.       if (mknod (argv[optind], newmode | S_IFBLK,
  135.          makedev (atoi (argv[optind + 2]), atoi (argv[optind + 3]))))
  136.     error (1, errno, "%s", argv[optind]);
  137. #endif
  138.       break;
  139.  
  140.     case 'c':            /* `character' */
  141.     case 'u':            /* `unbuffered' */
  142. #ifndef S_IFCHR
  143.       error (4, 0, "character special files not supported");
  144. #else
  145.       if (argc - optind != 4)
  146.     {
  147.       error (0, 0, "\
  148. when creating character special files, major and minor device\n\
  149. numbers must be specified");
  150.       usage (1);
  151.     }
  152.       if (mknod (argv[optind], newmode | S_IFCHR,
  153.          makedev (atoi (argv[optind + 2]), atoi (argv[optind + 3]))))
  154.     error (1, errno, "%s", argv[optind]);
  155. #endif
  156.       break;
  157.  
  158.     case 'p':            /* `pipe' */
  159. #ifndef S_ISFIFO
  160.       error (4, 0, "fifo files not supported");
  161. #else
  162.       if (argc - optind != 2)
  163.     {
  164.       error (0, 0, "\
  165. major and minor device numbers may not be specified for fifo files");
  166.       usage (1);
  167.     }
  168.       if (mkfifo (argv[optind], newmode))
  169.     error (1, errno, "%s", argv[optind]);
  170. #endif
  171.       break;
  172.  
  173.     default:
  174.       usage (1);
  175.     }
  176.  
  177.   exit (0);
  178. }
  179.  
  180. static void
  181. usage (status)
  182.      int status;
  183. {
  184.   if (status != 0)
  185.     fprintf (stderr, "Try `%s --help' for more information.\n",
  186.          program_name);
  187.   else
  188.     {
  189.       printf ("Usage: %s [OPTION]... PATH TYPE [MAJOR MINOR]\n", program_name);
  190.       printf ("\
  191. \n\
  192.   -m, --mode=MODE   set permission mode (as in chmod), not 0666 - umask\n\
  193.       --help        display this help and exit\n\
  194.       --version     output version information and exit\n\
  195. \n\
  196. MAJOR MINOR are forbidden for TYPE p, mandatory otherwise.  TYPE may be:\n\
  197. \n\
  198.   b      create a block (buffered) special file\n\
  199.   c, u   create a character (unbuffered) special file   \n\
  200.   p      create a FIFO\n");
  201.     }
  202.   exit (status);
  203. }
  204.