Example #1
0
File: main.c Project: hotbig/apue
int
main(int argc, char *argv[])
{
	int		n, fd;
	char	buf[BUFFSIZE], line[MAXLINE];

	/* read filename to cat from stdin */
	while (fgets(line, MAXLINE, stdin) != NULL) {
		if (line[strlen(line) - 1] == '\n')
			line[strlen(line) - 1] = 0; /* replace newline with null */

		/* open the file */
		if ((fd = csopen(line, O_RDONLY)) < 0)
			continue;	/* csopen() prints error from server */

		/* and cat to stdout */
		while ((n = read(fd, buf, BUFFSIZE)) > 0)
			if (write(STDOUT_FILENO, buf, n) != n)
            {
                printf("write error\n");
                exit(-1);
            
			//	err_sys("write error");
            }
		if (n < 0)
        {
            printf("read error\n");
            exit(-1);
		//	err_sys("read error");
        }
		close(fd);
	}

	exit(0);
}
Example #2
0
int main(int argc, char *argv[])
{
	int n, fd;
	char buf[BUFFSIZE], line[MAXLINE];

	//read file to cat from stdin
	while (NULL != fgets(line, MAXLINE, stdin))
	{
		if ('\n' == line[strlen(line)-1])
		{
			line[strlen(line)-1] = '\0';
		}
		if (0 > (fd = csopen(line, O_RDONLY)))
		{
			fprintf(stderr, "csopen failed\n");
			continue;
		}
		//cat file to std out
		while (0 < (n = read(fd, buf, BUFFSIZE)))
		{
			if (n != write(STDOUT_FILENO, buf, n))
			{
				perror("write");
				return EXIT_FAILURE;
			}
			close(fd);
		}
	}
	return EXIT_SUCCESS;
}
Example #3
0
int
main(int argc, char *argv[])
{
	int		n, fd;
	char	buf[BUFFSIZE], line[MAXLINE];

	/* read filename to cat from stdin 从标准输入读一个路径名*/
	while (fgets(line, MAXLINE, stdin) != NULL) {
		if (line[strlen(line) - 1] == '\n')
			line[strlen(line) - 1] = 0; /* replace newline with null */

		/* open the file 使用csopen来联系open服务器进程,从其返回一个打开描述符*/
		if ((fd = csopen(line, O_RDONLY)) < 0)
			continue;	/* csopen() prints error from server */

		/* and cat to stdout 将该文件复制到标准输出*/
		while ((n = read(fd, buf, BUFFSIZE)) > 0)
			if (write(STDOUT_FILENO, buf, n) != n)
				err_sys("write error");
		if (n < 0)
			err_sys("read error");
		close(fd);
	}

	exit(0);
}
Example #4
0
int C2Lopen (char *name, char *mode, FILE **ifp, FILE **ofp) {
    int fd;

    if ((fd = csopen (name, CS_OPEN_READ)) == -1)
        return -1;
    fcntl (fd, F_SETFD, FD_CLOEXEC);
    *ifp = fdopen (fd, "r"), *ofp = fdopen (fd, "a+");
    return 0;
}
Example #5
0
int
csping(register Cs_t* state, const char* name)
{
	register int	fd;
	register int	n;

	sfsprintf(state->temp, sizeof(state->path), "/dev/tcp/%s/inet.echo", name);
	if ((fd = csopen(state, state->temp, 0)) < 0) return -1;
	n = (cswrite(state, fd, M, N) != N || csread(state, fd, state->temp, N, CS_LINE) != N || strncmp(M, state->temp, N)) ? -1 : 0;
	close(fd);
	if (n) messagef((state->id, NiL, -1, "ping: %s: no contact", name));
	return n;
}
Example #6
0
static int
reopen(register Cs_t* state, char* path)
{
	int		ret;
	Cs_t		tmp;

	static int	level;

	if (level >= 8)
		return -1;
	tmp = *state;
	level++;
	ret = csopen(&tmp, path, CS_OPEN_TEST);
	level--;
	state->addr = tmp.addr;
	state->port = tmp.port;
	return ret;
}
Example #7
0
static int
svc_connect(void* handle, int fd, Cs_id_t* id, int clone, char** argv)
{
	register State_t*	state = (State_t*)handle;
	register File_t*	fp;
	register char*		s;
	int			ad;
	int			flags = 0;
	Fid_t			fid;
	struct stat		st;

	NoP(id);
	NoP(clone);
	if (!argv)
		return(-1);
	while ((s = *argv++) && *s != '/')
		switch (*s)
		{
		case 'm':
			flags |= CAT_MSG;
			break;
		}
	if (!s || (ad = csopen(s, 0)) < 0 && (ad = open(s, O_CREAT|O_APPEND|O_WRONLY|O_BINARY, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0)
		return(-1);
	if (fstat(ad, &st))
	{
		close(ad);
		return(-1);
	}
	fid.dev = st.st_dev;
	fid.ino = st.st_ino;
	if (!(fp = (File_t*)hashlook(state->files, (char*)&fid, HASH_CREATE|HASH_SIZE(sizeof(File_t)), NiL)))
	{
		close(ad);
		return(-1);
	}
	if (!fp->reference++) fp->fd = ad;
	else close(ad);
	fp->flags |= flags;
	state->cat[fd] = fp;
	state->active++;
	state->dormant = 0;
	return(0);
}
Example #8
0
int
csclient(Cs_t* cs, int fd, const char* service, const char* prompt, char** argv, unsigned int flags)
{
	register int	i;
	char*		s;
	Sfio_t*		tmp;
	int		done;
	int		promptlen;
	int		timeout;
	ssize_t		n;
	int		sdf[2];
	Cspoll_t	fds[2];
	char		buf[8 * 1024];

	if (fd < 0 && (fd = csopen(cs, service, CS_OPEN_TEST)) < 0)
	{
		if (errno == ENOENT)
			error(3, "%s: server not running", service);
		else
			error(ERROR_SYSTEM|3, "%s: cannot open connect stream", service);
	}
#if _hdr_termios
	if (flags & CS_CLIENT_RAW)
	{
		tcgetattr(0, &state.old_term);
		atexit(restore);
		state.new_term = state.old_term;
		state.new_term.c_iflag &= ~(BRKINT|IGNPAR|PARMRK|INLCR|IGNCR|ICRNL);
		state.new_term.c_lflag &= ~(ECHO|ECHOK|ICANON|ISIG);
		state.new_term.c_cc[VTIME] = 0;
		state.new_term.c_cc[VMIN] = 1;
		tcsetattr(0, TCSANOW, &state.new_term);
	}
#endif
	sdf[0] = fd;
	sdf[1] = 1;
	if (argv && *argv)
	{
		fds[0].fd = 1;
		fds[0].events = CS_POLL_WRITE;
	}
	else
	{
		argv = 0;
		fds[0].fd = 0;
		fds[0].events = CS_POLL_READ;
	}
	fds[1].fd = fd;
	fds[1].events = CS_POLL_READ;
	done = 0;
	if (promptlen = (!argv && prompt && isatty(fds[0].fd) && isatty(1)) ? strlen(prompt) : 0)
		write(1, prompt, promptlen);
	timeout = CS_NEVER;
	tmp = 0;
	while (cspoll(cs, fds, elementsof(fds), timeout) > 0)
		for (i = 0; i < elementsof(fds); i++)
			if (fds[i].status & (CS_POLL_READ|CS_POLL_WRITE))
			{
				if (!i && argv)
				{
					if (!*argv)
					{
						argv = 0;
						fds[0].fd = 0;
						if (flags & CS_CLIENT_ARGV)
						{
							if (done++)
								return 0;
							timeout = 500;
						}
						fds[0].events = CS_POLL_READ;
						continue;
					}
					if (!tmp && !(tmp = sfstropen()))
						error(ERROR_SYSTEM|3, "out of space");
					for (;;)
					{
						s = *argv++;
						if ((flags & CS_CLIENT_SEP) && *s == ':' && !*(s + 1))
							break;
						if (sfstrtell(tmp))
							sfputc(tmp, ' ');
						sfprintf(tmp, "%s", s);
						if (!(flags & CS_CLIENT_SEP) || !*argv)
							break;
					}
					sfputc(tmp, '\n');
					n = sfstrtell(tmp);
					s = sfstruse(tmp);
				}
				else if ((n = read(fds[i].fd, s = buf, sizeof(buf) - 1)) < 0)
					error(ERROR_SYSTEM|3, "/dev/fd/%d: read error", fds[i].fd);
				if (!n)
				{
					if (done++)
						return 0;
					if (!i)
						write(sdf[i], "quit\n", 5);
					continue;
				}
				if (!i)
				{
#if _hdr_termios
					register char*	u;
					register int	m;

					s[n] = 0;
					if ((u = strchr(s, 035)))
					{
						if ((m = u - s) > 0 && write(sdf[i], s, m) != m)
							error(ERROR_SYSTEM|3, "/dev/fd/%d: write error", sdf[i]);
						tcsetattr(0, TCSANOW, &state.old_term);
						if (promptlen)
							write(1, prompt, promptlen);
						if ((n = read(fds[i].fd, s = buf, sizeof(buf) - 1)) <= 0)
						{
							write(1, "\n", 1);
							return 0;
						}
						buf[n - 1] = 0;
						if (*u == 'q' || *u == 'Q')
							return 0;
						tcsetattr(0, TCSANOW, &state.new_term);
						if (*u)
							error(1, "%s: unknown command", u);
						continue;
					}
#endif
				}
				if (write(sdf[i], s, n) != n)
					error(ERROR_SYSTEM|3, "/dev/fd/%d: write error", sdf[i]);
				if (sdf[i] == 1 && promptlen)
					write(1, prompt, promptlen);
			}
	return error_info.errors != 0;
}
Example #9
0
int
_cs_open(const char* apath, int op)
{
	return csopen(&cs, apath, op);
}
Example #10
0
int
sendsmtp(Sfio_t* fp, char* host, char** argv, off_t original)
{
	register char*	s;
	register char*	t;
	char*		e;
	int		n;
	int		fd;
	int		r;
	off_t		z;
	Sfio_t*		sp;
	Sfio_t*		rp;
	char		buf[PATH_MAX];
	char		svc[PATH_MAX];

	/*
	 * connect to the service
	 */

	sfsprintf(svc, sizeof(svc), "/dev/tcp/%s/inet.smtp", host);
	if ((fd = csopen(&cs, svc, 0)) < 0)
	{
		note(SYSTEM, "smtp: %s: cannot connect to service", svc);
		return -1;
	}
	if (!(sp = sfnew(NiL, NiL, SF_UNBOUND, fd, SF_WRITE)) ||
	    !(rp = sfnew(NiL, NiL, SF_UNBOUND, fd, SF_READ)))
	{
		if (sp)
			sfclose(sp);
		else
			close(fd);
		note(SYSTEM, "smtp: %s: cannot buffer service", svc);
		return -1;
	}

	/*
	 * verify
	 */

	do
	{
		if (!(s = sfgetr(rp, '\n', 1)))
			goto bad_recv;
		if (strtol(s, &e, 10) != SMTP_READY)
			goto bad_prot;
	} while (*e == '-');

	/*
	 * identify
	 */

	if (!(s = state.var.domain) || !*s)
		s = state.var.hostname;
	if (sfprintf(sp, "HELO %s\r\n", s) < 0)
		goto bad_send;
	do
	{
		if (!(s = sfgetr(rp, '\n', 1)))
			goto bad_recv;
		if (strtol(s, &e, 10) != SMTP_OK)
			goto bad_prot;
	} while (*(unsigned char*)e == SMTP_OK);

	/*
	 * from
	 */

	if (original)
	{
		if (!(s = sfgetr(fp, '\n', 1)) || !strneq(s, "From ", 5))
			goto bad_mesg;
		for (s += 5; isspace(*s); s++);
		for (t = s; *t && !isspace(*t); t++);
		if (!(n = t - s))
			goto bad_mesg;
		z = sfvalue(fp);
		if (sfprintf(sp, "MAIL FROM:<%*.*s>\r\n", n, n, s) < 0)
			goto bad_send;
	}
	else
	{
		z = 0;
		if ((state.var.domain ?
		     sfprintf(sp, "MAIL FROM:<%s@%s>\r\n", state.var.user, state.var.domain) :
		     sfprintf(sp, "MAIL FROM:<%s>\r\n", state.var.user)) < 0)
			goto bad_send;
	}
	do
	{
		if (!(s = sfgetr(rp, '\n', 1)))
			goto bad_recv;
		if (strtol(s, &e, 10) != SMTP_OK)
			goto bad_prot;
	} while (*e == '-');

	/*
	 * to
	 */

	while (s = *argv++)
	{
		if ((state.var.domain && !strchr(s, '@') ?
		     sfprintf(sp, "RCPT TO:<%s@%s>\r\n", s, state.var.domain) :
		     sfprintf(sp, "RCPT TO:<%s>\r\n", s)) < 0)
			goto bad_send;
		do
		{
			if (!(s = sfgetr(rp, '\n', 1)))
				goto bad_recv;
			if (strtol(s, &e, 10) != SMTP_OK)
				goto bad_prot;
		} while (*e == '-');
	}

	/*
	 * body
	 */

	if (sfprintf(sp, "DATA\r\n") < 0)
		goto bad_send;
	do
	{
		if (!(s = sfgetr(rp, '\n', 1)))
			goto bad_recv;
		if (strtol(s, &e, 10) != SMTP_START)
			goto bad_prot;
	} while (*e == '-');
	tmfmt(buf, sizeof(buf), "%+uDate: %a, %d %b %Y %H:%M:%S UT", NiL);
	if (sfputr(sp, buf, '\n') < 0)
		goto bad_send;
	if (sfprintf(sp, "From: <%s@%s>\n", state.var.user, host) < 0)
		goto bad_send;
	while (s = sfgetr(fp, '\n', 1))
	{
		if (sfprintf(sp, "%s%s\r\n", *s == '.' ? "." : "", s) < 0)
			goto bad_send;
		if (original && (z += sfvalue(fp)) >= original)
			break;
	}
	if (sfprintf(sp, ".\r\n") < 0)
		goto bad_send;
	do
	{
		if (!(s = sfgetr(rp, '\n', 1)))
			goto bad_recv;
		if (strtol(s, &e, 10) != SMTP_OK)
			goto bad_prot;
	} while (*e == '-');

	/*
	 * quit
	 */

	if (sfprintf(sp, "QUIT\r\n") < 0)
		goto bad_send;
	do
	{
		if (!(s = sfgetr(rp, '\n', 1)))
			goto bad_recv;
		if (strtol(s, &e, 10) != SMTP_CLOSE)
			goto bad_prot;
	} while (*e == '-');
	r = 0;
	goto done;
 bad_mesg:
	note(0, "smtp: bad message -- no From header");
	goto bad;
 bad_prot:
	if ((n = strlen(e)) > 0 && e[n - 1] == '\r')
		e[n - 1] = 0;
	note(0, "smtp: %s: service error:%s", svc, e);
	goto bad;
 bad_send:
	note(SYSTEM, "smtp: %s: service write error", svc);
	goto bad;
 bad_recv:
	note(SYSTEM, "smtp: %s: service read error", svc);
 bad:
	r = -1;
 done:
	sfclose(sp);
	sfclose(rp);
	return r;
}