-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
ifdata.1: ifdata.docbook
docbook2x-man $<
+ifne.1: ifne.docbook
+ docbook2x-man $<
+
pee.1: pee.docbook
docbook2x-man $<
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
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
--- /dev/null
+
+/*
+ *
+ * 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;
+}
--- /dev/null
+<?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>