bashgcccode-size

finding total size of C files in gcc


I'm trying to compute the source code size of gcc by considering cpp files first:

# NOTE: the cpp loop finishes immediately
LOC=0
BYTES=0
FILES=$(find . -name "*.cpp")
for f in ${FILES};
do
    BYTES_TAG=$(stat --printf="%d" $f)
    LOC_TAG=$(cat $f | wc -l)
    BYTES=$((BYTES+BYTES_TAG))
    LOC=$((LOC+LOC_TAG))
done
echo "LOC = $LOC, SIZE = $BYTES"

Then I try to sum the *.c files, but the bash loop doesn't stop. here is my gcc version:

$ wget https://ftp.gnu.org/gnu/gcc/gcc-11.2.0/gcc-11.2.0.tar.gz
$ tar -xf gcc-11.2.0.tar.gz

This is weird because counting all the files is immediate with:

$ find . -type f | wc -l

Solution

  • Size of all *.c and *.cpp files in bytes:

    find . -name *.cpp -o -name *.c -exec wc -c {} \; | sed "s/ .*//" | paste -sd+ | bc
    

    Number of lines in all *.c and *.cpp files:

    find . -name *.cpp -o -name *.c -exec wc -l {} \; | sed "s/ .*//" | paste -sd+ | bc
    

    Explanation: find ... -exec executes a command on all files it finds, replacing the {} in the -exec part with the file name(s). If you end the -exec part with \;, it will be executed once for each file. In some cases, ending the -exec part with + is more efficient -- this will execute for as many files as will fit in one command line.

    wc -l and wc -c will print one line per file, with the number of lines / characters (bytes) followed by the file name. The `sed "s/ .*//"' will cut away everything after the number.

    paste -sd+ will then concatenate all the lines (-s) and separate them with plus signs (-d+). Piping this to bc makes bc execute the addition and give you the total you are looking for.

    Meta answer: Learn about find ... -exec. Don't loop over find output, because you will get into trouble e.g. when file names contain spaces -- and xargs is in most cases unnecessary.