Esempio n. 1
0
int main(int argc, char **argv)
{
	const char *filename;
	size_t lines;
	int ch;
	struct stat st;

	static const struct option longopts[] = {
		{ "lines",   required_argument, 0, 'n' },
		{ "version", no_argument,	0, 'V' },
		{ "help",    no_argument,	0, 'h' },
		{ NULL,      0, 0, 0 }
	};

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	atexit(close_stdout);

	if (!old_style_option(&argc, argv, &lines))
		lines = DEFAULT_LINES;

	while ((ch = getopt_long(argc, argv, "n:N:Vh", longopts, NULL)) != -1)
		switch ((char)ch) {
		case 'n':
		case 'N':
			lines = strtoul_or_err(optarg,
					_("failed to parse number of lines"));
			break;
		case 'V':
			printf(UTIL_LINUX_VERSION);
			exit(EXIT_SUCCESS);
		case 'h':
			usage(stdout);
		default:
			usage(stderr);
		}

	if (argc == optind)
		errx(EXIT_FAILURE, _("no input file specified"));

	filename = argv[optind];

	if (stat(filename, &st) != 0)
		err(EXIT_FAILURE, _("stat of %s failed"), filename);
	if (!S_ISREG(st.st_mode))
		errx(EXIT_FAILURE, _("%s: is not a file"), filename);
	if (st.st_size)
		tailf(filename, lines, &st);

#ifdef HAVE_INOTIFY_INIT
	if (!watch_file_inotify(filename, &st))
#endif
		watch_file(filename, &st);

	return EXIT_SUCCESS;
}
Esempio n. 2
0
int main(int argc, char **argv)
{
	unsigned long first = 0, last = 0;
	int opt;

	static const struct option longopts[] = {
		{"version", no_argument, 0, 'V'},
		{"help", no_argument, 0, 'h'},
		{NULL, 0, 0, 0}
	};

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	atexit(close_stdout);

	while ((opt =
		getopt_long(argc, argv, "bfhl:pxVH", longopts,
			    NULL)) != -1)
		switch (opt) {
		case 'V':
			printf(_("%s from %s\n"),
			       program_invocation_short_name,
			       PACKAGE_STRING);
			return EXIT_SUCCESS;
		case 'h':
			usage(stdout);
		default:
			usage(stderr);
		}

	if (argc > 1)
		first = strtoul_or_err(*++argv, _("first argument"));
	if (argc > 2)
		last = strtoul_or_err(*++argv, _("second argument"));

	while (process_input(first, last))
		;

	fflush(stdout);
	return EXIT_SUCCESS;
}
Esempio n. 3
0
/* parses -N option */
static long old_style_option(int *argc, char **argv, size_t *lines)
{
	int i = 1, nargs = *argc, ret = 0;

	while(i < nargs) {
		if (argv[i][0] == '-' && isdigit(argv[i][1])) {
			*lines = strtoul_or_err(argv[i] + 1,
					_("failed to parse number of lines"));
			nargs--;
			ret = 1;
			if (nargs - i)
				memmove(argv + i, argv + i + 1,
						sizeof(char *) * (nargs - i));
		} else
			i++;
	}
	*argc = nargs;
	return ret;
}
Esempio n. 4
0
File: logger.c Progetto: alisw/uuid
/*
 * logger -- read and log utility
 *
 *	Reads from an input and arranges to write the result on the system
 *	log.
 */
