From: Simone Piccardi Date: Sat, 18 Aug 2007 12:35:00 +0000 (+0000) Subject: Versione (parzialmente) funzionante, ho il dubbio che come su Feisty a X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=commitdiff_plain;h=fb1020bd2b32afc135baa00f2443bb0ceed3520a Versione (parzialmente) funzionante, ho il dubbio che come su Feisty a 32 bit ci sia qualcosa ancora che non va anche sulla 64 bit (perlomeno le syscall vanno, ma pare non prendere {{{SPLICE_F_NONBLOCK}}}. --- diff --git a/sources/tee.c b/sources/tee.c index 4c4a872..18f01a2 100644 --- a/sources/tee.c +++ b/sources/tee.c @@ -1,22 +1,62 @@ +/* tee.c + * + * Copyright (C) 2007 Simone Piccardi + * + * 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 + * 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., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +/**************************************************************** + * + * Program tee.c: + * A sample program that duolicete stdin to stdout and a file + * + * Author: Simone Piccardi + * Aug. 2007 + * + ****************************************************************/ +/* + * Include needed headers + */ #define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include - +#include /* file control functions */ +#include /* unix standard library */ +#include /* C standard library */ +#include /* error definitions and routines */ +#include /* standard I/O library */ +#include /* C strings library */ +#include /* file characteristics constants and functions */ +#include /* primitive system data types */ +#include /* system limits constants, types and functions */ +#include "macros.h" +/* + * Function and globals definitions + */ +void usage(void); /* Help printing routine */ +/* + * Main program + */ int main(int argc, char *argv[]) { /* * Variables definition */ int i; - int size = 4096; + size_t size = 4096; int fd; int len, nwrite; + struct stat sb; /* * Input section: decode command line parameters * Use getopt function @@ -56,7 +96,27 @@ int main(int argc, char *argv[]) strerror(errno)); exit(EXIT_FAILURE); } + + + if (fstat(STDIN_FILENO, &sb) < 0) { + perror("stat"); + exit(EXIT_FAILURE); + } + if (!S_ISFIFO(sb.st_mode)) { + fprintf(stderr, "stdin must be a pipe\n"); + exit(EXIT_FAILURE); + } + if (fstat(STDOUT_FILENO, &sb) < 0) { + perror("stat"); + exit(EXIT_FAILURE); + } + if (!S_ISFIFO(sb.st_mode)) { + fprintf(stderr, "stdout must be a pipe\n"); + exit(EXIT_FAILURE); + } + /* tee loop */ + //debug("Size %d\n", size); while (1) { /* copy stdin to stdout */ len = tee(STDIN_FILENO, STDOUT_FILENO, size, SPLICE_F_NONBLOCK); @@ -70,6 +130,7 @@ int main(int argc, char *argv[]) } else { if (len == 0) break; } + fprintf(stderr, "Copied %d byte\n", len); /* write data to the file using splice */ while (len > 0) { nwrite = splice(STDIN_FILENO, NULL, fd, NULL, len, SPLICE_F_MOVE); @@ -83,3 +144,16 @@ int main(int argc, char *argv[]) close(fd); exit(EXIT_SUCCESS); } + +/* + * routine to print usage info and exit + */ +void usage(void) { + printf("Program tee: duplicate stdin to stdout and a file\n"); + printf("Usage:\n"); + printf(" splicecp [-h] [-s N] filename\n"); + printf(" -h print this help\n"); + printf(" -s N set a buffer size of N bytes \n"); + exit(1); +} +