blob: 4df1b9f2f5a8a9c8fd6da21c25164121d6011ae7 [file] [log] [blame]
Dmitry V. Levin9514ac72014-12-06 03:53:16 +00001#include "defs.h"
2
3#include <fcntl.h>
4
5#include "xlat/modetypes.h"
6
7const char *
8sprintmode(int mode)
9{
10 static char buf[sizeof("S_IFSOCK|S_ISUID|S_ISGID|S_ISVTX|%o")
11 + sizeof(int)*3
12 + /*paranoia:*/ 8];
13 const char *s;
14
15 if ((mode & S_IFMT) == 0)
16 s = "";
17 else if ((s = xlookup(modetypes, mode & S_IFMT)) == NULL) {
18 sprintf(buf, "%#o", mode);
19 return buf;
20 }
21 s = buf + sprintf(buf, "%s%s%s%s", s,
22 (mode & S_ISUID) ? "|S_ISUID" : "",
23 (mode & S_ISGID) ? "|S_ISGID" : "",
24 (mode & S_ISVTX) ? "|S_ISVTX" : "");
25 mode &= ~(S_IFMT|S_ISUID|S_ISGID|S_ISVTX);
26 if (mode)
27 sprintf((char*)s, "|%#o", mode);
28 s = (*buf == '|') ? buf + 1 : buf;
29 return *s ? s : "0";
30}