int main(int argc, char **argv)
{
	struct logger_ctl ctl = {
		.fd = -1,
		.pid = 0,
		.pri = LOG_USER | LOG_NOTICE,
		.prio_prefix = 0,
		.tag = NULL,
		.unix_socket = NULL,
		.unix_socket_errors = 0,
		.server = NULL,
		.port = NULL,
		.hdr = NULL,
		.msgid = NULL,
		.socket_type = ALL_TYPES,
		.max_message_size = 1024,
		.rfc5424_time = 1,
		.rfc5424_tq = 1,
		.rfc5424_host = 1,
		.skip_empty_lines = 0
	};
	int ch;
	int stdout_reopened = 0;
	int unix_socket_errors_mode = AF_UNIX_ERRORS_AUTO;
#ifdef HAVE_LIBSYSTEMD
	FILE *jfd = NULL;
#endif
	static const struct option longopts[] = {
		{ "id",		   optional_argument, 0, OPT_ID		   },
		{ "stderr",	   no_argument,	      0, 's'		   },
		{ "file",	   required_argument, 0, 'f'		   },
		{ "no-act",        no_argument,       0, OPT_NOACT,	   },
		{ "priority",	   required_argument, 0, 'p'		   },
		{ "tag",	   required_argument, 0, 't'		   },
		{ "socket",	   required_argument, 0, 'u'		   },
		{ "socket-errors", required_argument, 0, OPT_SOCKET_ERRORS },
		{ "udp",	   no_argument,	      0, 'd'		   },
		{ "tcp",	   no_argument,	      0, 'T'		   },
		{ "server",	   required_argument, 0, 'n'		   },
		{ "port",	   required_argument, 0, 'P'		   },
		{ "version",	   no_argument,	      0, 'V'		   },
		{ "help",	   no_argument,	      0, 'h'		   },
		{ "octet-count",   no_argument,	      0, OPT_OCTET_COUNT   },
		{ "prio-prefix",   no_argument,	      0, OPT_PRIO_PREFIX   },
		{ "rfc3164",	   no_argument,	      0, OPT_RFC3164	   },
		{ "rfc5424",	   optional_argument, 0, OPT_RFC5424	   },
		{ "size",	   required_argument, 0, 'S'		   },
		{ "msgid",	   required_argument, 0, OPT_MSGID	   },
		{ "skip-empty",	   no_argument,	      0, 'e'		   },
#ifdef HAVE_LIBSYSTEMD
		{ "journald",	   optional_argument, 0, OPT_JOURNALD	   },
#endif
		{ NULL,		   0,		      0, 0		   }
	};

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	atexit(close_stdout);

	while ((ch = getopt_long(argc, argv, "ef:ip:S:st:u:dTn:P:Vh",
					    longopts, NULL)) != -1) {
		switch (ch) {
		case 'f':		/* file to log */
			if (freopen(optarg, "r", stdin) == NULL)
				err(EXIT_FAILURE, _("file %s"), optarg);
			stdout_reopened = 1;
			break;
		case 'e':
			ctl.skip_empty_lines = 1;
			break;
		case 'i':		/* log process id also */
			ctl.pid = logger_getpid();
			break;
		case OPT_ID:
			if (optarg) {
				const char *p = optarg;

				if (*p == '=')
					p++;
				ctl.pid = strtoul_or_err(optarg, _("failed to parse id"));
			} else
				ctl.pid = logger_getpid();
			break;
		case 'p':		/* priority */
			ctl.pri = pencode(optarg);
			break;
		case 's':		/* log to standard error */
			ctl.stderr_printout = 1;
			break;
		case 't':		/* tag */
			ctl.tag = optarg;
			break;
		case 'u':		/* unix socket */
			ctl.unix_socket = optarg;
			break;
		case 'S':		/* max message size */
			ctl.max_message_size = strtosize_or_err(optarg,
				_("failed to parse message size"));
			break;
		case 'd':
			ctl.socket_type = TYPE_UDP;
			break;
		case 'T':
			ctl.socket_type = TYPE_TCP;
			break;
		case 'n':
			ctl.server = optarg;
			break;
		case 'P':
			ctl.port = optarg;
			break;
		case 'V':
			printf(UTIL_LINUX_VERSION);
			exit(EXIT_SUCCESS);
		case 'h':
			usage(stdout);
		case OPT_OCTET_COUNT:
			ctl.octet_count = 1;
			break;
		case OPT_PRIO_PREFIX:
			ctl.prio_prefix = 1;
			break;
		case OPT_RFC3164:
			ctl.syslogfp = syslog_rfc3164_header;
			break;
		case OPT_RFC5424:
			ctl.syslogfp = syslog_rfc5424_header;
			if (optarg)
				parse_rfc5424_flags(&ctl, optarg);
			break;
		case OPT_MSGID:
			if (strchr(optarg, ' '))
				errx(EXIT_FAILURE, _("--msgid cannot contain space"));
			ctl.msgid = optarg;
			break;
#ifdef HAVE_LIBSYSTEMD
		case OPT_JOURNALD:
			if (optarg) {
				jfd = fopen(optarg, "r");
				if (!jfd)
					err(EXIT_FAILURE, _("cannot open %s"),
					    optarg);
			} else
				jfd = stdin;
			break;
#endif
		case OPT_SOCKET_ERRORS:
			unix_socket_errors_mode = parse_unix_socket_errors_flags(optarg);
			break;
		case OPT_NOACT:
			ctl.noact = 1;
			break;
		case '?':
		default:
			usage(stderr);
		}
	}
	argc -= optind;
	argv += optind;
	if (stdout_reopened && argc)
		warnx(_("--file <file> and <message> are mutually exclusive, message is ignored"));
#ifdef HAVE_LIBSYSTEMD
	if (jfd) {
		int ret = journald_entry(&ctl, jfd);
		if (stdin != jfd)
			fclose(jfd);
		if (ret)
			errx(EXIT_FAILURE, _("journald entry could not be written"));
		return EXIT_SUCCESS;
	}
#endif
	switch (unix_socket_errors_mode) {
	case AF_UNIX_ERRORS_OFF:
		ctl.unix_socket_errors = 0;
		break;
	case AF_UNIX_ERRORS_ON:
		ctl.unix_socket_errors = 1;
		break;
	case AF_UNIX_ERRORS_AUTO:
		ctl.unix_socket_errors = ctl.noact || ctl.stderr_printout;
#ifdef HAVE_LIBSYSTEMD
		ctl.unix_socket_errors |= !!sd_booted();
#endif
		break;
	default:
		abort();
	}
	logger_open(&ctl);
	if (0 < argc)
		logger_command_line(&ctl, argv);
	else
		/* Note. --file <arg> reopens stdin making the below
		 * function to be used for file inputs. */
		logger_stdin(&ctl);
	logger_close(&ctl);
	return EXIT_SUCCESS;
}
Esempio n. 5
0
/* Handle command line options. */
static void parse_args(int argc, char **argv, char **device)
{
	static const struct option long_opts[] =
	{
		{"auto",	required_argument, NULL, 'a'},
		{"cdrom",	no_argument,	   NULL, 'r'},
		{"cdspeed",	required_argument, NULL, 'x'},
		{"changerslot", required_argument, NULL, 'c'},
		{"default",	no_argument,	   NULL, 'd'},
		{"floppy",	no_argument,	   NULL, 'f'},
		{"force",       no_argument,       NULL, 'F'},
		{"help",	no_argument,	   NULL, 'h'},
		{"listspeed",   no_argument,       NULL, 'X'},
		{"manualeject", required_argument, NULL, 'i'},
		{"noop",	no_argument,	   NULL, 'n'},
		{"no-unmount",	no_argument,	   NULL, 'm'},
		{"no-partitions-unmount", no_argument, NULL, 'M' },
		{"proc",	no_argument,	   NULL, 'p'},
		{"scsi",	no_argument,	   NULL, 's'},
		{"tape",	no_argument,	   NULL, 'q'},
		{"trayclose",	no_argument,	   NULL, 't'},
		{"traytoggle",	no_argument,	   NULL, 'T'},
		{"verbose",	no_argument,	   NULL, 'v'},
		{"version",	no_argument,	   NULL, 'V'},
		{0, 0, 0, 0}
	};
	int c;

	while ((c = getopt_long(argc, argv,
				"a:c:i:x:dfFhnqrstTXvVpmM", long_opts, NULL)) != -1) {
		switch (c) {
		case 'a':
			a_option = 1;
			if (!strcmp(optarg, "0") || !strcmp(optarg, "off"))
				a_arg = 0;
			else if (!strcmp(optarg, "1") || !strcmp(optarg, "on"))
				a_arg = 1;
			else
				errx(EXIT_FAILURE, _("invalid argument to --auto/-a option"));
			break;
		case 'c':
			c_option = 1;
			c_arg = strtoul_or_err(optarg, _("invalid argument to --changerslot/-c option"));
			break;
		case 'x':
			x_option = 1;
			x_arg = strtoul_or_err(optarg, _("invalid argument to --cdspeed/-x option"));
			break;
		case 'd':
			d_option = 1;
			break;
		case 'f':
			f_option = 1;
			break;
		case 'F':
			F_option = 1;
			break;
		case 'h':
			usage(stdout);
			break;
		case 'i':
			i_option = 1;
			if (!strcmp(optarg, "0") || !strcmp(optarg, "off"))
				i_arg = 0;
			else if (!strcmp(optarg, "1") || !strcmp(optarg, "on"))
				i_arg = 1;
			else
				errx(EXIT_FAILURE, _("invalid argument to --manualeject/-i option"));
			break;
		case 'm':
			m_option = 1;
			break;
		case 'M':
			M_option = 1;
			break;
		case 'n':
			n_option = 1;
			break;
		case 'p':
			p_option = 1;
			break;
		case 'q':
			q_option = 1;
			break;
		case 'r':
			r_option = 1;
			break;
		case 's':
			s_option = 1;
			break;
		case 't':
			t_option = 1;
			break;
		case 'T':
			T_option = 1;
			break;
		case 'X':
			X_option = 1;
			break;
		case 'v':
			v_option = 1;
			break;
		case 'V':
			printf(UTIL_LINUX_VERSION);
			exit(EXIT_SUCCESS);
			break;
		default:
		case '?':
			usage(stderr);
			break;
		}
	}

	/* check for a single additional argument */
	if ((argc - optind) > 1)
		errx(EXIT_FAILURE, _("too many arguments"));

	if ((argc - optind) == 1)
		*device = xstrdup(argv[optind]);
}
Esempio n. 6
0
/* Handle command line options. */
static void parse_args(struct eject_control *ctl, int argc, char **argv)
{
	static const struct option long_opts[] =
	{
		{"auto",	required_argument, NULL, 'a'},
		{"cdrom",	no_argument,	   NULL, 'r'},
		{"cdspeed",	required_argument, NULL, 'x'},
		{"changerslot", required_argument, NULL, 'c'},
		{"default",	no_argument,	   NULL, 'd'},
		{"floppy",	no_argument,	   NULL, 'f'},
		{"force",       no_argument,       NULL, 'F'},
		{"help",	no_argument,	   NULL, 'h'},
		{"listspeed",   no_argument,       NULL, 'X'},
		{"manualeject", required_argument, NULL, 'i'},
		{"noop",	no_argument,	   NULL, 'n'},
		{"no-unmount",	no_argument,	   NULL, 'm'},
		{"no-partitions-unmount", no_argument, NULL, 'M' },
		{"proc",	no_argument,	   NULL, 'p'},
		{"scsi",	no_argument,	   NULL, 's'},
		{"tape",	no_argument,	   NULL, 'q'},
		{"trayclose",	no_argument,	   NULL, 't'},
		{"traytoggle",	no_argument,	   NULL, 'T'},
		{"verbose",	no_argument,	   NULL, 'v'},
		{"version",	no_argument,	   NULL, 'V'},
		{0, 0, 0, 0}
	};
	int c;

	while ((c = getopt_long(argc, argv,
				"a:c:i:x:dfFhnqrstTXvVpmM", long_opts, NULL)) != -1) {
		switch (c) {
		case 'a':
			ctl->a_option = 1;
			ctl->a_arg = parse_switch(optarg, _("argument error"),
						"on", "off",  "1", "0",  NULL);
			break;
		case 'c':
			ctl->c_option = 1;
			ctl->c_arg = strtoul_or_err(optarg, _("invalid argument to --changerslot/-c option"));
			break;
		case 'x':
			ctl->x_option = 1;
			ctl->x_arg = strtoul_or_err(optarg, _("invalid argument to --cdspeed/-x option"));
			break;
		case 'd':
			ctl->d_option = 1;
			break;
		case 'f':
			ctl->f_option = 1;
			break;
		case 'F':
			ctl->F_option = 1;
			break;
		case 'h':
			usage(stdout);
			break;
		case 'i':
			ctl->i_option = 1;
			ctl->i_arg = parse_switch(optarg, _("argument error"),
						"on", "off",  "1", "0",  NULL);
			break;
		case 'm':
			ctl->m_option = 1;
			break;
		case 'M':
			ctl->M_option = 1;
			break;
		case 'n':
			ctl->n_option = 1;
			break;
		case 'p':
			ctl->p_option = 1;
			break;
		case 'q':
			ctl->q_option = 1;
			break;
		case 'r':
			ctl->r_option = 1;
			break;
		case 's':
			ctl->s_option = 1;
			break;
		case 't':
			ctl->t_option = 1;
			break;
		case 'T':
			ctl->T_option = 1;
			break;
		case 'X':
			ctl->X_option = 1;
			break;
		case 'v':
			ctl->v_option = 1;
			break;
		case 'V':
			printf(UTIL_LINUX_VERSION);
			exit(EXIT_SUCCESS);
			break;
		default:
		case '?':
			usage(stderr);
			break;
		}
	}

	/* check for a single additional argument */
	if ((argc - optind) > 1)
		errx(EXIT_FAILURE, _("too many arguments"));

	if ((argc - optind) == 1)
		ctl->device = xstrdup(argv[optind]);
}
Esempio n. 7
0
/*
 * logger -- read and log utility
 *
 *	Reads from an input and arranges to write the result on the system
 *	log.
 */
