blob: f0135232817977eb931c342448761ece03ac944a [file] [log] [blame]
Ben Lindstrombf555ba2001-01-18 02:04:35 +00001/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
Damien Miller329638e2003-06-03 12:12:50 +100013 * 3. Neither the name of the University nor the names of its contributors
Ben Lindstrombf555ba2001-01-18 02:04:35 +000014 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include "includes.h"
31#ifndef HAVE_STRMODE
32
33#if defined(LIBC_SCCS) && !defined(lint)
Damien Miller329638e2003-06-03 12:12:50 +100034static char *rcsid = "$OpenBSD: strmode.c,v 1.4 2003/06/02 20:18:38 millert Exp $";
Ben Lindstrombf555ba2001-01-18 02:04:35 +000035#endif /* LIBC_SCCS and not lint */
36
37#include <sys/types.h>
38#include <sys/stat.h>
39#include <string.h>
40
41void
Ben Lindstromb30768f2001-06-09 02:22:16 +000042strmode(register mode_t mode, register char *p)
Ben Lindstrombf555ba2001-01-18 02:04:35 +000043{
44 /* print type */
45 switch (mode & S_IFMT) {
46 case S_IFDIR: /* directory */
47 *p++ = 'd';
48 break;
49 case S_IFCHR: /* character special */
50 *p++ = 'c';
51 break;
52 case S_IFBLK: /* block special */
53 *p++ = 'b';
54 break;
55 case S_IFREG: /* regular */
56 *p++ = '-';
57 break;
58 case S_IFLNK: /* symbolic link */
59 *p++ = 'l';
60 break;
Ben Lindstrom2396b302001-01-23 16:54:29 +000061#ifdef S_IFSOCK
Ben Lindstrombf555ba2001-01-18 02:04:35 +000062 case S_IFSOCK: /* socket */
63 *p++ = 's';
64 break;
Ben Lindstrom2396b302001-01-23 16:54:29 +000065#endif
Ben Lindstrombf555ba2001-01-18 02:04:35 +000066#ifdef S_IFIFO
67 case S_IFIFO: /* fifo */
68 *p++ = 'p';
69 break;
70#endif
71#ifdef S_IFWHT
72 case S_IFWHT: /* whiteout */
73 *p++ = 'w';
74 break;
75#endif
76 default: /* unknown */
77 *p++ = '?';
78 break;
79 }
80 /* usr */
81 if (mode & S_IRUSR)
82 *p++ = 'r';
83 else
84 *p++ = '-';
85 if (mode & S_IWUSR)
86 *p++ = 'w';
87 else
88 *p++ = '-';
89 switch (mode & (S_IXUSR | S_ISUID)) {
90 case 0:
91 *p++ = '-';
92 break;
93 case S_IXUSR:
94 *p++ = 'x';
95 break;
96 case S_ISUID:
97 *p++ = 'S';
98 break;
99 case S_IXUSR | S_ISUID:
100 *p++ = 's';
101 break;
102 }
103 /* group */
104 if (mode & S_IRGRP)
105 *p++ = 'r';
106 else
107 *p++ = '-';
108 if (mode & S_IWGRP)
109 *p++ = 'w';
110 else
111 *p++ = '-';
112 switch (mode & (S_IXGRP | S_ISGID)) {
113 case 0:
114 *p++ = '-';
115 break;
116 case S_IXGRP:
117 *p++ = 'x';
118 break;
119 case S_ISGID:
120 *p++ = 'S';
121 break;
122 case S_IXGRP | S_ISGID:
123 *p++ = 's';
124 break;
125 }
126 /* other */
127 if (mode & S_IROTH)
128 *p++ = 'r';
129 else
130 *p++ = '-';
131 if (mode & S_IWOTH)
132 *p++ = 'w';
133 else
134 *p++ = '-';
135 switch (mode & (S_IXOTH | S_ISVTX)) {
136 case 0:
137 *p++ = '-';
138 break;
139 case S_IXOTH:
140 *p++ = 'x';
141 break;
142 case S_ISVTX:
143 *p++ = 'T';
144 break;
145 case S_IXOTH | S_ISVTX:
146 *p++ = 't';
147 break;
148 }
149 *p++ = ' '; /* will be a '+' if ACL's implemented */
150 *p = '\0';
151}
152#endif