1*9e66e6d7Sabs /* $NetBSD: strmode.c,v 1.19 2012/06/25 22:32:46 abs Exp $ */
23d2267c8Schristos
361f28255Scgd /*-
45f8a6c06Sperry * Copyright (c) 1990, 1993
55f8a6c06Sperry * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
15eb7c1594Sagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
32b70c743bSapb #if HAVE_NBTOOL_CONFIG_H
33b70c743bSapb #include "nbtool_config.h"
34b70c743bSapb #endif
35b70c743bSapb
363d2267c8Schristos #include <sys/cdefs.h>
3761f28255Scgd #if defined(LIBC_SCCS) && !defined(lint)
383d2267c8Schristos #if 0
395f8a6c06Sperry static char sccsid[] = "@(#)strmode.c 8.3 (Berkeley) 8/15/94";
403d2267c8Schristos #else
41*9e66e6d7Sabs __RCSID("$NetBSD: strmode.c,v 1.19 2012/06/25 22:32:46 abs Exp $");
423d2267c8Schristos #endif
4361f28255Scgd #endif /* LIBC_SCCS and not lint */
4461f28255Scgd
459fbd8888Stv #include "namespace.h"
4661f28255Scgd #include <sys/types.h>
4761f28255Scgd #include <sys/stat.h>
48b48252f3Slukem
49b48252f3Slukem #include <assert.h>
5016331346Schristos #include <unistd.h>
5161f28255Scgd
5209256aacSelad #if !HAVE_STRMODE
5361f28255Scgd void
strmode(mode_t mode,char * p)54*9e66e6d7Sabs strmode(mode_t mode, char *p)
5561f28255Scgd {
56b48252f3Slukem
57b48252f3Slukem _DIAGASSERT(p != NULL);
58b48252f3Slukem
5961f28255Scgd /* print type */
6061f28255Scgd switch (mode & S_IFMT) {
6161f28255Scgd case S_IFDIR: /* directory */
6261f28255Scgd *p++ = 'd';
6361f28255Scgd break;
6461f28255Scgd case S_IFCHR: /* character special */
6561f28255Scgd *p++ = 'c';
6661f28255Scgd break;
6761f28255Scgd case S_IFBLK: /* block special */
6861f28255Scgd *p++ = 'b';
6961f28255Scgd break;
7061f28255Scgd case S_IFREG: /* regular */
710217e4fdSwrstuden #ifdef S_ARCH2
720217e4fdSwrstuden if ((mode & S_ARCH2) != 0) {
730217e4fdSwrstuden *p++ = 'A';
740217e4fdSwrstuden } else if ((mode & S_ARCH1) != 0) {
750217e4fdSwrstuden *p++ = 'a';
760217e4fdSwrstuden } else {
770217e4fdSwrstuden #endif
7861f28255Scgd *p++ = '-';
790217e4fdSwrstuden #ifdef S_ARCH2
800217e4fdSwrstuden }
810217e4fdSwrstuden #endif
8261f28255Scgd break;
8361f28255Scgd case S_IFLNK: /* symbolic link */
8461f28255Scgd *p++ = 'l';
8561f28255Scgd break;
86b2f78261Sjmc #ifdef S_IFSOCK
8761f28255Scgd case S_IFSOCK: /* socket */
8861f28255Scgd *p++ = 's';
8961f28255Scgd break;
90b2f78261Sjmc #endif
9161f28255Scgd #ifdef S_IFIFO
9261f28255Scgd case S_IFIFO: /* fifo */
9361f28255Scgd *p++ = 'p';
9461f28255Scgd break;
9561f28255Scgd #endif
9697a71285Smatthias #ifdef S_IFWHT
9797a71285Smatthias case S_IFWHT: /* whiteout */
9897a71285Smatthias *p++ = 'w';
9997a71285Smatthias break;
10097a71285Smatthias #endif
101d5fb7818Satatat #ifdef S_IFDOOR
102d5fb7818Satatat case S_IFDOOR: /* door */
103d5fb7818Satatat *p++ = 'D';
104d5fb7818Satatat break;
105d5fb7818Satatat #endif
10661f28255Scgd default: /* unknown */
10761f28255Scgd *p++ = '?';
10861f28255Scgd break;
10961f28255Scgd }
11061f28255Scgd /* usr */
11161f28255Scgd if (mode & S_IRUSR)
11261f28255Scgd *p++ = 'r';
11361f28255Scgd else
11461f28255Scgd *p++ = '-';
11561f28255Scgd if (mode & S_IWUSR)
11661f28255Scgd *p++ = 'w';
11761f28255Scgd else
11861f28255Scgd *p++ = '-';
11961f28255Scgd switch (mode & (S_IXUSR | S_ISUID)) {
12061f28255Scgd case 0:
12161f28255Scgd *p++ = '-';
12261f28255Scgd break;
12361f28255Scgd case S_IXUSR:
12461f28255Scgd *p++ = 'x';
12561f28255Scgd break;
12661f28255Scgd case S_ISUID:
12761f28255Scgd *p++ = 'S';
12861f28255Scgd break;
12961f28255Scgd case S_IXUSR | S_ISUID:
13061f28255Scgd *p++ = 's';
13161f28255Scgd break;
13261f28255Scgd }
13361f28255Scgd /* group */
13461f28255Scgd if (mode & S_IRGRP)
13561f28255Scgd *p++ = 'r';
13661f28255Scgd else
13761f28255Scgd *p++ = '-';
13861f28255Scgd if (mode & S_IWGRP)
13961f28255Scgd *p++ = 'w';
14061f28255Scgd else
14161f28255Scgd *p++ = '-';
14261f28255Scgd switch (mode & (S_IXGRP | S_ISGID)) {
14361f28255Scgd case 0:
14461f28255Scgd *p++ = '-';
14561f28255Scgd break;
14661f28255Scgd case S_IXGRP:
14761f28255Scgd *p++ = 'x';
14861f28255Scgd break;
14961f28255Scgd case S_ISGID:
15061f28255Scgd *p++ = 'S';
15161f28255Scgd break;
15261f28255Scgd case S_IXGRP | S_ISGID:
15361f28255Scgd *p++ = 's';
15461f28255Scgd break;
15561f28255Scgd }
15661f28255Scgd /* other */
15761f28255Scgd if (mode & S_IROTH)
15861f28255Scgd *p++ = 'r';
15961f28255Scgd else
16061f28255Scgd *p++ = '-';
16161f28255Scgd if (mode & S_IWOTH)
16261f28255Scgd *p++ = 'w';
16361f28255Scgd else
16461f28255Scgd *p++ = '-';
16561f28255Scgd switch (mode & (S_IXOTH | S_ISVTX)) {
16661f28255Scgd case 0:
16761f28255Scgd *p++ = '-';
16861f28255Scgd break;
16961f28255Scgd case S_IXOTH:
17061f28255Scgd *p++ = 'x';
17161f28255Scgd break;
17261f28255Scgd case S_ISVTX:
17361f28255Scgd *p++ = 'T';
17461f28255Scgd break;
17561f28255Scgd case S_IXOTH | S_ISVTX:
17661f28255Scgd *p++ = 't';
17761f28255Scgd break;
17861f28255Scgd }
17961f28255Scgd *p++ = ' '; /* will be a '+' if ACL's implemented */
18061f28255Scgd *p = '\0';
18161f28255Scgd }
18209256aacSelad #endif /* !HAVE_STRMODE */
183