home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / useful / dist / gnu / fileutils / fileutils-3.9-amiga / src / mkfifo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-13  |  3.4 KB  |  150 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. #ifdef HAVE_CONFIG_H
  26. #if defined (CONFIG_BROKETS)
  27. /* We use <config.h> instead of "config.h" so that a compilation
  28.    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
  29.    (which it would do because it found this file in $srcdir).  */
  30. #include <config.h>
  31. #else
  32. #include "config.h"
  33. #endif
  34. #endif
  35.  
  36. #include <stdio.h>
  37. #include <getopt.h>
  38. #include <sys/types.h>
  39. #include "system.h"
  40. #include "modechange.h"
  41. #include "version.h"
  42.  
  43. void error ();
  44.  
  45. static void usage ();
  46.  
  47. /* The name this program was run with. */
  48. char *program_name;
  49.  
  50. /* If non-zero, display usage information and exit.  */
  51. static int show_help;
  52.  
  53. /* If non-zero, print the version on standard output and exit.  */
  54. static int show_version;
  55.  
  56. static struct option const longopts[] =
  57. {
  58.   {"mode", required_argument, NULL, 'm'},
  59.   {"help", no_argument, &show_help, 1},
  60.   {"version", no_argument, &show_version, 1},
  61.   {NULL, 0, NULL, 0}
  62. };
  63.  
  64. main (argc, argv)
  65.      int argc;
  66.      char **argv;
  67. {
  68.   unsigned short newmode;
  69.   struct mode_change *change;
  70.   char *symbolic_mode;
  71.   int errors = 0;
  72.   int optc;
  73.  
  74.   program_name = argv[0];
  75.   symbolic_mode = NULL;
  76.  
  77. #ifndef S_ISFIFO
  78.   error (4, 0, "fifo files not supported");
  79. #else
  80.   while ((optc = getopt_long (argc, argv, "m:", longopts, (int *) 0)) != EOF)
  81.     {
  82.       switch (optc)
  83.     {
  84.     case 0:
  85.       break;
  86.     case 'm':
  87.       symbolic_mode = optarg;
  88.       break;
  89.     default:
  90.       usage (1);
  91.     }
  92.     }
  93.  
  94.   if (show_version)
  95.     {
  96.       printf ("%s\n", version_string);
  97.       exit (0);
  98.     }
  99.  
  100.   if (show_help)
  101.     usage (0);
  102.  
  103.   if (optind == argc)
  104.     usage (1);
  105.  
  106.   newmode = 0666 & ~umask (0);
  107.   if (symbolic_mode)
  108.     {
  109.       change = mode_compile (symbolic_mode, 0);
  110.       if (change == MODE_INVALID)
  111.     error (1, 0, "invalid mode");
  112.       else if (change == MODE_MEMORY_EXHAUSTED)
  113.     error (1, 0, "virtual memory exhausted");
  114.       newmode = mode_adjust (newmode, change);
  115.     }
  116.  
  117.   for (; optind < argc; ++optind)
  118.     {
  119.       if (mkfifo (argv[optind], newmode))
  120.     {
  121.       error (0, errno, "cannot make fifo `%s'", argv[optind]);
  122.       errors = 1;
  123.     }
  124.     }
  125.  
  126.   exit (errors);
  127. #endif
  128. }
  129.  
  130. #ifdef S_ISFIFO
  131. static void
  132. usage (status)
  133.      int status;
  134. {
  135.   if (status != 0)
  136.     fprintf (stderr, "Try `%s --help' for more information.\n",
  137.          program_name);
  138.   else
  139.     {
  140.       printf ("Usage: %s [OPTION] PATH...\n", program_name);
  141.       printf ("\
  142. \n\
  143.   -m, --mode MODE   set permission mode (as in chmod), not 0666 - umask\n\
  144.       --help        display this help and exit\n\
  145.       --version     output version information and exit\n");
  146.     }
  147.   exit (status);
  148. }
  149. #endif
  150.