Jan 2007
Useful bash alias and function for grep'ing header files
Occasionally I find myself grep'ing the header files in /usr/include, usually because I'm curious about what a typedef or #define I'm using actually is. Since I tend to forget grep's options between each time I do this, and because even when I remember them they are tedious to write, I've put this little alias in my .bashrc:

    alias hgrep="grep -Hnr --include="*.h" --color=auto"


Now I can enter, for instance, hgrep 'typedef.*\Wwchar_t;' /usr/include in Terminal to see all the places wchar_t is defined or hgrep 'typedef.*\Wunichar;' /System/Library/Frameworks to see the definitions of unichar.

Taking this one step further, I can also create a bash function that looks for typedefs:

function findtypedef
{
grep -Hnr --include="*.h" --color=auto "typedef\\W.*\\W$*;" \
/usr/include \
/System/Library/Frameworks
}


With this I can simply type findtypedef wchar_t or findtypedef unichar.
Getting time with with sub-second precision
The function time() declared in <time.h> only measures the time in seconds, which can be a bit imprecise. For better precision, use the function gettimeofday() declared in <sys/time.h> which returns the number of seconds and microseconds since midnight, January 1, 1970.

The following demonstrates basic usage:

#include <stdio.h>
#include <sys/time.h>

int main()

{
struct timeval t;
gettimeofday(&t, NULL);
printf("Time elapsed since midnight, Jan. 1st 1970 is %d s and %d us",
t.tv_sec, t.tv_usec);
return 0;
}


The function takes an optional parameter (it's NULL in this example) for retrieving time zone information, but its use has been deprecated on the Mac.