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