Skip to content

Instantly share code, notes, and snippets.

@radiofreejohn
Created January 16, 2012 05:40
Show Gist options
  • Save radiofreejohn/1619238 to your computer and use it in GitHub Desktop.
Save radiofreejohn/1619238 to your computer and use it in GitHub Desktop.
create a skeleton C source with main() and includes
#!/bin/bash
#+
# Usage
# newc filename [include[.h] ...]
#
# Output
# Creates a file (filename) with a skeleton main function, with
# optional include files in the header. If the file exists in
# /usr/include then #include <include.h> is added, if it is not
# found in /usr/include, but is found in the current directory
# then #include "include.h" is added.
#
# Examples
# newc test stdio
# newc test.c stdio stdlib string test
# newc test.c stdio.h stdlib.h string.h test.h
#-
if [ "$1" == "" ]
then
echo "Usage: newc filename"
exit
fi
if [ ! -e $1 ] ; then
FILENAME="`basename $1 .c`.c"
shift
for i in $@ ; do
if [ -f "/usr/include/`basename $i .h`.h" ] ; then
echo "#include <`basename $i .h`.h>"
echo "#include <`basename $i .h`.h>" >> $FILENAME
elif [ -f "`basename $i .h`.h" ] ; then
echo "#include \"`basename $i .h`.h\""
echo "#include \"`basename $i .h`.h\"" >> $FILENAME
fi
done
echo "" >> $FILENAME
echo "int main(int argc, char *argv[]) {" >> $FILENAME
echo "" >> $FILENAME
echo "" >> $FILENAME
echo " return 0;" >> $FILENAME
echo "}" >> $FILENAME
echo "$FILENAME: created."
else echo "$FILENAME: file exists"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment