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