Produce a string representation of Unix mode bits like that used by ls(1). * The "buf" buffer must be at least 11 characters. */
| 25 | /* Produce a string representation of Unix mode bits like that used by ls(1). |
| 26 | * The "buf" buffer must be at least 11 characters. */ |
| 27 | void permstring(char *perms, mode_t mode) |
| 28 | { |
| 29 | static const char *perm_map = "rwxrwxrwx"; |
| 30 | int i; |
| 31 | |
| 32 | strlcpy(perms, "----------", 11); |
| 33 | |
| 34 | for (i = 0; i < 9; i++) { |
| 35 | if (mode & (1 << i)) |
| 36 | perms[9-i] = perm_map[8-i]; |
| 37 | } |
| 38 | |
| 39 | /* Handle setuid/sticky bits. You might think the indices are |
| 40 | * off by one, but remember there's a type char at the |
| 41 | * start. */ |
| 42 | if (mode & S_ISUID) |
| 43 | perms[3] = (mode & S_IXUSR) ? 's' : 'S'; |
| 44 | |
| 45 | if (mode & S_ISGID) |
| 46 | perms[6] = (mode & S_IXGRP) ? 's' : 'S'; |
| 47 | |
| 48 | #ifdef S_ISVTX |
| 49 | if (mode & S_ISVTX) |
| 50 | perms[9] = (mode & S_IXOTH) ? 't' : 'T'; |
| 51 | #endif |
| 52 | |
| 53 | if (S_ISDIR(mode)) |
| 54 | perms[0] = 'd'; |
| 55 | else if (S_ISLNK(mode)) |
| 56 | perms[0] = 'l'; |
| 57 | else if (S_ISBLK(mode)) |
| 58 | perms[0] = 'b'; |
| 59 | else if (S_ISCHR(mode)) |
| 60 | perms[0] = 'c'; |
| 61 | else if (S_ISSOCK(mode)) |
| 62 | perms[0] = 's'; |
| 63 | else if (S_ISFIFO(mode)) |
| 64 | perms[0] = 'p'; |
| 65 | } |
no test coverage detected