|
| 1 | +/*- |
| 2 | + * Copyright (c) 2023 classabbyamp. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions |
| 7 | + * are met: |
| 8 | + * 1. Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer in the |
| 12 | + * documentation and/or other materials provided with the distribution. |
| 13 | + * |
| 14 | + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 | + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 16 | + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 17 | + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 18 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 19 | + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 20 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 21 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 23 | + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | + */ |
| 25 | + |
| 26 | +#include <sys/types.h> |
| 27 | +#include <sys/stat.h> |
| 28 | + |
| 29 | +#include "compat.h" |
| 30 | + |
| 31 | +void HIDDEN |
| 32 | +strmode(mode_t mode, char *buf) { |
| 33 | + switch (mode & S_IFMT) { |
| 34 | + // many of these are not currently packageable, but why not keep them for future compat? |
| 35 | + case S_IFSOCK: *buf++ = 's'; break; |
| 36 | + case S_IFLNK: *buf++ = 'l'; break; |
| 37 | + case S_IFREG: *buf++ = '-'; break; |
| 38 | + case S_IFBLK: *buf++ = 'b'; break; |
| 39 | + case S_IFDIR: *buf++ = 'd'; break; |
| 40 | + case S_IFCHR: *buf++ = 'c'; break; |
| 41 | + case S_IFIFO: *buf++ = 'p'; break; |
| 42 | +#ifdef S_IFWHT |
| 43 | + case S_IFWHT: *buf++ = 'w'; break; |
| 44 | +#endif |
| 45 | + default: *buf++ = '?'; break; |
| 46 | + } |
| 47 | + *buf++ = (mode & S_IRUSR) ? 'r' : '-'; |
| 48 | + *buf++ = (mode & S_IWUSR) ? 'w' : '-'; |
| 49 | + switch (mode & (S_IXUSR | S_ISUID)) { |
| 50 | + case (S_IXUSR | S_ISUID): *buf++ = 's'; break; |
| 51 | + case S_ISUID: *buf++ = 'S'; break; |
| 52 | + case S_IXUSR: *buf++ = 'x'; break; |
| 53 | + default: *buf++ = '-'; break; |
| 54 | + } |
| 55 | + |
| 56 | + *buf++ = (mode & S_IRGRP) ? 'r' : '-'; |
| 57 | + *buf++ = (mode & S_IWGRP) ? 'w' : '-'; |
| 58 | + switch (mode & (S_IXGRP | S_ISGID)) { |
| 59 | + case S_IXGRP | S_ISGID: *buf++ = 's'; break; |
| 60 | + case S_ISUID: *buf++ = 'S'; break; |
| 61 | + case S_IXGRP: *buf++ = 'x'; break; |
| 62 | + default: *buf++ = '-'; break; |
| 63 | + } |
| 64 | + |
| 65 | + *buf++ = (mode & S_IROTH) ? 'r' : '-'; |
| 66 | + *buf++ = (mode & S_IWOTH) ? 'w' : '-'; |
| 67 | + switch (mode & (S_IXOTH | S_ISVTX)) { |
| 68 | + case S_IXOTH | S_ISVTX: *buf++ = 't'; break; |
| 69 | + case S_ISVTX: *buf++ = 'T'; break; |
| 70 | + case S_IXOTH: *buf++ = 'x'; break; |
| 71 | + default: *buf++ = '-'; break; |
| 72 | + } |
| 73 | + |
| 74 | + *buf = '\0'; |
| 75 | +} |
0 commit comments