home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
magazine
/
drdobbs
/
c_spec
/
sources
/
ls.c
< prev
next >
Wrap
C/C++ Source or Header
|
1986-02-20
|
5KB
|
173 lines
#include <stdio.h>
#include "/include/getargs.h"
#include "/include/dir.h"
#define STAND_ALONE 1
/*----------------------------------------------------------------------+
* LS.C: An MSDOS directory utility |
* |
* (c) Copyright 1985, Allen I. Holub. All rights reserved. |
* This program may be reproduced for personal, non-profit use only. |
* |
* Exit status: 0 if files are found. |
* 1 if no files are found. |
* 2 if internal error. |
* |
* Note that though this version of ls doesn't do a sort by extension |
* if you say "ls *.c *.exe" the sub groups will stay together |
*----------------------------------------------------------------------+
*/
extern DIRECTORY *mk_dir();
#define ISTRUE(x) (x != 0)
#define ISFALSE(x) (x == 0)
#define MAXBUF 132
#define MAXDIRS 256 /* Max number of directories that will be printed */
static int Longfmt = 0; /* Global variables set by command line */
static int Dont_expand = 0;
static int Files_only = 0;
static int No_graphics = 0;
static int Dirs_only = 0;
static int List_all = 0;
static int Num_cols = 0;
static int Pathname = 0;
static ARG Argtab[] =
{
{ 'a', BOOLEAN, &List_all, "List all files (including hidden)" },
{ 'c', INTEGER, &Num_cols, "Print output in <num> columns" },
{ 'd', BOOLEAN, &Dirs_only, "List only directories" },
{ 'f', BOOLEAN, &Files_only, "List only files" },
{ 'l', BOOLEAN, &Longfmt, "Print directory in long format" },
{ 'p', BOOLEAN, &Pathname, "Prepend pathname if one is given" },
{ 's', BOOLEAN, &No_graphics,"Suppress ANSI graphics chars" },
{ 'x', BOOLEAN, &Dont_expand,"Don't expand directory names" }
};
#define TSIZE (sizeof(Argtab)/sizeof(ARG))
/*----------------------------------------------------------------------*/
static void printdir(dirc, dirv, maxwidth)
int dirc ;
char **dirv ;
{
/* Print out the directory in Numcolumns. Note that if
* Numcols is not specified on the command line then
* the proper number of columns will be computed here.
*/
if( !Num_cols )
Num_cols = 79 / (maxwidth+1) ;
printf("\n");
ptext( dirc, dirv, stdout, Num_cols, 80/Num_cols,
(dirc/Num_cols) + (dirc % Num_cols != 0) );
printf("\n");
}
/*----------------------------------------------------------------------*/
static DIRECTORY *makedir()
{
register DIRECTORY *dp;
if( !(dp = mk_dir(MAXDIRS)) )
{
fprintf(stderr, "ls: out of memory!");
exit( 2 );
}
dp->longf = ISTRUE ( Longfmt );
dp->files = ISFALSE ( Dirs_only );
dp->dirs = ISFALSE ( Files_only );
dp->graphics = ISFALSE ( No_graphics );
dp->hidden = ISTRUE ( List_all );
dp->path = ISTRUE ( Pathname );
dp->label = 1;
dp->sort = 1;
dp->exp = ISFALSE ( Dont_expand );
return dp;
}
/*----------------------------------------------------------------------*/
void ls( argc, argv )
char **argv;
{
static DIRECTORY *dp ;
unsigned tc, spc, bps, ac;/* Total clusters, sectors/cluster,
* bytes/sector, available clusters.
*/
argc = getargs( argc, argv, Argtab, TSIZE );
if( Longfmt ) /* -l is always printed in 1 column */
Num_cols = 1; /* even if -cN was given on the cmd */
/* line */
dp = makedir();
--argc;
++argv;
diskinfo( (**argv && argv[0][1]==':') ? (toupper(**argv)-'A')+1: 0,
&spc, &bps, &ac, &tc );
if( argc <= 0 )
dir( "*.*" , dp );
else
for(; --argc >= 0 ; dir(*argv++, dp) )
;
printdir( dp->nfiles + dp->ndirs, dp->dirv, dp->width);
if( dp->nfiles )
printf("%d file%s (%4.1f K out of %ld K)",
dp->nfiles, dp->nfiles == 1 ? "" : "s",
(double)((double)dp->nbytes/1024.0),
(long)(((long)tc * (long)spc * (long)bps)/1024L));
if( dp->ndirs )
{
if( dp->nfiles )
printf(", ");
printf("%d director%s",dp->ndirs, dp->ndirs==1 ? "y" :"ies");
}
if( *dp->vol_label && (dp->nfiles || dp->ndirs) )
printf( ", Volume: %s" , dp->vol_label );
printf("\n");
if( dp->maxdirs <= 0 )
{
printf("\007Maximum directory count reached,");
printf(" list may be truncated.\n");
}
exit( !(dp->nfiles + dp->ndirs) );
}
/*----------------------------------------------------------------------*/
#ifdef STAND_ALONE
main( argc, argv )
char **argv;
{
ctlc();
reargv(&argc, &argv);
fprintf(stderr,"LS: Copyright (C) 1986, Allen I. Holub. ");
fprintf(stderr,"All rights reserved.\n");
ls( argc, argv );
}
#endif