Disable compiler optimisation for a single function in C

To disable optimisation for a single function, append


to the prototype. E.g.

static int my_function(int arg) __attribute__((optimize("0")));


Const and pointers in C

The syntax of constant pointers in C can be confusing. Try reading from right to left, away from the variable name. E.g.

Pointer to a constant of type char:

const char *ptr_name;

Constant pointer to a variable of type char:

char *const ptr_name;

Constant pointer, to a constant of type char:

const char *const ptr_name;

Using git as a subversion interface – git-svn

Git is my favourite source control system but unfortunately some companies still use historic subversion repos that we have to put up with. This is my basic workflow when dealing with svn.

Importing the repo

Cloning the svn repo to git:
git svn clone https://address/to/svn/repo

For a very large repo this periodically failed with:
error: git-svn died of signal 13
To continue from the same spot just run:
git svn fetch

This happened quite a few times when cloning from a slow server that took all day.

Note that git-svn doesn’t recursively clone svn externals (nested repositories). There are a few complex solutions on stackoverflow, haven’t picked my favourite yet.

Pulling changes

Instead of a git pull I do:

git stash; git svn fetch; git svn rebase; git stash pop

Commiting changes

For simple projects I think a major advantage of git is the ability to easily queue commits from the command line. I use my normal process of the interactive prompt to add files and a commit message:

git add -i
git commit -m "Message"

Then instead of a git push I do:

git stash; git svn dcommit; git svn rebase; git stash pop

Bash – Loops using pipe and read

Perform some operation for every line in the output of a command, e.g. add .txt to every file in a directory:

ls /var/* | while read fname; do cp $fname $fname.txt; done

Instead of:

for fname in `ls /var/*`; do cp $fname $fname.txt; done

Neat trick I actually picked up in a job interview. Easier to read if the original command is long as it avoids the backticks, and bash is all about readability, right?? 🙂

Bash String Manipulation

Bash has some convenient string manipulation functions. I often use substring removal which is very handy for trimming suffixes and prefixes from things like file names. These operations all evaluate to a new string and don’t modify the original variable.

Suffix removal
Remove the longest match from end:
Remove the shortest match from end:

Prefix removal
Remove the longest match from front:
Remove the shortest match from front:

Great reference for more string operations: http://tldp.org/LDP/abs/html/string-manipulation.html

Mozilla Game On: Hackable Javascript + HTML5 Game

I have submitted my entry to the Mozilla Game On competition under the ‘hackable games’ category. It can be played online now at: http://austinmarton.github.com/phys-ed/


I developed a basic platformer written from scratch in Javascript using HTML5 canvas and some jQuery. It has a ‘mod mode’ in which the user can view some of what is going on behind the scenes and change some of the physical parameters. Of course I ran out of time in the end but the competition was a great push to at least get a playable prototype out there.

It is released under GPLv3 so please check out or even fork the source code on github!

Receiving raw packets in Linux without pcap

Since there was a lot of interest in my post on sending raw Ethernet packets, this is an example of receiving packets on a raw socket. A compiling version can be found on github: https://gist.github.com/2862515. The application that I developed this for was to receive packets sent to a specific MAC address. The main sections are explained below:

Open raw socket listening for ETHER_TYPE

	if ((sockfd = socket(PF_PACKET, SOCK_RAW, htons(ETHER_TYPE))) == -1) {
		perror("listener: socket");	
		return -1;

Set interface to promiscuous mode

	strncpy(ifopts.ifr_name, ifName, IFNAMSIZ-1);
	ioctl(sockfd, SIOCGIFFLAGS, &ifopts);
	ifopts.ifr_flags |= IFF_PROMISC;
	ioctl(sockfd, SIOCSIFFLAGS, &ifopts);

Allow socket to be reused

	if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof sockopt) == -1) {

Bind to device

	if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, ifName, IFNAMSIZ-1) == -1)	{

Receive on socket

	numbytes = recvfrom(sockfd, buf, BUF_SIZ, 0, NULL, NULL);
	printf("listener: got packet %lu bytes\n", numbytes);

Print the data

	for (i=0; i<numbytes; i++) printf("%02x:", buf[i]);

Close socket