/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * $Id: pipesplit.c,v 1.5 2012/01/25 19:39:01 james.stevenson Exp $ * * Author: * NAME: James Stevenson * WWW: http://www.stev.org * */ #include #include #include #include #include #include void print_usage(FILE *fp, char *app) { fprintf(fp, "Usage: %s [opts] \n", app); fprintf(fp, "\n"); fprintf(fp, " -h Print this help\n"); fprintf(fp, " -n Number of processes\n"); fprintf(fp, "\n"); } int main(int argc, char **argv) { char *cmd = 0; char *buf = 0; size_t buflen = 0; FILE **pipes = 0; int pcount = 0; int cur = 0; int i = 0; int c = 0; while( (c = getopt(argc, argv, "n:h:")) != -1) { switch(c) { case 'n': pcount = atoi(optarg); if (pcount <= 0) { print_usage(stderr, argv[0]); exit(EXIT_FAILURE); } break; case 'h': print_usage(stdout, argv[0]); exit(EXIT_SUCCESS); default: break; } } if (optind < argc) { cmd = argv[optind]; } else { print_usage(stderr, argv[0]); exit(EXIT_FAILURE); } pipes = malloc(sizeof(FILE *) * pcount); if (!pipes) { perror("malloc"); exit(EXIT_FAILURE); } for(i=0;i= 0) { if (cur == pcount) cur = 0; fprintf(pipes[cur], "%s", buf); cur++; } if (buf != NULL) { free(buf); buf = 0; } for(i=0;i