Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:19

0001 /*-
0002  * Copyright (c) 1990, 1993
0003  *  The Regents of the University of California.  All rights reserved.
0004  *
0005  * Redistribution and use in source and binary forms, with or without
0006  * modification, are permitted provided that the following conditions
0007  * are met:
0008  * 1. Redistributions of source code must retain the above copyright
0009  *    notice, this list of conditions and the following disclaimer.
0010  * 2. Redistributions in binary form must reproduce the above copyright
0011  *    notice, this list of conditions and the following disclaimer in the
0012  *    documentation and/or other materials provided with the distribution.
0013  * 4. Neither the name of the University nor the names of its contributors
0014  *    may be used to endorse or promote products derived from this software
0015  *    without specific prior written permission.
0016  *
0017  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
0018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
0021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
0023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
0026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0027  * SUCH DAMAGE.
0028  */
0029 
0030 #ifdef HAVE_CONFIG_H
0031 #include "config.h"
0032 #endif
0033 
0034 #if defined(LIBC_SCCS) && !defined(lint)
0035 static char sccsid[] = "@(#)strmode.c   8.3 (Berkeley) 8/15/94";
0036 #endif /* LIBC_SCCS and not lint */
0037 #include <sys/cdefs.h>
0038 __FBSDID("$FreeBSD: src/lib/libc/string/strmode.c,v 1.5 2007/01/09 00:28:12 imp Exp $");
0039 
0040 #include <sys/types.h>
0041 #include <sys/stat.h>
0042 #include <string.h>
0043 
0044 #include "internal.h"
0045 
0046 void
0047 strmode(
0048     mode_t mode,
0049     char *p)
0050 {
0051      /* print type */
0052     switch (mode & S_IFMT) {
0053     case S_IFDIR:           /* directory */
0054         *p++ = 'd';
0055         break;
0056     case S_IFCHR:           /* character special */
0057         *p++ = 'c';
0058         break;
0059     case S_IFBLK:           /* block special */
0060         *p++ = 'b';
0061         break;
0062     case S_IFREG:           /* regular */
0063         *p++ = '-';
0064         break;
0065     case S_IFLNK:           /* symbolic link */
0066         *p++ = 'l';
0067         break;
0068     case S_IFSOCK:          /* socket */
0069         *p++ = 's';
0070         break;
0071 #ifdef S_IFIFO
0072     case S_IFIFO:           /* fifo */
0073         *p++ = 'p';
0074         break;
0075 #endif
0076 #ifdef S_IFWHT
0077     case S_IFWHT:           /* whiteout */
0078         *p++ = 'w';
0079         break;
0080 #endif
0081     default:            /* unknown */
0082         *p++ = '?';
0083         break;
0084     }
0085     /* usr */
0086     if (mode & S_IRUSR)
0087         *p++ = 'r';
0088     else
0089         *p++ = '-';
0090     if (mode & S_IWUSR)
0091         *p++ = 'w';
0092     else
0093         *p++ = '-';
0094     switch (mode & (S_IXUSR | S_ISUID)) {
0095     case 0:
0096         *p++ = '-';
0097         break;
0098     case S_IXUSR:
0099         *p++ = 'x';
0100         break;
0101     case S_ISUID:
0102         *p++ = 'S';
0103         break;
0104     case S_IXUSR | S_ISUID:
0105         *p++ = 's';
0106         break;
0107     }
0108     /* group */
0109     if (mode & S_IRGRP)
0110         *p++ = 'r';
0111     else
0112         *p++ = '-';
0113     if (mode & S_IWGRP)
0114         *p++ = 'w';
0115     else
0116         *p++ = '-';
0117     switch (mode & (S_IXGRP | S_ISGID)) {
0118     case 0:
0119         *p++ = '-';
0120         break;
0121     case S_IXGRP:
0122         *p++ = 'x';
0123         break;
0124     case S_ISGID:
0125         *p++ = 'S';
0126         break;
0127     case S_IXGRP | S_ISGID:
0128         *p++ = 's';
0129         break;
0130     }
0131     /* other */
0132     if (mode & S_IROTH)
0133         *p++ = 'r';
0134     else
0135         *p++ = '-';
0136     if (mode & S_IWOTH)
0137         *p++ = 'w';
0138     else
0139         *p++ = '-';
0140     switch (mode & (S_IXOTH | S_ISVTX)) {
0141     case 0:
0142         *p++ = '-';
0143         break;
0144     case S_IXOTH:
0145         *p++ = 'x';
0146         break;
0147     case S_ISVTX:
0148         *p++ = 'T';
0149         break;
0150     case S_IXOTH | S_ISVTX:
0151         *p++ = 't';
0152         break;
0153     }
0154     *p++ = ' ';     /* will be a '+' if ACL's implemented */
0155     *p = '\0';
0156 }