xref: /titanic_50/usr/src/lib/libast/common/string/fmtls.c (revision 356f72340a69936724c69f2f87fffa6f5887f885)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 /*
24  * Glenn Fowler
25  * AT&T Bell Laboratories
26  *
27  * ls formatter
28  */
29 
30 #include <ast.h>
31 #include <ls.h>
32 #include <tm.h>
33 
34 #ifndef LS_W_MAX
35 #define LS_W_MAX	128
36 #endif
37 
38 /*
39  * ls formatter
40  *
41  *	buf	results placed here
42  *	name	file name
43  *	st	file stat buffer
44  *	info	optional info
45  *	link	link text if != 0
46  *	flags	LS_* flags
47  *
48  *	return	end of formatted buf
49  */
50 
51 char*
52 fmtls(char* buf, const char* name, register struct stat* st, const char* info, const char* link, register int flags)
53 {
54 	register char*		s;
55 	time_t			tm;
56 	Sfoff_t			n;
57 
58 	s = buf;
59 	if (flags & LS_INUMBER)
60 		s += sfsprintf(s, LS_W_MAX, "%*I*u ", LS_W_INUMBER - 1, sizeof(st->st_ino), st->st_ino);
61 	if (flags & LS_BLOCKS)
62 	{
63 		n = iblocks(st);
64 		s += sfsprintf(s, LS_W_MAX, "%*I*u ", LS_W_BLOCKS - 1, sizeof(n), n);
65 	}
66 	if (flags & LS_LONG)
67 	{
68 		s += sfsprintf(s, LS_W_MAX, "%s%3u", fmtmode(st->st_mode, flags & LS_EXTERNAL), (unsigned int)st->st_nlink);
69 		if (!(flags & LS_NOUSER))
70 		{
71 			if (flags & LS_NUMBER)
72 				s += sfsprintf(s, LS_W_MAX, " %-*I*d", LS_W_NAME - 1, sizeof(st->st_uid), st->st_uid);
73 			else
74 				s += sfsprintf(s, LS_W_MAX, " %-*s", LS_W_NAME - 1, fmtuid(st->st_uid));
75 		}
76 		if (!(flags & LS_NOGROUP))
77 		{
78 			if (flags & LS_NUMBER)
79 				s += sfsprintf(s, LS_W_MAX, " %-*I*d", LS_W_NAME - 1, sizeof(st->st_gid), st->st_gid);
80 			else
81 				s += sfsprintf(s, LS_W_MAX, " %-*s", LS_W_NAME - 1, fmtgid(st->st_gid));
82 		}
83 		if (S_ISBLK(st->st_mode) || S_ISCHR(st->st_mode))
84 			s += sfsprintf(s, LS_W_MAX, "%8s ", fmtdev(st));
85 		else
86 			s += sfsprintf(s, LS_W_MAX, "%8I*u ", sizeof(st->st_size), st->st_size);
87 		tm = (flags & LS_ATIME) ? st->st_atime : (flags & LS_CTIME) ? st->st_ctime : st->st_mtime;
88 		s = tmfmt(s, LS_W_LONG / 2, "%?%l", &tm);
89 		*s++ = ' ';
90 	}
91 	if (info)
92 	{
93 		while (*s = *info++)
94 			s++;
95 		*s++ = ' ';
96 	}
97 	while (*s = *name++)
98 		s++;
99 	if (flags & LS_MARK)
100 	{
101 		if (S_ISDIR(st->st_mode))
102 			*s++ = '/';
103 #ifdef S_ISLNK
104 		else if (S_ISLNK(st->st_mode))
105 			*s++ = '@';
106 #endif
107 		else if (st->st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))
108 			*s++ = '*';
109 	}
110 	if (link)
111 	{
112 		s += sfsprintf(s, LS_W_MAX, " %s %s",
113 #ifdef S_ISLNK
114 			S_ISLNK(st->st_mode) ? "->" :
115 #endif
116 				"==", link);
117 	}
118 	*s = 0;
119 	return s;
120 }
121