#!/bin/sh
# incgrep --- search all header files in known include directories for pattern

# Author: Noah Friedman <friedman@splode.com>
# Created: 1996-01-29
# Public domain.

# $Id: incgrep,v 2.7 2018/11/06 22:15:07 friedman Exp $

cc=${CC-gcc}
cpp=cpp

lang=c

# Only used if not using gcc/gnu cpp
includes=/usr/include

if ($cc -v) > /dev/null 2>&1; then
    version=`$cc -v 2>&1 | sed -ne '/gcc version/!d' -e 's/gcc version  *//p'`
    case "$version" in
        2.95.* | [3-9].* )
            cpp=`$cc -print-prog-name=cpp` ;;
        2.* )
            ccdir=`$cc -print-libgcc-file-name | sed -e 's/\/[^/]*$//'`
            cpp=$ccdir/cpp ;;
    esac

    case $1 in
        -lang-* )
            lang=`echo "$1" | sed -e 's/^-lang-//'`
            cpp="$cpp -x $lang"
            shift ;;
    esac

    includes=`$cpp -v 2>&1 < /dev/null \
              | sed -ne '/#include <...> search starts here:/!d
                         n
                         :l
                         /^End of search list./!{
                           p
                           n
                           b l
                         }'`
fi

follow=
if (find --version) > /dev/null 2>&1 ; then
    follow=-follow
else
    case `(uname -s) 2> /dev/null` in
        FreeBSD ) follow=-follow ;;
    esac
fi

{
    case $lang in
        c ) find $includes $follow -name '*.h' -print0 ;;
        * ) find $includes $follow -type f     -print0 ;;
    esac
} 2> /dev/null | xargs -0 egrep "$@"

# eof
