home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / useful / dist / gnu / fileutils / fileutils-3.9-amiga / src / mknod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-13  |  4.7 KB  |  190 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. #ifdef HAVE_CONFIG_H
  31. #if defined (CONFIG_BROKETS)
  32. /* We use <config.h> instead of "config.h" so that a compilation
  33.    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
  34.    (which it would do because it found this file in $srcdir).  */
  35. #include <config.h>
  36. #else
  37. #include "config.h"
  38. #endif
  39. #endif
  40.  
  41. #include <stdio.h>
  42. #include <getopt.h>
  43. #include <sys/types.h>
  44. #include "system.h"
  45. #include "modechange.h"
  46. #include "version.h"
  47.  
  48. void error ();
  49.  
  50. static void usage ();
  51.  
  52. /* The name this program was run with. */
  53. char *program_name;
  54.  
  55. /* If non-zero, display usage information and exit.  */
  56. static int show_help;
  57.  
  58. /* If non-zero, print the version on standard output and exit.  */
  59. static int show_version;
  60.  
  61. static struct option const longopts[] =
  62. {
  63.   {"mode", required_argument, NULL, 'm'},
  64.   {"help", no_argument, &show_help, 1},
  65.   {"version", no_argument, &show_version, 1},
  66.   {NULL, 0, NULL, 0}
  67. };
  68.  
  69. main (argc, argv)
  70.      int argc;
  71.      char **argv;
  72. {
  73.   unsigned short newmode;
  74.   struct mode_change *change;
  75.   char *symbolic_mode;
  76.   int optc;
  77.  
  78.   program_name = argv[0];
  79.   symbolic_mode = NULL;
  80.  
  81.   while ((optc = getopt_long (argc, argv, "m:", longopts, (int *) 0)) != EOF)
  82.     {
  83.       switch (optc)
  84.     {
  85.     case 0:
  86.       break;
  87.     case 'm':
  88.       symbolic_mode = optarg;
  89.       break;
  90.     default:
  91.       usage (1);
  92.     }
  93.     }
  94.  
  95.   if (show_version)
  96.     {
  97.       printf ("%s\n", version_string);
  98.       exit (0);
  99.     }
  100.  
  101.   if (show_help)
  102.     usage (0);
  103.  
  104.   newmode = 0666 & ~umask (0);
  105.   if (symbolic_mode)
  106.     {
  107.       change = mode_compile (symbolic_mode, 0);
  108.       if (change == MODE_INVALID)
  109.     error (1, 0, "invalid mode");
  110.       else if (change == MODE_MEMORY_EXHAUSTED)
  111.     error (1, 0, "virtual memory exhausted");
  112.       newmode = mode_adjust (newmode, change);
  113.     }
  114.  
  115.   if (argc - optind != 2 && argc - optind != 4)
  116.     usage (1);
  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.     usage (1);
  129.       if (mknod (argv[optind], newmode | S_IFBLK,
  130.          makedev (atoi (argv[optind + 2]), atoi (argv[optind + 3]))))
  131.     error (1, errno, "%s", argv[optind]);
  132. #endif
  133.       break;
  134.  
  135.     case 'c':            /* `character' */
  136.     case 'u':            /* `unbuffered' */
  137. #ifndef S_IFCHR
  138.       error (4, 0, "character special files not supported");
  139. #else
  140.       if (argc - optind != 4)
  141.     usage (1);
  142.       if (mknod (argv[optind], newmode | S_IFCHR,
  143.          makedev (atoi (argv[optind + 2]), atoi (argv[optind + 3]))))
  144.     error (1, errno, "%s", argv[optind]);
  145. #endif
  146.       break;
  147.  
  148.     case 'p':            /* `pipe' */
  149. #ifndef S_ISFIFO
  150.       error (4, 0, "fifo files not supported");
  151. #else
  152.       if (argc - optind != 2)
  153.     usage (1);
  154.       if (mkfifo (argv[optind], newmode))
  155.     error (1, errno, "%s", argv[optind]);
  156. #endif
  157.       break;
  158.  
  159.     default:
  160.       usage (1);
  161.     }
  162.  
  163.   exit (0);
  164. }
  165.  
  166. static void
  167. usage (status)
  168.      int status;
  169. {
  170.   if (status != 0)
  171.     fprintf (stderr, "Try `%s --help' for more information.\n",
  172.          program_name);
  173.   else
  174.     {
  175.       printf ("Usage: %s [OPTION]... PATH TYPE [MAJOR MINOR]\n", program_name);
  176.       printf ("\
  177. \n\
  178.   -m, --mode MODE   set permission mode (as in chmod), not 0666 - umask\n\
  179.       --help        display this help and exit\n\
  180.       --version     output version information and exit\n\
  181. \n\
  182. MAJOR MINOR are forbidden for TYPE p, mandatory otherwise.  TYPE may be:\n\
  183. \n\
  184.   b      create a block (buffered) special file\n\
  185.   c, u   create a character (unbuffered) special file   \n\
  186.   p      create a FIFO\n");
  187.     }
  188.   exit (status);
  189. }
  190.