CSC 209H: Assignment 2

Weight: 10% of course grade.

Due date: 10:00 p.m. Tuesday, July 4.


Introduction

In this assignment you will be asked to write some C programs that manipulate files and directories. You are free to use any standard system or library calls available on CDF, but you must check the return value from each function call for errors. Your assignment must compile and run on the CDF system without any unavoidable compiler warnings or errors.

Part 0 - memlayout

Write a short program in C (this shouldn't be more than 20 lines of code) named memlayout that outputs:

The output from your program should be formatted EXACTLY as follows:

pid: 9999
stack: 0xaabbccdd
heap: 0x11223344
code segment: 0x89abcdef
data segment: 0x76543210

Your program should not take any command line arguments nor any input. The output should be printed to standard output.

Hints and Clarifications


Part 1 - mycut

The cut command is a useful system tool for extracting selected parts of lines from files. One limitation is that cut does not combine adjacent field delimiters (i.e., there is no way to ignore empty fields). We would like to have a system tool similar to cut that overcomes this limitation.

Your task is to write, in C, a program named mycut that behaves somewhat like cut, with this additional feature. Your mycut program should support the following usage:

In the table which follows, the byte-list, character-list, and field-list are one or more numbers or ranges (two numbers separated by a dash) separated by commas. Bytes, characters, and fields are numbered from starting at 1. Incomplete ranges may be given: "-m" means "1-m"; "n-" means "n" through end of line or last field.

The following options may be specified:

If -f is specified and multiple fields are printed, printed fields are separated by the delimiter character.

Your program should print the selected parts of each line in each file specified on the command line. If no files are specified, mycut should read from standard input instead.

If there are not enough characters or fields (if -f is specified) are present on a line to satisfy a number of range in the list, that portion is not printed for that line. In particular, if nothing is printed for a particular line, a blank line should be output.

You may assume any command line argument that begins with a dash is meant to be an option. You may assume all options precede any specified files. You may assume that the option specifier (i.e., -d) and the option argument (i.e., delimiter) are separate command line arguments.

Hints and Clarifications


Part 2 - organizer

In this question, you will write, in C, a program called organizer that will sort a collection of mp3 files, grouping them by album, year, and artist.

Suppose a given directory contains a number of mp3 files that are named very methodically using the following convention:
tracknum - artist - year - album - trackname.mp3

Your program should take an optional single argument: the path to a directory containing such a collection of mp3 files. If no argument is given, your program will operate on the current working directory.

Your program should reorganize the files so that they are filed in subdirectories. One subdirectory per artist should be created. Inside this subdirectory, one subdirectory per year for which there is at least one track for this artist should be created. Inside the "year" subdirectory, create one subdirectory per album for that year and artist. Finally, the mp3 files should be moved to the correct subdirectory and renamed so that they are now simply "tracknum - trackname.mp3".

In addition, your program should create one directory per year for which there is at least one track in the collection. That directory will contain symbolic links to all albums released that year for which at least one track exists. The symbolic link should be to the directory containing the album, and should be named "artist - album".

Examples

A simple example should make things perfectly clear. Let the following be the content of /home/user/mp3s:

02 - Portishead - 1997 - Portishead - All Mine.mp3
02 - Radiohead - 2001 - Amnesiac - Pyramid Song.mp3
03 - Radiohead - 2001 - I Might Be Wrong - Morning Bell.mp3
05 - Radiohead - 2001 - I Might Be Wrong - Idiotheque.mp3
06 - Radiohead - 1997 - OK Computer - Karma Police.mp3

After executing organizer /home/user/mp3s, the content of /home/user/mp3s should be organized as follows:

Portishead
        1997
                Portishead
                        02 - All Mine.mp3
Radiohead
        1997
                OK Computer
                        06 - Karma Police.mp3
        2001
                Amnesiac
                        02 - Pyramid Song.mp3
                I Might Be Wrong
                        03 - Morning Bell.mp3
                        05 - Idiotheque.mp3

1997
        Portishead - Portishead (symlink to /home/user/mp3s/Portishead/1997/Portishead)
        Radiohead - OK Computer (symlink to /home/user/mp3s/Radiohead/1997/OK Computer)
2001
        Radiohead - Amnesiac (symlink to /home/user/mp3s/Radiohead/2001/Amnesiac)
        Radiohead - I Might Be Wrong (symlink to /home/user/mp3s/Radiohead/2001/I Might Be Wrong)

Hints and Clarifications


Learning Objectives

What to hand in

You will commit to the a2 directory of your CSC209 repository the following files:

Include your name, student number, CDF login name, and a brief description of the usage of the program/contents of the file at the top of each file you submit for marking.

You are strongly encouraged to take advantage of the version control system and commit your work frequently so that you can keep track of your progress. Please note that perfectly fine (and even recommended) that you keep any additional files related to this assignment (such as files used for testing) under version control. The markers will simply ignore such files.