xref: /onnv-gate/usr/src/lib/libast/common/string/fmtls.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin /*
244887Schin  * Glenn Fowler
254887Schin  * AT&T Bell Laboratories
264887Schin  *
274887Schin  * ls formatter
284887Schin  */
294887Schin 
304887Schin #include <ast.h>
314887Schin #include <ls.h>
324887Schin #include <tm.h>
334887Schin 
344887Schin #ifndef LS_W_MAX
354887Schin #define LS_W_MAX	128
364887Schin #endif
374887Schin 
384887Schin /*
394887Schin  * ls formatter
404887Schin  *
414887Schin  *	buf	results placed here
424887Schin  *	name	file name
434887Schin  *	st	file stat buffer
444887Schin  *	info	optional info
454887Schin  *	link	link text if != 0
464887Schin  *	flags	LS_* flags
474887Schin  *
484887Schin  *	return	end of formatted buf
494887Schin  */
504887Schin 
514887Schin char*
fmtls(char * buf,const char * name,register struct stat * st,const char * info,const char * link,register int flags)524887Schin fmtls(char* buf, const char* name, register struct stat* st, const char* info, const char* link, register int flags)
534887Schin {
544887Schin 	register char*		s;
554887Schin 	time_t			tm;
564887Schin 	Sfoff_t			n;
574887Schin 
584887Schin 	s = buf;
594887Schin 	if (flags & LS_INUMBER)
604887Schin 		s += sfsprintf(s, LS_W_MAX, "%*I*u ", LS_W_INUMBER - 1, sizeof(st->st_ino), st->st_ino);
614887Schin 	if (flags & LS_BLOCKS)
624887Schin 	{
634887Schin 		n = iblocks(st);
644887Schin 		s += sfsprintf(s, LS_W_MAX, "%*I*u ", LS_W_BLOCKS - 1, sizeof(n), n);
654887Schin 	}
664887Schin 	if (flags & LS_LONG)
674887Schin 	{
684887Schin 		s += sfsprintf(s, LS_W_MAX, "%s%3u", fmtmode(st->st_mode, flags & LS_EXTERNAL), (unsigned int)st->st_nlink);
694887Schin 		if (!(flags & LS_NOUSER))
704887Schin 		{
714887Schin 			if (flags & LS_NUMBER)
724887Schin 				s += sfsprintf(s, LS_W_MAX, " %-*I*d", LS_W_NAME - 1, sizeof(st->st_uid), st->st_uid);
734887Schin 			else
744887Schin 				s += sfsprintf(s, LS_W_MAX, " %-*s", LS_W_NAME - 1, fmtuid(st->st_uid));
754887Schin 		}
764887Schin 		if (!(flags & LS_NOGROUP))
774887Schin 		{
784887Schin 			if (flags & LS_NUMBER)
794887Schin 				s += sfsprintf(s, LS_W_MAX, " %-*I*d", LS_W_NAME - 1, sizeof(st->st_gid), st->st_gid);
804887Schin 			else
814887Schin 				s += sfsprintf(s, LS_W_MAX, " %-*s", LS_W_NAME - 1, fmtgid(st->st_gid));
824887Schin 		}
834887Schin 		if (S_ISBLK(st->st_mode) || S_ISCHR(st->st_mode))
844887Schin 			s += sfsprintf(s, LS_W_MAX, "%8s ", fmtdev(st));
854887Schin 		else
864887Schin 			s += sfsprintf(s, LS_W_MAX, "%8I*u ", sizeof(st->st_size), st->st_size);
874887Schin 		tm = (flags & LS_ATIME) ? st->st_atime : (flags & LS_CTIME) ? st->st_ctime : st->st_mtime;
884887Schin 		s = tmfmt(s, LS_W_LONG / 2, "%?%l", &tm);
894887Schin 		*s++ = ' ';
904887Schin 	}
914887Schin 	if (info)
924887Schin 	{
934887Schin 		while (*s = *info++)
944887Schin 			s++;
954887Schin 		*s++ = ' ';
964887Schin 	}
974887Schin 	while (*s = *name++)
984887Schin 		s++;
994887Schin 	if (flags & LS_MARK)
1004887Schin 	{
1014887Schin 		if (S_ISDIR(st->st_mode))
1024887Schin 			*s++ = '/';
1034887Schin #ifdef S_ISLNK
1044887Schin 		else if (S_ISLNK(st->st_mode))
1054887Schin 			*s++ = '@';
1064887Schin #endif
1074887Schin 		else if (st->st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))
1084887Schin 			*s++ = '*';
1094887Schin 	}
1104887Schin 	if (link)
1114887Schin 	{
1124887Schin 		s += sfsprintf(s, LS_W_MAX, " %s %s",
1134887Schin #ifdef S_ISLNK
1144887Schin 			S_ISLNK(st->st_mode) ? "->" :
1154887Schin #endif
1164887Schin 				"==", link);
1174887Schin 	}
1184887Schin 	*s = 0;
1194887Schin 	return s;
1204887Schin }
121