Dec 192017
Display a listing of files in UNIX format (ls). C source.
File FILEDIRS.ZIP from The Programmer’s Corner in
Category C Source Code
Display a listing of files in UNIX format (ls). C source.
File Name File Size Zip Size Zip Type
LS.C 18944 5372 deflated
LS.DOC 9728 3838 deflated
LS.EXE 17408 9014 deflated

Download File FILEDIRS.ZIP Here

Contents of the LS.DOC file

ls - a Unix-like directory listing program for MS-DOS

(c) 1984, R. Edward Nather

Syntax: ls [-aclrstuR] [(path)name ... ]

Options may appear in any order, grouped or separated; if separate,
each must be preceded by a dash. The name(s) may refer to files or
directories. If no name is given the current directory is listed.


(none) Show filenames (only) sorted alphabetically
-a all: include system files, hidden files, "." and ".."
-c columnar: list information in 1-column format
-l long listing: include file's size, date, time, attributes
-r reverse the sorting direction
-s report size(s) only
-t sort by time of last file modification
-u include actual disk use, with totals & available space
-R recursively list all subdirectories


With no options specified, "ls" lists the filenames in the current directory,
sorted alphabetically into 5 columns; if a (path)name of a directory is
included, its contents will be displayed. If two or more names are given
they will be sorted and shown successively, identified by their (path)names.
Wild card characters ("*","?") are acceptable in (path)name arguments.
Subdirectories are identified by a trailing file-separator character in this
display format.

System files, "hidden" files and the directory synonyms "." and ".." are
normally not included in the listing, but the -a option will bring them out
of the woodwork. "Volume Labels" are always ignored. If they have any
logic or value it has been carefully hidden by Microsoft.

The listed filenames will be sorted according to their last modification time
if the -t option is included, normally latest first. The direction of sort
(alphabetic or time sort) is reversed by the -r option.

The -l option yields a two-column "long" listing, sorted vertically by column,
showing nearly everything known about the files (i.e. all the news that fits).
The file attributes are shown symbolically by single characters: "d" for
directories or "s" for system files, "h" for hidden files, and "w" for writable
(not read-only) files. The lack of one of these attributes is shown by a dash.
The actual length of the file, in bytes, is given unless the -u option is
included (see below). The month and time of the last modification are shown if
the file is less than 12 months old, otherwise month and year are printed.

Note that the file sizes displayed in the "long" listing will always be smaller
than the actual disk space used, because MS-DOS never writes a record smaller
than the "cluster" size -- which is 1024 bytes on a 9-sector floppy and 4096
bytes on a hard disk with only a DOS partition present.

The -u option tells you how much disk space is actually used. If only one
filename or directory is listed, its total disk use is shown, followed by
its identifying (path)name. If the -l option is included with -u, the size
shown for each file is the amount of disk space used -- almost always larger
than the file size. More than one name on the command line will get you
successive listings, with disk use subtotals preceding the identifying
(path)name, and followed by the total disk space used by the files listed.
All this is followed by the space remaining on the drive; if more than one
drive is involved, it is the space remaining on the last one on the command

The -c option forces output into a single column, so "ls -c" will give a
sorted list of filenames (only) in a single column, and "ls -lc" will
provide a "long" listing in one column instead of two. These formats can
be useful if you want to use the file names or information for further
operations (e.g. as arguments in ".bat" files). This format is the 4.2bsd
Unix default when sending the listing to any device other than the display;
here you must explicitly ask for single column output to get it.

The -R option will include (recursively) the contents of any subdirectories
encounterd, and their subdirectories, ad finitum. This can be a lengthy
listing on a hard disk; if started in the root directory with the -a option
it lists the whole shebang.

The -s option omits filenames (unless they are specified on the command line)
and lists sizes only. This effectively shuts off the -l, -t and -r options,
and turns -u on, yielding a nifty summary of disk usage by directory, along
with a total at the end and the space remaining on the drive. Include -a if
you want hidden and system files to show up.

