xref: /titanic_50/usr/src/lib/libast/common/string/fmtfs.c (revision ff3124eff995e6cd8ebd8c6543648e0670920034)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
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 /*
25  * Glenn Fowler
26  * AT&T Bell Laboratories
27  *
28  * return fs type string given stat
29  */
30 
31 #include <ast.h>
32 #include <ls.h>
33 #include <mnt.h>
34 
35 #include "FEATURE/fs"
36 
37 #if _str_st_fstype
38 
39 char*
40 fmtfs(struct stat* st)
41 {
42 	return st->st_fstype;
43 }
44 
45 #else
46 
47 #include <cdt.h>
48 
49 typedef struct Id_s
50 {
51 	Dtlink_t	link;
52 	dev_t		id;
53 	char		name[1];
54 } Id_t;
55 
56 char*
57 fmtfs(struct stat* st)
58 {
59 	register Id_t*		ip;
60 	register void*		mp;
61 	register Mnt_t*		mnt;
62 	register char*		s;
63 	struct stat		rt;
64 	char*			buf;
65 
66 	static Dt_t*		dict;
67 	static Dtdisc_t		disc;
68 
69 	if (!dict)
70 	{
71 		disc.key = offsetof(Id_t, id);
72 		disc.size = sizeof(dev_t);
73 		dict = dtopen(&disc, Dthash);
74 	}
75 	else if (ip = (Id_t*)dtmatch(dict, &st->st_dev))
76 		return ip->name;
77 	s = FS_default;
78 	if (mp = mntopen(NiL, "r"))
79 	{
80 		while ((mnt = mntread(mp)) && (stat(mnt->dir, &rt) || rt.st_dev != st->st_dev));
81 		if (mnt && mnt->type)
82 			s = mnt->type;
83 	}
84 	if (!dict || !(ip = newof(0, Id_t, 1, strlen(s))))
85 	{
86 		if (!mp)
87 			return s;
88 		buf = fmtbuf(strlen(s) + 1);
89 		strcpy(buf, s);
90 		mntclose(mp);
91 		return buf;
92 	}
93 	strcpy(ip->name, s);
94 	if (mp)
95 		mntclose(mp);
96 	dtinsert(dict, ip);
97 	return ip->name;
98 }
99 
100 #endif
101