|
Tobin Fricke's Lab Notebook
|
|
|
| C library manpages |
[Jun. 27th, 2009|03:49 am] |
My Ubuntu installation has annoyed me for a long time by not having manpages for the usual C library functions. Of course there's an easy fix:
sudo apt-get install manpages-dev ahh, such a relief. |
|
|
| shell |
[Nov. 20th, 2008|09:22 pm] |
Why are shell scripting languages so bad? csh is terrible, but even bash is agonizing; it knows how to do arithmetic, but only of integers. |
|
|
| unix job control |
[Nov. 6th, 2008|12:53 am] |
Of course Ctrl-Z, "bg", and "fg" are staples of any user of the Unix shell. But for some reason I didn't know until today (when I needed it and looked it up) that you can also pause and resume processes by sending them STOP and CONT signals, i.e. "kill -STOP [pid]" and "kill -CONT [pid]". This is useful when you need to pause a process that you didn't start from your current shell. |
|
|
| sshfs, or, long live Plan 9! |
[Oct. 11th, 2007|10:56 pm] |
My favorite unix utility of the week is sshfs, which allows you to easily mount a remote filesystem into your local filesystem using nothing more exotic than garden variety SSH. For instance, if I simply type
sshfs shell.pas.rochester.edu:/home/tobin /home/tobin/ur Then my home directory on the far-away machine in Rochester is magically grafted onto my laptop's filesystem, rooted at ~/ur. I can then manipulate files on the remote system exactly as if they were stored on my laptop. It's like a breath of fresh air after all this past silliness moving files around with scp or even placing them temporarily on a web page.
This magic is made possible by FUSE, which allows you to implement a new filesystem as an ordinary unix program that talks to the kernel through a special, simple interface. In your program you then implement a subset of the file-related system-calls, like read, open, fileattr, etc. Being able to implement file systems as ordinary user-space programs already makes things vastly easier (versus having to mess around with kernel code). But the real excitement is in making "filesystems" that are not really filesystems" at all. You can use this to patch just about anything into the unix file system. Even, say, Wikipedia.
This is not a new idea at all; it was pretty much the core design principle of the operating system Plan 9. Plan 9 is pretty much dead now, but it is very heartening to see its ideas taking root and flourishing in Linux.
If you're running Debian/Ubuntu, you can apt-get sshfs and be up and running in seconds. If you're running OS X, you're in luck! Google has come up with a port of FUSE to OS X. |
|
|
| BibTeX |
[Oct. 16th, 2006|04:23 am] |
It will surprise no-one to learn that BibTeX, the bibliography-formatting accessory to LaTeX (which is itself a set of macros extending Knuth's TeX), contains an entire only-kind-of-domain-specific language to specify bibliography styles; see, for instance, the implementation of the "plain" style. The language is described in Designing BibTeX styles, otherwise known as BTXHAK.
An exercise for the reader: Perform a census of all mini-languages present in a typical Unix installation. How many are Turing-complete? |
|
|
| Delete sent mail? |
[Aug. 1st, 2006|12:40 am] |
I am appalled at PINE's monthly offer to delete my sent mail. Fortunately this behavior can be stopped:
pruning-rule =
Set Rule Values
--- ----------------------
( ) ask about rename, ask about deleting (default)
( ) ask about rename, don't delete
( ) always rename, ask about deleting
(*) always rename, don't delete
( ) don't rename, ask about deleting
( ) don't rename, don't delete
How much mail do I send? (Yes, GNU sort offers a "--month-sort" option!)
$ grep --count --extended-regexp "^From " sent-mail-* |
> sed "s/sent-mail-\([a-z]*\)-\([0-9]*\):/\2 \1\t/g" |
> sort --key=2 --month-sort |
> sort --key=1 --numeric-sort --stable
2005 mar 325
2005 apr 205
2005 may 145
2005 jun 120
2005 jul 59
2005 aug 43
2005 sep 67
2005 oct 46
2005 nov 127
2005 dec 201
2006 jan 330
2006 feb 366
2006 mar 297
2006 apr 208
2006 may 245
2006 jun 109
2006 jul 258
That could doubtlessly be condensed a little by changing the field separator to sort (eliminating the need for sed) and combining the two sort commands. |
|
|
| 2002-03-07 Adventures in Unix |
[Apr. 12th, 2005|05:01 pm] |
Reading some text on writing secure unix programs, I learned that the unix environment (or a copy thereof) is stored as a simple array of strings (ie, char **) pointed to by the variable environ defined in unistd.h. Each string is just in the format ``name=value''. Remarkably primitive, eh? Anyway, you can dump the environment quite simply:
#include <unistd.h>
#include <stdio.h>
extern char **environ;
int main(void) {
for (char **foo = environ; foo && *foo; foo++)
printf("\"%s\"\n",*foo);
return 0;
}
Here's something else I didn't know. The ``%n'' format specifier directs printf to write the number of bytes written so far into the corresponding variable in the argument list. Example:
#include <stdio.h>
int main(void) {
int foo;
printf("This is a test message.\n%n",&foo);
printf("The above message contained %d bytes.\n",foo);
return 0;
}
The following code snippet uses a buffer overflow to change the return address of main(). That's right, main() apparently is called by some function in the C library, so it has a real return address. The stack grows towards lower memory addresses, so we can find the return address by allocating a word (buffer[0]) on the stack; skip one word (the stack pointer) after that allocated word, and the next (ie, buffer[2]) will coincide with the saved return address. Then we can just change it so something else.
#include <stdio.h>
void albatross(void) {
printf("albatross!\n");
exit(0);
}
int main(void) {
int buffer[1];
buffer[2] = (int)albatross;
return 0;
}
|
|
|
| navigation |
| [ |
viewing |
| |
most recent entries |
] |
| |
|
|