User Tools

Site Tools


user:bh011695:portfolio:listdir

Project: Directory Listing

A project for Systems Programming by Brad Hammond during the Fall 2011.

Objectives

To better understand reading data from a directory.

Prerequisites

In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:

  • read chapters 1 & 2 in the Systems Programming book

Background

ls is one of the most used UNIX utilities. Listing the contents of a directory is something we do on a fairly constant basis. However, most of what is happening when we display this data is obscured from us. This project will encompass learning of what goes on “under the hood” when we run ls by writing our own mini version of the utility.

Scope

File access is an important concept to be familiar with. In addition to regular files, being able to deal with directories is especially important.

This project will implement our own version of ls which can be used to list files in directories on the system. It will do so by manipulating directory files and accessing their contents, and displaying the information in a readable form to STDOUT.

Attributes

State and justify the attributes you'd like to receive upon successful approval and completion of this project.

  • directories: directory files will be manipulated in this project
  • command line arguments: directory name will be gotten from an argument
  • pointers: directory pointer will be used in opening the directory
  • malloc/free: allocate memory for the name of the directory

Code

#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <stdlib.h>
 
#define NAME_SIZE 128
 
int main(int argc, char** argv)
{
	char *dirName; 					//Store directory name
	DIR *myDir;						//Pointer to directory to list
	struct dirent *entries;			//Pointer to directory entries
 
	dirName = (char*)malloc(sizeof(char) * NAME_SIZE);
 
	if (argc == 1) 					//Getting the name of the directory
		dirName = ".";
	else 
		dirName = argv[1];
 
	myDir = opendir(dirName);		//Open the directory
 
	if (myDir == NULL)
		printf("ls: cannot open directory\n");
	else {
		printf("%s\n", dirName);	//Print the name of the directory
		entries = readdir(myDir);	//Read the directory into the struct
 
									//Print each entry's name to stdout
		while (entries != NULL) {
			if (entries -> d_name[0] != '.') 
				printf("%s\n", entries -> d_name);
			entries = readdir(myDir); //Read the next directory
		}
 
		closedir(myDir);			//Close the direcotry
		free(dirName);				//free memory
	}
 
	return 0;
}

Execution

Again, if there is associated code with the project, and you haven't already indicated how to run it, provide a sample run of your code:

brad@Lucid-Lynx:/media/06F6-9DC2/Notepad++/Programs$ ./lsVersionFourPointOBeta LinkedList
LinkedList
List.h
LinkedList.c
qTest
Queue.h
main.c
main
Queue.c
main2.c
Makefile
main2
copyList.c
qTest.c
copyList
stackTest.c
stackTest
Stack.h
Stack.c
lStack
lStack.c
listTest.c
listTest
binSearchTree.c

Reflection

Working with directories is almost identical to working with files. The biggest difference seems to be that your using DIR pointers instead of FILE pointers. A directory is opened just like a file is. dirPtr = opendir(“path/to/dir”) while opening a file looks as so filePtr = fopen(“path/to/file”, “mode”). With files you need to determine a mode for the file. Also, when reading from a directory we must have a structure that we can use to store the different attributes of the directory. We need a place to store permissions, file names, and things like that.

References

In performing this project, the following resources were referenced:

  • Guide to UNIX/Linux Programming

Generally, state where you got informative and useful information to help you accomplish this project when you originally worked on it (from Google, other wiki documents on the Lab46 wiki, etc.)

user/bh011695/portfolio/listdir.txt · Last modified: 2011/12/13 14:04 by bh011695