home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / gnu / fileutils-3.12-src.lha / fileutils-3.12 / src / mkfifo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-12  |  3.2 KB  |  143 lines

  1. /* mkfifo -- make fifo's (named pipes)
  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. /* Options:
  19.    -m, --mode=mode    Set the mode of created fifo's to MODE, which is
  20.             symbolic as in chmod and uses the umask as a point of
  21.             departure.
  22.  
  23.    David MacKenzie <djm@ai.mit.edu>  */
  24.  
  25. #include <config.h>
  26. #include <stdio.h>
  27. #include <getopt.h>
  28. #include <sys/types.h>
  29. #include "system.h"
  30. #include "modechange.h"
  31. #include "version.h"
  32.  
  33. void error ();
  34.  
  35. static void usage ();
  36.  
  37. /* The name this program was run with. */
  38. char *program_name;
  39.  
  40. /* If non-zero, display usage information and exit.  */
  41. static int show_help;
  42.  
  43. /* If non-zero, print the version on standard output and exit.  */
  44. static int show_version;
  45.  
  46. static struct option const longopts[] =
  47. {
  48.   {"mode", required_argument, NULL, 'm'},
  49.   {"help", no_argument, &show_help, 1},
  50.   {"version", no_argument, &show_version, 1},
  51.   {NULL, 0, NULL, 0}
  52. };
  53.  
  54. main (argc, argv)
  55.      int argc;
  56.      char **argv;
  57. {
  58.   unsigned short newmode;
  59.   struct mode_change *change;
  60.   char *symbolic_mode;
  61.   int errors = 0;
  62.   int optc;
  63.  
  64.   program_name = argv[0];
  65.   symbolic_mode = NULL;
  66.  
  67. #ifndef S_ISFIFO
  68.   error (4, 0, "fifo files not supported");
  69. #else
  70.   while ((optc = getopt_long (argc, argv, "m:", longopts, (int *) 0)) != EOF)
  71.     {
  72.       switch (optc)
  73.     {
  74.     case 0:
  75.       break;
  76.     case 'm':
  77.       symbolic_mode = optarg;
  78.       break;
  79.     default:
  80.       usage (1);
  81.     }
  82.     }
  83.  
  84.   if (show_version)
  85.     {
  86.       printf ("%s\n", version_string);
  87.       exit (0);
  88.     }
  89.  
  90.   if (show_help)
  91.     usage (0);
  92.  
  93.   if (optind == argc)
  94.     {
  95.       error (0, 0, "too few arguments");
  96.       usage (1);
  97.     }
  98.  
  99.   newmode = 0666 & ~umask (0);
  100.   if (symbolic_mode)
  101.     {
  102.       change = mode_compile (symbolic_mode, 0);
  103.       if (change == MODE_INVALID)
  104.     error (1, 0, "invalid mode");
  105.       else if (change == MODE_MEMORY_EXHAUSTED)
  106.     error (1, 0, "virtual memory exhausted");
  107.       newmode = mode_adjust (newmode, change);
  108.     }
  109.  
  110.   for (; optind < argc; ++optind)
  111.     {
  112.       if (mkfifo (argv[optind], newmode))
  113.     {
  114.       error (0, errno, "cannot make fifo `%s'", argv[optind]);
  115.       errors = 1;
  116.     }
  117.     }
  118.  
  119.   exit (errors);
  120. #endif
  121. }
  122.  
  123. #ifdef S_ISFIFO
  124. static void
  125. usage (status)
  126.      int status;
  127. {
  128.   if (status != 0)
  129.     fprintf (stderr, "Try `%s --help' for more information.\n",
  130.          program_name);
  131.   else
  132.     {
  133.       printf ("Usage: %s [OPTION] PATH...\n", program_name);
  134.       printf ("\
  135. \n\
  136.   -m, --mode=MODE   set permission mode (as in chmod), not 0666 - umask\n\
  137.       --help        display this help and exit\n\
  138.       --version     output version information and exit\n");
  139.     }
  140.   exit (status);
  141. }
  142. #endif
  143.