xref: /original-bsd/usr.bin/mkfifo/mkfifo.c (revision 84bbc80e)
1 /*
2  * Copyright (c) 1990 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1990 Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)mkfifo.c	5.2 (Berkeley) 05/15/90";
26 #endif /* not lint */
27 
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <string.h>
33 
34 main(argc, argv)
35 	int argc;
36 	char **argv;
37 {
38 	extern int errno, optind;
39 	int ch, exitval, pflag;
40 
41 	pflag = 0;
42 	while ((ch = getopt(argc, argv, "p")) != EOF)
43 		switch(ch) {
44 		case 'p':
45 			pflag = 1;
46 			break;
47 		case '?':
48 		default:
49 			usage();
50 		}
51 
52 	if (!*(argv += optind))
53 		usage();
54 
55 	for (exitval = 0; *argv; ++argv) {
56 		if (pflag && build(*argv)) {
57 			exitval |= 1;
58 			continue;
59 		}
60 		if (mkfifo(*argv, 0777) < 0) {
61 			(void)fprintf(stderr, "mkfifo: %s: %s\n",
62 			    *argv, strerror(errno));
63 			exitval |= 1;
64 		}
65 	}
66 	exit(exitval);
67 }
68 
69 build(path)
70 	char *path;
71 {
72 	register char *p;
73 	struct stat sb;
74 
75 	for (p = path; *p; p++) {
76 		if (*p  != '/')
77 			continue;
78 		if (stat(path, &sb)) {
79 			if (errno != ENOENT || mkdir(path, 0777) < 0) {
80 				(void)fprintf(stderr, "mkdir: %s: %s\n",
81 				    path, strerror(errno));
82 				return(1);
83 			}
84 		}
85 		*p = '/';
86 	}
87 	return(0);
88 }
89 
90 usage()
91 {
92 	(void)fprintf(stderr, "usage: mkfifo [-p] fifoname ...\n");
93 	exit(1);
94 }
95