home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / fileutils-3.6 / src / mkfifo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-14  |  2.8 KB  |  130 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 <stdio.h>
  26. #include <getopt.h>
  27. #include <sys/types.h>
  28. #include "system.h"
  29. #include "modechange.h"
  30. #include "version.h"
  31.  
  32. void error ();
  33.  
  34. static void usage ();
  35.  
  36. /* The name this program was run with. */
  37. char *program_name;
  38.  
  39. /* If non-zero, display usage information and exit.  */
  40. static int flag_help;
  41.  
  42. /* If non-zero, print the version on standard error.  */
  43. static int flag_version;
  44.  
  45. static struct option const longopts[] =
  46. {
  47.   {"mode", required_argument, NULL, 'm'},
  48.   {"help", no_argument, &flag_help, 1},
  49.   {"version", no_argument, &flag_version, 1},
  50.   {NULL, 0, NULL, 0}
  51. };
  52.  
  53. void
  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 ();
  81.     }
  82.     }
  83.  
  84.   if (flag_version)
  85.     {
  86.       fprintf (stderr, "%s\n", version_string);
  87.       exit (0);
  88.     }
  89.  
  90.   if (flag_help)
  91.     usage ();
  92.  
  93.   if (optind == argc)
  94.     usage ();
  95.  
  96.   newmode = 0666 & ~umask (0);
  97.   if (symbolic_mode)
  98.     {
  99.       change = mode_compile (symbolic_mode, 0);
  100.       if (change == MODE_INVALID)
  101.     error (1, 0, "invalid mode");
  102.       else if (change == MODE_MEMORY_EXHAUSTED)
  103.     error (1, 0, "virtual memory exhausted");
  104.       newmode = mode_adjust (newmode, change);
  105.     }
  106.  
  107.   for (; optind < argc; ++optind)
  108.     {
  109.       if (mkfifo (argv[optind], newmode))
  110.     {
  111.       error (0, errno, "cannot make fifo `%s'", argv[optind]);
  112.       errors = 1;
  113.     }
  114.     }
  115.  
  116.   exit (errors);
  117. #endif
  118. }
  119.  
  120. #ifdef S_ISFIFO
  121. static void
  122. usage ()
  123. {
  124.   fprintf (stderr, "\
  125. Usage: %s [-m mode] [--mode=mode] [--help] [--version] path...\n",
  126.        program_name);
  127.   exit (1);
  128. }
  129. #endif
  130.