Beispiel #1
0
int bb_test(int argc, char **argv)
{
	int res;

	if (LONE_CHAR(argv[0], '[')) {
		--argc;
		if (NOT_LONE_CHAR(argv[argc], ']')) {
			bb_error_msg("missing ]");
			return 2;
		}
		argv[argc] = NULL;
	} else if (strcmp(argv[0], "[[") == 0) {
		--argc;
		if (strcmp(argv[argc], "]]")) {
			bb_error_msg("missing ]]");
			return 2;
		}
		argv[argc] = NULL;
	}

	res = setjmp(leaving);
	if (res)
		return res;

	/* resetting ngroups is probably unnecessary.  it will
	 * force a new call to getgroups(), which prevents using
	 * group data fetched during a previous call.  but the
	 * only way the group data could be stale is if there's
	 * been an intervening call to setgroups(), and this
	 * isn't likely in the case of a shell.  paranoia
	 * prevails...
	 */
	ngroups = 0;

	/* Implement special cases from POSIX.2, section 4.62.4 */
	if (argc == 1)
		return 1;
	if (argc == 2)
		return *argv[1] == '\0';
//assert(argc);
	if (LONE_CHAR(argv[1], '!')) {
		bool _off;
		if (argc == 3)
			return *argv[2] != '\0';
		_off = argc - 4;
		t_lex(argv[2 + _off]);
		if (t_wp_op && t_wp_op->op_type == BINOP) {
			t_wp = &argv[1 + _off];
			return binop() == 0;
		}
	}
	t_wp = &argv[1];
	res = !oexpr(t_lex(*t_wp));

	if (*t_wp != NULL && *++t_wp != NULL) {
		bb_error_msg("%s: unknown operand", *t_wp);
		return 2;
	}
	return res;
}
Beispiel #2
0
int logger_main(int argc UNUSED_PARAM, char **argv)
{
	char *str_p, *str_t;
	int opt;
	int i = 0;
	FILE *f = NULL;

	/* Fill out the name string early (may be overwritten later) */
	str_t = uid2uname_utoa(geteuid());

	/* Parse any options */
	opt = getopt32(argv, "p:st:c", &str_p, &str_t);

	if (opt & 0x2) /* -s */
		i |= LOG_PERROR;
	if (opt & 0x8) { /* -c */
		f = fopen_for_write(DEV_CONSOLE);
		if (!f)
			bb_error_msg("can't open console: %d %s\n", errno, strerror(errno));
	}
	//if (opt & 0x4) /* -t */
	openlog(str_t, i, 0);
	i = LOG_USER | LOG_WARNING;
	if (opt & 0x1) /* -p */
		i = pencode(str_p);

	argv += optind;
	if (!argv[0]) {
		while (fgets(strbuf, COMMON_BUFSIZE, stdin)) {
			if (strbuf[0]
			 && NOT_LONE_CHAR(strbuf, '\n')
			) {
				/* Neither "" nor "\n" */
				syslog(i, "%s", strbuf);
				if (f)
					fprintf(f, "%s", strbuf);
			}
		}
	} else {
		char *message = NULL;
		int len = 0;
		int pos = 0;
		do {
			len += strlen(*argv) + 1;
			message = xrealloc(message, len + 1);
			sprintf(message + pos, " %s", *argv),
			pos = len;
		} while (*++argv);
		syslog(i, "%s", message + 1); /* skip leading " " */
		if (f)
			fprintf(f, "%s", message + 1);
	}

	closelog();
	if (f)
		fclose(f);
	return EXIT_SUCCESS;
}
int logger_main(int argc, char **argv)
{
	char *str_p, *str_t;
	int i = 0;
	char name[80];

	/* Fill out the name string early (may be overwritten later) */
	bb_getpwuid(name, sizeof(name), geteuid());
	str_t = name;

	/* Parse any options */
	getopt32(argv, "p:st:", &str_p, &str_t);

	if (option_mask32 & 0x2) /* -s */
		i |= LOG_PERROR;
	//if (option_mask32 & 0x4) /* -t */
	openlog(str_t, i, 0);
	i = LOG_USER | LOG_NOTICE;
	if (option_mask32 & 0x1) /* -p */
		i = pencode(str_p);

	argc -= optind;
	argv += optind;
	if (!argc) {
#define strbuf bb_common_bufsiz1
		while (fgets(strbuf, COMMON_BUFSIZE, stdin)) {
			if (strbuf[0]
			 && NOT_LONE_CHAR(strbuf, '\n')
			) {
				/* Neither "" nor "\n" */
				syslog(i, "%s", strbuf);
			}
		}
	} else {
		char *message = NULL;
		int len = 0;
		int pos = 0;
		do {
			len += strlen(*argv) + 1;
			message = xrealloc(message, len + 1);
			sprintf(message + pos, " %s", *argv),
			pos = len;
		} while (*++argv);
		syslog(i, "%s", message + 1); /* skip leading " " */
	}

	closelog();
	return EXIT_SUCCESS;
}
int rmdir_main(int argc UNUSED_PARAM, char **argv)
{
	int status = EXIT_SUCCESS;
	int flags;
	char *path;

#if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
	static const char rmdir_longopts[] ALIGN1 =
		"parents\0"                  No_argument "p"
		/* Debian etch: many packages fail to be purged or installed
		 * because they desperately want this option: */
		"ignore-fail-on-non-empty\0" No_argument "\xff"
		;
	applet_long_options = rmdir_longopts;
#endif
	flags = getopt32(argv, "p");
	argv += optind;

	if (!*argv) {
		bb_show_usage();
	}

	do {
		path = *argv;

		while (1) {
			if (rmdir(path) < 0) {
#if ENABLE_FEATURE_RMDIR_LONG_OPTIONS
				if ((flags & IGNORE_NON_EMPTY) && errno == ENOTEMPTY)
					break;
#endif
				bb_perror_msg("'%s'", path);	/* Match gnu rmdir msg. */
				status = EXIT_FAILURE;
			} else if (flags & PARENTS) {
				/* Note: path was not "" since rmdir succeeded. */
				path = dirname(path);
				/* Path is now just the parent component.  Dirname
				 * returns "." if there are no parents.
				 */
				if (NOT_LONE_CHAR(path, '.')) {
					continue;
				}
			}
			break;
		}
	} while (*++argv);

	return status;
}
Beispiel #5
0
int expand_main(int argc, char **argv)
{
	/* Default 8 spaces for 1 tab */
	const char *opt_t = "8";
	FILE *file;
	unsigned tab_size;
	unsigned opt;
	int exit_status = EXIT_SUCCESS;

#if ENABLE_FEATURE_EXPAND_LONG_OPTIONS
	static const char expand_longopts[] ALIGN1 =
		/* name, has_arg, val */
		"initial\0"          No_argument       "i"
		"tabs\0"             Required_argument "t"
	;
#endif
#if ENABLE_FEATURE_UNEXPAND_LONG_OPTIONS
	static const char unexpand_longopts[] ALIGN1 =
		/* name, has_arg, val */
		"first-only\0"       No_argument       "i"
		"tabs\0"             Required_argument "t"
		"all\0"              No_argument       "a"
	;
#endif

	if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e')) {
		USE_FEATURE_EXPAND_LONG_OPTIONS(applet_long_options = expand_longopts);
		opt = getopt32(argv, "it:", &opt_t);
	} else if (ENABLE_UNEXPAND) {
		USE_FEATURE_UNEXPAND_LONG_OPTIONS(applet_long_options = unexpand_longopts);
		/* -t NUM sets also -a */
		opt_complementary = "ta";
		opt = getopt32(argv, "ft:a", &opt_t);
		/* -f --first-only is the default */
		if (!(opt & OPT_ALL)) opt |= OPT_INITIAL;
	}
	tab_size = xatou_range(opt_t, 1, UINT_MAX);

	argv += optind;

	/* If no args are given, read from stdin */
	if (!*argv) {
		*--argv = (char*)bb_msg_standard_input;
		goto use_stdin;
	}

	do {
		if (NOT_LONE_CHAR(*argv, '-')) {
			file = fopen_or_warn(*argv, "r");
			if (!file) {
				exit_status = EXIT_FAILURE;
				continue;
			}
		} else {
 use_stdin:
			file = stdin;
		}

		if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e'))
			USE_EXPAND(expand(file, tab_size, opt));
		else if (ENABLE_UNEXPAND)
			USE_UNEXPAND(unexpand(file, tab_size, opt));

		/* Check and close the file */
		/* We do want all of them to execute, thus | instead of || */
		if (ferror(file) | fclose_if_not_stdin(file)) {
			bb_perror_msg("%s", *argv);
			exit_status = EXIT_FAILURE;
		}
		/* If stdin also clear EOF */
		if (file == stdin)
			clearerr(file);
	} while (*++argv);

	/* Now close stdin also */
	/* (if we didn't read from it, it's a no-op) */
	if (fclose(stdin))
		bb_perror_msg_and_die(bb_msg_standard_input);

	fflush_stdout_and_exit(exit_status);
}
Beispiel #6
0
static void exe_n_cwd_tab_completion(char *command, int type)
{
	DIR *dir;
	struct dirent *next;
	char dirbuf[MAX_LINELEN];
	struct stat st;
	char *path1[1];
	char **paths = path1;
	int npaths;
	int i;
	char *found;
	char *pfind = strrchr(command, '/');

	npaths = 1;
	path1[0] = (char*)".";

	if (pfind == NULL) {
		/* no dir, if flags==EXE_ONLY - get paths, else "." */
		npaths = path_parse(&paths, type);
		pfind = command;
	} else {
		/* dirbuf = ".../.../.../" */
		safe_strncpy(dirbuf, command, (pfind - command) + 2);
#if ENABLE_FEATURE_USERNAME_COMPLETION
		if (dirbuf[0] == '~')   /* ~/... or ~user/... */
			username_tab_completion(dirbuf, dirbuf);
#endif
		paths[0] = dirbuf;
		/* point to 'l' in "..../last_component" */
		pfind++;
	}

	for (i = 0; i < npaths; i++) {
		dir = opendir(paths[i]);
		if (!dir)                       /* Don't print an error */
			continue;

		while ((next = readdir(dir)) != NULL) {
			int len1;
			const char *str_found = next->d_name;

			/* matched? */
			if (strncmp(str_found, pfind, strlen(pfind)))
				continue;
			/* not see .name without .match */
			if (*str_found == '.' && *pfind == 0) {
				if (NOT_LONE_CHAR(paths[i], '/') || str_found[1])
					continue;
				str_found = ""; /* only "/" */
			}
			found = concat_path_file(paths[i], str_found);
			/* hmm, remover in progress? */
			if (stat(found, &st) < 0)
				goto cont;
			/* find with dirs? */
			if (paths[i] != dirbuf)
				strcpy(found, next->d_name);    /* only name */

			len1 = strlen(found);
			found = xrealloc(found, len1 + 2);
			found[len1] = '\0';
			found[len1+1] = '\0';

			if (S_ISDIR(st.st_mode)) {
				/* name is directory      */
				if (found[len1-1] != '/') {
					found[len1] = '/';
				}
			} else {
				/* not put found file if search only dirs for cd */
				if (type == FIND_DIR_ONLY)
					goto cont;
			}
			/* Add it to the list */
			add_match(found);
			continue;
 cont:
			free(found);
		}
		closedir(dir);
	}
	if (paths != path1) {
		free(paths[0]);                 /* allocated memory only in first member */
		free(paths);
	}
}
Beispiel #7
0
int logger_main(int argc UNUSED_PARAM, char **argv)
{
	char *str_p, *str_t;
	int opt;
	int i = 0;
	int fd = -1;

	setup_common_bufsiz();

	/* Fill out the name string early (may be overwritten later) */
	str_t = uid2uname_utoa(geteuid());

	/* Parse any options */
	opt = getopt32(argv, "p:st:c", &str_p, &str_t);

	if (opt & 0x2) /* -s */
		i |= LOG_PERROR;
	//if (opt & 0x4) /* -t */
	openlog(str_t, i, 0);
	if (opt & 0x8) { /* -c */
		fd = device_open(DEV_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK);
		if (fd < 0)
			bb_error_msg("can't open console");
	}
	i = LOG_USER | LOG_WARNING;
	if (opt & 0x1) /* -p */
		i = pencode(str_p);

	argv += optind;
	if (!argv[0]) {
		while (fgets(strbuf, COMMON_BUFSIZE, stdin)) {
			if (strbuf[0]
			 && NOT_LONE_CHAR(strbuf, '\n')
			) {
				/* Neither "" nor "\n" */
				syslog(i, "%s", strbuf);
				if (fd >= 0) {
					fdprintf(fd, "%s: %s%s", str_t, strbuf,
						 strchr(strbuf, '\n') ? "" : "\n");
				}
			}
		}
	} else {
		char *message = NULL;
		int len = 0;
		int pos = 0;
		do {
			len += strlen(*argv) + 1;
			message = xrealloc(message, len + 1);
			sprintf(message + pos, " %s", *argv),
			pos = len;
		} while (*++argv);
		syslog(i, "%s", message + 1); /* skip leading " " */
		if (fd >= 0 && len) {
			fdprintf(fd, "%s:%s%s", str_t, message,
				 message[len - 1] == '\n' ? "" : "\n");
		}
	}

	closelog();
	if (fd >= 0)
		close(fd);
	return EXIT_SUCCESS;
}