xref: /onnv-gate/usr/src/lib/libast/common/string/fmtfs.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 /*
254887Schin  * Glenn Fowler
264887Schin  * AT&T Bell Laboratories
274887Schin  *
284887Schin  * return fs type string given stat
294887Schin  */
304887Schin 
314887Schin #include <ast.h>
324887Schin #include <ls.h>
334887Schin #include <mnt.h>
344887Schin 
354887Schin #include "FEATURE/fs"
364887Schin 
374887Schin #if _str_st_fstype
384887Schin 
394887Schin char*
fmtfs(struct stat * st)404887Schin fmtfs(struct stat* st)
414887Schin {
424887Schin 	return st->st_fstype;
434887Schin }
444887Schin 
454887Schin #else
464887Schin 
474887Schin #include <cdt.h>
484887Schin 
494887Schin typedef struct Id_s
504887Schin {
514887Schin 	Dtlink_t	link;
524887Schin 	dev_t		id;
534887Schin 	char		name[1];
544887Schin } Id_t;
554887Schin 
564887Schin char*
fmtfs(struct stat * st)574887Schin fmtfs(struct stat* st)
584887Schin {
594887Schin 	register Id_t*		ip;
604887Schin 	register void*		mp;
614887Schin 	register Mnt_t*		mnt;
624887Schin 	register char*		s;
634887Schin 	struct stat		rt;
644887Schin 	char*			buf;
654887Schin 
664887Schin 	static Dt_t*		dict;
674887Schin 	static Dtdisc_t		disc;
684887Schin 
694887Schin 	if (!dict)
704887Schin 	{
714887Schin 		disc.key = offsetof(Id_t, id);
724887Schin 		disc.size = sizeof(dev_t);
734887Schin 		dict = dtopen(&disc, Dthash);
744887Schin 	}
754887Schin 	else if (ip = (Id_t*)dtmatch(dict, &st->st_dev))
764887Schin 		return ip->name;
774887Schin 	s = FS_default;
784887Schin 	if (mp = mntopen(NiL, "r"))
794887Schin 	{
804887Schin 		while ((mnt = mntread(mp)) && (stat(mnt->dir, &rt) || rt.st_dev != st->st_dev));
814887Schin 		if (mnt && mnt->type)
824887Schin 			s = mnt->type;
834887Schin 	}
844887Schin 	if (!dict || !(ip = newof(0, Id_t, 1, strlen(s))))
854887Schin 	{
864887Schin 		if (!mp)
874887Schin 			return s;
884887Schin 		buf = fmtbuf(strlen(s) + 1);
894887Schin 		strcpy(buf, s);
904887Schin 		mntclose(mp);
914887Schin 		return buf;
924887Schin 	}
934887Schin 	strcpy(ip->name, s);
944887Schin 	if (mp)
954887Schin 		mntclose(mp);
964887Schin 	dtinsert(dict, ip);
974887Schin 	return ip->name;
984887Schin }
994887Schin 
1004887Schin #endif
101