Exemple #1
0
static int
c_mknod(char **wp)
{
	int argc, optc, ismkfifo = 0, ret;
	char **argv;
	void *set = NULL;
	mode_t mode = 0, oldmode = 0;

	while ((optc = ksh_getopt(wp, &builtin_opt, "m:")) != -1) {
		switch (optc) {
		case 'm':
			set = setmode(builtin_opt.optarg);
			if (set == NULL) {
				bi_errorf("invalid file mode");
				return 1;
			}
			mode = getmode(set, DEFFILEMODE);
			free(set);
			break;
		default:
			goto usage;
		}
	}
	argv = &wp[builtin_opt.optind];
	if (argv[0] == NULL)
		goto usage;
	for (argc = 0; argv[argc]; argc++)
		;
	if (argc == 2 && argv[1][0] == 'p') {
		ismkfifo = 1;
		argc--;
	} else if (argc != 4)
		goto usage;

	if (set)
		oldmode = umask(0);
	else
		mode = DEFFILEMODE;

	if (ismkfifo)
		ret = domkfifo(argc, argv, mode);
	else
		ret = domknod(argc, argv, mode);

	if (set)
		umask(oldmode);
	return ret;
usage:
	builtin_argv0 = NULL;
	bi_errorf("usage: mknod [-m mode] name b|c major minor");
	bi_errorf("usage: mknod [-m mode] name p");
	return 1;
}
Exemple #2
0
int
main(int argc, char *argv[])
{
	int ch, ismkfifo = 0;
	void *set = NULL;
	mode_t mode = 0;

	setlocale (LC_ALL, "");

	if (strcmp(__progname, "mkfifo") == 0)
		ismkfifo = 1;

	while ((ch = getopt(argc, argv, "m:")) != -1)
		switch(ch) {
		case 'm':
			if (!(set = setmode(optarg))) {
				errx(1, "invalid file mode.");
				/* NOTREACHED */
			}

			/*
			 * In symbolic mode strings, the + and - operators are
			 * interpreted relative to an assumed initial mode of
			 * a=rw.
			 */
			mode = getmode(set, DEFFILEMODE);
			free(set);
			break;
		case '?':
		default:
			usage(ismkfifo);
		}
	argc -= optind;
	argv += optind;

	if (argv[0] == NULL)
		usage(ismkfifo);
	if (!ismkfifo) {
		if (argc == 2 && argv[1][0] == 'p') {
			ismkfifo = 2;
			argc--;
			argv[1] = NULL;
		} else if (argc != 4) {
			usage(ismkfifo);
			/* NOTREACHED */
		}
	}

	/*
	 * If the user specified a mode via `-m', don't allow the umask
	 * to modified it.  If no `-m' flag was specified, the default
	 * mode is the value of the bitwise inclusive or of S_IRUSR,
	 * S_IWUSR, S_IRGRP, S_IWGRP, S_IROTH, and S_IWOTH as modified by
	 * the umask.
	 */
	if (set)
		(void)umask(0);
	else
		mode = DEFFILEMODE;

	if (ismkfifo)
		exit(domkfifo(argv, mode));
	else
		exit(domknod(argv, mode));
}