| 1 | /*      $NetBSD: strmode.c,v 1.16 2004/06/20 22:20:15 jmc Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /*- | 
|---|
| 4 | * Copyright (c) 1990, 1993 | 
|---|
| 5 | *      The Regents of the University of California.  All rights reserved. | 
|---|
| 6 | * | 
|---|
| 7 | * Redistribution and use in source and binary forms, with or without | 
|---|
| 8 | * modification, are permitted provided that the following conditions | 
|---|
| 9 | * are met: | 
|---|
| 10 | * 1. Redistributions of source code must retain the above copyright | 
|---|
| 11 | *    notice, this list of conditions and the following disclaimer. | 
|---|
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | 
|---|
| 13 | *    notice, this list of conditions and the following disclaimer in the | 
|---|
| 14 | *    documentation and/or other materials provided with the distribution. | 
|---|
| 15 | * 3. Neither the name of the University nor the names of its contributors | 
|---|
| 16 | *    may be used to endorse or promote products derived from this software | 
|---|
| 17 | *    without specific prior written permission. | 
|---|
| 18 | * | 
|---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 
|---|
| 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
|---|
| 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 
|---|
| 22 | * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 
|---|
| 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
|---|
| 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 
|---|
| 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 
|---|
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 
|---|
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 
|---|
| 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|---|
| 29 | * SUCH DAMAGE. | 
|---|
| 30 | */ | 
|---|
| 31 |  | 
|---|
| 32 | /*#include <sys/cdefs.h>*/ | 
|---|
| 33 | #if defined(LIBC_SCCS) && !defined(lint) | 
|---|
| 34 | #if 0 | 
|---|
| 35 | static char sccsid[] = "@(#)strmode.c   8.3 (Berkeley) 8/15/94"; | 
|---|
| 36 | #else | 
|---|
| 37 | __RCSID("$NetBSD: strmode.c,v 1.16 2004/06/20 22:20:15 jmc Exp $"); | 
|---|
| 38 | #endif | 
|---|
| 39 | #endif /* LIBC_SCCS and not lint */ | 
|---|
| 40 |  | 
|---|
| 41 | /*#include "namespace.h"*/ | 
|---|
| 42 | #include <sys/types.h> | 
|---|
| 43 | #include <sys/stat.h> | 
|---|
| 44 |  | 
|---|
| 45 | #include <assert.h> | 
|---|
| 46 | #ifndef _MSC_VER | 
|---|
| 47 | #include <unistd.h> | 
|---|
| 48 | #else | 
|---|
| 49 | #include "mscfakes.h" | 
|---|
| 50 | #endif | 
|---|
| 51 |  | 
|---|
| 52 | #ifndef _DIAGASSERT | 
|---|
| 53 | #define _DIAGASSERT assert | 
|---|
| 54 | #endif | 
|---|
| 55 |  | 
|---|
| 56 | void | 
|---|
| 57 | bsd_strmode(mode, p) | 
|---|
| 58 | mode_t mode; | 
|---|
| 59 | char *p; | 
|---|
| 60 | { | 
|---|
| 61 |  | 
|---|
| 62 | _DIAGASSERT(p != NULL); | 
|---|
| 63 |  | 
|---|
| 64 | /* print type */ | 
|---|
| 65 | switch (mode & S_IFMT) { | 
|---|
| 66 | case S_IFDIR:                   /* directory */ | 
|---|
| 67 | *p++ = 'd'; | 
|---|
| 68 | break; | 
|---|
| 69 | case S_IFCHR:                   /* character special */ | 
|---|
| 70 | *p++ = 'c'; | 
|---|
| 71 | break; | 
|---|
| 72 | #ifdef S_IFBLK | 
|---|
| 73 | case S_IFBLK:                   /* block special */ | 
|---|
| 74 | *p++ = 'b'; | 
|---|
| 75 | break; | 
|---|
| 76 | #endif | 
|---|
| 77 | case S_IFREG:                   /* regular */ | 
|---|
| 78 | #ifdef S_ARCH2 | 
|---|
| 79 | if ((mode & S_ARCH2) != 0) { | 
|---|
| 80 | *p++ = 'A'; | 
|---|
| 81 | } else if ((mode & S_ARCH1) != 0) { | 
|---|
| 82 | *p++ = 'a'; | 
|---|
| 83 | } else { | 
|---|
| 84 | #endif | 
|---|
| 85 | *p++ = '-'; | 
|---|
| 86 | #ifdef S_ARCH2 | 
|---|
| 87 | } | 
|---|
| 88 | #endif | 
|---|
| 89 | break; | 
|---|
| 90 | #ifdef S_IFLNK | 
|---|
| 91 | case S_IFLNK:                   /* symbolic link */ | 
|---|
| 92 | *p++ = 'l'; | 
|---|
| 93 | break; | 
|---|
| 94 | #endif | 
|---|
| 95 | #ifdef S_IFSOCK | 
|---|
| 96 | case S_IFSOCK:                  /* socket */ | 
|---|
| 97 | *p++ = 's'; | 
|---|
| 98 | break; | 
|---|
| 99 | #endif | 
|---|
| 100 | #ifdef S_IFIFO | 
|---|
| 101 | case S_IFIFO:                   /* fifo */ | 
|---|
| 102 | *p++ = 'p'; | 
|---|
| 103 | break; | 
|---|
| 104 | #endif | 
|---|
| 105 | #ifdef S_IFWHT | 
|---|
| 106 | case S_IFWHT:                   /* whiteout */ | 
|---|
| 107 | *p++ = 'w'; | 
|---|
| 108 | break; | 
|---|
| 109 | #endif | 
|---|
| 110 | #ifdef S_IFDOOR | 
|---|
| 111 | case S_IFDOOR:                  /* door */ | 
|---|
| 112 | *p++ = 'D'; | 
|---|
| 113 | break; | 
|---|
| 114 | #endif | 
|---|
| 115 | default:                        /* unknown */ | 
|---|
| 116 | *p++ = '?'; | 
|---|
| 117 | break; | 
|---|
| 118 | } | 
|---|
| 119 | /* usr */ | 
|---|
| 120 | if (mode & S_IRUSR) | 
|---|
| 121 | *p++ = 'r'; | 
|---|
| 122 | else | 
|---|
| 123 | *p++ = '-'; | 
|---|
| 124 | if (mode & S_IWUSR) | 
|---|
| 125 | *p++ = 'w'; | 
|---|
| 126 | else | 
|---|
| 127 | *p++ = '-'; | 
|---|
| 128 | switch (mode & (S_IXUSR | S_ISUID)) { | 
|---|
| 129 | case 0: | 
|---|
| 130 | *p++ = '-'; | 
|---|
| 131 | break; | 
|---|
| 132 | case S_IXUSR: | 
|---|
| 133 | *p++ = 'x'; | 
|---|
| 134 | break; | 
|---|
| 135 | case S_ISUID: | 
|---|
| 136 | *p++ = 'S'; | 
|---|
| 137 | break; | 
|---|
| 138 | case S_IXUSR | S_ISUID: | 
|---|
| 139 | *p++ = 's'; | 
|---|
| 140 | break; | 
|---|
| 141 | } | 
|---|
| 142 | /* group */ | 
|---|
| 143 | if (mode & S_IRGRP) | 
|---|
| 144 | *p++ = 'r'; | 
|---|
| 145 | else | 
|---|
| 146 | *p++ = '-'; | 
|---|
| 147 | if (mode & S_IWGRP) | 
|---|
| 148 | *p++ = 'w'; | 
|---|
| 149 | else | 
|---|
| 150 | *p++ = '-'; | 
|---|
| 151 | switch (mode & (S_IXGRP | S_ISGID)) { | 
|---|
| 152 | case 0: | 
|---|
| 153 | *p++ = '-'; | 
|---|
| 154 | break; | 
|---|
| 155 | case S_IXGRP: | 
|---|
| 156 | *p++ = 'x'; | 
|---|
| 157 | break; | 
|---|
| 158 | case S_ISGID: | 
|---|
| 159 | *p++ = 'S'; | 
|---|
| 160 | break; | 
|---|
| 161 | case S_IXGRP | S_ISGID: | 
|---|
| 162 | *p++ = 's'; | 
|---|
| 163 | break; | 
|---|
| 164 | } | 
|---|
| 165 | /* other */ | 
|---|
| 166 | if (mode & S_IROTH) | 
|---|
| 167 | *p++ = 'r'; | 
|---|
| 168 | else | 
|---|
| 169 | *p++ = '-'; | 
|---|
| 170 | if (mode & S_IWOTH) | 
|---|
| 171 | *p++ = 'w'; | 
|---|
| 172 | else | 
|---|
| 173 | *p++ = '-'; | 
|---|
| 174 | #ifdef S_ISVTX | 
|---|
| 175 | switch (mode & (S_IXOTH | S_ISVTX)) { | 
|---|
| 176 | #else | 
|---|
| 177 | switch (mode & (S_IXOTH)) { | 
|---|
| 178 | #endif | 
|---|
| 179 | case 0: | 
|---|
| 180 | *p++ = '-'; | 
|---|
| 181 | break; | 
|---|
| 182 | case S_IXOTH: | 
|---|
| 183 | *p++ = 'x'; | 
|---|
| 184 | break; | 
|---|
| 185 | #ifdef S_ISVTX | 
|---|
| 186 | case S_ISVTX: | 
|---|
| 187 | *p++ = 'T'; | 
|---|
| 188 | break; | 
|---|
| 189 | case S_IXOTH | S_ISVTX: | 
|---|
| 190 | *p++ = 't'; | 
|---|
| 191 | break; | 
|---|
| 192 | #endif | 
|---|
| 193 | } | 
|---|
| 194 | *p++ = ' ';             /* will be a '+' if ACL's implemented */ | 
|---|
| 195 | *p = '\0'; | 
|---|
| 196 | } | 
|---|