Utility Code

First up is a utility that I wrote for my bash prompt. It gives the total size of all the files in the current working directory. I wrote it in C because the original example used a bash function that I found quite slow at the time (about 8 years ago). This version has been kept up-to-date over the past 8 years and should now be ready for public consumption.

lsbytesum.c

Here is another handy command line utility. It takes any number of filenames on the command line and it will either rename them all to upper-case or lower-case depending on the name of the executable. The way it is typically built is as lcase with a symbolic link to it called ucase. This quickly solves the problem that typically occurs when you get an older zip archive that has filenames in uppercase.

lcase.c

This one is a bit different. I wrote it because I wanted a way to truncate a file containing a Ext-2 filesystem down to the new size after running resizee2fs. The truncate(2) system call works perfectly for this, but I didn’t see anything that just exposed that functionality via the command line. So, this program was born. There isn’t much in the way of error checking or anything for that matter. It simply takes a filename and the number of blocks you want it to be (in 4KB blocks) and truncates the file. If you need to, you can also specify a different blocksize. Please be careful with this one — it can be dangerous.

trunc.c

Here is a much safer utility — it simply reads stdin, strips out extraneous whitespace, and writes it back to stdout.  It’s great to use with cut -f when you have something that uses multiple spaces and/or tabs to split up it’s fields.

ws_strip.c

This one has, admittedly, become less useful with tools like watch available these days. But, at one time it served a purpose. This utility will monitor the specified file, watching for changes in the file size. Once the file stops changing for 10 seconds (or any other user specified number of seconds), the program will exit. This was useful in scripts where you had to wait for a file to finish being written to before doing something else with it.

monfile.c

Last of the utilities for a while — this one takes a large file and breaks it up into a series of smaller files. Great for fitting large files on multiple CDs (or floppies… if you’re still into that). This code is an example of something that I would most likely write differently if I was writing it today. For example, today I would probably use mmap() instead of reading the chunks into memory and then writing them back out to the new file. Maybe if I’m ever bored or determine that this program is too slow, I’ll re-write. Most likey not, though.

chunkem.c

Advertisements


  1. So…no ‘split’ in MacOS X?

  2. well, since you asked:

    $ which split
    /usr/bin/split

    That’s on OS X 10.4, so yes, split is on OS X.

    But, if you check the date in the file, you’ll see that it was written a while ago. I honestly don’t know where I was using that originally and when, but it’s pretty likely I didn’t have access to split back then.

    Hey, it’s free code… what do you want? 😀

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Monthly Archives


%d bloggers like this: