Debian Codegraph @

src/windows/getmode.cc

raw
1 #include <sys/stat.h>
2 #include <errno.h>
3 #include <string>
4
5 #include <R.h>
6 #include <Rinternals.h> /* for Rf_error */
7
8 /* code adapted from https://cgit.freedesktop.org/libbsd/tree/src/setmode.c */
9 unsigned short getmode__(const char* mode_str, unsigned short mode) {
10 const char* p = mode_str;
11 char* ep;
12 char op;
13 mode_t perm, who, out;
14 long lval;
15
16 p = mode_str;
17 perm = who = out = 0;
18
19 /*
20 * If an absolute number, get it and return; disallow non-octal digits
21 * or illegal bits.
22 */
23 if (isdigit((unsigned char)*p)) {
24 errno = 0;
25 lval = strtol(p, &ep, 8);
26 if (*ep) {
27 Rf_error("Invalid mode '%s'", mode_str);
28 }
29 if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) {
30 Rf_error("Invalid mode '%s'", mode_str);
31 }
32 perm = (mode_t)lval;
33 who = S_IRWXU;
34 op = '=';
35 goto apply;
36 }
37 /* First, find out which bits might be modified. */
38 for (;; ++p) {
39 switch (*p) {
40 case 'a':
41 case 'u':
42 who |= S_IRWXU;
43 break;
44 default:
45 goto getop;
46 }
47 }
48
49 getop:
50 if ((op = *p++) != '+' && op != '-' && op != '=') {
51 errno = EINVAL;
52 Rf_error("Invalid mode '%s'", mode_str);
53 }
54 for (perm = 0;; ++p) {
55 switch (*p) {
56 case 'r':
57 perm |= S_IRUSR;
58 break;
59 case 'w':
60 perm |= S_IWUSR;
61 break;
62 case 'x':
63 perm |= S_IXUSR;
64 break;
65 default:
66 goto apply;
67 }
68 }
69 apply:
70 switch (op) {
71 case '=':
72 out = perm;
73 break;
74 case '+':
75 out = mode | perm;
76 break;
77 case '-':
78 out = mode & ~perm;
79 break;
80 default:
81 Rf_error("Invalid mode '%s'", mode_str);
82 }
83 out &= who;
84 errno = 0;
85 return out;
86 }
87
88 #if defined(LIBC_SCCS) && !defined(lint)
89 static char sccsid[] = "@(#)strmode.c 8.3 (Berkeley) 8/15/94";
90 #endif /* LIBC_SCCS and not lint */
91 #include <sys/cdefs.h>
92
93 #include <string.h>
94 #include <sys/stat.h>
95 #include <sys/types.h>
96
97 /* Adapted from
98 * https://cgit.freedesktop.org/libbsd/plain/src/strmode.c?id=8dbfb3529b0253ba8067042dabaebe3ad89cb039
99 */
100 /*-
101 * Copyright (c) 1990, 1993
102 * The Regents of the University of California. All rights reserved.
103 *
104 * Redistribution and use in source and binary forms, with or without
105 * modification, are permitted provided that the following conditions
106 * are met:
107 * 1. Redistributions of source code must retain the above copyright
108 * notice, this list of conditions and the following disclaimer.
109 * 2. Redistributions in binary form must reproduce the above copyright
110 * notice, this list of conditions and the following disclaimer in the
111 * documentation and/or other materials provided with the distribution.
112 * 4. Neither the name of the University nor the names of its contributors
113 * may be used to endorse or promote products derived from this software
114 * without specific prior written permission.
115 *
116 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
117 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
118 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
119 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
120 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
121 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
122 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
123 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
124 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
125 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
126 * SUCH DAMAGE.
127 */
128 void strmode(mode_t mode, char* p) {
129 /* print type */
130 switch (mode & S_IFMT) {
131 case S_IFDIR: /* directory */
132 *p++ = 'd';
133 break;
134 case S_IFCHR: /* character special */
135 *p++ = 'c';
136 break;
137 case S_IFBLK: /* block special */
138 *p++ = 'b';
139 break;
140 case S_IFREG: /* regular */
141 *p++ = '-';
142 break;
143 default: /* unknown */
144 *p++ = '?';
145 break;
146 }
147 /* usr */
148 if (mode & S_IRUSR)
149 *p++ = 'r';
150 else
151 *p++ = '-';
152 if (mode & S_IWUSR)
153 *p++ = 'w';
154 else
155 *p++ = '-';
156 switch (mode & (S_IXUSR)) {
157 case 0:
158 *p++ = '-';
159 break;
160 case S_IXUSR:
161 *p++ = 'x';
162 break;
163 }
164 *p++ = ' '; /* will be a '+' if ACL's implemented */
165 *p = '\0';
166 }
167
168 std::string strmode__(mode_t mode) {
169 char out[5];
170 strmode(mode, out);
171 out[4] = '\0';
172
173 // The first character is the file type, so we do not return it.
174 return out + 1;
175 }
176
177 #define WIN_S_IFLNK 0120000
178
179 std::string file_code__(const std::string& path, unsigned short mode) {
180 switch (mode & S_IFMT) {
181 case S_IFDIR:
182 return "di";
183 case WIN_S_IFLNK:
184 return "ln";
185 case S_IFIFO:
186 return "pi";
187 case S_IFBLK:
188 return "db";
189 case S_IFCHR:
190 return "cd";
191 default:;
192 }
193 if (mode & S_IXUSR) {
194 return "ex";
195 }
196 return "";
197 }