Saturday, May 22, 2010

Multidimensional arrays with GCC's variable length arrays

GCC's extensions allow you to do some crazy things with multidimensional arrays. For C99, GCC implements variable-length arrays. So a declaration like this:

int matrix[a][b + 1];

Can be passed by reference to a function with this prototype:

void foo(int a, int b, int matrix[a][b+1]);

Or, if you want to get really crazy, you can use forward declarations and change around the parameter order:

void foo(int a; int b; int matrix[a][b+1], int a, int b);

No malloc needed! Much better than the old way.

Saturday, April 10, 2010

Which application is using that port?

See which ports are open, as an attacker would:

nmap 127.0.0.1

(Superuser seems to need to be used on some the following commands)

See which process is using port 25:

netstat -nlp | grep 25

Same, with a bit less info:

fuser -n tcp 25

Or you could also do:

fuser -u smtp/tcp

Discovered here

Wednesday, March 17, 2010

Sansa Clip and Karmic

Upon upgrade to Karmic Koala Ubuntu (9.10), my trusty Sansa Clip player was not autodetecting like it did under the Ubuntu distribution I was previously using. So I scoured the web and found this site which gives a workaround: when the Sansa Clip is disconnected, go to Settings->USB Mode on the Sansa Clip and set it to MSC. It should now present itself to the computer as a normal file system. Unless, of course, the file system is screwed up, in which case you'll have to go to Settings->Format and format the Sansa Clip drive before connecting it again.

Friday, March 05, 2010

Funny C tricks

Taken from Bill Rowan's Stanford ACM presentation:

The "downto" operator:

int main()
{
int i = 5;
while(i --> 0) // --> is the downto operator!
{
printf("%d\n", i);
}
return 0;
}

Cast any type to "bool" type (that is, 1 or 0):
!!foo
"Computed goto" (compiler-dependent && unary operator):
void print_loop(int s, int e) {
assert(s < e);
top:
printf("%d\n", s);
goto *( &&top + ( !!(s++/e) ) * ( &&end - &&top ) );
end:
;
}

Tuesday, January 12, 2010

Make a big file into many small files (and back again)

Adapted from this slashdot thread

Create many little files called "chunks00000" etc. from bigfile that are all just under 10MB in size: split -a 5 -b 10MB -d bigfile chunks

Put the chunks back together: cat * > newbigfile

Do the same, except with compression: tar -zc bigfile | split -a 5 -b 10MB -d - chunks

Put the compressed chunks back together: cat chunks000* | tar -zxO > newbigfile