Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* doio.c |
a687059c | 2 | * |
9607fc9c | 3 | * Copyright (c) 1991-1997, Larry Wall |
a687059c | 4 | * |
6e21c824 LW |
5 | * You may distribute under the terms of either the GNU General Public |
6 | * License or the Artistic License, as specified in the README file. | |
a687059c | 7 | * |
a0d0e21e LW |
8 | */ |
9 | ||
10 | /* | |
11 | * "Far below them they saw the white waters pour into a foaming bowl, and | |
12 | * then swirl darkly about a deep oval basin in the rocks, until they found | |
13 | * their way out again through a narrow gate, and flowed away, fuming and | |
14 | * chattering, into calmer and more level reaches." | |
a687059c LW |
15 | */ |
16 | ||
17 | #include "EXTERN.h" | |
18 | #include "perl.h" | |
19 | ||
fe14fcc3 | 20 | #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM) |
c2ab57d4 | 21 | #include <sys/ipc.h> |
fe14fcc3 | 22 | #ifdef HAS_MSG |
c2ab57d4 | 23 | #include <sys/msg.h> |
e5d73d77 | 24 | #endif |
fe14fcc3 | 25 | #ifdef HAS_SEM |
c2ab57d4 | 26 | #include <sys/sem.h> |
e5d73d77 | 27 | #endif |
fe14fcc3 | 28 | #ifdef HAS_SHM |
c2ab57d4 | 29 | #include <sys/shm.h> |
a0d0e21e LW |
30 | # ifndef HAS_SHMAT_PROTOTYPE |
31 | extern Shmat_t shmat _((int, char *, int)); | |
32 | # endif | |
c2ab57d4 | 33 | #endif |
e5d73d77 | 34 | #endif |
c2ab57d4 | 35 | |
663a0e37 | 36 | #ifdef I_UTIME |
3e3baf6d | 37 | # ifdef _MSC_VER |
3fe9a6f1 | 38 | # include <sys/utime.h> |
39 | # else | |
40 | # include <utime.h> | |
41 | # endif | |
663a0e37 | 42 | #endif |
ff8e2863 LW |
43 | #ifdef I_FCNTL |
44 | #include <fcntl.h> | |
45 | #endif | |
fe14fcc3 LW |
46 | #ifdef I_SYS_FILE |
47 | #include <sys/file.h> | |
48 | #endif | |
a687059c | 49 | |
76121258 | 50 | #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX) |
51 | #include <signal.h> | |
52 | #endif | |
53 | ||
54 | /* XXX If this causes problems, set i_unistd=undef in the hint file. */ | |
55 | #ifdef I_UNISTD | |
56 | # include <unistd.h> | |
57 | #endif | |
58 | ||
232e078e AD |
59 | #if defined(HAS_SOCKET) && !defined(VMS) /* VMS handles sockets via vmsish.h */ |
60 | # include <sys/socket.h> | |
61 | # include <netdb.h> | |
62 | # ifndef ENOTSOCK | |
63 | # ifdef I_NET_ERRNO | |
64 | # include <net/errno.h> | |
65 | # endif | |
66 | # endif | |
67 | #endif | |
68 | ||
d574b85e CS |
69 | /* Put this after #includes because <unistd.h> defines _XOPEN_*. */ |
70 | #ifndef Sock_size_t | |
137443ea | 71 | # if _XOPEN_VERSION >= 5 || defined(_XOPEN_SOURCE_EXTENDED) || defined(__GLIBC__) |
d574b85e CS |
72 | # define Sock_size_t Size_t |
73 | # else | |
74 | # define Sock_size_t int | |
75 | # endif | |
76 | #endif | |
77 | ||
a687059c | 78 | bool |
8ac85365 | 79 | do_open(GV *gv, register char *name, I32 len, int as_raw, int rawmode, int rawperm, FILE *supplied_fp) |
a687059c | 80 | { |
a0d0e21e | 81 | register IO *io = GvIOn(gv); |
760ac839 LW |
82 | PerlIO *saveifp = Nullfp; |
83 | PerlIO *saveofp = Nullfp; | |
6e21c824 | 84 | char savetype = ' '; |
c07a80fd | 85 | int writing = 0; |
760ac839 | 86 | PerlIO *fp; |
c07a80fd | 87 | int fd; |
88 | int result; | |
a687059c | 89 | |
a687059c | 90 | forkprocess = 1; /* assume true if no fork */ |
c07a80fd | 91 | |
a0d0e21e | 92 | if (IoIFP(io)) { |
760ac839 | 93 | fd = PerlIO_fileno(IoIFP(io)); |
8990e307 | 94 | if (IoTYPE(io) == '-') |
c2ab57d4 | 95 | result = 0; |
6e21c824 | 96 | else if (fd <= maxsysfd) { |
8990e307 LW |
97 | saveifp = IoIFP(io); |
98 | saveofp = IoOFP(io); | |
99 | savetype = IoTYPE(io); | |
6e21c824 LW |
100 | result = 0; |
101 | } | |
8990e307 LW |
102 | else if (IoTYPE(io) == '|') |
103 | result = my_pclose(IoIFP(io)); | |
104 | else if (IoIFP(io) != IoOFP(io)) { | |
105 | if (IoOFP(io)) { | |
760ac839 LW |
106 | result = PerlIO_close(IoOFP(io)); |
107 | PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */ | |
c2ab57d4 LW |
108 | } |
109 | else | |
760ac839 | 110 | result = PerlIO_close(IoIFP(io)); |
a687059c | 111 | } |
a687059c | 112 | else |
760ac839 | 113 | result = PerlIO_close(IoIFP(io)); |
6e21c824 | 114 | if (result == EOF && fd > maxsysfd) |
760ac839 | 115 | PerlIO_printf(PerlIO_stderr(), "Warning: unable to close filehandle %s properly.\n", |
79072805 | 116 | GvENAME(gv)); |
8990e307 | 117 | IoOFP(io) = IoIFP(io) = Nullfp; |
a687059c | 118 | } |
c07a80fd | 119 | |
120 | if (as_raw) { | |
121 | result = rawmode & 3; | |
122 | IoTYPE(io) = "<>++"[result]; | |
123 | writing = (result > 0); | |
124 | fd = open(name, rawmode, rawperm); | |
125 | if (fd == -1) | |
126 | fp = NULL; | |
127 | else { | |
360e5741 CS |
128 | char *fpmode; |
129 | if (result == 0) | |
130 | fpmode = "r"; | |
131 | #ifdef O_APPEND | |
132 | else if (rawmode & O_APPEND) | |
133 | fpmode = (result == 1) ? "a" : "a+"; | |
134 | #endif | |
135 | else | |
136 | fpmode = (result == 1) ? "w" : "r+"; | |
137 | fp = PerlIO_fdopen(fd, fpmode); | |
c07a80fd | 138 | if (!fp) |
139 | close(fd); | |
140 | } | |
a687059c | 141 | } |
c07a80fd | 142 | else { |
143 | char *myname; | |
144 | char mode[3]; /* stdio file mode ("r\0" or "r+\0") */ | |
145 | int dodup; | |
146 | ||
147 | myname = savepvn(name, len); | |
148 | SAVEFREEPV(myname); | |
149 | name = myname; | |
150 | while (len && isSPACE(name[len-1])) | |
151 | name[--len] = '\0'; | |
152 | ||
153 | mode[0] = mode[1] = mode[2] = '\0'; | |
154 | IoTYPE(io) = *name; | |
155 | if (*name == '+' && len > 1 && name[len-1] != '|') { /* scary */ | |
156 | mode[1] = *name++; | |
157 | --len; | |
158 | writing = 1; | |
a687059c | 159 | } |
c07a80fd | 160 | |
161 | if (*name == '|') { | |
162 | /*SUPPRESS 530*/ | |
163 | for (name++; isSPACE(*name); name++) ; | |
164 | if (strNE(name,"-")) | |
165 | TAINT_ENV(); | |
166 | TAINT_PROPER("piped open"); | |
167 | if (dowarn && name[strlen(name)-1] == '|') | |
168 | warn("Can't do bidirectional pipe"); | |
169 | fp = my_popen(name,"w"); | |
170 | writing = 1; | |
171 | } | |
172 | else if (*name == '>') { | |
173 | TAINT_PROPER("open"); | |
bf38876a | 174 | name++; |
c07a80fd | 175 | if (*name == '>') { |
176 | mode[0] = IoTYPE(io) = 'a'; | |
bf38876a | 177 | name++; |
a0d0e21e | 178 | } |
c07a80fd | 179 | else |
180 | mode[0] = 'w'; | |
181 | writing = 1; | |
182 | ||
183 | if (*name == '&') { | |
184 | duplicity: | |
185 | dodup = 1; | |
186 | name++; | |
187 | if (*name == '=') { | |
188 | dodup = 0; | |
a0d0e21e | 189 | name++; |
c07a80fd | 190 | } |
191 | if (!*name && supplied_fp) | |
192 | fp = supplied_fp; | |
a0d0e21e | 193 | else { |
c07a80fd | 194 | /*SUPPRESS 530*/ |
195 | for (; isSPACE(*name); name++) ; | |
196 | if (isDIGIT(*name)) | |
197 | fd = atoi(name); | |
198 | else { | |
199 | IO* thatio; | |
200 | gv = gv_fetchpv(name,FALSE,SVt_PVIO); | |
201 | thatio = GvIO(gv); | |
202 | if (!thatio) { | |
6e21c824 | 203 | #ifdef EINVAL |
c07a80fd | 204 | SETERRNO(EINVAL,SS$_IVCHAN); |
6e21c824 | 205 | #endif |
c07a80fd | 206 | goto say_false; |
207 | } | |
208 | if (IoIFP(thatio)) { | |
760ac839 | 209 | fd = PerlIO_fileno(IoIFP(thatio)); |
c07a80fd | 210 | if (IoTYPE(thatio) == 's') |
211 | IoTYPE(io) = 's'; | |
212 | } | |
213 | else | |
214 | fd = -1; | |
a0d0e21e | 215 | } |
fec02dd3 | 216 | if (dodup) |
c07a80fd | 217 | fd = dup(fd); |
760ac839 | 218 | if (!(fp = PerlIO_fdopen(fd,mode))) { |
c07a80fd | 219 | if (dodup) |
220 | close(fd); | |
517844ec | 221 | } |
c07a80fd | 222 | } |
bf38876a | 223 | } |
c07a80fd | 224 | else { |
225 | /*SUPPRESS 530*/ | |
226 | for (; isSPACE(*name); name++) ; | |
227 | if (strEQ(name,"-")) { | |
760ac839 | 228 | fp = PerlIO_stdout(); |
c07a80fd | 229 | IoTYPE(io) = '-'; |
230 | } | |
231 | else { | |
760ac839 | 232 | fp = PerlIO_open(name,mode); |
c07a80fd | 233 | } |
bf38876a LW |
234 | } |
235 | } | |
c07a80fd | 236 | else if (*name == '<') { |
237 | /*SUPPRESS 530*/ | |
238 | for (name++; isSPACE(*name); name++) ; | |
bf38876a | 239 | mode[0] = 'r'; |
bf38876a LW |
240 | if (*name == '&') |
241 | goto duplicity; | |
a687059c | 242 | if (strEQ(name,"-")) { |
760ac839 | 243 | fp = PerlIO_stdin(); |
8990e307 | 244 | IoTYPE(io) = '-'; |
a687059c | 245 | } |
bf38876a | 246 | else |
760ac839 | 247 | fp = PerlIO_open(name,mode); |
a687059c LW |
248 | } |
249 | else if (name[len-1] == '|') { | |
a687059c | 250 | name[--len] = '\0'; |
99b89507 | 251 | while (len && isSPACE(name[len-1])) |
a687059c | 252 | name[--len] = '\0'; |
99b89507 LW |
253 | /*SUPPRESS 530*/ |
254 | for (; isSPACE(*name); name++) ; | |
79072805 LW |
255 | if (strNE(name,"-")) |
256 | TAINT_ENV(); | |
257 | TAINT_PROPER("piped open"); | |
258 | fp = my_popen(name,"r"); | |
8990e307 | 259 | IoTYPE(io) = '|'; |
a687059c LW |
260 | } |
261 | else { | |
8990e307 | 262 | IoTYPE(io) = '<'; |
99b89507 LW |
263 | /*SUPPRESS 530*/ |
264 | for (; isSPACE(*name); name++) ; | |
a687059c | 265 | if (strEQ(name,"-")) { |
760ac839 | 266 | fp = PerlIO_stdin(); |
8990e307 | 267 | IoTYPE(io) = '-'; |
a687059c LW |
268 | } |
269 | else | |
760ac839 | 270 | fp = PerlIO_open(name,"r"); |
a687059c LW |
271 | } |
272 | } | |
bee1dbe2 | 273 | if (!fp) { |
8990e307 | 274 | if (dowarn && IoTYPE(io) == '<' && strchr(name, '\n')) |
bee1dbe2 | 275 | warn(warn_nl, "open"); |
6e21c824 | 276 | goto say_false; |
bee1dbe2 | 277 | } |
8990e307 LW |
278 | if (IoTYPE(io) && |
279 | IoTYPE(io) != '|' && IoTYPE(io) != '-') { | |
96827780 | 280 | dTHR; |
760ac839 LW |
281 | if (Fstat(PerlIO_fileno(fp),&statbuf) < 0) { |
282 | (void)PerlIO_close(fp); | |
6e21c824 | 283 | goto say_false; |
a687059c | 284 | } |
1462b684 | 285 | if (S_ISSOCK(statbuf.st_mode)) |
8990e307 | 286 | IoTYPE(io) = 's'; /* in case a socket was passed in to us */ |
99b89507 LW |
287 | #ifdef HAS_SOCKET |
288 | else if ( | |
c623bd54 | 289 | #ifdef S_IFMT |
99b89507 LW |
290 | !(statbuf.st_mode & S_IFMT) |
291 | #else | |
292 | !statbuf.st_mode | |
293 | #endif | |
294 | ) { | |
96827780 MB |
295 | char tmpbuf[256]; |
296 | Sock_size_t buflen = sizeof tmpbuf; | |
297 | if (getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf, | |
d574b85e CS |
298 | &buflen) >= 0 |
299 | || errno != ENOTSOCK) | |
8990e307 | 300 | IoTYPE(io) = 's'; /* some OS's return 0 on fstat()ed socket */ |
99b89507 LW |
301 | /* but some return 0 for streams too, sigh */ |
302 | } | |
bf38876a | 303 | #endif |
a687059c | 304 | } |
6e21c824 | 305 | if (saveifp) { /* must use old fp? */ |
760ac839 | 306 | fd = PerlIO_fileno(saveifp); |
6e21c824 | 307 | if (saveofp) { |
760ac839 | 308 | PerlIO_flush(saveofp); /* emulate PerlIO_close() */ |
6e21c824 | 309 | if (saveofp != saveifp) { /* was a socket? */ |
760ac839 | 310 | PerlIO_close(saveofp); |
99b89507 LW |
311 | if (fd > 2) |
312 | Safefree(saveofp); | |
6e21c824 LW |
313 | } |
314 | } | |
760ac839 | 315 | if (fd != PerlIO_fileno(fp)) { |
bee1dbe2 | 316 | int pid; |
79072805 | 317 | SV *sv; |
bee1dbe2 | 318 | |
760ac839 LW |
319 | dup2(PerlIO_fileno(fp), fd); |
320 | sv = *av_fetch(fdpid,PerlIO_fileno(fp),TRUE); | |
a0d0e21e | 321 | (void)SvUPGRADE(sv, SVt_IV); |
463ee0b2 LW |
322 | pid = SvIVX(sv); |
323 | SvIVX(sv) = 0; | |
79072805 | 324 | sv = *av_fetch(fdpid,fd,TRUE); |
a0d0e21e | 325 | (void)SvUPGRADE(sv, SVt_IV); |
463ee0b2 | 326 | SvIVX(sv) = pid; |
760ac839 | 327 | PerlIO_close(fp); |
bee1dbe2 | 328 | |
6e21c824 LW |
329 | } |
330 | fp = saveifp; | |
760ac839 | 331 | PerlIO_clearerr(fp); |
6e21c824 | 332 | } |
a0d0e21e | 333 | #if defined(HAS_FCNTL) && defined(F_SETFD) |
760ac839 | 334 | fd = PerlIO_fileno(fp); |
a0d0e21e | 335 | fcntl(fd,F_SETFD,fd > maxsysfd); |
1462b684 | 336 | #endif |
8990e307 | 337 | IoIFP(io) = fp; |
bf38876a | 338 | if (writing) { |
96827780 | 339 | dTHR; |
8990e307 LW |
340 | if (IoTYPE(io) == 's' |
341 | || (IoTYPE(io) == '>' && S_ISCHR(statbuf.st_mode)) ) { | |
760ac839 LW |
342 | if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),"w"))) { |
343 | PerlIO_close(fp); | |
8990e307 | 344 | IoIFP(io) = Nullfp; |
6e21c824 | 345 | goto say_false; |
fe14fcc3 | 346 | } |
1462b684 LW |
347 | } |
348 | else | |
8990e307 | 349 | IoOFP(io) = fp; |
bf38876a | 350 | } |
a687059c | 351 | return TRUE; |
6e21c824 LW |
352 | |
353 | say_false: | |
8990e307 LW |
354 | IoIFP(io) = saveifp; |
355 | IoOFP(io) = saveofp; | |
356 | IoTYPE(io) = savetype; | |
6e21c824 | 357 | return FALSE; |
a687059c LW |
358 | } |
359 | ||
760ac839 | 360 | PerlIO * |
8ac85365 | 361 | nextargv(register GV *gv) |
a687059c | 362 | { |
79072805 | 363 | register SV *sv; |
99b89507 | 364 | #ifndef FLEXFILENAMES |
c623bd54 LW |
365 | int filedev; |
366 | int fileino; | |
99b89507 | 367 | #endif |
c623bd54 LW |
368 | int fileuid; |
369 | int filegid; | |
fe14fcc3 | 370 | |
79072805 | 371 | if (!argvoutgv) |
85e6fe83 | 372 | argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO); |
fe14fcc3 | 373 | if (filemode & (S_ISUID|S_ISGID)) { |
760ac839 | 374 | PerlIO_flush(IoIFP(GvIOn(argvoutgv))); /* chmod must follow last write */ |
fe14fcc3 LW |
375 | #ifdef HAS_FCHMOD |
376 | (void)fchmod(lastfd,filemode); | |
377 | #else | |
378 | (void)chmod(oldname,filemode); | |
379 | #endif | |
380 | } | |
381 | filemode = 0; | |
79072805 | 382 | while (av_len(GvAV(gv)) >= 0) { |
11343788 | 383 | dTHR; |
463ee0b2 | 384 | STRLEN len; |
79072805 | 385 | sv = av_shift(GvAV(gv)); |
8990e307 | 386 | SAVEFREESV(sv); |
79072805 LW |
387 | sv_setsv(GvSV(gv),sv); |
388 | SvSETMAGIC(GvSV(gv)); | |
463ee0b2 | 389 | oldname = SvPVx(GvSV(gv), len); |
c07a80fd | 390 | if (do_open(gv,oldname,len,FALSE,0,0,Nullfp)) { |
a687059c | 391 | if (inplace) { |
79072805 | 392 | TAINT_PROPER("inplace open"); |
c623bd54 | 393 | if (strEQ(oldname,"-")) { |
4633a7c4 | 394 | setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO)); |
a0d0e21e | 395 | return IoIFP(GvIOp(gv)); |
c623bd54 | 396 | } |
99b89507 | 397 | #ifndef FLEXFILENAMES |
c623bd54 LW |
398 | filedev = statbuf.st_dev; |
399 | fileino = statbuf.st_ino; | |
99b89507 | 400 | #endif |
a687059c LW |
401 | filemode = statbuf.st_mode; |
402 | fileuid = statbuf.st_uid; | |
403 | filegid = statbuf.st_gid; | |
c623bd54 LW |
404 | if (!S_ISREG(filemode)) { |
405 | warn("Can't do inplace edit: %s is not a regular file", | |
406 | oldname ); | |
79072805 | 407 | do_close(gv,FALSE); |
c623bd54 LW |
408 | continue; |
409 | } | |
a687059c | 410 | if (*inplace) { |
ff8e2863 | 411 | #ifdef SUFFIX |
79072805 | 412 | add_suffix(sv,inplace); |
ff8e2863 | 413 | #else |
79072805 | 414 | sv_catpv(sv,inplace); |
ff8e2863 | 415 | #endif |
c623bd54 | 416 | #ifndef FLEXFILENAMES |
a0d0e21e | 417 | if (Stat(SvPVX(sv),&statbuf) >= 0 |
c623bd54 LW |
418 | && statbuf.st_dev == filedev |
419 | && statbuf.st_ino == fileino ) { | |
420 | warn("Can't do inplace edit: %s > 14 characters", | |
463ee0b2 | 421 | SvPVX(sv) ); |
79072805 | 422 | do_close(gv,FALSE); |
c623bd54 LW |
423 | continue; |
424 | } | |
425 | #endif | |
fe14fcc3 | 426 | #ifdef HAS_RENAME |
bee1dbe2 | 427 | #ifndef DOSISH |
463ee0b2 | 428 | if (rename(oldname,SvPVX(sv)) < 0) { |
c623bd54 | 429 | warn("Can't rename %s to %s: %s, skipping file", |
2304df62 | 430 | oldname, SvPVX(sv), Strerror(errno) ); |
79072805 | 431 | do_close(gv,FALSE); |
c623bd54 LW |
432 | continue; |
433 | } | |
a687059c | 434 | #else |
79072805 | 435 | do_close(gv,FALSE); |
463ee0b2 LW |
436 | (void)unlink(SvPVX(sv)); |
437 | (void)rename(oldname,SvPVX(sv)); | |
1193dd27 | 438 | do_open(gv,SvPVX(sv),SvCUR(sv),FALSE,0,0,Nullfp); |
55497cff | 439 | #endif /* DOSISH */ |
ff8e2863 | 440 | #else |
463ee0b2 LW |
441 | (void)UNLINK(SvPVX(sv)); |
442 | if (link(oldname,SvPVX(sv)) < 0) { | |
c623bd54 | 443 | warn("Can't rename %s to %s: %s, skipping file", |
2304df62 | 444 | oldname, SvPVX(sv), Strerror(errno) ); |
79072805 | 445 | do_close(gv,FALSE); |
c623bd54 LW |
446 | continue; |
447 | } | |
a687059c LW |
448 | (void)UNLINK(oldname); |
449 | #endif | |
450 | } | |
451 | else { | |
a8c18271 | 452 | #if !defined(DOSISH) && !defined(AMIGAOS) |
edc7bc49 | 453 | # ifndef VMS /* Don't delete; use automatic file versioning */ |
fe14fcc3 LW |
454 | if (UNLINK(oldname) < 0) { |
455 | warn("Can't rename %s to %s: %s, skipping file", | |
2304df62 | 456 | oldname, SvPVX(sv), Strerror(errno) ); |
79072805 | 457 | do_close(gv,FALSE); |
fe14fcc3 LW |
458 | continue; |
459 | } | |
edc7bc49 | 460 | # endif |
ff8e2863 | 461 | #else |
463ee0b2 | 462 | croak("Can't do inplace edit without backup"); |
ff8e2863 | 463 | #endif |
a687059c LW |
464 | } |
465 | ||
79072805 LW |
466 | sv_setpvn(sv,">",1); |
467 | sv_catpv(sv,oldname); | |
748a9306 | 468 | SETERRNO(0,0); /* in case sprintf set errno */ |
c07a80fd | 469 | if (!do_open(argvoutgv,SvPVX(sv),SvCUR(sv),FALSE,0,0,Nullfp)) { |
c623bd54 | 470 | warn("Can't do inplace edit on %s: %s", |
2304df62 | 471 | oldname, Strerror(errno) ); |
79072805 | 472 | do_close(gv,FALSE); |
fe14fcc3 LW |
473 | continue; |
474 | } | |
4633a7c4 | 475 | setdefout(argvoutgv); |
760ac839 | 476 | lastfd = PerlIO_fileno(IoIFP(GvIOp(argvoutgv))); |
a0d0e21e | 477 | (void)Fstat(lastfd,&statbuf); |
fe14fcc3 LW |
478 | #ifdef HAS_FCHMOD |
479 | (void)fchmod(lastfd,filemode); | |
a687059c | 480 | #else |
3e3baf6d TB |
481 | # if !(defined(WIN32) && defined(__BORLANDC__)) |
482 | /* Borland runtime creates a readonly file! */ | |
a687059c | 483 | (void)chmod(oldname,filemode); |
3e3baf6d | 484 | # endif |
a687059c | 485 | #endif |
fe14fcc3 LW |
486 | if (fileuid != statbuf.st_uid || filegid != statbuf.st_gid) { |
487 | #ifdef HAS_FCHOWN | |
488 | (void)fchown(lastfd,fileuid,filegid); | |
a687059c | 489 | #else |
fe14fcc3 LW |
490 | #ifdef HAS_CHOWN |
491 | (void)chown(oldname,fileuid,filegid); | |
a687059c | 492 | #endif |
b1248f16 | 493 | #endif |
fe14fcc3 | 494 | } |
a687059c | 495 | } |
a0d0e21e | 496 | return IoIFP(GvIOp(gv)); |
a687059c LW |
497 | } |
498 | else | |
760ac839 | 499 | PerlIO_printf(PerlIO_stderr(), "Can't open %s: %s\n",SvPV(sv, na), Strerror(errno)); |
a687059c LW |
500 | } |
501 | if (inplace) { | |
79072805 | 502 | (void)do_close(argvoutgv,FALSE); |
4633a7c4 | 503 | setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO)); |
a687059c LW |
504 | } |
505 | return Nullfp; | |
506 | } | |
507 | ||
fe14fcc3 | 508 | #ifdef HAS_PIPE |
afd9f252 | 509 | void |
8ac85365 | 510 | do_pipe(SV *sv, GV *rgv, GV *wgv) |
afd9f252 | 511 | { |
79072805 LW |
512 | register IO *rstio; |
513 | register IO *wstio; | |
afd9f252 LW |
514 | int fd[2]; |
515 | ||
79072805 | 516 | if (!rgv) |
afd9f252 | 517 | goto badexit; |
79072805 | 518 | if (!wgv) |
afd9f252 LW |
519 | goto badexit; |
520 | ||
a0d0e21e LW |
521 | rstio = GvIOn(rgv); |
522 | wstio = GvIOn(wgv); | |
afd9f252 | 523 | |
a0d0e21e | 524 | if (IoIFP(rstio)) |
79072805 | 525 | do_close(rgv,FALSE); |
a0d0e21e | 526 | if (IoIFP(wstio)) |
79072805 | 527 | do_close(wgv,FALSE); |
afd9f252 LW |
528 | |
529 | if (pipe(fd) < 0) | |
530 | goto badexit; | |
760ac839 LW |
531 | IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"); |
532 | IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"); | |
8990e307 LW |
533 | IoIFP(wstio) = IoOFP(wstio); |
534 | IoTYPE(rstio) = '<'; | |
535 | IoTYPE(wstio) = '>'; | |
536 | if (!IoIFP(rstio) || !IoOFP(wstio)) { | |
760ac839 | 537 | if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio)); |
fe14fcc3 | 538 | else close(fd[0]); |
760ac839 | 539 | if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio)); |
fe14fcc3 LW |
540 | else close(fd[1]); |
541 | goto badexit; | |
542 | } | |
afd9f252 | 543 | |
79072805 | 544 | sv_setsv(sv,&sv_yes); |
afd9f252 LW |
545 | return; |
546 | ||
547 | badexit: | |
79072805 | 548 | sv_setsv(sv,&sv_undef); |
afd9f252 LW |
549 | return; |
550 | } | |
b1248f16 | 551 | #endif |
afd9f252 | 552 | |
517844ec | 553 | /* explicit renamed to avoid C++ conflict -- kja */ |
a687059c | 554 | bool |
a0d0e21e | 555 | #ifndef CAN_PROTOTYPE |
517844ec | 556 | do_close(gv,not_implicit) |
79072805 | 557 | GV *gv; |
517844ec | 558 | bool not_implicit; |
8990e307 | 559 | #else |
517844ec | 560 | do_close(GV *gv, bool not_implicit) |
a0d0e21e | 561 | #endif /* CAN_PROTOTYPE */ |
a687059c | 562 | { |
1193dd27 IZ |
563 | bool retval; |
564 | IO *io; | |
a687059c | 565 | |
79072805 LW |
566 | if (!gv) |
567 | gv = argvgv; | |
a0d0e21e | 568 | if (!gv || SvTYPE(gv) != SVt_PVGV) { |
748a9306 | 569 | SETERRNO(EBADF,SS$_IVCHAN); |
c2ab57d4 | 570 | return FALSE; |
99b89507 | 571 | } |
79072805 LW |
572 | io = GvIO(gv); |
573 | if (!io) { /* never opened */ | |
517844ec | 574 | if (dowarn && not_implicit) |
79072805 | 575 | warn("Close on unopened file <%s>",GvENAME(gv)); |
a687059c LW |
576 | return FALSE; |
577 | } | |
1193dd27 | 578 | retval = io_close(io); |
517844ec | 579 | if (not_implicit) { |
1193dd27 IZ |
580 | IoLINES(io) = 0; |
581 | IoPAGE(io) = 0; | |
582 | IoLINES_LEFT(io) = IoPAGE_LEN(io); | |
583 | } | |
584 | IoTYPE(io) = ' '; | |
585 | return retval; | |
586 | } | |
587 | ||
588 | bool | |
8ac85365 | 589 | io_close(IO *io) |
1193dd27 IZ |
590 | { |
591 | bool retval = FALSE; | |
592 | int status; | |
593 | ||
8990e307 LW |
594 | if (IoIFP(io)) { |
595 | if (IoTYPE(io) == '|') { | |
596 | status = my_pclose(IoIFP(io)); | |
f86702cc | 597 | STATUS_NATIVE_SET(status); |
1e422769 | 598 | retval = (STATUS_POSIX == 0); |
a687059c | 599 | } |
8990e307 | 600 | else if (IoTYPE(io) == '-') |
a687059c LW |
601 | retval = TRUE; |
602 | else { | |
8990e307 | 603 | if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */ |
760ac839 LW |
604 | retval = (PerlIO_close(IoOFP(io)) != EOF); |
605 | PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */ | |
c2ab57d4 LW |
606 | } |
607 | else | |
760ac839 | 608 | retval = (PerlIO_close(IoIFP(io)) != EOF); |
a687059c | 609 | } |
8990e307 | 610 | IoOFP(io) = IoIFP(io) = Nullfp; |
79072805 | 611 | } |
1193dd27 | 612 | |
a687059c LW |
613 | return retval; |
614 | } | |
615 | ||
616 | bool | |
8ac85365 | 617 | do_eof(GV *gv) |
a687059c | 618 | { |
11343788 | 619 | dTHR; |
79072805 | 620 | register IO *io; |
a687059c LW |
621 | int ch; |
622 | ||
79072805 | 623 | io = GvIO(gv); |
a687059c | 624 | |
79072805 | 625 | if (!io) |
a687059c LW |
626 | return TRUE; |
627 | ||
8990e307 | 628 | while (IoIFP(io)) { |
a687059c | 629 | |
760ac839 LW |
630 | if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */ |
631 | if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */ | |
632 | return FALSE; /* this is the most usual case */ | |
633 | } | |
a687059c | 634 | |
760ac839 | 635 | ch = PerlIO_getc(IoIFP(io)); |
a687059c | 636 | if (ch != EOF) { |
760ac839 | 637 | (void)PerlIO_ungetc(IoIFP(io),ch); |
a687059c LW |
638 | return FALSE; |
639 | } | |
760ac839 LW |
640 | if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) { |
641 | if (PerlIO_get_cnt(IoIFP(io)) < -1) | |
642 | PerlIO_set_cnt(IoIFP(io),-1); | |
643 | } | |
8990e307 | 644 | if (op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */ |
79072805 | 645 | if (!nextargv(argvgv)) /* get another fp handy */ |
a687059c LW |
646 | return TRUE; |
647 | } | |
648 | else | |
649 | return TRUE; /* normal fp, definitely end of file */ | |
650 | } | |
651 | return TRUE; | |
652 | } | |
653 | ||
654 | long | |
8ac85365 | 655 | do_tell(GV *gv) |
a687059c | 656 | { |
79072805 | 657 | register IO *io; |
96e4d5b1 | 658 | register PerlIO *fp; |
a687059c | 659 | |
96e4d5b1 | 660 | if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) { |
bee1dbe2 | 661 | #ifdef ULTRIX_STDIO_BOTCH |
96e4d5b1 | 662 | if (PerlIO_eof(fp)) |
663 | (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */ | |
bee1dbe2 | 664 | #endif |
8903cb82 | 665 | return PerlIO_tell(fp); |
96e4d5b1 | 666 | } |
a687059c | 667 | if (dowarn) |
8903cb82 | 668 | warn("tell() on unopened file"); |
748a9306 | 669 | SETERRNO(EBADF,RMS$_IFI); |
a687059c LW |
670 | return -1L; |
671 | } | |
672 | ||
673 | bool | |
8ac85365 | 674 | do_seek(GV *gv, long int pos, int whence) |
a687059c | 675 | { |
79072805 | 676 | register IO *io; |
137443ea | 677 | register PerlIO *fp; |
a687059c | 678 | |
137443ea | 679 | if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) { |
bee1dbe2 | 680 | #ifdef ULTRIX_STDIO_BOTCH |
137443ea | 681 | if (PerlIO_eof(fp)) |
682 | (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */ | |
bee1dbe2 | 683 | #endif |
8903cb82 | 684 | return PerlIO_seek(fp, pos, whence) >= 0; |
137443ea | 685 | } |
a687059c | 686 | if (dowarn) |
8903cb82 | 687 | warn("seek() on unopened file"); |
748a9306 | 688 | SETERRNO(EBADF,RMS$_IFI); |
a687059c LW |
689 | return FALSE; |
690 | } | |
691 | ||
8903cb82 | 692 | long |
8ac85365 | 693 | do_sysseek(GV *gv, long int pos, int whence) |
8903cb82 | 694 | { |
695 | register IO *io; | |
696 | register PerlIO *fp; | |
697 | ||
698 | if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) | |
699 | return lseek(PerlIO_fileno(fp), pos, whence); | |
700 | if (dowarn) | |
701 | warn("sysseek() on unopened file"); | |
702 | SETERRNO(EBADF,RMS$_IFI); | |
703 | return -1L; | |
704 | } | |
705 | ||
a0d0e21e | 706 | #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP) |
c2ab57d4 | 707 | /* code courtesy of William Kucharski */ |
fe14fcc3 | 708 | #define HAS_CHSIZE |
6eb13c3b | 709 | |
517844ec | 710 | I32 my_chsize(fd, length) |
79072805 | 711 | I32 fd; /* file descriptor */ |
85e6fe83 | 712 | Off_t length; /* length to set file to */ |
6eb13c3b | 713 | { |
6eb13c3b LW |
714 | struct flock fl; |
715 | struct stat filebuf; | |
716 | ||
a0d0e21e | 717 | if (Fstat(fd, &filebuf) < 0) |
6eb13c3b LW |
718 | return -1; |
719 | ||
720 | if (filebuf.st_size < length) { | |
721 | ||
722 | /* extend file length */ | |
723 | ||
724 | if ((lseek(fd, (length - 1), 0)) < 0) | |
725 | return -1; | |
726 | ||
727 | /* write a "0" byte */ | |
728 | ||
729 | if ((write(fd, "", 1)) != 1) | |
730 | return -1; | |
731 | } | |
732 | else { | |
733 | /* truncate length */ | |
734 | ||
735 | fl.l_whence = 0; | |
736 | fl.l_len = 0; | |
737 | fl.l_start = length; | |
a0d0e21e | 738 | fl.l_type = F_WRLCK; /* write lock on file space */ |
6eb13c3b LW |
739 | |
740 | /* | |
a0d0e21e | 741 | * This relies on the UNDOCUMENTED F_FREESP argument to |
6eb13c3b LW |
742 | * fcntl(2), which truncates the file so that it ends at the |
743 | * position indicated by fl.l_start. | |
744 | * | |
745 | * Will minor miracles never cease? | |
746 | */ | |
747 | ||
a0d0e21e | 748 | if (fcntl(fd, F_FREESP, &fl) < 0) |
6eb13c3b LW |
749 | return -1; |
750 | ||
751 | } | |
752 | ||
753 | return 0; | |
754 | } | |
a0d0e21e | 755 | #endif /* F_FREESP */ |
ff8e2863 | 756 | |
a687059c | 757 | bool |
8ac85365 | 758 | do_print(register SV *sv, FILE *fp) |
a687059c LW |
759 | { |
760 | register char *tmps; | |
463ee0b2 | 761 | STRLEN len; |
a687059c | 762 | |
79072805 LW |
763 | /* assuming fp is checked earlier */ |
764 | if (!sv) | |
765 | return TRUE; | |
766 | if (ofmt) { | |
8990e307 | 767 | if (SvGMAGICAL(sv)) |
79072805 | 768 | mg_get(sv); |
463ee0b2 | 769 | if (SvIOK(sv) && SvIVX(sv) != 0) { |
760ac839 LW |
770 | PerlIO_printf(fp, ofmt, (double)SvIVX(sv)); |
771 | return !PerlIO_error(fp); | |
79072805 | 772 | } |
463ee0b2 | 773 | if ( (SvNOK(sv) && SvNVX(sv) != 0.0) |
79072805 | 774 | || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) { |
760ac839 LW |
775 | PerlIO_printf(fp, ofmt, SvNVX(sv)); |
776 | return !PerlIO_error(fp); | |
79072805 | 777 | } |
a687059c | 778 | } |
79072805 LW |
779 | switch (SvTYPE(sv)) { |
780 | case SVt_NULL: | |
8990e307 LW |
781 | if (dowarn) |
782 | warn(warn_uninit); | |
ff8e2863 | 783 | return TRUE; |
79072805 | 784 | case SVt_IV: |
a0d0e21e LW |
785 | if (SvIOK(sv)) { |
786 | if (SvGMAGICAL(sv)) | |
787 | mg_get(sv); | |
760ac839 LW |
788 | PerlIO_printf(fp, "%ld", (long)SvIVX(sv)); |
789 | return !PerlIO_error(fp); | |
a0d0e21e LW |
790 | } |
791 | /* FALL THROUGH */ | |
79072805 | 792 | default: |
463ee0b2 | 793 | tmps = SvPV(sv, len); |
79072805 | 794 | break; |
ff8e2863 | 795 | } |
760ac839 | 796 | if (len && (PerlIO_write(fp,tmps,len) == 0 || PerlIO_error(fp))) |
a687059c | 797 | return FALSE; |
760ac839 | 798 | return !PerlIO_error(fp); |
a687059c LW |
799 | } |
800 | ||
79072805 | 801 | I32 |
8ac85365 | 802 | my_stat(ARGSproto) |
a687059c | 803 | { |
4e35701f | 804 | djSP; |
79072805 | 805 | IO *io; |
748a9306 | 806 | GV* tmpgv; |
79072805 | 807 | |
a0d0e21e | 808 | if (op->op_flags & OPf_REF) { |
79072805 | 809 | EXTEND(sp,1); |
748a9306 LW |
810 | tmpgv = cGVOP->op_gv; |
811 | do_fstat: | |
812 | io = GvIO(tmpgv); | |
8990e307 | 813 | if (io && IoIFP(io)) { |
748a9306 | 814 | statgv = tmpgv; |
79072805 LW |
815 | sv_setpv(statname,""); |
816 | laststype = OP_STAT; | |
760ac839 | 817 | return (laststatval = Fstat(PerlIO_fileno(IoIFP(io)), &statcache)); |
a687059c LW |
818 | } |
819 | else { | |
748a9306 | 820 | if (tmpgv == defgv) |
57ebbfd0 | 821 | return laststatval; |
a687059c LW |
822 | if (dowarn) |
823 | warn("Stat on unopened file <%s>", | |
748a9306 | 824 | GvENAME(tmpgv)); |
79072805 LW |
825 | statgv = Nullgv; |
826 | sv_setpv(statname,""); | |
57ebbfd0 | 827 | return (laststatval = -1); |
a687059c LW |
828 | } |
829 | } | |
830 | else { | |
748a9306 | 831 | SV* sv = POPs; |
79072805 | 832 | PUTBACK; |
748a9306 LW |
833 | if (SvTYPE(sv) == SVt_PVGV) { |
834 | tmpgv = (GV*)sv; | |
835 | goto do_fstat; | |
836 | } | |
837 | else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) { | |
838 | tmpgv = (GV*)SvRV(sv); | |
839 | goto do_fstat; | |
840 | } | |
841 | ||
79072805 | 842 | statgv = Nullgv; |
463ee0b2 | 843 | sv_setpv(statname,SvPV(sv, na)); |
79072805 | 844 | laststype = OP_STAT; |
a0d0e21e | 845 | laststatval = Stat(SvPV(sv, na),&statcache); |
463ee0b2 | 846 | if (laststatval < 0 && dowarn && strchr(SvPV(sv, na), '\n')) |
bee1dbe2 LW |
847 | warn(warn_nl, "stat"); |
848 | return laststatval; | |
a687059c LW |
849 | } |
850 | } | |
851 | ||
79072805 | 852 | I32 |
8ac85365 | 853 | my_lstat(ARGSproto) |
c623bd54 | 854 | { |
4e35701f | 855 | djSP; |
79072805 | 856 | SV *sv; |
a0d0e21e | 857 | if (op->op_flags & OPf_REF) { |
79072805 LW |
858 | EXTEND(sp,1); |
859 | if (cGVOP->op_gv == defgv) { | |
860 | if (laststype != OP_LSTAT) | |
463ee0b2 | 861 | croak("The stat preceding -l _ wasn't an lstat"); |
fe14fcc3 LW |
862 | return laststatval; |
863 | } | |
463ee0b2 | 864 | croak("You can't use -l on a filehandle"); |
fe14fcc3 | 865 | } |
c623bd54 | 866 | |
79072805 LW |
867 | laststype = OP_LSTAT; |
868 | statgv = Nullgv; | |
869 | sv = POPs; | |
870 | PUTBACK; | |
463ee0b2 | 871 | sv_setpv(statname,SvPV(sv, na)); |
fe14fcc3 | 872 | #ifdef HAS_LSTAT |
463ee0b2 | 873 | laststatval = lstat(SvPV(sv, na),&statcache); |
c623bd54 | 874 | #else |
a0d0e21e | 875 | laststatval = Stat(SvPV(sv, na),&statcache); |
c623bd54 | 876 | #endif |
463ee0b2 | 877 | if (laststatval < 0 && dowarn && strchr(SvPV(sv, na), '\n')) |
bee1dbe2 LW |
878 | warn(warn_nl, "lstat"); |
879 | return laststatval; | |
c623bd54 LW |
880 | } |
881 | ||
a687059c | 882 | bool |
8ac85365 | 883 | do_aexec(SV *really, register SV **mark, register SV **sp) |
a687059c | 884 | { |
a687059c | 885 | register char **a; |
a687059c LW |
886 | char *tmps; |
887 | ||
79072805 | 888 | if (sp > mark) { |
11343788 | 889 | dTHR; |
79072805 | 890 | New(401,Argv, sp - mark + 1, char*); |
bee1dbe2 | 891 | a = Argv; |
79072805 LW |
892 | while (++mark <= sp) { |
893 | if (*mark) | |
463ee0b2 | 894 | *a++ = SvPVx(*mark, na); |
a687059c LW |
895 | else |
896 | *a++ = ""; | |
897 | } | |
898 | *a = Nullch; | |
bee1dbe2 | 899 | if (*Argv[0] != '/') /* will execvp use PATH? */ |
79072805 | 900 | TAINT_ENV(); /* testing IFS here is overkill, probably */ |
463ee0b2 | 901 | if (really && *(tmps = SvPV(really, na))) |
bee1dbe2 | 902 | execvp(tmps,Argv); |
a687059c | 903 | else |
bee1dbe2 | 904 | execvp(Argv[0],Argv); |
a0d0e21e LW |
905 | if (dowarn) |
906 | warn("Can't exec \"%s\": %s", Argv[0], Strerror(errno)); | |
a687059c | 907 | } |
bee1dbe2 | 908 | do_execfree(); |
a687059c LW |
909 | return FALSE; |
910 | } | |
911 | ||
fe14fcc3 | 912 | void |
8ac85365 | 913 | do_execfree(void) |
ff8e2863 LW |
914 | { |
915 | if (Argv) { | |
916 | Safefree(Argv); | |
917 | Argv = Null(char **); | |
918 | } | |
919 | if (Cmd) { | |
920 | Safefree(Cmd); | |
921 | Cmd = Nullch; | |
922 | } | |
923 | } | |
924 | ||
6890e559 | 925 | #if !defined(OS2) && !defined(WIN32) |
760ac839 | 926 | |
a687059c | 927 | bool |
8ac85365 | 928 | do_exec(char *cmd) |
a687059c LW |
929 | { |
930 | register char **a; | |
931 | register char *s; | |
a687059c LW |
932 | char flags[10]; |
933 | ||
748a9306 LW |
934 | while (*cmd && isSPACE(*cmd)) |
935 | cmd++; | |
936 | ||
a687059c LW |
937 | /* save an extra exec if possible */ |
938 | ||
bf38876a LW |
939 | #ifdef CSH |
940 | if (strnEQ(cmd,cshname,cshlen) && strnEQ(cmd+cshlen," -c",3)) { | |
a687059c | 941 | strcpy(flags,"-c"); |
bf38876a | 942 | s = cmd+cshlen+3; |
a687059c LW |
943 | if (*s == 'f') { |
944 | s++; | |
945 | strcat(flags,"f"); | |
946 | } | |
947 | if (*s == ' ') | |
948 | s++; | |
949 | if (*s++ == '\'') { | |
950 | char *ncmd = s; | |
951 | ||
952 | while (*s) | |
953 | s++; | |
954 | if (s[-1] == '\n') | |
955 | *--s = '\0'; | |
956 | if (s[-1] == '\'') { | |
957 | *--s = '\0'; | |
bf38876a | 958 | execl(cshname,"csh", flags,ncmd,(char*)0); |
a687059c LW |
959 | *s = '\''; |
960 | return FALSE; | |
961 | } | |
962 | } | |
963 | } | |
bf38876a | 964 | #endif /* CSH */ |
a687059c LW |
965 | |
966 | /* see if there are shell metacharacters in it */ | |
967 | ||
748a9306 LW |
968 | if (*cmd == '.' && isSPACE(cmd[1])) |
969 | goto doshell; | |
970 | ||
971 | if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4])) | |
972 | goto doshell; | |
973 | ||
99b89507 | 974 | for (s = cmd; *s && isALPHA(*s); s++) ; /* catch VAR=val gizmo */ |
63f2c1e1 LW |
975 | if (*s == '=') |
976 | goto doshell; | |
748a9306 | 977 | |
a687059c | 978 | for (s = cmd; *s; s++) { |
93a17b20 | 979 | if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) { |
a687059c LW |
980 | if (*s == '\n' && !s[1]) { |
981 | *s = '\0'; | |
982 | break; | |
983 | } | |
984 | doshell: | |
ff68c719 | 985 | execl(sh_path, "sh", "-c", cmd, (char*)0); |
a687059c LW |
986 | return FALSE; |
987 | } | |
988 | } | |
748a9306 | 989 | |
ff8e2863 | 990 | New(402,Argv, (s - cmd) / 2 + 2, char*); |
a0d0e21e | 991 | Cmd = savepvn(cmd, s-cmd); |
ff8e2863 LW |
992 | a = Argv; |
993 | for (s = Cmd; *s;) { | |
99b89507 | 994 | while (*s && isSPACE(*s)) s++; |
a687059c LW |
995 | if (*s) |
996 | *(a++) = s; | |
99b89507 | 997 | while (*s && !isSPACE(*s)) s++; |
a687059c LW |
998 | if (*s) |
999 | *s++ = '\0'; | |
1000 | } | |
1001 | *a = Nullch; | |
ff8e2863 LW |
1002 | if (Argv[0]) { |
1003 | execvp(Argv[0],Argv); | |
b1248f16 | 1004 | if (errno == ENOEXEC) { /* for system V NIH syndrome */ |
ff8e2863 | 1005 | do_execfree(); |
a687059c | 1006 | goto doshell; |
b1248f16 | 1007 | } |
a0d0e21e LW |
1008 | if (dowarn) |
1009 | warn("Can't exec \"%s\": %s", Argv[0], Strerror(errno)); | |
a687059c | 1010 | } |
ff8e2863 | 1011 | do_execfree(); |
a687059c LW |
1012 | return FALSE; |
1013 | } | |
1014 | ||
6890e559 | 1015 | #endif /* OS2 || WIN32 */ |
760ac839 | 1016 | |
79072805 | 1017 | I32 |
8ac85365 | 1018 | apply(I32 type, register SV **mark, register SV **sp) |
a687059c | 1019 | { |
11343788 | 1020 | dTHR; |
79072805 LW |
1021 | register I32 val; |
1022 | register I32 val2; | |
1023 | register I32 tot = 0; | |
a687059c | 1024 | char *s; |
79072805 | 1025 | SV **oldmark = mark; |
a687059c | 1026 | |
463ee0b2 LW |
1027 | if (tainting) { |
1028 | while (++mark <= sp) { | |
bbce6d69 | 1029 | if (SvTAINTED(*mark)) { |
1030 | TAINT; | |
1031 | break; | |
1032 | } | |
463ee0b2 LW |
1033 | } |
1034 | mark = oldmark; | |
1035 | } | |
a687059c | 1036 | switch (type) { |
79072805 LW |
1037 | case OP_CHMOD: |
1038 | TAINT_PROPER("chmod"); | |
1039 | if (++mark <= sp) { | |
1040 | tot = sp - mark; | |
463ee0b2 | 1041 | val = SvIVx(*mark); |
79072805 | 1042 | while (++mark <= sp) { |
463ee0b2 | 1043 | if (chmod(SvPVx(*mark, na),val)) |
a687059c LW |
1044 | tot--; |
1045 | } | |
1046 | } | |
1047 | break; | |
fe14fcc3 | 1048 | #ifdef HAS_CHOWN |
79072805 LW |
1049 | case OP_CHOWN: |
1050 | TAINT_PROPER("chown"); | |
1051 | if (sp - mark > 2) { | |
463ee0b2 LW |
1052 | val = SvIVx(*++mark); |
1053 | val2 = SvIVx(*++mark); | |
a0d0e21e | 1054 | tot = sp - mark; |
79072805 | 1055 | while (++mark <= sp) { |
463ee0b2 | 1056 | if (chown(SvPVx(*mark, na),val,val2)) |
a687059c LW |
1057 | tot--; |
1058 | } | |
1059 | } | |
1060 | break; | |
b1248f16 | 1061 | #endif |
fe14fcc3 | 1062 | #ifdef HAS_KILL |
79072805 LW |
1063 | case OP_KILL: |
1064 | TAINT_PROPER("kill"); | |
55497cff | 1065 | if (mark == sp) |
1066 | break; | |
463ee0b2 | 1067 | s = SvPVx(*++mark, na); |
79072805 LW |
1068 | tot = sp - mark; |
1069 | if (isUPPER(*s)) { | |
1070 | if (*s == 'S' && s[1] == 'I' && s[2] == 'G') | |
1071 | s += 3; | |
1072 | if (!(val = whichsig(s))) | |
463ee0b2 | 1073 | croak("Unrecognized signal name \"%s\"",s); |
79072805 LW |
1074 | } |
1075 | else | |
463ee0b2 | 1076 | val = SvIVx(*mark); |
3595fcef | 1077 | #ifdef VMS |
1078 | /* kill() doesn't do process groups (job trees?) under VMS */ | |
1079 | if (val < 0) val = -val; | |
1080 | if (val == SIGKILL) { | |
1081 | # include <starlet.h> | |
1082 | /* Use native sys$delprc() to insure that target process is | |
1083 | * deleted; supervisor-mode images don't pay attention to | |
1084 | * CRTL's emulation of Unix-style signals and kill() | |
1085 | */ | |
1086 | while (++mark <= sp) { | |
1087 | I32 proc = SvIVx(*mark); | |
1088 | register unsigned long int __vmssts; | |
1089 | if (!((__vmssts = sys$delprc(&proc,0)) & 1)) { | |
1090 | tot--; | |
1091 | switch (__vmssts) { | |
1092 | case SS$_NONEXPR: | |
1093 | case SS$_NOSUCHNODE: | |
1094 | SETERRNO(ESRCH,__vmssts); | |
1095 | break; | |
1096 | case SS$_NOPRIV: | |
1097 | SETERRNO(EPERM,__vmssts); | |
1098 | break; | |
1099 | default: | |
1100 | SETERRNO(EVMSERR,__vmssts); | |
1101 | } | |
1102 | } | |
1103 | } | |
1104 | break; | |
1105 | } | |
1106 | #endif | |
79072805 LW |
1107 | if (val < 0) { |
1108 | val = -val; | |
1109 | while (++mark <= sp) { | |
463ee0b2 | 1110 | I32 proc = SvIVx(*mark); |
fe14fcc3 | 1111 | #ifdef HAS_KILLPG |
79072805 | 1112 | if (killpg(proc,val)) /* BSD */ |
a687059c | 1113 | #else |
79072805 | 1114 | if (kill(-proc,val)) /* SYSV */ |
a687059c | 1115 | #endif |
79072805 | 1116 | tot--; |
a687059c | 1117 | } |
79072805 LW |
1118 | } |
1119 | else { | |
1120 | while (++mark <= sp) { | |
463ee0b2 | 1121 | if (kill(SvIVx(*mark),val)) |
79072805 | 1122 | tot--; |
a687059c LW |
1123 | } |
1124 | } | |
1125 | break; | |
b1248f16 | 1126 | #endif |
79072805 LW |
1127 | case OP_UNLINK: |
1128 | TAINT_PROPER("unlink"); | |
1129 | tot = sp - mark; | |
1130 | while (++mark <= sp) { | |
463ee0b2 | 1131 | s = SvPVx(*mark, na); |
a687059c LW |
1132 | if (euid || unsafe) { |
1133 | if (UNLINK(s)) | |
1134 | tot--; | |
1135 | } | |
1136 | else { /* don't let root wipe out directories without -U */ | |
fe14fcc3 | 1137 | #ifdef HAS_LSTAT |
c623bd54 | 1138 | if (lstat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode)) |
a687059c | 1139 | #else |
a0d0e21e | 1140 | if (Stat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode)) |
a687059c | 1141 | #endif |
a687059c LW |
1142 | tot--; |
1143 | else { | |
1144 | if (UNLINK(s)) | |
1145 | tot--; | |
1146 | } | |
1147 | } | |
1148 | } | |
1149 | break; | |
a0d0e21e | 1150 | #ifdef HAS_UTIME |
79072805 LW |
1151 | case OP_UTIME: |
1152 | TAINT_PROPER("utime"); | |
1153 | if (sp - mark > 2) { | |
748a9306 | 1154 | #if defined(I_UTIME) || defined(VMS) |
663a0e37 LW |
1155 | struct utimbuf utbuf; |
1156 | #else | |
a687059c | 1157 | struct { |
663a0e37 LW |
1158 | long actime; |
1159 | long modtime; | |
a687059c | 1160 | } utbuf; |
663a0e37 | 1161 | #endif |
a687059c | 1162 | |
afd9f252 | 1163 | Zero(&utbuf, sizeof utbuf, char); |
517844ec | 1164 | #ifdef BIG_TIME |
1165 | utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */ | |
1166 | utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */ | |
1167 | #else | |
463ee0b2 LW |
1168 | utbuf.actime = SvIVx(*++mark); /* time accessed */ |
1169 | utbuf.modtime = SvIVx(*++mark); /* time modified */ | |
517844ec | 1170 | #endif |
79072805 LW |
1171 | tot = sp - mark; |
1172 | while (++mark <= sp) { | |
463ee0b2 | 1173 | if (utime(SvPVx(*mark, na),&utbuf)) |
a687059c LW |
1174 | tot--; |
1175 | } | |
a687059c LW |
1176 | } |
1177 | else | |
79072805 | 1178 | tot = 0; |
a687059c | 1179 | break; |
a0d0e21e | 1180 | #endif |
a687059c LW |
1181 | } |
1182 | return tot; | |
1183 | } | |
1184 | ||
1185 | /* Do the permissions allow some operation? Assumes statcache already set. */ | |
a0d0e21e | 1186 | #ifndef VMS /* VMS' cando is in vms.c */ |
79072805 | 1187 | I32 |
8ac85365 | 1188 | cando(I32 bit, I32 effective, register struct stat *statbufp) |
a687059c | 1189 | { |
bee1dbe2 | 1190 | #ifdef DOSISH |
fe14fcc3 LW |
1191 | /* [Comments and code from Len Reed] |
1192 | * MS-DOS "user" is similar to UNIX's "superuser," but can't write | |
1193 | * to write-protected files. The execute permission bit is set | |
1194 | * by the Miscrosoft C library stat() function for the following: | |
1195 | * .exe files | |
1196 | * .com files | |
1197 | * .bat files | |
1198 | * directories | |
1199 | * All files and directories are readable. | |
1200 | * Directories and special files, e.g. "CON", cannot be | |
1201 | * write-protected. | |
1202 | * [Comment by Tom Dinger -- a directory can have the write-protect | |
1203 | * bit set in the file system, but DOS permits changes to | |
1204 | * the directory anyway. In addition, all bets are off | |
1205 | * here for networked software, such as Novell and | |
1206 | * Sun's PC-NFS.] | |
1207 | */ | |
1208 | ||
bee1dbe2 LW |
1209 | /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat |
1210 | * too so it will actually look into the files for magic numbers | |
1211 | */ | |
fe14fcc3 LW |
1212 | return (bit & statbufp->st_mode) ? TRUE : FALSE; |
1213 | ||
55497cff | 1214 | #else /* ! DOSISH */ |
a687059c | 1215 | if ((effective ? euid : uid) == 0) { /* root is special */ |
c623bd54 LW |
1216 | if (bit == S_IXUSR) { |
1217 | if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode)) | |
a687059c LW |
1218 | return TRUE; |
1219 | } | |
1220 | else | |
1221 | return TRUE; /* root reads and writes anything */ | |
1222 | return FALSE; | |
1223 | } | |
1224 | if (statbufp->st_uid == (effective ? euid : uid) ) { | |
1225 | if (statbufp->st_mode & bit) | |
1226 | return TRUE; /* ok as "user" */ | |
1227 | } | |
79072805 | 1228 | else if (ingroup((I32)statbufp->st_gid,effective)) { |
a687059c LW |
1229 | if (statbufp->st_mode & bit >> 3) |
1230 | return TRUE; /* ok as "group" */ | |
1231 | } | |
1232 | else if (statbufp->st_mode & bit >> 6) | |
1233 | return TRUE; /* ok as "other" */ | |
1234 | return FALSE; | |
55497cff | 1235 | #endif /* ! DOSISH */ |
a687059c | 1236 | } |
a0d0e21e | 1237 | #endif /* ! VMS */ |
a687059c | 1238 | |
79072805 | 1239 | I32 |
8ac85365 | 1240 | ingroup(I32 testgid, I32 effective) |
a687059c LW |
1241 | { |
1242 | if (testgid == (effective ? egid : gid)) | |
1243 | return TRUE; | |
fe14fcc3 | 1244 | #ifdef HAS_GETGROUPS |
a687059c LW |
1245 | #ifndef NGROUPS |
1246 | #define NGROUPS 32 | |
1247 | #endif | |
1248 | { | |
a0d0e21e | 1249 | Groups_t gary[NGROUPS]; |
79072805 | 1250 | I32 anum; |
a687059c LW |
1251 | |
1252 | anum = getgroups(NGROUPS,gary); | |
1253 | while (--anum >= 0) | |
1254 | if (gary[anum] == testgid) | |
1255 | return TRUE; | |
1256 | } | |
1257 | #endif | |
1258 | return FALSE; | |
1259 | } | |
c2ab57d4 | 1260 | |
fe14fcc3 | 1261 | #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM) |
c2ab57d4 | 1262 | |
79072805 | 1263 | I32 |
8ac85365 | 1264 | do_ipcget(I32 optype, SV **mark, SV **sp) |
c2ab57d4 | 1265 | { |
11343788 | 1266 | dTHR; |
c2ab57d4 | 1267 | key_t key; |
79072805 | 1268 | I32 n, flags; |
c2ab57d4 | 1269 | |
463ee0b2 LW |
1270 | key = (key_t)SvNVx(*++mark); |
1271 | n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark); | |
1272 | flags = SvIVx(*++mark); | |
748a9306 | 1273 | SETERRNO(0,0); |
c2ab57d4 LW |
1274 | switch (optype) |
1275 | { | |
fe14fcc3 | 1276 | #ifdef HAS_MSG |
79072805 | 1277 | case OP_MSGGET: |
c2ab57d4 | 1278 | return msgget(key, flags); |
e5d73d77 | 1279 | #endif |
fe14fcc3 | 1280 | #ifdef HAS_SEM |
79072805 | 1281 | case OP_SEMGET: |
c2ab57d4 | 1282 | return semget(key, n, flags); |
e5d73d77 | 1283 | #endif |
fe14fcc3 | 1284 | #ifdef HAS_SHM |
79072805 | 1285 | case OP_SHMGET: |
c2ab57d4 | 1286 | return shmget(key, n, flags); |
e5d73d77 | 1287 | #endif |
fe14fcc3 | 1288 | #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM) |
e5d73d77 | 1289 | default: |
c07a80fd | 1290 | croak("%s not implemented", op_desc[optype]); |
e5d73d77 | 1291 | #endif |
c2ab57d4 LW |
1292 | } |
1293 | return -1; /* should never happen */ | |
1294 | } | |
1295 | ||
79072805 | 1296 | I32 |
8ac85365 | 1297 | do_ipcctl(I32 optype, SV **mark, SV **sp) |
c2ab57d4 | 1298 | { |
11343788 | 1299 | dTHR; |
79072805 | 1300 | SV *astr; |
c2ab57d4 | 1301 | char *a; |
a0d0e21e LW |
1302 | I32 id, n, cmd, infosize, getinfo; |
1303 | I32 ret = -1; | |
3e3baf6d TB |
1304 | #ifdef __linux__ /* XXX Need metaconfig test */ |
1305 | union semun unsemds; | |
1306 | #endif | |
c2ab57d4 | 1307 | |
463ee0b2 LW |
1308 | id = SvIVx(*++mark); |
1309 | n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0; | |
1310 | cmd = SvIVx(*++mark); | |
79072805 | 1311 | astr = *++mark; |
c2ab57d4 LW |
1312 | infosize = 0; |
1313 | getinfo = (cmd == IPC_STAT); | |
1314 | ||
1315 | switch (optype) | |
1316 | { | |
fe14fcc3 | 1317 | #ifdef HAS_MSG |
79072805 | 1318 | case OP_MSGCTL: |
c2ab57d4 LW |
1319 | if (cmd == IPC_STAT || cmd == IPC_SET) |
1320 | infosize = sizeof(struct msqid_ds); | |
1321 | break; | |
e5d73d77 | 1322 | #endif |
fe14fcc3 | 1323 | #ifdef HAS_SHM |
79072805 | 1324 | case OP_SHMCTL: |
c2ab57d4 LW |
1325 | if (cmd == IPC_STAT || cmd == IPC_SET) |
1326 | infosize = sizeof(struct shmid_ds); | |
1327 | break; | |
e5d73d77 | 1328 | #endif |
fe14fcc3 | 1329 | #ifdef HAS_SEM |
79072805 | 1330 | case OP_SEMCTL: |
c2ab57d4 LW |
1331 | if (cmd == IPC_STAT || cmd == IPC_SET) |
1332 | infosize = sizeof(struct semid_ds); | |
1333 | else if (cmd == GETALL || cmd == SETALL) | |
1334 | { | |
8e591e46 | 1335 | struct semid_ds semds; |
3e3baf6d | 1336 | #ifdef __linux__ /* XXX Need metaconfig test */ |
84902520 TB |
1337 | /* linux (and Solaris2?) uses : |
1338 | int semctl (int semid, int semnum, int cmd, union semun arg) | |
3e3baf6d TB |
1339 | union semun { |
1340 | int val; | |
1341 | struct semid_ds *buf; | |
1342 | ushort *array; | |
1343 | }; | |
1344 | */ | |
84902520 TB |
1345 | union semun semun; |
1346 | semun.buf = &semds; | |
1347 | if (semctl(id, 0, IPC_STAT, semun) == -1) | |
3e3baf6d | 1348 | #else |
c2ab57d4 | 1349 | if (semctl(id, 0, IPC_STAT, &semds) == -1) |
3e3baf6d | 1350 | #endif |
c2ab57d4 LW |
1351 | return -1; |
1352 | getinfo = (cmd == GETALL); | |
6e21c824 LW |
1353 | infosize = semds.sem_nsems * sizeof(short); |
1354 | /* "short" is technically wrong but much more portable | |
1355 | than guessing about u_?short(_t)? */ | |
c2ab57d4 LW |
1356 | } |
1357 | break; | |
e5d73d77 | 1358 | #endif |
fe14fcc3 | 1359 | #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM) |
e5d73d77 | 1360 | default: |
c07a80fd | 1361 | croak("%s not implemented", op_desc[optype]); |
e5d73d77 | 1362 | #endif |
c2ab57d4 LW |
1363 | } |
1364 | ||
1365 | if (infosize) | |
1366 | { | |
a0d0e21e | 1367 | STRLEN len; |
c2ab57d4 LW |
1368 | if (getinfo) |
1369 | { | |
a0d0e21e LW |
1370 | SvPV_force(astr, len); |
1371 | a = SvGROW(astr, infosize+1); | |
c2ab57d4 LW |
1372 | } |
1373 | else | |
1374 | { | |
463ee0b2 LW |
1375 | a = SvPV(astr, len); |
1376 | if (len != infosize) | |
9607fc9c | 1377 | croak("Bad arg length for %s, is %lu, should be %ld", |
1378 | op_desc[optype], (unsigned long)len, (long)infosize); | |
c2ab57d4 LW |
1379 | } |
1380 | } | |
1381 | else | |
1382 | { | |
c030ccd9 | 1383 | IV i = SvIV(astr); |
c2ab57d4 LW |
1384 | a = (char *)i; /* ouch */ |
1385 | } | |
748a9306 | 1386 | SETERRNO(0,0); |
c2ab57d4 LW |
1387 | switch (optype) |
1388 | { | |
fe14fcc3 | 1389 | #ifdef HAS_MSG |
79072805 | 1390 | case OP_MSGCTL: |
bee1dbe2 | 1391 | ret = msgctl(id, cmd, (struct msqid_ds *)a); |
c2ab57d4 | 1392 | break; |
e5d73d77 | 1393 | #endif |
fe14fcc3 | 1394 | #ifdef HAS_SEM |
79072805 | 1395 | case OP_SEMCTL: |
3e3baf6d TB |
1396 | #ifdef __linux__ /* XXX Need metaconfig test */ |
1397 | unsemds.buf = (struct semid_ds *)a; | |
1398 | ret = semctl(id, n, cmd, unsemds); | |
1399 | #else | |
79072805 | 1400 | ret = semctl(id, n, cmd, (struct semid_ds *)a); |
3e3baf6d | 1401 | #endif |
c2ab57d4 | 1402 | break; |
e5d73d77 | 1403 | #endif |
fe14fcc3 | 1404 | #ifdef HAS_SHM |
79072805 | 1405 | case OP_SHMCTL: |
bee1dbe2 | 1406 | ret = shmctl(id, cmd, (struct shmid_ds *)a); |
c2ab57d4 | 1407 | break; |
e5d73d77 | 1408 | #endif |
c2ab57d4 LW |
1409 | } |
1410 | if (getinfo && ret >= 0) { | |
79072805 LW |
1411 | SvCUR_set(astr, infosize); |
1412 | *SvEND(astr) = '\0'; | |
a0d0e21e | 1413 | SvSETMAGIC(astr); |
c2ab57d4 LW |
1414 | } |
1415 | return ret; | |
1416 | } | |
1417 | ||
79072805 | 1418 | I32 |
8ac85365 | 1419 | do_msgsnd(SV **mark, SV **sp) |
c2ab57d4 | 1420 | { |
fe14fcc3 | 1421 | #ifdef HAS_MSG |
11343788 | 1422 | dTHR; |
79072805 | 1423 | SV *mstr; |
c2ab57d4 | 1424 | char *mbuf; |
79072805 | 1425 | I32 id, msize, flags; |
463ee0b2 | 1426 | STRLEN len; |
c2ab57d4 | 1427 | |
463ee0b2 | 1428 | id = SvIVx(*++mark); |
79072805 | 1429 | mstr = *++mark; |
463ee0b2 LW |
1430 | flags = SvIVx(*++mark); |
1431 | mbuf = SvPV(mstr, len); | |
1432 | if ((msize = len - sizeof(long)) < 0) | |
1433 | croak("Arg too short for msgsnd"); | |
748a9306 | 1434 | SETERRNO(0,0); |
bee1dbe2 | 1435 | return msgsnd(id, (struct msgbuf *)mbuf, msize, flags); |
e5d73d77 | 1436 | #else |
463ee0b2 | 1437 | croak("msgsnd not implemented"); |
e5d73d77 | 1438 | #endif |
c2ab57d4 LW |
1439 | } |
1440 | ||
79072805 | 1441 | I32 |
8ac85365 | 1442 | do_msgrcv(SV **mark, SV **sp) |
c2ab57d4 | 1443 | { |
fe14fcc3 | 1444 | #ifdef HAS_MSG |
11343788 | 1445 | dTHR; |
79072805 | 1446 | SV *mstr; |
c2ab57d4 LW |
1447 | char *mbuf; |
1448 | long mtype; | |
79072805 | 1449 | I32 id, msize, flags, ret; |
463ee0b2 | 1450 | STRLEN len; |
79072805 | 1451 | |
463ee0b2 | 1452 | id = SvIVx(*++mark); |
79072805 | 1453 | mstr = *++mark; |
463ee0b2 LW |
1454 | msize = SvIVx(*++mark); |
1455 | mtype = (long)SvIVx(*++mark); | |
1456 | flags = SvIVx(*++mark); | |
ed6116ce LW |
1457 | if (SvTHINKFIRST(mstr)) { |
1458 | if (SvREADONLY(mstr)) | |
1459 | croak("Can't msgrcv to readonly var"); | |
1460 | if (SvROK(mstr)) | |
1461 | sv_unref(mstr); | |
1462 | } | |
a0d0e21e LW |
1463 | SvPV_force(mstr, len); |
1464 | mbuf = SvGROW(mstr, sizeof(long)+msize+1); | |
1465 | ||
748a9306 | 1466 | SETERRNO(0,0); |
bee1dbe2 | 1467 | ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags); |
c2ab57d4 | 1468 | if (ret >= 0) { |
79072805 LW |
1469 | SvCUR_set(mstr, sizeof(long)+ret); |
1470 | *SvEND(mstr) = '\0'; | |
c2ab57d4 LW |
1471 | } |
1472 | return ret; | |
e5d73d77 | 1473 | #else |
463ee0b2 | 1474 | croak("msgrcv not implemented"); |
e5d73d77 | 1475 | #endif |
c2ab57d4 LW |
1476 | } |
1477 | ||
79072805 | 1478 | I32 |
8ac85365 | 1479 | do_semop(SV **mark, SV **sp) |
c2ab57d4 | 1480 | { |
fe14fcc3 | 1481 | #ifdef HAS_SEM |
11343788 | 1482 | dTHR; |
79072805 | 1483 | SV *opstr; |
c2ab57d4 | 1484 | char *opbuf; |
463ee0b2 LW |
1485 | I32 id; |
1486 | STRLEN opsize; | |
c2ab57d4 | 1487 | |
463ee0b2 | 1488 | id = SvIVx(*++mark); |
79072805 | 1489 | opstr = *++mark; |
463ee0b2 | 1490 | opbuf = SvPV(opstr, opsize); |
c2ab57d4 LW |
1491 | if (opsize < sizeof(struct sembuf) |
1492 | || (opsize % sizeof(struct sembuf)) != 0) { | |
748a9306 | 1493 | SETERRNO(EINVAL,LIB$_INVARG); |
c2ab57d4 LW |
1494 | return -1; |
1495 | } | |
748a9306 | 1496 | SETERRNO(0,0); |
6e21c824 | 1497 | return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf)); |
e5d73d77 | 1498 | #else |
463ee0b2 | 1499 | croak("semop not implemented"); |
e5d73d77 | 1500 | #endif |
c2ab57d4 LW |
1501 | } |
1502 | ||
79072805 | 1503 | I32 |
8ac85365 | 1504 | do_shmio(I32 optype, SV **mark, SV **sp) |
c2ab57d4 | 1505 | { |
fe14fcc3 | 1506 | #ifdef HAS_SHM |
11343788 | 1507 | dTHR; |
79072805 | 1508 | SV *mstr; |
c2ab57d4 | 1509 | char *mbuf, *shm; |
79072805 | 1510 | I32 id, mpos, msize; |
463ee0b2 | 1511 | STRLEN len; |
c2ab57d4 | 1512 | struct shmid_ds shmds; |
c2ab57d4 | 1513 | |
463ee0b2 | 1514 | id = SvIVx(*++mark); |
79072805 | 1515 | mstr = *++mark; |
463ee0b2 LW |
1516 | mpos = SvIVx(*++mark); |
1517 | msize = SvIVx(*++mark); | |
748a9306 | 1518 | SETERRNO(0,0); |
c2ab57d4 LW |
1519 | if (shmctl(id, IPC_STAT, &shmds) == -1) |
1520 | return -1; | |
1521 | if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) { | |
748a9306 | 1522 | SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */ |
c2ab57d4 LW |
1523 | return -1; |
1524 | } | |
8ac85365 | 1525 | shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0); |
c2ab57d4 LW |
1526 | if (shm == (char *)-1) /* I hate System V IPC, I really do */ |
1527 | return -1; | |
79072805 | 1528 | if (optype == OP_SHMREAD) { |
a0d0e21e LW |
1529 | SvPV_force(mstr, len); |
1530 | mbuf = SvGROW(mstr, msize+1); | |
1531 | ||
bee1dbe2 | 1532 | Copy(shm + mpos, mbuf, msize, char); |
79072805 LW |
1533 | SvCUR_set(mstr, msize); |
1534 | *SvEND(mstr) = '\0'; | |
a0d0e21e | 1535 | SvSETMAGIC(mstr); |
c2ab57d4 LW |
1536 | } |
1537 | else { | |
79072805 | 1538 | I32 n; |
c2ab57d4 | 1539 | |
a0d0e21e | 1540 | mbuf = SvPV(mstr, len); |
463ee0b2 | 1541 | if ((n = len) > msize) |
c2ab57d4 | 1542 | n = msize; |
bee1dbe2 | 1543 | Copy(mbuf, shm + mpos, n, char); |
c2ab57d4 | 1544 | if (n < msize) |
bee1dbe2 | 1545 | memzero(shm + mpos + n, msize - n); |
c2ab57d4 LW |
1546 | } |
1547 | return shmdt(shm); | |
e5d73d77 | 1548 | #else |
463ee0b2 | 1549 | croak("shm I/O not implemented"); |
e5d73d77 | 1550 | #endif |
c2ab57d4 LW |
1551 | } |
1552 | ||
fe14fcc3 | 1553 | #endif /* SYSV IPC */ |
4e35701f | 1554 |