Example #1
0
/*
 * Read a line from the user.  Call our buf_args() function to
 * break it into an argv-style array, and call userfunc() to
 * process the arguments.
 */
int
prompt_read(char *prompt, int (*userfunc)(int, char **))
{
	int		n;
	char	c, *ptr;

	tty_reset(STDIN_FILENO);	/* allow user's editing chars */

	n = strlen(prompt);
	if (write(STDOUT_FILENO, prompt, n) != n)
		err_sys("write error");

	ptr = cmdargs;
	for ( ; ; ) {
		if ((n = read(STDIN_FILENO, &c, 1)) < 0)
			err_sys("read error");
		else if (n == 0)
			break;
		if (c == '\n')
			break;
		if (ptr < &cmdargs[MAXLINE-2])
			*ptr++ = c;
	}
	*ptr = 0;		/* null terminate */

	tty_raw(STDIN_FILENO);		/* reset tty mode to raw */

	/*
	 * Return whatever userfunc() returns.
	 */
	return(buf_args(cmdargs, userfunc));
}
Example #2
0
File: opend.c Project: clamxyz/apue
void request(char *buf, int nread, int fd)
{
	int newfd;

	if (buf[nread-1] != 0)
	{
		sprintf(errmsg, "request not null terminated:%*.*s\n", nread, nread, buf);
		send_err(fd, -1, errmsg);
		return;
	}

	if (buf_args(buf, cli_args) < 0) {
		send_err(fd, -1, errmsg);
		return;
	}

	if ( (newfd = open(pathname, oflag)) < 0)
	{
		sprintf(errmsg, "can't open %s: %s\n", pathname, strerror(errno));
		send_err(fd, -1, errmsg);
		return;
	}
	
	if (send_fd(fd, newfd) < 0)
		err_sys("send_fd error", errno);
	close(newfd);	
}
Example #3
0
void
handle_request(char *buf, int nread, int fd)
{
	int		newfd;

	if (buf[nread-1] != 0) {
		snprintf(errmsg, MAXLINE-1,
		  "request not null terminated: %*.*s\n", nread, nread, buf);
		send_err(fd, -1, errmsg);
		return;
	}
	if (buf_args(buf, cli_args) < 0) {	/* parse args & set options */
		send_err(fd, -1, errmsg);
		return;
	}
	if ((newfd = open(pathname, oflag)) < 0) {
		snprintf(errmsg, MAXLINE-1, "can't open %s: %s\n", pathname,
		  strerror(errno));
		send_err(fd, -1, errmsg);
		return;
	}
	if (send_fd(fd, newfd) < 0)		/* send the descriptor */
		err_sys("send_fd error");
	close(newfd);		/* we're done with descriptor */
}
Example #4
0
void handle_request(char *buf, int nread, int clifd, uid_t uid)
{
	int	newfd;

	if (buf[nread - 1] != 0) {
		snprintf(errmsg, MAXLINE - 1,
				"request from uid %d not null terminated: %*.*s\n",
				uid, nread, nread, buf);
		send_err(clifd, -1, errmsg);
		return;
	}
	log_msg("request: %s, from uid %d", buf, uid);
	/* parse the arguments, set options */
	if (buf_args(buf, cli_args) < 0) {
		send_err(clifd, -1, errmsg);
		log_msg(errmsg);
		return;
	}

	if ((newfd = open(pathname, oflag)) < 0) {
		snprintf(errmsg, MAXLINE - 1, "can't open %s: %s\n",
				pathname, strerror(errno));
		send_err(clifd, -1, errmsg);
		log_msg(errmsg);
		return;
	}

	/* send the desrciptor */
	if (send_fd(clifd, newfd) < 0)
		log_sys("send_fd error");
	log_msg("send fd %d over fd %d for %s", newfd, clifd, pathname);
	close(newfd);	/* we're done with desrciptor */
}
Example #5
0
void
handle_request(char *buf, int nread, int fd)
{
	int		newfd;

	if (buf[nread-1] != 0) {
		snprintf(errmsg, MAXLINE-1,
		  "request not null terminated: %*.*s\n", nread, nread, buf);
		send_err(fd, -1, errmsg);
		return;
	}
	if (buf_args(buf, cli_args) < 0) {	/* 将客户进程请求分解成标准argv型的参数表,调用cli_args处理客户进程的参数*/
		send_err(fd, -1, errmsg);
		return;
	}
	if ((newfd = open(pathname, oflag)) < 0) { // 打开相应文件
		snprintf(errmsg, MAXLINE-1, "can't open %s: %s\n", pathname,
		  strerror(errno));
		send_err(fd, -1, errmsg);
		return;
	}
	if (send_fd(fd, newfd) < 0)		/* 经由fd管道将描述符回送给客户进程*/
		err_sys("send_fd error");
	close(newfd);		/* we're done with descriptor */
}
Example #6
0
void handle_request(char *buf, int nread, int fd)
{
	int	newfd;

	if (buf[nread - 1] != 0) {
		/*
		 * "%*.*s", m, n, "string"
		 * total field width is m
		 * precision is n.
		 * this manner can limit print max(m, n) chars up to 
		 * max(n, m)
		 */
		snprintf(errmsg, MAXLINE - 1,
				"request not null terminated: %*.*s\n",
				nread, nread, buf);
		send_err(fd, -1, errmsg);
		return;
	}
	if (buf_args(buf, cli_args) < 0) { /* parse args & set options */
		send_err(fd, -1, errmsg);
		return;
	}
	if ((newfd = open(pathname, oflag)) < 0) {
		snprintf(errmsg, MAXLINE - 1, "can't open %s: %s\n", pathname,
				strerror(errno));
		send_err(fd, -1, errmsg);
		return;
	}
	if (send_fd(fd, newfd) < 0) /* send the descriptor */
		err_sys("send_fd error");
	close(newfd); /* we're done with descriptor */
}
Example #7
0
static void msg_parse_arg(char *buf, int nread, int clifd, uid_t uid)
{
	int newfd;
	int res;

	if (buf[nread-1] != 0) {
	dlog(L_ERR, "request from uid %d not null terminated: %*.*s\n",
	uid, nread, nread, buf);
	serv_send_ack(clifd, -1);
	return;
	}

	dlog(L_INFO, "request: %s, from uid %d\n", buf, uid);

	/* parse the arguments, set options */
	if ((res = buf_args(buf, clifd, msg_arg_handle)) < 0) {
	serv_send_ack(clifd, res);
		dlog(L_ERR, "parse arg from uid %d error\n", uid);
	return;
	}

	serv_send_ack(clifd, 0);

}