Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/core/coreutils/src/compat/strmode.c
1067 views
1
/*-
2
* SPDX-License-Identifier: BSD-3-Clause
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. Neither the name of the University nor the names of its contributors
16
* may be used to endorse or promote products derived from this software
17
* without specific prior written permission.
18
*
19
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29
* SUCH DAMAGE.
30
*/
31
32
#include <sys/types.h>
33
#include <sys/stat.h>
34
#include <string.h>
35
36
void strmode(/* mode_t */ int mode, char *p) {
37
/* print type */
38
switch (mode & S_IFMT) {
39
case S_IFDIR: /* directory */
40
*p++ = 'd';
41
break;
42
case S_IFCHR: /* character special */
43
*p++ = 'c';
44
break;
45
case S_IFBLK: /* block special */
46
*p++ = 'b';
47
break;
48
case S_IFREG: /* regular */
49
*p++ = '-';
50
break;
51
case S_IFLNK: /* symbolic link */
52
*p++ = 'l';
53
break;
54
case S_IFSOCK: /* socket */
55
*p++ = 's';
56
break;
57
#ifdef S_IFWHT
58
case S_IFWHT: /* whiteout */
59
*p++ = 'w';
60
break;
61
#endif
62
default: /* unknown */
63
*p++ = '?';
64
break;
65
}
66
/* usr */
67
if (mode & S_IRUSR)
68
*p++ = 'r';
69
else
70
*p++ = '-';
71
if (mode & S_IWUSR)
72
*p++ = 'w';
73
else
74
*p++ = '-';
75
switch (mode & (S_IXUSR | S_ISUID)) {
76
case 0:
77
*p++ = '-';
78
break;
79
case S_IXUSR:
80
*p++ = 'x';
81
break;
82
case S_ISUID:
83
*p++ = 'S';
84
break;
85
case S_IXUSR | S_ISUID:
86
*p++ = 's';
87
break;
88
}
89
/* group */
90
if (mode & S_IRGRP)
91
*p++ = 'r';
92
else
93
*p++ = '-';
94
if (mode & S_IWGRP)
95
*p++ = 'w';
96
else
97
*p++ = '-';
98
switch (mode & (S_IXGRP | S_ISGID)) {
99
case 0:
100
*p++ = '-';
101
break;
102
case S_IXGRP:
103
*p++ = 'x';
104
break;
105
case S_ISGID:
106
*p++ = 'S';
107
break;
108
case S_IXGRP | S_ISGID:
109
*p++ = 's';
110
break;
111
}
112
/* other */
113
if (mode & S_IROTH)
114
*p++ = 'r';
115
else
116
*p++ = '-';
117
if (mode & S_IWOTH)
118
*p++ = 'w';
119
else
120
*p++ = '-';
121
switch (mode & (S_IXOTH | S_ISVTX)) {
122
case 0:
123
*p++ = '-';
124
break;
125
case S_IXOTH:
126
*p++ = 'x';
127
break;
128
case S_ISVTX:
129
*p++ = 'T';
130
break;
131
case S_IXOTH | S_ISVTX:
132
*p++ = 't';
133
break;
134
}
135
*p++ = ' ';
136
*p = '\0';
137
}
138
139