(usage): Include one- or two-line synopsis in --help output.
[platform/upstream/coreutils.git] / src / mkfifo.c
1 /* mkfifo -- make fifo's (named pipes)
2    Copyright (C) 1990, 1991, 1995 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
30 #include "system.h"
31 #include "modechange.h"
32 #include "version.h"
33 #include "error.h"
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 void
55 main (argc, argv)
56      int argc;
57      char **argv;
58 {
59   unsigned short newmode;
60   struct mode_change *change;
61   char *symbolic_mode;
62   int errors = 0;
63   int optc;
64
65   program_name = argv[0];
66   symbolic_mode = NULL;
67
68 #ifndef S_ISFIFO
69   error (4, 0, "fifo files not supported");
70 #else
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   if (optind == argc)
95     {
96       error (0, 0, "too few arguments");
97       usage (1);
98     }
99
100   newmode = 0666 & ~umask (0);
101   if (symbolic_mode)
102     {
103       change = mode_compile (symbolic_mode, 0);
104       if (change == MODE_INVALID)
105         error (1, 0, "invalid mode");
106       else if (change == MODE_MEMORY_EXHAUSTED)
107         error (1, 0, "virtual memory exhausted");
108       newmode = mode_adjust (newmode, change);
109     }
110
111   for (; optind < argc; ++optind)
112     {
113       if (mkfifo (argv[optind], newmode))
114         {
115           error (0, errno, "cannot make fifo `%s'", argv[optind]);
116           errors = 1;
117         }
118     }
119
120   exit (errors);
121 #endif
122 }
123
124 #ifdef S_ISFIFO
125 static void
126 usage (status)
127      int status;
128 {
129   if (status != 0)
130     fprintf (stderr, "Try `%s --help' for more information.\n",
131              program_name);
132   else
133     {
134       printf ("Usage: %s [OPTION] PATH...\n", program_name);
135       printf ("\
136 \n\
137   -m, --mode=MODE   set permission mode (as in chmod), not 0666 - umask\n\
138       --help        display this help and exit\n\
139       --version     output version information and exit\n");
140     }
141   exit (status);
142 }
143 #endif