Exemplo n.º 1
0
void leave(void)
{
    if (tflag)
	clear();
    else
	writec('\n');
    fixtty(&old);
    execlp("backgammon", "backgammon", "-n", args[1] ? args : 0, NULL);
    writel("Help! Backgammon program is missing\007!!\n");
    exit(1);
}
Exemplo n.º 2
0
void
getout(int dummy __unused)
{
	/* go to bottom of screen */
	if (tflag) {
		curmove(23, 0);
		cline();
	} else
		writec('\n');

	/* fix terminal status */
	fixtty(&old);
	exit(0);
}
Exemplo n.º 3
0
void
leave(void)
{
	int i;

	if (tflag)
		clear();
	else
		writec('\n');
	fixtty(old);
	args[0] = strdup("backgammon");
	args[acnt++] = strdup("-n");
	args[acnt] = 0;
	execv(EXEC, args);
	for (i = 0; i < acnt; i++)
		free(args[i]);
	writel("Help! Backgammon program is missing\007!!\n");
	exit(-1);
}
Exemplo n.º 4
0
Arquivo: escseq.c Projeto: NgoHuy/uim
void init_escseq(const struct attribute_tag *attr_uim)
{
  s_attr_uim = *attr_uim;

  s_enter_underline_num = escseq2n(enter_underline_mode);
  s_exit_underline_num = escseq2n(exit_underline_mode);
  s_enter_standout_num = escseq2n(enter_standout_mode);
  s_exit_standout_num = escseq2n(exit_standout_mode);
  s_bold_num = escseq2n(enter_bold_mode);
  s_blink_num = escseq2n(enter_blink_mode);
  s_orig_pair_num = escseq2n(orig_pair);
  escseq2n2(orig_pair, &s_orig_fore_num, &s_orig_back_num);
  s_enter_uim_mode = attr2escseq(&s_attr_uim);
  if (s_enter_uim_mode != NULL) {
    s_enter_uim_mode = uim_strdup(s_enter_uim_mode);
  }

  fixtty();
  check_escseq();
  s_init = TRUE;
}
Exemplo n.º 5
0
int
text(const char *const *txt)
{
	const char *a;
	char b;
	const char *c;
	int i;

	fixtty(noech);
	while (*txt) {
		a = *(txt++);
		if (*a != '\0') {
			c = a;
			for (i = 0; *(c++) != '\0'; i--)
			    ; /* nothing */
			writel(a);
			writec('\n');
		} else {
			fixtty(raw);
			writel(prompt);
			for (;;) {
				if ((b = readc()) == '?') {
					if (tflag) {
						if (begscr) {
							curmove(18, 0);
							clend();
						} else
							clear();
					} else
						writec('\n');
					text(list);
					writel(prompt);
					continue;
				}
				i = 0;
				if (b == '\n')
					break;
				while (i < 11) {
					if (b == opts[i])
						break;
					i++;
				}
				if (i == 11)
					writec('\007');
				else
					break;
			}
			if (tflag) {
				if (begscr) {
					curmove(18, 0);
					clend();
				} else
					clear();
			} else
				writec('\n');
			if (i)
				return (i);
			fixtty(noech);
			if (tflag)
				curmove(curr, 0);
		}
	}
	fixtty(raw);
	return (0);
}
Exemplo n.º 6
0
static	int
findopt(int argc, char **argv)
{
	int	eargc = 0;
	int	c;
	int	mflg = 0;
	int	aflg = 0;
	int	optnum;
	int	argv_ind;
	int	end_opt;
	int	i;
	int	isarg = 0;

	fixtty();

	/* Handle page number option */
	for (optnum = 1, end_opt = 0; optnum < argc && !end_opt; optnum++) {
		switch (*argv[optnum]) {
		case '+':
			/* check for all digits */
			if (strlen(&argv[optnum][1]) !=
			    strspn(&argv[optnum][1], "0123456789")) {
				(void) fprintf(stderr, gettext(
				    "pr: Badly formed number\n"));
				exit(1);
			}

			if ((Fpage = (int)strtol(&argv[optnum][1],
			    (char **)NULL, 10)) < 0) {
				(void) fprintf(stderr, gettext(
				    "pr: Badly formed number\n"));
				exit(1);
			}
			REMOVE_ARG(argc, &argv[optnum]);
			optnum--;
			break;

		case '-':
			/* Check for end of options */
			if (argv[optnum][1] == '-') {
				end_opt++;
				break;
			}

			if (argv[optnum][1] == 'h' || argv[optnum][1] == 'l' ||
			    argv[optnum][1] == 'o' || argv[optnum][1] == 'w')
				isarg = 1;
			else
				isarg = 0;

			break;

		default:
			if (isarg == 0)
				end_opt++;
			else
				isarg = 0;
			break;
		}
	}

	/*
	 * Handle options with optional arguments.
	 * If optional arguments are present they may not be separated
	 * from the option letter.
	 */

	for (optnum = 1; optnum < argc; optnum++) {
		if (argv[optnum][0] == '-' && argv[optnum][1] == '-')
			/* End of options */
			break;

		if (argv[optnum][0] == '-' && argv[optnum][1] == '\0')
			/* stdin file name */
			continue;

		if (argv[optnum][0] != '-')
			/* not option */
			continue;

		for (argv_ind = 1; argv[optnum][argv_ind] != '\0'; argv_ind++) {
			switch (argv[optnum][argv_ind]) {
			case 'e':
				SQUEEZE_ARG(argv[optnum], argv_ind, 1);
				if ((c = argv[optnum][argv_ind]) != '\0' &&
				    !isdigit(c)) {
					int	r;
					wchar_t	wc;
					r = mbtowc(&wc, &argv[optnum][argv_ind],
						mbcurmax);
					if (r == -1) {
						(void) fprintf(stderr, gettext(
"pr: Illegal character in -e option\n"));
						exit(1);
					}
					Etabc = wc;
					SQUEEZE_ARG(argv[optnum], argv_ind, r);
				}
				if (isdigit(argv[optnum][argv_ind])) {
					Etabn = (int)strtol(&argv[optnum]
					    [argv_ind], (char **)NULL, 10);
					while (isdigit(argv[optnum][argv_ind]))
					    SQUEEZE_ARG(argv[optnum],
							argv_ind, 1);
				}
				if (Etabn <= 0)
					Etabn = DEFTAB;
				argv_ind--;
				break;

			case 'i':
				SQUEEZE_ARG(argv[optnum], argv_ind, 1);
				if ((c = argv[optnum][argv_ind]) != '\0' &&
				    !isdigit(c)) {
					int	r;
					wchar_t	wc;
					r = mbtowc(&wc, &argv[optnum][argv_ind],
						mbcurmax);
					if (r == -1) {
						(void) fprintf(stderr, gettext(
"pr: Illegal character in -i option\n"));
						exit(1);
					}
					Itabc = wc;
					SQUEEZE_ARG(argv[optnum], argv_ind, r);
				}
				if (isdigit(argv[optnum][argv_ind])) {
					Itabn = (int)strtol(&argv[optnum]
					    [argv_ind], (char **)NULL, 10);
					while (isdigit(argv[optnum][argv_ind]))
					    SQUEEZE_ARG(argv[optnum],
							argv_ind, 1);
				}
				if (Itabn <= 0)
					Itabn = DEFTAB;
				argv_ind--;
				break;


			case 'n':
				++Lnumb;
				SQUEEZE_ARG(argv[optnum], argv_ind, 1);
				if ((c = argv[optnum][argv_ind]) != '\0' &&
				    !isdigit(c)) {
					int	r;
					wchar_t	wc;
					r = mbtowc(&wc, &argv[optnum][argv_ind],
						mbcurmax);
					if (r == -1) {
						(void) fprintf(stderr, gettext(
"pr: Illegal character in -n option\n"));
						exit(1);
					}
					Nsepc = wc;
					SQUEEZE_ARG(argv[optnum], argv_ind, r);
				}
				if (isdigit(argv[optnum][argv_ind])) {
					Numw = (int)strtol(&argv[optnum]
					    [argv_ind], (char **)NULL, 10);
					while (isdigit(argv[optnum][argv_ind]))
					    SQUEEZE_ARG(argv[optnum],
							argv_ind, 1);
				}
				argv_ind--;
				if (!Numw)
					Numw = NUMW;
				break;

			case 's':
				SQUEEZE_ARG(argv[optnum], argv_ind, 1);
				if ((Sepc = argv[optnum][argv_ind]) == '\0')
					Sepc = '\t';
				else {
					int	r;
					wchar_t	wc;
					r = mbtowc(&wc, &argv[optnum][argv_ind],
						mbcurmax);
					if (r == -1) {
						(void) fprintf(stderr, gettext(
"pr: Illegal character in -s option\n"));
						exit(1);
					}
					Sepc = wc;
					SQUEEZE_ARG(argv[optnum], argv_ind, r);
				}
				argv_ind--;
				break;

			default:
				break;
			}
		}
		if (argv[optnum][0] == '-' && argv[optnum][1] == '\0') {
			REMOVE_ARG(argc, &argv[optnum]);
			optnum--;
		}
	}

	/* Now get the other options */
	while ((c = getopt(argc, argv, "0123456789adfFh:l:mo:prtw:"))
	    != EOF) {
		switch (c) {
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
			Ncols *= 10;
			Ncols += c - '0';
			break;

		case 'a':
			aflg++;
			if (!Multi)
				Multi = c;
			break;

		case 'd':
			Dblspace = 2;
			break;

		case 'f':
			++Formfeed;
			++Pause;
			break;

		case 'h':
			Head = optarg;
			break;

		case 'l':
			if (strlen(optarg) != strspn(optarg, "0123456789"))
				usage(1);
			Length = (int)strtol(optarg, (char **)NULL, 10);
			break;

		case 'm':
			mflg++;
			Multi = c;
			break;

		case 'o':
			if (strlen(optarg) != strspn(optarg, "0123456789"))
				usage(1);
			Offset = (int)strtol(optarg, (char **)NULL, 10);
			break;

		case 'p':
			++Pause;
			break;

		case 'r':
			Report = 0;
			break;

		case 't':
			Margin = 0;
			break;

		case 'w':
			if (strlen(optarg) != strspn(optarg, "0123456789"))
				usage(1);
			Linew = (int)strtol(optarg, (char **)NULL, 10);
			break;

		case 'F':
#ifdef	XPG4
			++Formfeed;
#else
			fold++;
#endif
			break;

		case '?':
			usage(2);
			break;

		default :
			usage(2);
		}
	}

	/* Count the file names and strip options */
	for (i = 1; i < argc; i++) {
		/* Check for explicit stdin */
		if ((argv[i][0] == '-') && (argv[i][1] == '\0')) {
			argv[eargc++][0] = '\0';
			REMOVE_ARG(argc, &argv[i]);
			if (i < optind)
				optind--;
		}
	}
	for (i = eargc; optind < argc; i++, optind++) {
		argv[i] = argv[optind];
		eargc++;
	}

	/* Check options */
	if (Ncols == 0)
		Ncols = 1;

	if (mflg && (Ncols > 1)) {
		(void) fprintf(stderr,
		    gettext("pr: only one of either -m or -column allowed\n"));
		usage(1);
	}

	if (Ncols == 1 && fold)
		Multi = 'm';

	if (Length <= 0)
		Length = LENGTH;

	if (Length <= Margin)
		Margin = 0;

	Plength = Length - Margin/2;

	if (Multi == 'm')
		Ncols = eargc;

	switch (Ncols) {
	case 0:
		Ncols = 1;
		break;

	case 1:
		break;

	default:
		if (Etabn == 0)	/* respect explicit tab specification */
			Etabn = DEFTAB;
		if (Itabn == 0)
			Itabn = DEFTAB;
	}

	if ((Fcol = (foldinf *) malloc(sizeof (foldinf) * Ncols)) == NULL) {
		(void) fprintf(stderr, gettext("pr: malloc failed\n"));
		exit(1);
	}
	for (i = 0; i < Ncols; i++)
		Fcol[i].fold = Fcol[i].skip = 0;

	if (Linew == 0)
		Linew = Ncols != 1 && Sepc == 0 ? LINEW : 512;

	if (Lnumb) {
		int numw;

		if (Nsepc == '\t') {
			if (Itabn == 0)
				numw = Numw + DEFTAB - (Numw % DEFTAB);
			else
				numw = Numw + Itabn - (Numw % Itabn);
		} else {
			numw = Numw + ((iswprint(Nsepc)) ? 1 : 0);
		}
		Linew -= (Multi == 'm') ? numw : numw * Ncols;
	}

	if ((Colw = (Linew - Ncols + 1)/Ncols) < 1)
		die("width too small");

	if (Ncols != 1 && Multi == 0) {
		/* Buflen should take the number of wide characters */
		/* Not the size for Buffer */
		Buflen = ((UNS) (Plength / Dblspace + 1)) *
		    2 * (Linew + 1);
		/* Should allocate Buflen * sizeof (wchar_t) */
		Buffer = (wchar_t *)getspace(Buflen * sizeof (wchar_t));
		Bufptr = Bufend = &Buffer[Buflen];
		Colpts = (COLP) getspace((UNS) ((Ncols + 1) *
		    sizeof (*Colpts)));
		Colpts[0].c_lno = 0;
	}

	/* is stdin not a tty? */
	if (Ttyout && (Pause || Formfeed) && !ttyname(fileno(stdin)))
		Ttyin = fopen("/dev/tty", "r");

	return (eargc);
}
Exemplo n.º 7
0
int
main(int argc, char **argv) {
	struct sigaction sa;
	extern int optind;
	char *p;
	int ch;

	progname = argv[0];
	if ((p = strrchr(progname, '/')) != NULL)
		progname = p+1;


	setlocale(LC_ALL, "");
	setlocale(LC_NUMERIC, "C");	/* see comment above */
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	
	if (argc == 2) {
		if (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version")) {
			printf(_("%s (%s)\n"),
			       progname, PACKAGE_STRING);
			return 0;
		}
	}

	while ((ch = getopt(argc, argv, "ac:fqt")) != -1)
		switch((char)ch) {
		case 'a':
			aflg++;
			break;
		case 'c':
			cflg = optarg;
			break;
		case 'f':
			fflg++;
			break;
		case 'q':
			qflg++;
			break;
		case 't':
			tflg++;
			break;
		case '?':
		default:
			fprintf(stderr,
				_("usage: script [-a] [-f] [-q] [-t] [file]\n"));
			exit(1);
		}
	argc -= optind;
	argv += optind;

	if (argc > 0)
		fname = argv[0];
	else {
		fname = "typescript";
		die_if_link(fname);
	}
	if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL) {
		perror(fname);
		fail();
	}

	shell = getenv("SHELL");
	if (shell == NULL)
		shell = _PATH_BSHELL;

	getmaster();
	if (!qflg)
		printf(_("Script started, file is %s\n"), fname);
	fixtty();

	sigemptyset(&sa.sa_mask);
	sa.sa_flags = 0;

	sa.sa_handler = finish;
	sigaction(SIGCHLD, &sa, NULL);

	child = fork();
	if (child < 0) {
		perror("fork");
		fail();
	}
	if (child == 0) {
		subchild = child = fork();
		if (child < 0) {
			perror("fork");
			fail();
		}
		if (child)
			dooutput();
		else
			doshell();
	} else {
		sa.sa_handler = resize;
		sigaction(SIGWINCH, &sa, NULL);
	}
	doinput();

	return 0;
}
Exemplo n.º 8
0
int
main(int argc, char **argv) {
	sigset_t block_mask, unblock_mask;
	struct sigaction sa;
	int ch;
	FILE *timingfd = stderr;

	enum { FORCE_OPTION = CHAR_MAX + 1 };

	static const struct option longopts[] = {
		{ "append",	no_argument,	   NULL, 'a' },
		{ "command",	required_argument, NULL, 'c' },
		{ "return",	no_argument,	   NULL, 'e' },
		{ "flush",	no_argument,	   NULL, 'f' },
		{ "force",	no_argument,	   NULL, FORCE_OPTION, },
		{ "quiet",	no_argument,	   NULL, 'q' },
		{ "timing",	optional_argument, NULL, 't' },
		{ "version",	no_argument,	   NULL, 'V' },
		{ "help",	no_argument,	   NULL, 'h' },
		{ NULL,		0, NULL, 0 }
	};

	setlocale(LC_ALL, "");
	setlocale(LC_NUMERIC, "C");	/* see comment above */
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	atexit(close_stdout);

	while ((ch = getopt_long(argc, argv, "ac:efqt::Vh", longopts, NULL)) != -1)
		switch(ch) {
		case 'a':
			aflg = 1;
			break;
		case 'c':
			cflg = optarg;
			break;
		case 'e':
			eflg = 1;
			break;
		case 'f':
			fflg = 1;
			break;
		case FORCE_OPTION:
			forceflg = 1;
			break;
		case 'q':
			qflg = 1;
			break;
		case 't':
			if (optarg)
				if ((timingfd = fopen(optarg, "w")) == NULL)
					err(EXIT_FAILURE, _("cannot open timing file %s"), optarg);
			tflg = 1;
			break;
		case 'V':
			printf(_("%s from %s\n"), program_invocation_short_name,
						  PACKAGE_STRING);
			exit(EXIT_SUCCESS);
			break;
		case 'h':
			usage(stdout);
			break;
		case '?':
		default:
			usage(stderr);
		}
	argc -= optind;
	argv += optind;

	if (argc > 0)
		fname = argv[0];
	else {
		fname = DEFAULT_OUTPUT;
		die_if_link(fname);
	}
	if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL) {
		warn(_("open failed: %s"), fname);
		fail();
	}

	shell = getenv("SHELL");
	if (shell == NULL)
		shell = _PATH_BSHELL;

	getmaster();
	if (!qflg)
		printf(_("Script started, file is %s\n"), fname);
	fixtty();

