Beispiel #1
0
int main() {
  char s[21], t[21];
  int n;

  printf("Unesite dve niske: ");
  scanf("%s%s", s, t); /*unosimo niske */
  n = mystrcspn(s, t); /*pozivamo funkciju i povratnu vrednost zapisujemo u n */
  printf("%d\n", n);

}
Beispiel #2
0
/*
 * Remove/replace vt100/220 screen manipulation escape sequences so they do
 * not litter the output.
 */
int
filter(char *buf, int len)
{
    static regmatch_t	pmatch[1];
#define	N_REG		16		/* number of regexes in reg[][] */
#define	N_CRs		2		/* number of CR replacements */
    static regex_t	preg[N_REG];
    static char		reg[N_REG][50] = {	/* vt100/220 escape codes */
				"\x1B""7\x1B\\[1;24r\x1B""8",	/* ds */
				"\x1B""8",			/* fs */

				"\x1B\\[2J",
				"\x1B\\[2K",			/* kE */

				"\x1B\\[[0-9]+;[0-9]+r",	/* cs */
				"\x1B\\[[0-9]+;[0-9]+H",	/* cm */

				"\x1B\\[\\?6l",
				"\x1B\\[\\?7l",			/* RA */
				"\x1B\\[\\?25h",		/* ve */
				"\x1B\\[\\?25l",		/* vi */
				"\x1B\\[K",			/* ce */
				"\x1B\\[7m",			/* mr - ansi */
				"\x1B\\[6n",			/* u7 - ansi */
				"\x07",				/* bell */

				/* replace these with CR */
				"\x1B\\[0m",			/* me */
				"\x1B""E",
			};
    char		bufstr[3] = {ESC, '\x07', '\0'},
			ebuf[256];
    size_t		nmatch = 1;
    int			err,
			x;
    static int		init = 0;

    if (len == 0 || mystrcspn(buf, bufstr) >= len)
	return(len);

    if (! init) {
	init++;
	for (x = 0; x < N_REG; x++) {
	    memset(&preg[x], 0, sizeof(preg[x]));
	    if ((err = regcomp(&preg[x], reg[x], REG_EXTENDED))) {
		regerror(err, &preg[x], ebuf, 256);
		fprintf(stderr, "%s: regex compile failed: %s\n", progname,
			ebuf);
		abort();
	    }
	}
    }

    for (x = 0; x < N_REG - N_CRs; x++) {
	if ((err = regexec(&preg[x], buf, nmatch, pmatch, 0))) {
	    if (err != REG_NOMATCH) {
		regerror(err, &preg[x], ebuf, 256);
		fprintf(stderr, "%s: regexec failed: %s\n", progname, ebuf);
		abort();
	    }
	} else {
	    if (len - pmatch[0].rm_eo <= 0) {
		buf[pmatch[0].rm_so] = '\0';
	    } else {
		memmove(buf + pmatch[0].rm_so, buf + pmatch[0].rm_eo,
			len - pmatch[0].rm_eo + 1);
	    }
	    len -= pmatch[0].rm_eo - pmatch[0].rm_so;
	    /* start over with the first regex */
	    x = -1;
	}
    }

    /* now the CR NL replacements */
    for (x = N_REG - N_CRs; x < N_REG; x++) {
	if ((err = regexec(&preg[x], buf, nmatch, pmatch, 0))) {
	    if (err != REG_NOMATCH) {
		regerror(err, &preg[x], ebuf, 256);
		fprintf(stderr, "%s: regexec failed: %s\n", progname, ebuf);
		abort();
	    }
	} else {
	    *(buf + pmatch[0].rm_so++) = '\r';
	    *(buf + pmatch[0].rm_so++) = '\n';
	    if (len - pmatch[0].rm_eo == 0) {
		buf[pmatch[0].rm_so] = '\0';
	    } else {
		memmove(buf + pmatch[0].rm_so, buf + pmatch[0].rm_eo,
			len - pmatch[0].rm_eo + 1);
	    }
	    len -= pmatch[0].rm_eo - pmatch[0].rm_so;
	    /* start over with the first CR regex */
	    x = N_REG - 1 - N_CRs;
	}
    }

    return(len);
}
Beispiel #3
0
int
main(int argc, char **argv, char **ev)
{
    extern char		*optarg;
    extern int		optind;
    char		hbuf[BUFSZ],		/* hlogin buffer */
			ptyname[FILENAME_MAX + 1],
			tbuf[BUFSZ],		/* telnet/ssh buffer */
			tbufstr[5] = {ESC, '\x07', '\r', '\n', '\0'};
    int			bytes,			/* bytes read/written */
			devnull,
			i,
			rval = EX_OK,
			ptym,			/* master pty */
			ptys;			/* slave pty */
    ssize_t		idx,			/* strcspan span */
			hlen = 0,		/* len of hbuf */
			tlen = 0;		/* len of tbuf */
    struct pollfd	pfds[3];
    struct termios	tios;

    environ = ev;

    /* get just the basename() of our exec() name and strip a .* off the end */
    if ((progname = strrchr(argv[0], '/')) != NULL)
	progname += 1;
    else
	progname = argv[0];
    if (strrchr(progname, '.') != NULL)
	*(strrchr(progname, '.')) = '\0';

    while ((i = getopt(argc, argv, "dhvt:")) != -1 )
	switch (i) {
	case 'd':
	    debug++;
	    break;
	case 't':
	    timeo = atoi(optarg);
	    if (timeo < 1)
		timeo = 1;
	    break;
	case 'v':
	    vers();
	    return(EX_OK);
	case 'h':
	default:
	    usage();
	    return(EX_USAGE);
	}

    if (argc - optind < 2) {
	usage();
	return(EX_USAGE);
    }

    unsetenv("DISPLAY");

    for (sigrx = 3; sigrx < 10; sigrx++)
	close(sigrx);

    /* allocate pty for telnet/ssh, then fork and exec */
    if (openpty(&ptym, &ptys, ptyname, NULL, NULL)) {
	fprintf(stderr, "%s: could not allocate pty: %s\n", progname,
		strerror(errno));
	return(EX_TEMPFAIL);
    }
    /* make the pty raw */
    if (tcgetattr(ptys, &tios)) {
	fprintf(stderr, "%s: tcgetattr() failed: %s\n", progname,
		strerror(errno));
	return(EX_OSERR);
    }
    tios.c_lflag &= ~ICANON;
#ifdef VMIN
    tios.c_cc[VMIN] = 1;
    tios.c_cc[VTIME] = 0;
#endif
    if (tcsetattr(ptys, TCSANOW, &tios)) {
	fprintf(stderr, "%s: tcsetattr() failed: %s\n", progname,
		strerror(errno));
	return(EX_OSERR);
    }

    /*
     * if a tty, make it raw as the hp echos _everything_, including
     * passwords.
     */
    tios.c_lflag &= ~ECHO;
    if (isatty(fileno(stdin))) {
	if (tcgetattr(fileno(stdin), &tios)) {
	    fprintf(stderr, "%s: tcgetattr() failed: %s\n", progname,
		strerror(errno));
	    return(EX_OSERR);
	}
	tios.c_lflag &= ~ECHO;
	tios.c_lflag &= ~ICANON;
#ifdef VMIN
	tios.c_cc[VMIN] = 1;
	tios.c_cc[VTIME] = 0;
#endif
	if (tcsetattr(fileno(stdin), TCSANOW, &tios)) {
	    fprintf(stderr, "%s: tcsetattr() failed: %s\n", progname,
		strerror(errno));
	    return(EX_OSERR);
	}
    }

    /* zero the buffers */
    memset(hbuf, 0, BUFSZ);
    memset(tbuf, 0, BUFSZ);

    /* reap our children, must be set-up *after* openpty() */
    signal(SIGCHLD, reapchild);

    if ((child = fork()) == -1) {
	fprintf(stderr, "%s: fork() failed: %s\n", progname, strerror(errno));
	return(EX_TEMPFAIL);
    }

    if (child == 0) {
	struct winsize ws;

	/*
	 * Make sure our terminal length and width are something greater
	 * than 1, for pagers on stupid boxes.
	 */
	ioctl(ptys, TIOCGWINSZ, &ws);
	ws.ws_row = 24;
	ws.ws_col = 132;
	ioctl(ptys, TIOCSWINSZ, &ws);

	signal(SIGCHLD, SIG_DFL);
	/* close the master pty & std* inherited from the parent */
	close(ptym);
	if (ptys != 0)
	    close(0);
	if (ptys != 1)
	    close(1);
	if (ptys != 2)
	    close(2);
#ifdef TIOCSCTTY
	setsid();
	if (ioctl(ptys, TIOCSCTTY, NULL) == -1) {
	    snprintf(ptyname, FILENAME_MAX, "%s: could not set controlling "
		     "tty: %s\n", progname, strerror(errno));
	    write(0, ptyname, strlen(ptyname));
	    return(EX_OSERR);
	}
#endif

	/* close stdin/out/err and attach them to the pipes */
	if (dup2(ptys, 0) == -1 || dup2(ptys, 1) == -1 || dup2(ptys, 2) == -1) {
	    snprintf(ptyname, FILENAME_MAX, "%s: dup2() failed: %s\n", progname,
		     strerror(errno));
	    write(0, ptyname, strlen(ptyname));
	    return(EX_OSERR);
	}
	if (ptys > 2)
	    close(ptys);

	/* exec telnet/ssh */
	execvp(argv[optind], argv + optind);
	snprintf(ptyname, FILENAME_MAX, "%s: execvp() failed: %s\n", progname,
		 strerror(errno));
	write(0, ptyname, strlen(ptyname));
	return(EX_TEMPFAIL);
	/*NOTREACHED*/
    }

    /* parent */
    if (debug)
	fprintf(stderr, "child %d\n", (int)child);

    signal(SIGHUP, sighdlr);

    /* close the slave pty */
    close(ptys);

    devnull = open("/dev/null", O_RDWR);

    /* make FDs non-blocking */
    if (fcntl(ptym, F_SETFL, O_NONBLOCK) ||
	fcntl(fileno(stdin), F_SETFL, O_NONBLOCK) ||
	fcntl(fileno(stdout), F_SETFL, O_NONBLOCK)) {
	fprintf(stderr, "%s: fcntl(NONBLOCK) failed: %s\n", progname,
		strerror(errno));
	exit(EX_OSERR);
    }

    /* loop to read on stdin and ptym */
#define	POLLEXP	(POLLERR | POLLHUP | POLLNVAL)
    pfds[0].fd = fileno(stdin);
    pfds[0].events = POLLIN | POLLEXP;
    pfds[1].fd = fileno(stdout);
    pfds[1].events = POLLEXP;
    pfds[2].fd = ptym;
    pfds[2].events = POLLIN | POLLEXP;

    /* shuffle data across the pipes until we see EOF or a read/write error */
    sigrx = 0;
    while (1) {
	bytes = poll(pfds, 3, (timeo * 1000));
	if (bytes == 0) {
	    if (sigrx)
		break;
	    /* timeout */
	    continue;
	}
	if (bytes == -1) {
	    switch (errno) {
	    case EAGAIN:
	    case EINTR:
		break;
	    default:
		rval = EX_IOERR;
		break;
	    }
	    continue;
	}

	/*
	 * write buffers first
	 * write hbuf (aka hlogin/stdin/pfds[0]) -> telnet (aka ptym/pfds[2])
	 */
	if ((pfds[2].revents & POLLOUT) && hlen) {
	    if ((bytes = write(pfds[2].fd, hbuf, hlen)) < 0 &&
		errno != EINTR && errno != EAGAIN) {
		fprintf(stderr, "%s: write() failed: %s\n", progname,
			strerror(errno));
		hlen = 0;
		hbuf[0] = '\0';
		break;
	    } else if (bytes > 0) {
		hlen -= bytes;
		if (hlen < 1) {
		    pfds[2].events &= ~POLLOUT;
		    hbuf[0] = '\0';
		} else
		    memmove(hbuf, hbuf + bytes, hlen + 1);
	    }
	}
	if (pfds[2].revents & POLLEXP) {
	    hlen = 0;
	    hbuf[0] = '\0';
	    break;
	}

	/* write tbuf (aka telnet/ptym/pfds[2]) -> hlogin (stdout/pfds[1]) */
	if ((pfds[1].revents & POLLOUT) && tlen) {
	    /*
	     * if there is an escape char that was not filtered by filter(),
	     * we need to write only up to that point and wait for
	     * the bits that complete the escape sequence.  if at least
	     * two bytes follow it and it doesn't look like we should expect
	     * more data, write it anyway as filter() didnt match it.
	     */
	    bytes = tlen;
	    idx = mystrcspn(tbuf, tbufstr);
	    if (idx) {
		if (tbuf[idx] == ESC)
		    bytes = idx + complete_esc(&tbuf[idx], tlen - idx);
		if (tbuf[idx] == '\r' || tbuf[idx] == '\n') {
		    bytes = ++idx;
		    if (tbuf[idx] == '\r' || tbuf[idx] == '\n')
			bytes++;
		}
	    } else {
		if (tbuf[0] == ESC)
		    bytes = complete_esc(tbuf, tlen);
		if (tbuf[0] == '\r' || tbuf[0] == '\n') {
		    bytes = 1;
		    if (tbuf[1] == '\r' || tbuf[1] == '\n')
			bytes++;
		}
	    }

	    if ((bytes = write(pfds[1].fd, tbuf, bytes)) < 0 &&
		errno != EINTR && errno != EAGAIN) {
		fprintf(stderr, "%s: write() failed: %s\n", progname,
			strerror(errno));
		/* dont bother trying to flush tbuf */
		tlen = 0;
		tbuf[0] = '\0';
		break;
	    } else if (bytes > 0) {
		tlen -= bytes;
		if (tlen < 1) {
		    tbuf[0] = '\0';
		    pfds[1].events &= ~POLLOUT;
		} else
		    memmove(tbuf, tbuf + bytes, tlen + 1);
	    }
	}
	if (pfds[1].revents & POLLEXP) {
	    /* dont bother trying to flush tbuf */
	    tlen = 0;
	    tbuf[0] = '\0';
	    break;
	}

	/* read hlogin (aka stdin/pfds[0]) -> hbuf */
	if (pfds[0].revents & POLLIN) {
	    if (BUFSZ - hlen > 1) {
		bytes = read(pfds[0].fd, hbuf + hlen, (BUFSZ - 1) - hlen);
		if (bytes > 0) {
		    hlen += bytes;
		    hbuf[hlen] = '\0';
		    pfds[2].events |= POLLOUT;
		} else if (bytes < 0 && errno != EAGAIN && errno != EINTR) {
		    /* read error */
		    break;
		}
	    }
	}
	if (pfds[0].revents & POLLEXP)
	    break;

	/* read telnet/ssh (aka ptym/pfds[2]) -> tbuf, then filter */
	if (pfds[2].revents & POLLIN) {
	    if (BUFSZ - tlen > 1) {
		bytes = read(pfds[2].fd, tbuf + tlen, (BUFSZ - 1) - tlen);
		if (bytes > 0) {
		    tlen += bytes;
		    tbuf[tlen] = '\0';
		    tlen = filter(tbuf, tlen);
		    if (tlen > 0)
			pfds[1].events |= POLLOUT;
		} else if (bytes < 0 && errno != EAGAIN && errno != EINTR) {
		    /* read error */
		    break;
		}
	    }
	}
	if (pfds[2].revents & POLLEXP)
	    break;
    }
    /* try to flush any remaining data from our buffers */
    if (hlen) {
	(void)write(pfds[2].fd, hbuf, hlen);
	hlen = 0;
    }
    if (tlen) {
	(void)write(pfds[1].fd, tbuf, tlen);
	tlen = 0;
    }
    if ((bytes = read(pfds[2].fd, tbuf, (BUFSZ - 1))) > 0) {
	tbuf[bytes] = '\0';
	tlen = filter(tbuf, bytes);
	(void)write(pfds[1].fd, tbuf, tlen);
    }
    tcdrain(pfds[1].fd);
    if ((hlen = read(pfds[0].fd, hbuf, (BUFSZ - 1))) > 0) {
	(void)write(pfds[2].fd, hbuf, hlen);
    }
    tcdrain(pfds[2].fd);

    if (child && ! kill(child, SIGINT))
	reapchild(SIGCHLD);

    return(rval);
}