Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 1990 The Regents of the University of California. |
| 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. |
Damien Miller | 329638e | 2003-06-03 12:12:50 +1000 | [diff] [blame^] | 13 | * 3. Neither the name of the University nor the names of its contributors |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 14 | * may be used to endorse or promote products derived from this software |
| 15 | * without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #include "includes.h" |
| 31 | #ifndef HAVE_STRMODE |
| 32 | |
| 33 | #if defined(LIBC_SCCS) && !defined(lint) |
Damien Miller | 329638e | 2003-06-03 12:12:50 +1000 | [diff] [blame^] | 34 | static char *rcsid = "$OpenBSD: strmode.c,v 1.4 2003/06/02 20:18:38 millert Exp $"; |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 35 | #endif /* LIBC_SCCS and not lint */ |
| 36 | |
| 37 | #include <sys/types.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <string.h> |
| 40 | |
| 41 | void |
Ben Lindstrom | b30768f | 2001-06-09 02:22:16 +0000 | [diff] [blame] | 42 | strmode(register mode_t mode, register char *p) |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 43 | { |
| 44 | /* print type */ |
| 45 | switch (mode & S_IFMT) { |
| 46 | case S_IFDIR: /* directory */ |
| 47 | *p++ = 'd'; |
| 48 | break; |
| 49 | case S_IFCHR: /* character special */ |
| 50 | *p++ = 'c'; |
| 51 | break; |
| 52 | case S_IFBLK: /* block special */ |
| 53 | *p++ = 'b'; |
| 54 | break; |
| 55 | case S_IFREG: /* regular */ |
| 56 | *p++ = '-'; |
| 57 | break; |
| 58 | case S_IFLNK: /* symbolic link */ |
| 59 | *p++ = 'l'; |
| 60 | break; |
Ben Lindstrom | 2396b30 | 2001-01-23 16:54:29 +0000 | [diff] [blame] | 61 | #ifdef S_IFSOCK |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 62 | case S_IFSOCK: /* socket */ |
| 63 | *p++ = 's'; |
| 64 | break; |
Ben Lindstrom | 2396b30 | 2001-01-23 16:54:29 +0000 | [diff] [blame] | 65 | #endif |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 66 | #ifdef S_IFIFO |
| 67 | case S_IFIFO: /* fifo */ |
| 68 | *p++ = 'p'; |
| 69 | break; |
| 70 | #endif |
| 71 | #ifdef S_IFWHT |
| 72 | case S_IFWHT: /* whiteout */ |
| 73 | *p++ = 'w'; |
| 74 | break; |
| 75 | #endif |
| 76 | default: /* unknown */ |
| 77 | *p++ = '?'; |
| 78 | break; |
| 79 | } |
| 80 | /* usr */ |
| 81 | if (mode & S_IRUSR) |
| 82 | *p++ = 'r'; |
| 83 | else |
| 84 | *p++ = '-'; |
| 85 | if (mode & S_IWUSR) |
| 86 | *p++ = 'w'; |
| 87 | else |
| 88 | *p++ = '-'; |
| 89 | switch (mode & (S_IXUSR | S_ISUID)) { |
| 90 | case 0: |
| 91 | *p++ = '-'; |
| 92 | break; |
| 93 | case S_IXUSR: |
| 94 | *p++ = 'x'; |
| 95 | break; |
| 96 | case S_ISUID: |
| 97 | *p++ = 'S'; |
| 98 | break; |
| 99 | case S_IXUSR | S_ISUID: |
| 100 | *p++ = 's'; |
| 101 | break; |
| 102 | } |
| 103 | /* group */ |
| 104 | if (mode & S_IRGRP) |
| 105 | *p++ = 'r'; |
| 106 | else |
| 107 | *p++ = '-'; |
| 108 | if (mode & S_IWGRP) |
| 109 | *p++ = 'w'; |
| 110 | else |
| 111 | *p++ = '-'; |
| 112 | switch (mode & (S_IXGRP | S_ISGID)) { |
| 113 | case 0: |
| 114 | *p++ = '-'; |
| 115 | break; |
| 116 | case S_IXGRP: |
| 117 | *p++ = 'x'; |
| 118 | break; |
| 119 | case S_ISGID: |
| 120 | *p++ = 'S'; |
| 121 | break; |
| 122 | case S_IXGRP | S_ISGID: |
| 123 | *p++ = 's'; |
| 124 | break; |
| 125 | } |
| 126 | /* other */ |
| 127 | if (mode & S_IROTH) |
| 128 | *p++ = 'r'; |
| 129 | else |
| 130 | *p++ = '-'; |
| 131 | if (mode & S_IWOTH) |
| 132 | *p++ = 'w'; |
| 133 | else |
| 134 | *p++ = '-'; |
| 135 | switch (mode & (S_IXOTH | S_ISVTX)) { |
| 136 | case 0: |
| 137 | *p++ = '-'; |
| 138 | break; |
| 139 | case S_IXOTH: |
| 140 | *p++ = 'x'; |
| 141 | break; |
| 142 | case S_ISVTX: |
| 143 | *p++ = 'T'; |
| 144 | break; |
| 145 | case S_IXOTH | S_ISVTX: |
| 146 | *p++ = 't'; |
| 147 | break; |
| 148 | } |
| 149 | *p++ = ' '; /* will be a '+' if ACL's implemented */ |
| 150 | *p = '\0'; |
| 151 | } |
| 152 | #endif |