From: ysato@etl.go.jp (Yutaka Sato =?ISO-2022-JP?B?GyRAOjRGI0stGyhK?=)
Subject: ls -l (was Re: News system and file system)
Reply-To: ysato@etl.go.jp (Yutaka Sato =?ISO-2022-JP?B?GyRAOjRGI0stGyhK?=)
Organization: Electrotechnical Laboratory, Tsukuba Science City
	<1992Jul23.014709.24321@tsukuba.sanyo.co.jp>  
	<1992Jul23.173645.14753@iris.is.tsukuba.ac.jp>  
	<gRldi.ysato@etl.go.jp>  
	<Bs4qHI.5xz@fxisms.fxis.fujixerox.co.jp>  
	<gTWtT.ysato@etl.go.jp>  
	<TAKUMA.92Jul31124408@alps.acrl.msr.mei.co.jp>  
	<gTWtT.ysato@etl.go.jp> <gUBoZ.ysato@etl.go.jp> 
	<gRldi.ysato@etl.go.jp>
Distribution: fj
Date: Fri, 31 Jul 1992 07:56:53 GMT

$@EEAm8&$N:4F#$G$9!#$7$D$3$/!"(Jls -l$@$K$D$$$F(J(^_^)$@!#(J

On 07/31/92(13:18) ysato@etl.go.jp (Yutaka Sato $@:4F#K-(J) wrote
in <gUBoZ.ysato@etl.go.jp> (fj.unix:1723/etlss2):
 |On 07/29/92(12:48) ysato@etl.go.jp (Yutaka Sato $@:4F#K-(J) wrote
 |in <gTWtT.ysato@etl.go.jp> (fj.news.b:778/etlss2):
 | |$@$^$:!";d$,LdBj$K$7$F$$$k$N$O!"Bg$-$J%G%#%l%/%H%j(J
 | |$@$r(Jls -l$@$7$?$H$-$N!":G=i$N=PNO$,=P$F$/$k$^$G$N(J
 | |$@%l%9%]%s%9$G$9!#(Jls -l$@C1BN$NB.EY$@$1$r5$$K$9$k$J$i(J
 | |$@$I$&$G$b$h$$$N$G$9$,!"$=$N=PNO$r=hM}$9$k<!CJ(J
 | |($@IaDL?M4V$G$9$,(J^_^)$@$H$NJBNsF0:n@-$r>e$2$?$$$o$1$G$9!#(J
 |
 |$@$H$$$&$3$H$G!"(Jls -l$@$,40A4$K=*$k$^$G<!CJ$N=hM}$,9T$J$((J
 |$@$J$$$N$G$O!"!VA4BN$H$7$F$N!W<B<B9T;~4V$OD9$/$+$+$C$F(J
 |$@$7$^$$$^$9!#$G!"$3$l$K$h$kCY1d$K$/$i$Y$l$P!"AjBPE*$K(J
 |
 | |$@L>A0=g=PNO$O!"%G%#%l%/%H%j%U%!%$%k0l$DFI$`$@$1$G(J
 | |$@%=!<%H$G$-$^$9$+$i!"$=$l<+BN$O$3$N>u67$G$O$=$l$[$I(J
 | |$@$NCY1d$r$b$?$i$5$J$$$G$7$g$&!#(J
 |
 |$@$H;W$C$?$o$1$G$9!#(J

$@%G%#%l%/%H%j$rL>A0=g$K%=!<%H$7$F(Jlstat()$@$7$J$,$i=PNO$9$k%W%m%0%i%`(J
"lsd"($@IUO?(J)$@$H!"(Jls -la$@$rHf3S$7$F$_$^$7$?!#(J
$@Nc$K$h$C$F!"BP>]%G%#%l%/%H%j$O(Jspool/news/fj/jokes$@$G$9(J(^_^)

  etlss2[0]% cd /var/spool/news/fj/jokes; ls|wc
      5769    5769   34614

$@$^$:!";d$,ITK~$K;W$C$F$$$k!":G=i$N%(%s%H%j$,%j%9%H$5$l$k$^$G$N(J
$@;~4V$r$O$+$C$F$_$^$9!'(J

  etlss2[1]% time echo `lsd | head`
  ...
  0.0u 0.1s 0:01 8% 0+96k 1+0io 0pf+0w$@!!(J(real=$@#1IC(J)

  etlss2[2]% time echo `ls -la | head`
  ...
  0.0u 0.1s 0:37 0% 0+104k 5+0io 2pf+0w (real=$@#3#7IC(J)
  
$@IaDL$O(Jhead$@$J$I$G$O$J$/!"(Jmore$@$"$?$j$r;H$&$o$1$G$9$,!"(J
$@:G=i$N%b%N$,0l=V$G=P$F$/$k$+!"2?==ICBT$?$5$l$k$+$O!"%f!<%6$K(J
$@$H$C$F$OBg0c$$$@$H;W$$$^$9!#(J

$@<!$K!"$3$N(Jlsd$@$N<B8=$K$h$C$F$I$N$/$i$$(Jls$@$N@-G=$,Mn$A$k$+$rD4$Y$F(J
$@$_$^$9(J([5]$@$H(J[6]$@$OF1;~$K<B9T(J)$@!'(J

  etlss2[3]% time lsd > /dev/null
  3.4u 11.8s 0:41 36% 0+432k 204+81io 4pf+0w

  etlss2[4]% time ls -la > /dev/null
  3.5u  9.8s 0:37 35% 0+540k 218+96io 10pf+0w

  etlss2[5]% time lsd > /dev/null
  3.3u 23.6s 2:53 15% 0+444k 196+57io 2pf+0w

  etlss2[6]% time ls -la > /dev/null
  3.7u 19.7s 2:28 15% 0+536k 175+40io 4pf+0w

system$@;~4V$G#2#0!s$/$i$$!"(Jlsd$@$NJ}$,@-G=$,0-2=$7$F$$$^$9!#(J
$@$^$?!"$3$3$GBP>]$K$7$?$h$&$J%K%e!<%9%9%W!<%k$N>l9g!"L>A0(J
($@5-;vHV9f(J)$@=g$,!"%(%s%H%j=g$K6a$$$?$a$K!"$"$^$j@-G=$,0-2=(J
$@$7$J$$$o$1$G!"IaDL$N%G%#%l%/%H%j$G$O!"$b$C$H@-G=:9$,=P$k(J
$@$G$7$g$&$+!#(J
$@$3$l$O%7%9%F%`$K$H$C$F$OLdBj$G$9!#(J

$@$3$N@-G=:9$r9M$($k$H!"8=:_$N(Jls -l$@$N<B8=$OBEEv$J$N$+$bCN$l$^(J
$@$;$s!#$H$$$&$o$1$G!"(J

On 07/28/92(20:54) ysato@etl.go.jp (Yutaka Sato $@:4F#K-(J) wrote
in <gRldi.ysato@etl.go.jp> (fj.news.b:774/etlss2):
 | |ls -l $@$O!"#1EY!"%G%#%l%/%H%j$N=P8==g=x$G(J stat(2) $@$7$^$/$j!"(J
 | |$@A4ItD4$Y$?$"$H$G%=!<%H$7$F$$$k$h$&$G$9!#(JSun $@$@$H(J
 |
 |$@$0$o!<!#$=$l$O$H$s$G$b$J$$<BAu$G$9$M!#$I$*$j$G!"$?$@$N(Jls -l
 |$@$J$N$K7k2L$,%9%0$K=P$F$3$J$$$o$1$@!#(J

$@$3$l$O!"8@$$?y$G$7$?$M(J(^_^;)

$@$7$+$7!"$&!<$`!#$H9M$(9~$s$@8e!"$7$e$o$o$C$H%j%9%H$,=P$F$/$k(J
$@$N$O!"$d$C$Q$j:$$k!D(J
$@=8CfE*$K(Jstat()$@$5$l$k$N$b!"B>$N%W%m%;%9$K$H$C$FLBOG$@$H;W$&$7!D(J

$@(=(!(!(J $@(!(=(!(J $@(=(J
$@(7(!(J $@!\!\:4F#!w>pJs%Y!<%9!%EEAm8&!!!!!!!!!!!!!!2w?J7b%5%H%&$N$4$O$s(J
$@(?(!(!(J $@!!(?!!(J $@(?(!(!(!(!(!(!(!(!(!(!(!(!(!(!(!(!(!(!(!(!(!(!(!(!(!(!#!!z!y"{(J

($@2F5Y$_FCJLIUO?(J)
/*////////////////////////////////////////////////////////////////////////
Content-Type:	program/C; charset=US-ASCII
Program:	lsd.c (list the content of directory)
Author:		Yutaka Sato <ysato@etl.go.jp>
Synopsys:
		lsd [dir-name]
Description:
How-To-Make:
		cc -DMAIN lsd.c -O -o lsd
History:
    920731	extracted from cosmos library
///////////////////////////////////////////////////////////////////////*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/dir.h>
#include <time.h>
#include <pwd.h>

char *uid_to_uname(uid){
	struct passwd *pwent;
	static char uname[20];
	static int last_uid = -1;

	if( uid == last_uid )
		return uname;

	*uname = 0;
	pwent = getpwuid(uid);
	if( pwent == NULL )
		sprintf(uname,"%d",uid);
	else	strcpy(uname,pwent->pw_name);
	last_uid = uid;
	return uname;
}

char *
file_mode_str(mode){
	static char modes[20];
	int i,mask;

	modes[0] = '-';	/* (^_^;) */
	mask = 1;
	for(i = 0; i < 9; i++){
		if( mode & mask )
			modes[9-i] = "xwr"[ i % 3 ];
		else	modes[9-i] = '-';
		mask <<= 1;
	}
	return modes;
}

char *
file_date_str(clock){
	static char dates[20];
	struct tm *tm;

	tm = localtime(&clock);
	sprintf(dates,"%02d/%02d/%02d %02d:%02d:%02d",
		tm->tm_year,tm->tm_mon+1,tm->tm_mday,
		tm->tm_hour,tm->tm_min,  tm->tm_sec);
	return dates;
}

char *
file_attributes(stat,line)
	struct stat *stat;
	char *line;
{
	sprintf(line,"%s %-9s %7d %s",
		file_mode_str(stat->st_mode),
		uid_to_uname(stat->st_uid),
		stat->st_size,
		file_date_str(stat->st_ctime)
	);
	return line;
}

char *
FileAttributes(file,line) char *line; {
	struct stat statb;

	*line = 0;
	if( lstat(file,&statb) == 0 )
		file_attributes(&statb,line);
	else	perror("stat:");
	return line;
}

alphasort();
directory_ls(dir,options)
	char *dir,*options;
{	struct direct **namelist,*de;
	int nent,ei;
	char line[MAXNAMLEN];

	if( (nent=scandir(dir,&namelist,NULL,alphasort)) == -1 ){
		fprintf(stderr,"Cannot open directory: %s\n",dir);
		return;
	}
	for(ei = 0; ei < nent; ei++){
		de = namelist[ei];
		FileAttributes(de->d_name,line);
		printf("%s %s\n",line,de->d_name);
	}
	free(namelist);
}

#ifdef MAIN
main(ac,av)
	char *av[];
{	char *dir;

	if( ac < 2 )
		dir = ".";
	else{
		dir = av[1];
		chdir(dir);
	}
	directory_ls(dir,0);
}
#endif MAIN

Newsgroups: fj.unix,fj.questions.unix,fj.wanted