Exemplo n.º 1
0
int main(int argc, char *argv[])
{
	shr_key_t key;
	shr_t shr;
	shr_t revshr;
	ssize_t got = 1, wrote;
	pid_t pid;
	int closed = 0;
	size_t ptr, len;

	if (argc != 1) {
		fprintf(stderr, "See README for usage.\n");
		return 1;
	}

	SHR_PRIVATE(&key, BUFFER_SIZE, 3);
	t (shr_open(&shr, &key, SHR_WRITE));

	t (shr_reverse_dup(&shr, &revshr));
	t (pid = fork());

	if (pid)
		shr = revshr;

	while (!pid && got) {
		char* buf;
		t (shr_write(&shr, &buf));
		t (got = read(STDIN_FILENO, buf, BUFFER_SIZE));
		t (shr_write_done(&shr, (size_t)got));
	}

	while (pid && !closed) {
		const char* buf;
		t (shr_read(&shr, &buf, &len));
		for (ptr = 0; ptr < len;) {
			t (wrote = write(STDOUT_FILENO, buf, len));
			ptr += (size_t)wrote;
		}
		t (closed = shr_read_done(&shr));
	}

	shr_close(&shr);
	if (pid)
		shr_remove(&shr);
	return 0;
 fail:
	perror(called);
	shr_remove_by_key(&key);
	return 1;
	(void) argv;
}
Exemplo n.º 2
0
Arquivo: test6.c Projeto: rikxqd/misc
int main() {
 struct shr *s=NULL;
 int rc = -1;

 unlink(ring);
 if (shr_init(ring, 6, 0) < 0) goto done;

 s = shr_open(ring);
 if (s == NULL) goto done;

 printf("writing ...");
 if (shr_write(s, &data[0], 3) < 0) goto done;
 printf("ok\n");

 printf("reading ...");
 ssize_t nr;
 nr = shr_read(s, out, sizeof(out));
 if (nr < 0) goto done;
 printf("read %ld bytes\n", (long)nr);
 if (nr > 0) printf("%.*s\n", (int)nr, out);

 printf("writing ...");
 if (shr_write(s, &data[3], 3) < 0) goto done;
 printf("ok\n");

 printf("reading ...");
 nr = shr_read(s, out, sizeof(out));
 if (nr < 0) goto done;
 printf("read %ld bytes\n", (long)nr);
 if (nr > 0) printf("%.*s\n", (int)nr, out);

 rc = 0;

done:
 printf("end\n");
 if (s) shr_close(s);
 return rc;
}