From: joeyh Date: Thu, 23 Mar 2006 07:18:36 +0000 (+0000) Subject: * Back to Mithandir's C sponge, now fixed. X-Git-Tag: 0.8~3 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5942eb1d3bd089b4f01daab04ca0ad8518e8b2ef;p=moreutils * Back to Mithandir's C sponge, now fixed. --- diff --git a/Makefile b/Makefile index 7b96a92..ffed4b9 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -BINS=isutf8 ifdata pee -PERLSCRIPTS=vidir vipe ts combine sponge +BINS=isutf8 ifdata pee sponge +PERLSCRIPTS=vidir vipe ts combine MANS=sponge.1 vidir.1 vipe.1 isutf8.1 ts.1 combine.1 ifdata.1 pee.1 CFLAGS=-O2 -g -Wall @@ -28,5 +28,8 @@ ifdata.1: ifdata.docbook pee.1: pee.docbook docbook2x-man $< +sponge.1: sponge.docbook + docbook2x-man $< + %.1: % pod2man --center=" " --release="moreutils" $< > $@; diff --git a/debian/changelog b/debian/changelog index 0848452..465f114 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +moreutils (0.8) UNRELEASED; urgency=low + + * Back to Mithandir's C sponge, now fixed. + + -- Joey Hess Thu, 23 Mar 2006 02:18:22 -0500 + moreutils (0.7) unstable; urgency=low * Add pee (pipe tee) contributed by Miek Gieben. diff --git a/debian/copyright b/debian/copyright index dba6c9e..53ecc6c 100644 --- a/debian/copyright +++ b/debian/copyright @@ -4,27 +4,8 @@ This package was put together and debianized by Joey Hess isutf8 is Copyright (C) 2005 by Lars Wirzenius, under the terms of the GPL. -sponge has this copyright: -# Copyright (C) 2003 Colin Watson . -# -# Permission is hereby granted, free of charge, to any person -# obtaining a copy of this software and associated documentation files -# (the "Software"), to deal in the Software without restriction, -# including without limitation the rights to use, copy, modify, merge, -# publish, distribute, sublicense, and/or sell copies of the Software, -# and to permit persons to whom the Software is furnished to do so, -# subject to the following conditions: -# -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -# NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE -# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF -# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +spong is Copyright (C) 2006 by Tollef Fog Heen, under the terms of +the GPL version 2. Name and concept by Colin Watson. ifdata is Copyright (C) 2002 by Benjamin BAYART, under the terms of the GPL. It was originally called ifcfg and has a web page here: diff --git a/sponge b/sponge deleted file mode 100755 index 324970b..0000000 --- a/sponge +++ /dev/null @@ -1,54 +0,0 @@ -#!/usr/bin/perl - -=head1 NAME - -sponge - soak up standard input and write to a file - -=head1 SYNOPSIS - -sed '...' file | grep '...' | sponge file - -=head1 DESCRIPTION - -sponge reads standard input and writes it out to the specified file. -Unlike a shell redirect, sponge soaks up all its input before opening the -output file. This allows for constructing pipelines that read from and write -to the same file. - -=head1 AUTHOR - -Colin Watson - -=cut - -use warnings; -use strict; - -# Copyright (C) 2003 Colin Watson . -# -# Permission is hereby granted, free of charge, to any person -# obtaining a copy of this software and associated documentation files -# (the "Software"), to deal in the Software without restriction, -# including without limitation the rights to use, copy, modify, merge, -# publish, distribute, sublicense, and/or sell copies of the Software, -# and to permit persons to whom the Software is furnished to do so, -# subject to the following conditions: -# -# The above copyright notice and this permission notice shall be -# included in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -# NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE -# FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF -# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -my $file=shift || die "usage: sponge file\n"; -binmode STDIN; -undef $/; -my $input = ; -open OUT, ">$file" or die "couldn't open $file: $!"; -print OUT $input or die "couldn't write to $file: $!"; -close OUT or die "couldn't close $file: $!"; diff --git a/sponge.c b/sponge.c new file mode 100644 index 0000000..c6e4532 --- /dev/null +++ b/sponge.c @@ -0,0 +1,90 @@ +/* + * sponge.c - read in all available info from stdin, then output it to + * file named on the command line + * + * Copyright © 2006 Tollef Fog Heen + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * version 2 as published by the Free Software Foundation. + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +void usage() { + printf("sponge : suck in all input from stdin and write it to "); + exit(0); +} + +int main(int argc, char **argv) { + char *buf, *bufstart; + size_t bufsize = 8192; + size_t bufused = 0; + ssize_t i = 0; + int outfd; + + if (argc != 2) { + usage(); + } + + bufstart = buf = malloc(bufsize); + if (!buf) { + perror("malloc"); + exit(1); + } + + while ((i = read(0, buf, bufsize - bufused)) > 0) { + bufused = bufused+i; + if (bufused == bufsize) { + bufsize *= 2; + bufstart = realloc(bufstart, bufsize); + if (!bufstart) { + perror("realloc"); + exit(1); + } + } + buf = bufstart + bufused; + } + if (i == -1) { + perror("read"); + exit(1); + } + + outfd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666); + if (outfd == -1) { + fprintf(stderr, "Can't open %s: %s\n", argv[1], strerror(errno)); + exit(1); + } + + i = write(outfd, bufstart, bufused); + if (i == -1) { + perror("write"); + exit(1); + } + + i = close(outfd); + if (i == -1) { + perror("close"); + exit(1); + } + + return 0; +} diff --git a/sponge.docbook b/sponge.docbook new file mode 100644 index 0000000..674ef05 --- /dev/null +++ b/sponge.docbook @@ -0,0 +1,64 @@ + + + + + + + + + + + Joey + Hess + + 2006-02-19 + + + + sponge + 1 + + + + sponge + soak up standard input and write to a file + + + + + sed '...' file | grep '...' | sponge file + + + + + DESCRIPTION + + sponge reads standard input and + writes it out to the specified file. Unlike a shell + redirect, sponge soaks up all its input before + opening the output file. This allows for + constructing pipelines that read from and write to + the same file. + + +