int main(int argc, char **argv)
{
	struct logger_ctl ctl = {
		.fd = -1,
		.pid = 0,
		.pri = LOG_NOTICE,
		.prio_prefix = 0,
		.tag = NULL,
		.unix_socket = NULL,
		.server = NULL,
		.port = NULL,
		.socket_type = ALL_TYPES,
		.rfc5424_time = 1,
		.rfc5424_tq = 1,
		.rfc5424_host = 1,
	};
	int ch;
	int stdout_reopened = 0;
#ifdef HAVE_LIBSYSTEMD
	FILE *jfd = NULL;
#endif
	static const struct option longopts[] = {
		{ "id",		optional_argument,  0, 'i' },
		{ "stderr",	no_argument,	    0, 's' },
		{ "file",	required_argument,  0, 'f' },
		{ "priority",	required_argument,  0, 'p' },
		{ "tag",	required_argument,  0, 't' },
		{ "socket",	required_argument,  0, 'u' },
		{ "udp",	no_argument,	    0, 'd' },
		{ "tcp",	no_argument,	    0, 'T' },
		{ "server",	required_argument,  0, 'n' },
		{ "port",	required_argument,  0, 'P' },
		{ "version",	no_argument,	    0, 'V' },
		{ "help",	no_argument,	    0, 'h' },
		{ "prio-prefix", no_argument, 0, OPT_PRIO_PREFIX },
		{ "rfc3164",	no_argument,  0, OPT_RFC3164 },
		{ "rfc5424",	optional_argument,  0, OPT_RFC5424 },
#ifdef HAVE_LIBSYSTEMD
		{ "journald",   optional_argument,  0, OPT_JOURNALD },
#endif
		{ NULL,		0, 0, 0 }
	};

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	atexit(close_stdout);

	while ((ch = getopt_long(argc, argv, "f:i::p:st:u:dTn:P:Vh",
					    longopts, NULL)) != -1) {
		switch (ch) {
		case 'f':		/* file to log */
			if (freopen(optarg, "r", stdin) == NULL)
				err(EXIT_FAILURE, _("file %s"), optarg);
			stdout_reopened = 1;
			break;
		case 'i':		/* log process id also */
			if (optarg) {
				const char *p = optarg;

				if (*p == '=')
					p++;
				ctl.pid = strtoul_or_err(optarg, _("failed to parse id"));
			} else
				ctl.pid = getpid();
			break;
		case 'p':		/* priority */
			ctl.pri = pencode(optarg);
			break;
		case 's':		/* log to standard error */
			ctl.stderr_printout = 1;
			break;
		case 't':		/* tag */
			ctl.tag = optarg;
			break;
		case 'u':		/* unix socket */
			ctl.unix_socket = optarg;
			break;
		case 'd':
			ctl.socket_type = TYPE_UDP;
			break;
		case 'T':
			ctl.socket_type = TYPE_TCP;
			break;
		case 'n':
			ctl.server = optarg;
			break;
		case 'P':
			ctl.port = optarg;
			break;
		case 'V':
			printf(UTIL_LINUX_VERSION);
			exit(EXIT_SUCCESS);
		case 'h':
			usage(stdout);
		case OPT_PRIO_PREFIX:
			ctl.prio_prefix = 1;
			break;
		case OPT_RFC3164:
			ctl.syslogfp = syslog_rfc3164;
			break;
		case OPT_RFC5424:
			ctl.syslogfp = syslog_rfc5424;
			if (optarg)
				parse_rfc5424_flags(&ctl, optarg);
			break;
#ifdef HAVE_LIBSYSTEMD
		case OPT_JOURNALD:
			if (optarg) {
				jfd = fopen(optarg, "r");
				if (!jfd)
					err(EXIT_FAILURE, _("cannot open %s"),
					    optarg);
			} else
				jfd = stdin;
			break;
#endif
		case '?':
		default:
			usage(stderr);
		}
	}
	argc -= optind;
	argv += optind;
	if (stdout_reopened && argc)
		warnx(_("--file <file> and <message> are mutually exclusive, message is ignored"));
#ifdef HAVE_LIBSYSTEMD
	if (jfd) {
		int ret = journald_entry(jfd);
		if (stdin != jfd)
			fclose(jfd);
		if (ret)
			errx(EXIT_FAILURE, _("journald entry could not be wrote"));
		return EXIT_SUCCESS;
	}
#endif
	logger_open(&ctl);
	if (0 < argc)
		logger_command_line(&ctl, argv);
	else
		/* Note. --file <arg> reopens stdin making the below
		 * function to be used for file inputs. */
		logger_stdin(&ctl);
	logger_close(&ctl);
	return EXIT_SUCCESS;
}