Renamed files.
--- a/Makefile Fri Sep 14 21:04:27 2007 +0200
+++ b/Makefile Fri Sep 14 21:06:10 2007 +0200
@@ -1,3 +1,3 @@
CFLAGS=-g
-dicta: dicta.o usocket.o error.o
+stdinmix: main.o usocket.o error.o
--- a/dicta.c Fri Sep 14 21:04:27 2007 +0200
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,179 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-#include "dicta.h"
-
-static int max(int a, int b)
-{
- if (a > b)
- return a;
- return b;
-}
-
-void fork_app(int *opipe, char * const command[])
-{
- int p_input[2]; /* from mpg321 to us */
- int p_output[2]; /* from us to mpg321 */
- int pid;
- int res;
-
- pipe(p_input);
- pipe(p_output);
- opipe[0] = p_input[0]; /* For us to read */
- opipe[1] = p_output[1]; /* For us to write */
-
- pid = fork();
-
- switch(pid)
- {
- case 0: /* child */
- close(p_input[0]);
- res = dup2(p_input[1], 1);
- if (res == -1) perror("Dup2 1");
- res = dup2(p_input[1], 2);
- if (res == -1) perror("Dup2 2");
- close(p_input[1]);
- close(p_output[1]);
- res = dup2(p_output[0], 0);
- if (res == -1) perror("Dup2 3");
- close(p_output[0]);
-
- execvp(command[0], command);
-
- perror("Cannot execlp mpg321");
- exit(-1);
- case -1:
- perror("Failed fork");
- exit(-1);
- default: /* parent */
- close(p_input[1]);
- close(p_output[0]);
- }
-}
-
-int forward_app_data(int in, int out)
-{
- char buf[100];
- int res;
-
- res = read(in, buf, sizeof(buf));
- if (res > 0)
- write(out, buf, res);
-
- return res;
-}
-
-void loop(const int *child_pipe, int lsocket)
-{
- char buf[100];
- fd_set read_set;
- int child_read, child_write;
- int maxfd;
- int opened_socket;
- int stdin_opened;
-
- child_read = child_pipe[0];
- child_write = child_pipe[1];
-
- stdin_opened = 1;
- opened_socket = -1; /* no socket opened */
- do
- {
- FD_ZERO(&read_set);
-
- if (stdin_opened)
- FD_SET(0, &read_set);
- FD_SET(child_read, &read_set);
- maxfd = child_read;
- if (opened_socket >= 0)
- {
- FD_SET(opened_socket, &read_set);
- maxfd = max(maxfd, opened_socket);
- }
- else
- {
- /* We only accept if we don't have any
- * connetion opened. */
- FD_SET(lsocket, &read_set);
- maxfd = max(maxfd, lsocket);
- }
-
- /* Will block */
- select(maxfd + 1, &read_set, 0, 0, 0);
-
- if (FD_ISSET(child_read, &read_set))
- {
- int res;
- res = forward_app_data(child_read, 1);
- if (res == 0)
- break;
- }
- if (FD_ISSET(0, &read_set))
- {
- int res;
- res = forward_app_data(0, child_write);
- if (res == 0)
- {
- close(child_write);
- stdin_opened = 0;
- }
- }
- if (opened_socket >= 0 && FD_ISSET(opened_socket, &read_set))
- {
- int res;
- res = forward_app_data(opened_socket, child_write);
- if (res == 0)
- {
- close(opened_socket);
- opened_socket = -1; /* no socket open */
- }
- }
- if (opened_socket == -1 && FD_ISSET(lsocket, &read_set))
- {
- opened_socket = accept_connection(lsocket);
- }
- } while(1);
-}
-
-int server(int argn, char * const argv[])
-{
- int p[2];
- int lsocket;
-
- fork_app(p, &argv[1]);
-
- lsocket = serve_socket();
-
- loop(p, lsocket);
-
- remove_socket(lsocket);
-
- return 0;
-}
-
-int client()
-{
- int cs;
- int res;
- cs = connect_socket();
-
- do
- {
- res = forward_app_data(0, cs);
- } while (res > 0);
-
- close(cs);
- return 0;
-}
-
-int main(int argn, char **argv)
-{
- int res;
- if (argn > 1)
- res = server(argn, argv);
- else
- res = client();
- return res;
-}
--- a/dicta.h Fri Sep 14 21:04:27 2007 +0200
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,9 +0,0 @@
-
-/* error.c */
-void error(const char *msg);
-
-/* usocket.c */
-int serve_socket();
-void remove_socket(int ls);
-int accept_connection(int ls);
-int connect_socket();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.c Fri Sep 14 21:06:10 2007 +0200
@@ -0,0 +1,179 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "dicta.h"
+
+static int max(int a, int b)
+{
+ if (a > b)
+ return a;
+ return b;
+}
+
+void fork_app(int *opipe, char * const command[])
+{
+ int p_input[2]; /* from mpg321 to us */
+ int p_output[2]; /* from us to mpg321 */
+ int pid;
+ int res;
+
+ pipe(p_input);
+ pipe(p_output);
+ opipe[0] = p_input[0]; /* For us to read */
+ opipe[1] = p_output[1]; /* For us to write */
+
+ pid = fork();
+
+ switch(pid)
+ {
+ case 0: /* child */
+ close(p_input[0]);
+ res = dup2(p_input[1], 1);
+ if (res == -1) perror("Dup2 1");
+ res = dup2(p_input[1], 2);
+ if (res == -1) perror("Dup2 2");
+ close(p_input[1]);
+ close(p_output[1]);
+ res = dup2(p_output[0], 0);
+ if (res == -1) perror("Dup2 3");
+ close(p_output[0]);
+
+ execvp(command[0], command);
+
+ perror("Cannot execlp mpg321");
+ exit(-1);
+ case -1:
+ perror("Failed fork");
+ exit(-1);
+ default: /* parent */
+ close(p_input[1]);
+ close(p_output[0]);
+ }
+}
+
+int forward_app_data(int in, int out)
+{
+ char buf[100];
+ int res;
+
+ res = read(in, buf, sizeof(buf));
+ if (res > 0)
+ write(out, buf, res);
+
+ return res;
+}
+
+void loop(const int *child_pipe, int lsocket)
+{
+ char buf[100];
+ fd_set read_set;
+ int child_read, child_write;
+ int maxfd;
+ int opened_socket;
+ int stdin_opened;
+
+ child_read = child_pipe[0];
+ child_write = child_pipe[1];
+
+ stdin_opened = 1;
+ opened_socket = -1; /* no socket opened */
+ do
+ {
+ FD_ZERO(&read_set);
+
+ if (stdin_opened)
+ FD_SET(0, &read_set);
+ FD_SET(child_read, &read_set);
+ maxfd = child_read;
+ if (opened_socket >= 0)
+ {
+ FD_SET(opened_socket, &read_set);
+ maxfd = max(maxfd, opened_socket);
+ }
+ else
+ {
+ /* We only accept if we don't have any
+ * connetion opened. */
+ FD_SET(lsocket, &read_set);
+ maxfd = max(maxfd, lsocket);
+ }
+
+ /* Will block */
+ select(maxfd + 1, &read_set, 0, 0, 0);
+
+ if (FD_ISSET(child_read, &read_set))
+ {
+ int res;
+ res = forward_app_data(child_read, 1);
+ if (res == 0)
+ break;
+ }
+ if (FD_ISSET(0, &read_set))
+ {
+ int res;
+ res = forward_app_data(0, child_write);
+ if (res == 0)
+ {
+ close(child_write);
+ stdin_opened = 0;
+ }
+ }
+ if (opened_socket >= 0 && FD_ISSET(opened_socket, &read_set))
+ {
+ int res;
+ res = forward_app_data(opened_socket, child_write);
+ if (res == 0)
+ {
+ close(opened_socket);
+ opened_socket = -1; /* no socket open */
+ }
+ }
+ if (opened_socket == -1 && FD_ISSET(lsocket, &read_set))
+ {
+ opened_socket = accept_connection(lsocket);
+ }
+ } while(1);
+}
+
+int server(int argn, char * const argv[])
+{
+ int p[2];
+ int lsocket;
+
+ fork_app(p, &argv[1]);
+
+ lsocket = serve_socket();
+
+ loop(p, lsocket);
+
+ remove_socket(lsocket);
+
+ return 0;
+}
+
+int client()
+{
+ int cs;
+ int res;
+ cs = connect_socket();
+
+ do
+ {
+ res = forward_app_data(0, cs);
+ } while (res > 0);
+
+ close(cs);
+ return 0;
+}
+
+int main(int argn, char **argv)
+{
+ int res;
+ if (argn > 1)
+ res = server(argn, argv);
+ else
+ res = client();
+ return res;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.h Fri Sep 14 21:06:10 2007 +0200
@@ -0,0 +1,9 @@
+
+/* error.c */
+void error(const char *msg);
+
+/* usocket.c */
+int serve_socket();
+void remove_socket(int ls);
+int accept_connection(int ls);
+int connect_socket();