1 /* $OpenBSD: strmode.c,v 1.9 2024/06/23 07:08:26 otto Exp $ */ 2 /*- 3 * Copyright (c) 1990 The Regents of the University of California. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of the University nor the names of its contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/types.h> 32 #include <sys/stat.h> 33 #include <string.h> 34 35 void 36 strmode(mode_t mode, char *p) 37 { 38 /* print type */ 39 switch (mode & S_IFMT) { 40 case S_IFDIR: /* directory */ 41 *p++ = 'd'; 42 break; 43 case S_IFCHR: /* character special */ 44 *p++ = 'c'; 45 break; 46 case S_IFBLK: /* block special */ 47 *p++ = 'b'; 48 break; 49 case S_IFREG: /* regular */ 50 *p++ = '-'; 51 break; 52 case S_IFLNK: /* symbolic link */ 53 *p++ = 'l'; 54 break; 55 case S_IFSOCK: /* socket */ 56 *p++ = 's'; 57 break; 58 #ifdef S_IFIFO 59 case S_IFIFO: /* fifo */ 60 *p++ = 'p'; 61 break; 62 #endif 63 default: /* unknown */ 64 *p++ = '?'; 65 break; 66 } 67 /* usr */ 68 if (mode & S_IRUSR) 69 *p++ = 'r'; 70 else 71 *p++ = '-'; 72 if (mode & S_IWUSR) 73 *p++ = 'w'; 74 else 75 *p++ = '-'; 76 switch (mode & (S_IXUSR | S_ISUID)) { 77 case 0: 78 *p++ = '-'; 79 break; 80 case S_IXUSR: 81 *p++ = 'x'; 82 break; 83 case S_ISUID: 84 *p++ = 'S'; 85 break; 86 case S_IXUSR | S_ISUID: 87 *p++ = 's'; 88 break; 89 } 90 /* group */ 91 if (mode & S_IRGRP) 92 *p++ = 'r'; 93 else 94 *p++ = '-'; 95 if (mode & S_IWGRP) 96 *p++ = 'w'; 97 else 98 *p++ = '-'; 99 switch (mode & (S_IXGRP | S_ISGID)) { 100 case 0: 101 *p++ = '-'; 102 break; 103 case S_IXGRP: 104 *p++ = 'x'; 105 break; 106 case S_ISGID: 107 *p++ = 'S'; 108 break; 109 case S_IXGRP | S_ISGID: 110 *p++ = 's'; 111 break; 112 } 113 /* other */ 114 if (mode & S_IROTH) 115 *p++ = 'r'; 116 else 117 *p++ = '-'; 118 if (mode & S_IWOTH) 119 *p++ = 'w'; 120 else 121 *p++ = '-'; 122 switch (mode & (S_IXOTH | S_ISVTX)) { 123 case 0: 124 *p++ = '-'; 125 break; 126 case S_IXOTH: 127 *p++ = 'x'; 128 break; 129 case S_ISVTX: 130 *p++ = 'T'; 131 break; 132 case S_IXOTH | S_ISVTX: 133 *p++ = 't'; 134 break; 135 } 136 *p++ = ' '; /* will be a '+' if ACL's implemented */ 137 *p = '\0'; 138 } 139 DEF_WEAK(strmode); 140