Home | History | Annotate | Line # | Download | only in string
strmode.c revision 1.9
      1 /*	$NetBSD: strmode.c,v 1.9 1999/08/03 21:43:13 wrstuden 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #if defined(LIBC_SCCS) && !defined(lint)
     38 #if 0
     39 static char sccsid[] = "@(#)strmode.c	8.3 (Berkeley) 8/15/94";
     40 #else
     41 __RCSID("$NetBSD: strmode.c,v 1.9 1999/08/03 21:43:13 wrstuden Exp $");
     42 #endif
     43 #endif /* LIBC_SCCS and not lint */
     44 
     45 #include <sys/types.h>
     46 #include <sys/stat.h>
     47 #include <unistd.h>
     48 
     49 void
     50 strmode(mode, p)
     51 	mode_t mode;
     52 	char *p;
     53 {
     54 	 /* print type */
     55 	switch (mode & S_IFMT) {
     56 	case S_IFDIR:			/* directory */
     57 		*p++ = 'd';
     58 		break;
     59 	case S_IFCHR:			/* character special */
     60 		*p++ = 'c';
     61 		break;
     62 	case S_IFBLK:			/* block special */
     63 		*p++ = 'b';
     64 		break;
     65 	case S_IFREG:			/* regular */
     66 #ifdef S_ARCH2
     67 		if ((mode & S_ARCH2) != 0) {
     68 			*p++ = 'A';
     69 		} else if ((mode & S_ARCH1) != 0) {
     70 			*p++ = 'a';
     71 		} else {
     72 #endif
     73 			*p++ = '-';
     74 #ifdef S_ARCH2
     75 		}
     76 #endif
     77 		break;
     78 	case S_IFLNK:			/* symbolic link */
     79 		*p++ = 'l';
     80 		break;
     81 	case S_IFSOCK:			/* socket */
     82 		*p++ = 's';
     83 		break;
     84 #ifdef S_IFIFO
     85 	case S_IFIFO:			/* fifo */
     86 		*p++ = 'p';
     87 		break;
     88 #endif
     89 #ifdef S_IFWHT
     90 	case S_IFWHT:			/* whiteout */
     91 		*p++ = 'w';
     92 		break;
     93 #endif
     94 	default:			/* unknown */
     95 		*p++ = '?';
     96 		break;
     97 	}
     98 	/* usr */
     99 	if (mode & S_IRUSR)
    100 		*p++ = 'r';
    101 	else
    102 		*p++ = '-';
    103 	if (mode & S_IWUSR)
    104 		*p++ = 'w';
    105 	else
    106 		*p++ = '-';
    107 	switch (mode & (S_IXUSR | S_ISUID)) {
    108 	case 0:
    109 		*p++ = '-';
    110 		break;
    111 	case S_IXUSR:
    112 		*p++ = 'x';
    113 		break;
    114 	case S_ISUID:
    115 		*p++ = 'S';
    116 		break;
    117 	case S_IXUSR | S_ISUID:
    118 		*p++ = 's';
    119 		break;
    120 	}
    121 	/* group */
    122 	if (mode & S_IRGRP)
    123 		*p++ = 'r';
    124 	else
    125 		*p++ = '-';
    126 	if (mode & S_IWGRP)
    127 		*p++ = 'w';
    128 	else
    129 		*p++ = '-';
    130 	switch (mode & (S_IXGRP | S_ISGID)) {
    131 	case 0:
    132 		*p++ = '-';
    133 		break;
    134 	case S_IXGRP:
    135 		*p++ = 'x';
    136 		break;
    137 	case S_ISGID:
    138 		*p++ = 'S';
    139 		break;
    140 	case S_IXGRP | S_ISGID:
    141 		*p++ = 's';
    142 		break;
    143 	}
    144 	/* other */
    145 	if (mode & S_IROTH)
    146 		*p++ = 'r';
    147 	else
    148 		*p++ = '-';
    149 	if (mode & S_IWOTH)
    150 		*p++ = 'w';
    151 	else
    152 		*p++ = '-';
    153 	switch (mode & (S_IXOTH | S_ISVTX)) {
    154 	case 0:
    155 		*p++ = '-';
    156 		break;
    157 	case S_IXOTH:
    158 		*p++ = 'x';
    159 		break;
    160 	case S_ISVTX:
    161 		*p++ = 'T';
    162 		break;
    163 	case S_IXOTH | S_ISVTX:
    164 		*p++ = 't';
    165 		break;
    166 	}
    167 	*p++ = ' ';		/* will be a '+' if ACL's implemented */
    168 	*p = '\0';
    169 }
    170