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