1*f14fb602SLionel Sambuc /* $NetBSD: strmode.c,v 1.19 2012/06/25 22:32:46 abs Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras * Copyright (c) 1990, 1993
52fe8fb19SBen Gras * The Regents of the University of California. All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras * are met:
102fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras * 3. Neither the name of the University nor the names of its contributors
162fe8fb19SBen Gras * may be used to endorse or promote products derived from this software
172fe8fb19SBen Gras * without specific prior written permission.
182fe8fb19SBen Gras *
192fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
202fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
232fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292fe8fb19SBen Gras * SUCH DAMAGE.
302fe8fb19SBen Gras */
312fe8fb19SBen Gras
322fe8fb19SBen Gras #if HAVE_NBTOOL_CONFIG_H
332fe8fb19SBen Gras #include "nbtool_config.h"
342fe8fb19SBen Gras #endif
352fe8fb19SBen Gras
362fe8fb19SBen Gras #include <sys/cdefs.h>
372fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
382fe8fb19SBen Gras #if 0
392fe8fb19SBen Gras static char sccsid[] = "@(#)strmode.c 8.3 (Berkeley) 8/15/94";
402fe8fb19SBen Gras #else
41*f14fb602SLionel Sambuc __RCSID("$NetBSD: strmode.c,v 1.19 2012/06/25 22:32:46 abs Exp $");
422fe8fb19SBen Gras #endif
432fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
442fe8fb19SBen Gras
452fe8fb19SBen Gras #include "namespace.h"
462fe8fb19SBen Gras #include <sys/types.h>
472fe8fb19SBen Gras #include <sys/stat.h>
482fe8fb19SBen Gras
492fe8fb19SBen Gras #include <assert.h>
502fe8fb19SBen Gras #include <unistd.h>
512fe8fb19SBen Gras
522fe8fb19SBen Gras #if !HAVE_STRMODE
532fe8fb19SBen Gras void
strmode(mode_t mode,char * p)542fe8fb19SBen Gras strmode(mode_t mode, char *p)
552fe8fb19SBen Gras {
562fe8fb19SBen Gras
572fe8fb19SBen Gras _DIAGASSERT(p != NULL);
582fe8fb19SBen Gras
592fe8fb19SBen Gras /* print type */
602fe8fb19SBen Gras switch (mode & S_IFMT) {
612fe8fb19SBen Gras case S_IFDIR: /* directory */
622fe8fb19SBen Gras *p++ = 'd';
632fe8fb19SBen Gras break;
642fe8fb19SBen Gras case S_IFCHR: /* character special */
652fe8fb19SBen Gras *p++ = 'c';
662fe8fb19SBen Gras break;
672fe8fb19SBen Gras case S_IFBLK: /* block special */
682fe8fb19SBen Gras *p++ = 'b';
692fe8fb19SBen Gras break;
702fe8fb19SBen Gras case S_IFREG: /* regular */
712fe8fb19SBen Gras #ifdef S_ARCH2
722fe8fb19SBen Gras if ((mode & S_ARCH2) != 0) {
732fe8fb19SBen Gras *p++ = 'A';
742fe8fb19SBen Gras } else if ((mode & S_ARCH1) != 0) {
752fe8fb19SBen Gras *p++ = 'a';
762fe8fb19SBen Gras } else {
772fe8fb19SBen Gras #endif
782fe8fb19SBen Gras *p++ = '-';
792fe8fb19SBen Gras #ifdef S_ARCH2
802fe8fb19SBen Gras }
812fe8fb19SBen Gras #endif
822fe8fb19SBen Gras break;
832fe8fb19SBen Gras case S_IFLNK: /* symbolic link */
842fe8fb19SBen Gras *p++ = 'l';
852fe8fb19SBen Gras break;
862fe8fb19SBen Gras #ifdef S_IFSOCK
872fe8fb19SBen Gras case S_IFSOCK: /* socket */
882fe8fb19SBen Gras *p++ = 's';
892fe8fb19SBen Gras break;
902fe8fb19SBen Gras #endif
912fe8fb19SBen Gras #ifdef S_IFIFO
922fe8fb19SBen Gras case S_IFIFO: /* fifo */
932fe8fb19SBen Gras *p++ = 'p';
942fe8fb19SBen Gras break;
952fe8fb19SBen Gras #endif
962fe8fb19SBen Gras #ifdef S_IFWHT
972fe8fb19SBen Gras case S_IFWHT: /* whiteout */
982fe8fb19SBen Gras *p++ = 'w';
992fe8fb19SBen Gras break;
1002fe8fb19SBen Gras #endif
1012fe8fb19SBen Gras #ifdef S_IFDOOR
1022fe8fb19SBen Gras case S_IFDOOR: /* door */
1032fe8fb19SBen Gras *p++ = 'D';
1042fe8fb19SBen Gras break;
1052fe8fb19SBen Gras #endif
1062fe8fb19SBen Gras default: /* unknown */
1072fe8fb19SBen Gras *p++ = '?';
1082fe8fb19SBen Gras break;
1092fe8fb19SBen Gras }
1102fe8fb19SBen Gras /* usr */
1112fe8fb19SBen Gras if (mode & S_IRUSR)
1122fe8fb19SBen Gras *p++ = 'r';
1132fe8fb19SBen Gras else
1142fe8fb19SBen Gras *p++ = '-';
1152fe8fb19SBen Gras if (mode & S_IWUSR)
1162fe8fb19SBen Gras *p++ = 'w';
1172fe8fb19SBen Gras else
1182fe8fb19SBen Gras *p++ = '-';
1192fe8fb19SBen Gras switch (mode & (S_IXUSR | S_ISUID)) {
1202fe8fb19SBen Gras case 0:
1212fe8fb19SBen Gras *p++ = '-';
1222fe8fb19SBen Gras break;
1232fe8fb19SBen Gras case S_IXUSR:
1242fe8fb19SBen Gras *p++ = 'x';
1252fe8fb19SBen Gras break;
1262fe8fb19SBen Gras case S_ISUID:
1272fe8fb19SBen Gras *p++ = 'S';
1282fe8fb19SBen Gras break;
1292fe8fb19SBen Gras case S_IXUSR | S_ISUID:
1302fe8fb19SBen Gras *p++ = 's';
1312fe8fb19SBen Gras break;
1322fe8fb19SBen Gras }
1332fe8fb19SBen Gras /* group */
1342fe8fb19SBen Gras if (mode & S_IRGRP)
1352fe8fb19SBen Gras *p++ = 'r';
1362fe8fb19SBen Gras else
1372fe8fb19SBen Gras *p++ = '-';
1382fe8fb19SBen Gras if (mode & S_IWGRP)
1392fe8fb19SBen Gras *p++ = 'w';
1402fe8fb19SBen Gras else
1412fe8fb19SBen Gras *p++ = '-';
1422fe8fb19SBen Gras switch (mode & (S_IXGRP | S_ISGID)) {
1432fe8fb19SBen Gras case 0:
1442fe8fb19SBen Gras *p++ = '-';
1452fe8fb19SBen Gras break;
1462fe8fb19SBen Gras case S_IXGRP:
1472fe8fb19SBen Gras *p++ = 'x';
1482fe8fb19SBen Gras break;
1492fe8fb19SBen Gras case S_ISGID:
1502fe8fb19SBen Gras *p++ = 'S';
1512fe8fb19SBen Gras break;
1522fe8fb19SBen Gras case S_IXGRP | S_ISGID:
1532fe8fb19SBen Gras *p++ = 's';
1542fe8fb19SBen Gras break;
1552fe8fb19SBen Gras }
1562fe8fb19SBen Gras /* other */
1572fe8fb19SBen Gras if (mode & S_IROTH)
1582fe8fb19SBen Gras *p++ = 'r';
1592fe8fb19SBen Gras else
1602fe8fb19SBen Gras *p++ = '-';
1612fe8fb19SBen Gras if (mode & S_IWOTH)
1622fe8fb19SBen Gras *p++ = 'w';
1632fe8fb19SBen Gras else
1642fe8fb19SBen Gras *p++ = '-';
1652fe8fb19SBen Gras switch (mode & (S_IXOTH | S_ISVTX)) {
1662fe8fb19SBen Gras case 0:
1672fe8fb19SBen Gras *p++ = '-';
1682fe8fb19SBen Gras break;
1692fe8fb19SBen Gras case S_IXOTH:
1702fe8fb19SBen Gras *p++ = 'x';
1712fe8fb19SBen Gras break;
1722fe8fb19SBen Gras case S_ISVTX:
1732fe8fb19SBen Gras *p++ = 'T';
1742fe8fb19SBen Gras break;
1752fe8fb19SBen Gras case S_IXOTH | S_ISVTX:
1762fe8fb19SBen Gras *p++ = 't';
1772fe8fb19SBen Gras break;
1782fe8fb19SBen Gras }
1792fe8fb19SBen Gras *p++ = ' '; /* will be a '+' if ACL's implemented */
1802fe8fb19SBen Gras *p = '\0';
1812fe8fb19SBen Gras }
1822fe8fb19SBen Gras #endif /* !HAVE_STRMODE */
183