#ifdef HAVE_LIBUTEMPTER
	utempter_add_record(master, NULL);
#endif
	/* setup SIGCHLD handler */
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = 0;
	sa.sa_handler = finish;
	sigaction(SIGCHLD, &sa, NULL);

	/* init mask for SIGCHLD */
	sigprocmask(SIG_SETMASK, NULL, &block_mask);
	sigaddset(&block_mask, SIGCHLD);

	sigprocmask(SIG_SETMASK, &block_mask, &unblock_mask);
	child = fork();
	sigprocmask(SIG_SETMASK, &unblock_mask, NULL);

	if (child < 0) {
		warn(_("fork failed"));
		fail();
	}
	if (child == 0) {

		sigprocmask(SIG_SETMASK, &block_mask, NULL);
		subchild = child = fork();
		sigprocmask(SIG_SETMASK, &unblock_mask, NULL);

		if (child < 0) {
			warn(_("fork failed"));
			fail();
		}
		if (child)
			dooutput(timingfd);
		else
			doshell();
	} else {
		sa.sa_handler = resize;
		sigaction(SIGWINCH, &sa, NULL);
	}
	doinput();

	if (close_stream(timingfd) != 0)
		errx(EXIT_FAILURE, _("write error"));
	return EXIT_SUCCESS;
}
Exemplo n.º 9
0
int main(int argc, char **argv)
{
	char *tty = NULL;
	char *p;
	struct passwd *pwd;
	int c, fd = -1;
	int opt_e = 0;
	pid_t pid, pgrp, ppgrp, ttypgrp;

	/*
	 *	See if we have a timeout flag.
	 */
	opterr = 0;
	while((c = getopt(argc, argv, "ept:")) != EOF) switch(c) {
		case 't':
			timeout = atoi(optarg);
			break;
		case 'p':
			profile = 1;
			break;
		case 'e':
			opt_e = 1;
			break;
		default:
			usage();
			/* Do not exit! */
			break;
	}
#if 0
	if (geteuid() != 0) {
		fprintf(stderr, "sulogin: only root can run sulogin.\n");
		exit(1);
	}
#endif

	/*
	 *	See if we need to open an other tty device.
	 */
	saved_sigint  = signal(SIGINT,  SIG_IGN);
	saved_sigtstp = signal(SIGQUIT, SIG_IGN);
	saved_sigquit = signal(SIGTSTP, SIG_IGN);
	if (optind < argc) tty = argv[optind];

	if (tty || (tty = getenv("CONSOLE"))) {

		if ((fd = open(tty, O_RDWR)) < 0) {
			perror(tty);
			fd = dup(0);
		}

		if (!isatty(fd)) {
			fprintf(stderr, "%s: not a tty\n", tty);
			close(fd);
		} else {

			/*
			 *	Only go through this trouble if the new
			 *	tty doesn't fall in this process group.
			 */
			pid = getpid();
			pgrp = getpgid(0);
			ppgrp = getpgid(getppid());
			ttypgrp = tcgetpgrp(fd);

			if (pgrp != ttypgrp && ppgrp != ttypgrp) {
				if (pid != getsid(0)) {
					if (pid == getpgid(0))
						setpgid(0, getpgid(getppid()));
					setsid();
				}

				signal(SIGHUP, SIG_IGN);
				if (ttypgrp > 0)
					ioctl(0, TIOCNOTTY, (char *)1);
				signal(SIGHUP, SIG_DFL);
				close(0);
				close(1);
				close(2);
				if (fd > 2)
					close(fd);
				if ((fd = open(tty, O_RDWR|O_NOCTTY)) < 0) {
					perror(tty);
				} else {
					ioctl(0, TIOCSCTTY, (char *)1);
					tcsetpgrp(fd, ppgrp);
					dup2(fd, 0);
					dup2(fd, 1);
					dup2(fd, 2);
					if (fd > 2)
						close(fd);
				}
			} else
				if (fd > 2)
					close(fd);
		}
	} else if (getpid() == 1) {
		/* We are init. We hence need to set a session anyway */
		setsid();
		if (ioctl(0, TIOCSCTTY, (char *)1))
			perror("ioctl(TIOCSCTTY)");
	}

#if defined(SANE_TIO) && (SANE_TIO == 1)
	fixtty();
#endif

	/*
	 *	Get the root password.
	 */
	if ((pwd = getrootpwent(opt_e)) == NULL) {
		fprintf(stderr, "sulogin: cannot open password database!\n");
		sleep(2);
	}

	sushell(pwd);
	/*
	 *	Ask for the password.
	 */
	while(pwd) {
		if ((p = getpasswd(pwd->pw_passwd)) == NULL) break;
		if (pwd->pw_passwd[0] == 0 ||
		    strcmp(crypt(p, pwd->pw_passwd), pwd->pw_passwd) == 0)
			sushell(pwd);
		saved_sigquit = signal(SIGQUIT, SIG_IGN);
		saved_sigtstp = signal(SIGTSTP, SIG_IGN);
		saved_sigint  = signal(SIGINT,  SIG_IGN);
		printf("Login incorrect.\n");
	}

	/*
	 *	User pressed Control-D.
	 */
	return 0;
}
Exemplo n.º 10
0
int
Active_StartSession()
{	
	log = fopen("log_file", "w");

	/* FIXME hardcoded */
	if (pipe(xfer_fifo))
	{
		ERROR("Pipe creation failure");
		return 1;
	}
	/* FIXME end hardcoded */

	mpty = dopty();

	if (!mpty)
	{
		ERROR("Pty creation failed");
		return 1;
	}

	fixtty();

	if (USE_FORKS)
	{

		signal(SIGCHLD, finish);

		/* we create 2 forks, one acts as a new shell and the other 
		 * continues the process of this code.
		 *
		 * child 0 continues to run this code
		 * and child > 0 runs the new shell
		 */

		child = fork();

		if (child < 0)
		{
			ERROR("Creation of fork failed");
			return 1;
		}

		/* printf("CHILD %d\n", getpid()); */
		if (child == 0)
		{
			child = fork();

			/* printf("CHILD2 %d\n", getpid()); */
			if (child == 0)
			{
				subchild = child = fork();

				/* printf("CHILD3 %d\n", getpid()); */
				TRACE(Neuro_s("Process %d -- PTY size : col %d row %d", 
					getpid(),
					mpty->wsize.ws_col,
					mpty->wsize.ws_row));
				if (child == 0)
					doshell();
				else
					dooutput();
			}

			doinput();
		}
		else
			signal(SIGWINCH, resize);
	}
	else
	{
		signal(SIGWINCH, resize);

		child = fork();
		
		if (child < 0)
		{
			ERROR("Creation of fork failed");
			return 1;
		}

		if (child > 0)
			doshell();
	}

	fcntl(xfer_fifo[0], F_SETFL, O_NONBLOCK);
	close(xfer_fifo[1]);

	/* activate the polling */
	activated = 1;

	return 0;
}
Exemplo n.º 11
0
int main(int argc, char **argv)
{
	char *tty = NULL;
	char *p;
	struct passwd *pwd;
	int c, fd = -1;
	int opt_e = 0;
	pid_t pid, pgrp, ppgrp, ttypgrp;
	struct sigaction saved_sighup;

	static const struct option longopts[] = {
		{ "login-shell",  0, 0, 'p' },
		{ "timeout",      1, 0, 't' },
		{ "force",        0, 0, 'e' },
		{ "help",         0, 0, 'h' },
		{ "version",      0, 0, 'V' },
		{ NULL,           0, 0, 0 }
	};

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

	/*
	 * See if we have a timeout flag.
	 */
	while ((c = getopt_long(argc, argv, "ehpt:V", longopts, NULL)) != -1) {
		switch(c) {
		case 't':
			timeout = strtou32_or_err(optarg, _("invalid timeout argument"));
			break;
		case 'p':
			profile = 1;
			break;
		case 'e':
			opt_e = 1;
			break;
		case 'V':
			printf(UTIL_LINUX_VERSION);
			return EXIT_SUCCESS;
		case 'h':
			usage(stdout);
			return EXIT_SUCCESS;
		default:
			usage(stderr);
			/* Do not exit! */
			break;
		}
	}

	if (geteuid() != 0)
		errx(EXIT_FAILURE, _("only root can run this program."));

	/*
	 * See if we need to open an other tty device.
	 */
	mask_signal(SIGQUIT, SIG_IGN, &saved_sigquit);
	mask_signal(SIGTSTP, SIG_IGN, &saved_sigtstp);
	mask_signal(SIGINT,  SIG_IGN, &saved_sigint);
	if (optind < argc)
		tty = argv[optind];

	if (tty || (tty = getenv("CONSOLE"))) {

		if ((fd = open(tty, O_RDWR)) < 0) {
			warn(_("cannot open %s"), tty);
			fd = dup(0);
		}

		if (!isatty(fd)) {
			warn(_("%s: not a tty"), tty);
			close(fd);
		} else {

			/*
			 * Only go through this trouble if the new tty doesn't
			 * fall in this process group.
			 */
			pid = getpid();
			pgrp = getpgid(0);
			ppgrp = getpgid(getppid());
			ttypgrp = tcgetpgrp(fd);

			if (pgrp != ttypgrp && ppgrp != ttypgrp) {
				if (pid != getsid(0)) {
					if (pid == getpgid(0))
						setpgid(0, getpgid(getppid()));
					setsid();
				}

				sigaction(SIGHUP, NULL, &saved_sighup);
				if (ttypgrp > 0)
					ioctl(0, TIOCNOTTY, (char *)1);
				sigaction(SIGHUP, &saved_sighup, NULL);
				close(0);
				close(1);
				close(2);
				if (fd > 2)
					close(fd);
				if ((fd = open(tty, O_RDWR|O_NOCTTY)) < 0)
					warn(_("cannot open %s"), tty);
				else {
					ioctl(0, TIOCSCTTY, (char *)1);
					tcsetpgrp(fd, ppgrp);
					dup2(fd, 0);
					dup2(fd, 1);
					dup2(fd, 2);
					if (fd > 2)
						close(fd);
				}
			} else
				if (fd > 2)
					close(fd);
		}
	} else if (getpid() == 1) {
		/* We are init. We hence need to set a session anyway */
		setsid();
		if (ioctl(0, TIOCSCTTY, (char *)1))
			warn(_("TIOCSCTTY: ioctl failed"));
	}

	fixtty();

	/*
	 * Get the root password.
	 */
	if ((pwd = getrootpwent(opt_e)) == NULL) {
		warnx(_("cannot open password database."));
		sleep(2);
	}

	/*
	 * Ask for the password.
	 */
	while (pwd) {
		if ((p = getpasswd(pwd->pw_passwd)) == NULL)
			break;
		if (pwd->pw_passwd[0] == 0 ||
		    strcmp(crypt(p, pwd->pw_passwd), pwd->pw_passwd) == 0)
			sushell(pwd);
		mask_signal(SIGQUIT, SIG_IGN, &saved_sigquit);
		mask_signal(SIGTSTP, SIG_IGN, &saved_sigtstp);
		mask_signal(SIGINT,  SIG_IGN, &saved_sigint);
		fprintf(stderr, _("Login incorrect\n\n"));
	}

	/*
	 * User pressed Control-D.
	 */
	return EXIT_SUCCESS;
}
Exemplo n.º 12
0
int
main(int argc, char *argv[])
{
	uid_t ruidt;
	gid_t gidt;

	(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN	"SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);

	shell = getenv("SHELL");
	if (shell == NULL)
		shell = "/bin/sh";
	argc--, argv++;
	while (argc > 0 && argv[0][0] == '-') {
		switch (argv[0][1]) {

		case 'a':
			aflg++;
			break;

		default:
			fprintf(stderr,
			    gettext("usage: script [ -a ] [ typescript ]\n"));
			exit(1);
		}
		argc--, argv++;
	}
	if (argc > 0)
		fname = argv[0];
	ruidt = getuid();
	gidt = getgid();
	if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL) {
		perror(fname);
		fail();
	}
	setbuf(fscript, NULL);
	chown(fname, ruidt, gidt);
	getmaster();
	printf(gettext("Script started, file is %s\n"), fname);
	fixtty();

	(void) signal(SIGCHLD, finish);
	child = fork();
	if (child < 0) {
		perror("fork");
		fail();
	}
	if (child == 0) {
		subchild = child = fork();
		if (child < 0) {
			perror("fork");
			fail();
		}
		if (child)
			dooutput();
		else
			doshell();
	}
	doinput();
	/* NOTREACHED */
	return (0);
}
Exemplo n.º 13
0
/* zero if first move */
void
move(int okay)
{
    int i;			/* index */
    int l;			/* last man */

    l = 0;
    if (okay) {
        /* see if comp should double */
        if (gvalue < 64 && dlast != cturn && dblgood()) {
            writel(*Colorptr);
            dble();		/* double */
            /* return if declined */
            if (cturn != 1 && cturn != -1)
                return;
        }
        roll();
    }

    race = 0;
    for (i = 0; i < 26; i++) {
        if (board[i] < 0)
            l = i;
    }
    for (i = 0; i < l; i++) {
        if (board[i] > 0)
            break;
    }
    if (i == l)
        race = 1;

    /* print roll */
    if (tflag)
        curmove(cturn == -1 ? 18 : 19, 0);
    writel(*Colorptr);
    writel(" rolls ");
    writec(D0 + '0');
    writec(' ');
    writec(D1 + '0');
    /* make tty interruptable while thinking */
    if (tflag)
        cline();
    fixtty(noech);

    /* find out how many moves */
    mvlim = movallow();
    if (mvlim == 0) {
        writel(" but cannot use it.\n");
        nexturn();
        fixtty(raw);
        return;
    }

    /* initialize */
    for (i = 0; i < 4; i++)
        cp[i] = cg[i] = 0;

    /* strategize */
    trymove(0, 0);
    pickmove();

    /* print move */
    writel(" and moves ");
    for (i = 0; i < mvlim; i++) {
        if (i > 0)
            writec(',');
        wrint(p[i] = cp[i]);
        writec('-');
        wrint(g[i] = cg[i]);
        makmove(i);
    }
    writec('.');

    /* print blots hit */
    if (tflag)
        curmove(20, 0);
    else
        writec('\n');
    for (i = 0; i < mvlim; i++)
        if (h[i])
            wrhit(g[i]);
    /* get ready for next move */
    nexturn();
    if (!okay) {
        buflush();
        sleep(3);
    }
    fixtty(raw);		/* no more tty interrupt */
}
Exemplo n.º 14
0
void wrboard(void)
{
    int l;
    static const char bl[] = "|                       |   |                       |\n";
    static const char sv[] = "|                       |   |                       |    \n";

    fixtty(&noech);
    clear();

    if (tflag) {
	fboard();
	goto lastline;
    }
    writel("_____________________________________________________\n");
    writel(bl);
    strcpy(ln, bl);
    for (j = 1; j < 50; j += 4) {
	k = j / 4 + (j > 24 ? 12 : 13);
	ln[j + 1] = k % 10 + '0';
	ln[j] = k / 10 + '0';
	if (j == 21)
	    j += 4;
    }
    writel(ln);
    for (i = 0; i < 5; i++) {
	strcpy(ln, sv);
	for (j = 1; j < 50; j += 4) {
	    k = j / 4 + (j > 24 ? 12 : 13);
	    wrbsub();
	    if (j == 21)
		j += 4;
	}
	if (-board[25] > i)
	    ln[26] = 'w';
	if (-board[25] > i + 5)
	    ln[25] = 'w';
	if (-board[25] > i + 10)
	    ln[27] = 'w';
	l = 53;
	if (off[1] > i || (off[1] < 0 && off[1] + 15 > i)) {
	    ln[54] = 'r';
	    l = 55;
	}
	if (off[1] > i + 5 || (off[1] < 0 && off[1] + 15 > i + 5)) {
	    ln[55] = 'r';
	    l = 56;
	}
	if (off[1] > i + 10 || (off[1] < 0 && off[1] + 15 > i + 10)) {
	    ln[56] = 'r';
	    l = 57;
	}
	ln[l++] = '\n';
	ln[l] = '\0';
	writel(ln);
    }
    strcpy(ln, bl);
    ln[25] = 'B';
    ln[26] = 'A';
    ln[27] = 'R';
    writel(ln);
    strcpy(ln, sv);
    for (i = 4; i > -1; i--) {
	for (j = 1; j < 50; j += 4) {
	    k = ((j > 24 ? 53 : 49) - j) / 4;
	    wrbsub();
	    if (j == 21)
		j += 4;
	}
	if (board[0] > i)
	    ln[26] = 'r';
	if (board[0] > i + 5)
	    ln[25] = 'r';
	if (board[0] > i + 10)
	    ln[27] = 'r';
	l = 53;
	if (off[0] > i || (off[0] < 0 && off[0] + 15 > i)) {
	    ln[54] = 'w';
	    l = 55;
	}
	if (off[0] > i + 5 || (off[0] < 0 && off[0] + 15 > i + 5)) {
	    ln[55] = 'w';
	    l = 56;
	}
	if (off[0] > i + 10 || (off[0] < 0 && off[0] + 15 > i + 10)) {
	    ln[56] = 'w';
	    l = 57;
	}
	ln[l++] = '\n';
	ln[l] = '\0';
	writel(ln);
    }
    strcpy(ln, bl);
    for (j = 1; j < 50; j += 4) {
	k = ((j > 24 ? 53 : 49) - j) / 4;
	ln[j + 1] = k % 10 + '0';
	if (k > 9)
	    ln[j] = k / 10 + '0';
	if (j == 21)
	    j += 4;
    }
    writel(ln);
    writel("|_______________________|___|_______________________|\n");

  lastline:
    gwrite();
    if (tflag)
	curmove(18, 0);
    else {
	writec('\n');
	writec('\n');
    }
    fixtty(&bg_raw);
}
Exemplo n.º 15
0
/* zero if first move */
void
move(struct move *mm, int okay)
{
	int     i;		/* index */
	int     l;		/* last man */

	l = 0;
	if (okay) {
		/* see if comp should double */
		if (gvalue < 64 && dlast != cturn && dblgood()) {
			writel(*Colorptr);
			dble();	/* double */
			/* return if declined */
			if (cturn != 1 && cturn != -1)
				return;
		}
		roll(mm);
	}
	race = 0;
	for (i = 0; i < 26; i++) {
		if (board[i] < 0)
			l = i;
	}
	for (i = 0; i < l; i++) {
		if (board[i] > 0)
			break;
	}
	if (i == l)
		race = 1;

	/* print roll */
	if (tflag)
		curmove(cturn == -1 ? 18 : 19, 0);
	writel(*Colorptr);
	writel(" rolls ");
	writec(mm->D0 + '0');
	writec(' ');
	writec(mm->D1 + '0');
	/* make tty interruptable while thinking */
	if (tflag)
		cline();
	fixtty(&noech);

	/* find out how many moves */
	mm->mvlim = movallow(mm);
	if (mm->mvlim == 0) {
		writel(" but cannot use it.\n");
		nexturn();
		fixtty(&raw);
		return;
	}
	/* initialize */
	for (i = 0; i < 4; i++)
		cp[i] = cg[i] = 0;

	/* strategize */
	trymove(mm, 0, 0);
	pickmove(mm);

	/* print move */
	writel(" and moves ");
	for (i = 0; i < mm->mvlim; i++) {
		if (i > 0)
			writec(',');
		wrint(mm->p[i] = cp[i]);
		writec('-');
		wrint(mm->g[i] = cg[i]);
		makmove(mm, i);

		/*
		 * This assertion persuades gcc 4.5 that the loop
		 * doesn't result in signed overflow of i. mvlim
		 * isn't, or at least shouldn't be, changed by makmove
		 * at all.
		 */
		assert(mm->mvlim >= 0 && mm->mvlim <= 5);
	}
	writec('.');

	/* print blots hit */
	if (tflag)
		curmove(20, 0);
	else
		writec('\n');
	for (i = 0; i < mm->mvlim; i++)
		if (mm->h[i])
			wrhit(mm->g[i]);
	/* get ready for next move */
	nexturn();
	if (!okay) {
		buflush();
		sleep(3);
	}
	fixtty(&raw);		/* no more tty interrupt */
}
Exemplo n.º 16
0
int
ttyrec_main (int game, char *username, char *ttyrec_path, char* ttyrec_filename)
{
  char dirname[100];

  /* Note our PID to let children kill the main dgl process for idling */
  dgl_parent = getpid();
  child = subchild = input_child = 0;

  if (!ttyrec_path) {
      child = fork();
      if (child < 0) {
	  perror ("fork");
	  fail ();
      }
      if (child == 0) {
	  execvp (myconfig[game]->game_path, myconfig[game]->bin_args);
      } else {
	  int status;
	  (void) wait(&status);
      }
      return 0;
  }

  if (ttyrec_path[strlen(ttyrec_path)-1] == '/')
      snprintf (dirname, 100, "%s%s", ttyrec_path, ttyrec_filename);
  else
      snprintf (dirname, 100, "%s/%s", ttyrec_path, ttyrec_filename);

  atexit(&remove_ipfile);
  if ((fscript = fopen (dirname, "w")) == NULL)
    {
      perror (dirname);
      fail ();
    }
  setbuf (fscript, NULL);

  fixtty ();

  (void) signal (SIGCHLD, finish);
  child = fork ();
  if (child < 0)
    {
      perror ("fork");
      fail ();
    }
  if (child == 0)
    {
      subchild = child = fork ();
      if (child < 0)
        {
          perror ("fork");
          fail ();
        }
      if (child)
        {
          close (slave);
          ipfile = gen_inprogress_lock (game, child, ttyrec_filename);
	  ttyrec_id(game, username, ttyrec_filename);
          dooutput (myconfig[game]->max_idle_time);
        }
      else
	  doshell (game, username);
    }

  (void) fclose (fscript);

  wait_for_menu = 1;
  input_child = fork();
  if (input_child < 0)
  {
      perror ("fork2");
      fail ();
  }
  if (!input_child)
      doinput ();
  else
  {
      while (wait_for_menu)
	  sleep(1);
  }

  remove_ipfile();
  child = 0;

  return 0;
}