To print a list of files similar to UNIX's ls -l command or DOS's
DIR command, we write a utility program called lsl.cpp.
The lsl.cpp utility program makes a series of system calls to the
operating system to obtain information about files in the
current directory.
Then this information is formatted and printed.
To use the system calls correctly, it is essential that the
system documenation (called manual pages or man pages on UNIX)
be consulted.
Obtaining Documentation on UNIX: The man command
man 3b directory
man 2 stat
man 3 getpwent
It is sometimes useful to convert a man page directly to text.
One way to do so is to convert the man pages with the vi text editor:
% man 3b directory > dir-man.txt
% vi dir-man.txt
To remove control characters, type
:g/^V^H/s/.^V^H//g RETURN
:wq RETURN
where ``RETURN'' is the usual RETURN or ENTER key on the keyboard
and ^V means control-v and ^H means control-H. To type control-v,
hold down the CONTROL key and hit v.
How to Use Documentation
The #includes listed near the top of the manual page
should be added to your program if you want to use the functions
described on the man page.
For example, in the manual page produced by man 3b directory,
the following line appears:
#include <sys/dir.h>
This line should be added near the top of your program,
exactly as shown.
The names and types shown for functions near the man page,
do NOT show how to use the functions.
Instead they show how the functions are declared
in the relevant include file.
Example: In the file /usr/include/dir.h
there is a declaration for the opendir function that looks like:
DIR * opendir (char * fn);
This declaration says that opendir is a function that takes a
character string and returns a pointer to a DIR structured value.
Some ways to use the opendir function in your program are:
DIR *pDIR;
pDIR = opendir(".");
pDIR = opendir("MyDirName");
pDIR = opendir("/tmp/somedir/somesubdir");
What is a directory? (folder)
A directory is a special type of file containing
information about a group of files,
commonly called the files that are ``in the directory''.
Stored in a directory file is a series of directory entries,
each describing one file that is ``in the directory.''
The most important information in a directory entry
is the name of the file and the unique internal
identifier for the file, which is called the i-node number.
For example, two files in different directories have the same
name but different i-node numbers.
In more detail, each directory entry is a record,
of type DIR, which is a
simple name for struct direct:
In the file called /usr/include/dir.h, the structure is declared as:
typedef DIR struct direct;
and that structure is defined as:
struct direct {
(unsigned) u long d_ino; /* i-node number */
u short d_reclen;
u short d_namelen; /* length of filename */
char d_name [MAX_NAME_LENGTH + 1]; /* d_name = filename */
}
The stat system call
information about a file, such as the owner, group, protection,
and size in bytes, can be obtained using the stat system call.
int status;
status = stat("full path function", buf)
struct stat * buf; /* it allocates a structured element and returns a pointer */
Overview of lsl.cpp Program
open the relevant directory
-- use opendir function
BEGIN LOOP to process all entries in the directory
-- use while loop
read an entry, and obtain the name and i-node number of the file -- use readir function
using the name of the file, look up information,
such as the number of bytes and owner, about the file
-- use stat system call
print an entry describing the file according to this information
-- formatting is messy
formatting the rwx permission bits requires careful attention to the relevant part of the man page -- included in program for reference
converting a user id (uid) and a group id (gid) requires additional
functions -- use getpwuid and getgrgid
A script file is given that compares lsl.cpp to ls -l.
First the output for lsl.cpp is given, followed by that of ls -l.
Note that lsl.cpp does not sort the files in alphabetical order.
To do so, you could save the directory entries in an array,
sort them, and then print them.