As a special case, the -s option recognizes the numerals "1" and "2" (they
must immediately follow the "s" option) and will use disk block sizes
appropriate to single-sided diskettes (-s1) or double-sided diskettes (-s2)
in the calculatation of actual disk usage. This is useful if you want to
know if a given set of files is going to fit on a diskette that uses block
sizes different from those on the source disk. For example, MS-DOS is far
more profligate with disk space on a hard disk than on a floppy, and disk
use appears to "shrink" onto a floppy backup. These options can tell you
how much shrinkage to expect.

The program includes a built-in pager which stops scrolling after 22 lines
have been displayed on your terminal, and indicates there is more to be
shown if there really is. A carriage return shows one more line; any other
character gets another screenful. This action takes place only if the
output device is your console screen -- the pause will not occur if the
output is redirected to any other device or into a file.


The program assumes the file separator character is "/". If this assumption
is incorrect, you should change the two symbolic constants QS and DQS to
reflect the true condition. Note that you must use two backslash characters
(e.g. #define DQS "\\") to get one of them. While you're right there, if
your computer doesn't support the IBM extended character set (ASCII codes
above 127) you should change the long-listing separator value called BAR to
"| " (the space after '|' is important).

A set of symbolic "customizing constants" has been included that allow most
of the options to be either "on" or "off" by default. As delivered, all of
the options are off -- the corresponding symbolic constant is defined as
zero. To get the optional action, you must include the option on the command
line when you call "ls" into operation. However, you may want a particular
option to be *on* by default; it can then be turned *off* by including the
option on the command line. To bring this about, just set its symbolic
constant to 1 before you compile the program. Here's the list:

Name Action when off Action when on

ID Identify directories only Always identify directories.
when more than one is shown.

ALL Do not show system or hidden Show everything except (gag)
files, nor directory synonyms. volume labels.

COLM List file names in 5 columns, List in one column format.
long listing in 2 columns.

LONG List names of files and List everything known about a
directories only. Identify file or directory.
directories with QS character.

RSORT Sort in normal alphabetic Sort into reverse alphabetic
order or by most recent time. order or by oldest time.

TSORT Sort into alphabetic order. Sort by time of file creation
or modification.

DU Show file size based on Show file size based on total
number of bytes in the file. disk space actually used.

The program can be compiled with either the DeSmet or CI-C86 compilers.
For DeSmet C, the assembly langauge support program "sysint.a" must be
assembled separately, and either included in the library or linked
separately using the program "Bind."

For CI-C86, you must include the option -dCIC86 on the command line for
the first pass of the compiler "cc1" or add the line

#define CIC86

to the source code. The support routine "sysint" is present in the
CI-C86 library and will be automatically linked into the final .exe file.

Using "ls":

The program has to be loaded into memory before it can run, then it must
consult the disk directory to find things. This is a bit slow on a floppy
but is gratifyingly fast if the program is located on a hard disk or in
a ram disk, if there are less than a screenful of filenames to be shown.
It starts a bit slower if disk usage is requested, and it takes a second
or so to find and sort more than a hundred filenames at one whack.

Since the program has several options, you might want to encapsulate useful
combinations as ".bat" files, and give each a different name. For example,
I have a file called "ll.bat" that contains

ls -l %1 %2 %3 %4 %5 %6 %7 %8 %9

so that "ll" means "long listing." Other options can be included on the "ll"
command line and they will work properly. I have another called "du.bat" that

ls -asR %1 %2 %3 %5 %6 %7 %8 %9

and summarizes disk use in a compact format.


The options are far from being mutually orthogonal; in particular, the
command "ls -u filename", for a single file that is not a directory, gives
a dumb-looking printout.

If you find yourself using DIR after you have used "ls" then I have

 December 19, 2017  Add comments

Leave a Reply