]> err.no Git - moreutils/commitdiff
Moreutils: Added ifne
authorJavier Merino <cibervicho@gmail.com>
Thu, 20 Mar 2008 12:03:26 +0000 (13:03 +0100)
committerJoey Hess <joey@kodama.kitenet.net>
Thu, 20 Mar 2008 16:55:40 +0000 (12:55 -0400)
Hi, I read the suggestion about ifne in the Discussion page of
moreutils and found it interesting. It's a command that runs another
process if the standard input is not empty. I think it can be very
useful in admin scripts so I've implemented it. I attach a patch
against the current head of moreutils' git. You may include it in
moreutils if you think it's worth it.

I've considered making an "ifempty" or "ifne -v" that would run a
command if the standard input is empty, but I'm not sure if that could
be useful. Any comments will be appreciated. Regards,

Javi

Makefile
README
debian/control
ifne.c [new file with mode: 0644]
ifne.docbook [new file with mode: 0644]

index a15226e8a6fe773f7d1c8d0722860ef4d3081f53..f5142db3609d9e0b4041e9bbd244042206c426df 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
-BINS=isutf8 ifdata pee sponge mispipe lckdo
+BINS=isutf8 ifdata ifne pee sponge mispipe lckdo
 PERLSCRIPTS=vidir vipe ts combine zrun
-MANS=sponge.1 vidir.1 vipe.1 isutf8.1 ts.1 combine.1 ifdata.1 pee.1 zrun.1 mispipe.1 lckdo.1
+MANS=sponge.1 vidir.1 vipe.1 isutf8.1 ts.1 combine.1 ifdata.1 ifne.1 pee.1 zrun.1 mispipe.1 lckdo.1
 CFLAGS=-O2 -g -Wall
 INSTALL_BIN=install -s
 
@@ -26,6 +26,9 @@ isutf8.1: isutf8.docbook
 ifdata.1: ifdata.docbook
        docbook2x-man $<
 
+ifne.1: ifne.docbook
+       docbook2x-man $<
+
 pee.1: pee.docbook
        docbook2x-man $<
 
diff --git a/README b/README
index 702bb0d800a712028fd3f4664e7f8a9c04fe415e..ccc257cd3e6190cafffba1b73125677a16beea33 100644 (file)
--- a/README
+++ b/README
@@ -7,6 +7,8 @@ ifdata
        get network interface info without parsing ifconfig output
 isutf8
        check if a file or standard input is utf-8
+ifne
+        run a command if the standard input is not empty
 lckdo
        execute a program with a lock held
 mispipe
index f69a5eeb79439148a0aa5e3f015db8228bc0e324..5db3b331ed0ad5007be213ca9f534694c6a5dd97 100644 (file)
@@ -20,6 +20,7 @@ Description: additional unix utilities
  So far, it includes the following utilities:
   - sponge: soak up standard input and write to a file
   - ifdata: get network interface info without parsing ifconfig output
+  - ifne: run a program if the standard input is not empty
   - vidir: edit a directory in your text editor
   - vipe: insert a text editor into a pipe
   - ts: timestamp standard input
diff --git a/ifne.c b/ifne.c
new file mode 100644 (file)
index 0000000..9af19fa
--- /dev/null
+++ b/ifne.c
@@ -0,0 +1,99 @@
+
+/* 
+ *
+ * Copyright 2008 Javier Merino <cibervicho@gmail.com>
+ * 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.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/wait.h>
+#include <sys/types.h>
+
+int main(int argc, char **argv) {
+       ssize_t r;
+       int fds[2];
+       int child_status;
+       pid_t child_pid;
+       char buf[BUFSIZ];
+
+       if (argc < 2) {
+               /* Noop */
+               return EXIT_SUCCESS;
+       }
+
+       r = read(0, buf, BUFSIZ*sizeof(char));
+
+       if (r == 0)
+               return EXIT_SUCCESS;
+       else if (r == -1) {
+               perror("read");
+               return EXIT_FAILURE;
+       }
+
+       if (pipe(fds)) {
+               perror("pipe");
+               return EXIT_FAILURE;
+       }
+
+       child_pid = fork();
+       if (!child_pid) {
+               /* child process: rebind stdin and exec the subcommand */
+               close(fds[1]);
+               if (dup2(fds[0], 0)) {
+                       perror("dup2");
+                       return EXIT_FAILURE;
+                }
+
+               execvp(argv[1], &argv[1]);
+               perror(argv[1]);
+               close(fds[0]);
+               return EXIT_FAILURE;
+       } else if (child_pid == -1) {
+               perror("fork");
+               return EXIT_FAILURE;
+       }
+
+       /* Parent: write in fds[1] our stdin */
+       close(fds[0]);
+
+       do {
+               if (write(fds[1], buf, r*sizeof(char)) == -1) {
+                       fprintf(stderr, "Write error to %s\n", argv[1]);
+                       exit(EXIT_FAILURE);
+               }
+               r = read(0, buf, BUFSIZ*sizeof(char));
+       } while (r > 0);
+       if (r == -1) {
+               perror("read");
+               exit(EXIT_FAILURE);
+       }
+       
+       close(fds[1]);
+       if (waitpid(child_pid, &child_status, 0) != child_pid) {
+               perror("waitpid");
+               return EXIT_FAILURE;
+       }
+       if (WIFEXITED(child_status)) {
+               return (WEXITSTATUS(child_status));
+       } else if (WIFSIGNALED(child_status)) {
+               raise(WTERMSIG(child_status));
+               return EXIT_FAILURE;
+       }
+
+       return EXIT_FAILURE;
+}
diff --git a/ifne.docbook b/ifne.docbook
new file mode 100644 (file)
index 0000000..92c7f48
--- /dev/null
@@ -0,0 +1,68 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!--
+
+Copyright 2008 Javier Merino <cibervicho@gmail.com>
+
+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.,
+59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+-->
+
+<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook V4.4//EN"
+"file:///usr/share/xml/docbook/schema/dtd/4.4/docbookx.dtd"
+[]>
+
+<refentry>
+       <refentryinfo>
+               <address>
+                       <email>cibervicho@gmail.com</email>
+               </address>
+               <author>
+                       <firstname>Javier</firstname>
+                       <surname>Merino</surname>
+               </author>
+               <date>2008-03-19</date>
+       </refentryinfo>
+
+       <refmeta>
+               <refentrytitle>ifne</refentrytitle>
+               <manvolnum>1</manvolnum>
+       </refmeta>
+
+       <refnamediv>
+               <refname>ifne</refname> 
+               <refpurpose>Run command if the standard input is not empty</refpurpose>
+       </refnamediv>
+
+       <refsynopsisdiv>
+               <cmdsynopsis>
+                       <command>ifne command</command>
+               </cmdsynopsis>
+       </refsynopsisdiv>
+       
+       <refsect1>
+               <title>DESCRIPTION</title>
+               
+                <para><command>ifne</command> runs the following command if and only if
+                the standard input is not empty.</para>
+       </refsect1>
+       
+       <refsect1>
+               <title>EXAMPLE</title>
+               <cmdsynopsis>
+                       <command>find . -name core | ifne mail -s "Core files found" root</command>
+               </cmdsynopsis>
+       </refsect1>
+</refentry>