1
|
1 // file-io.cc -*- C++ -*- |
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1993, 1994, 1995 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
21 |
|
22 */ |
|
23 |
|
24 // Written by John C. Campbell <jcc@che.utexas.edu>. |
1230
|
25 // |
|
26 // Thomas Baier <baier@ci.tuwien.ac.at> added the following functions: |
|
27 // |
|
28 // popen pclose execute sync_system async_system |
|
29 // waitpid mkfifo unlink |
|
30 |
1
|
31 |
240
|
32 #ifdef HAVE_CONFIG_H |
1230
|
33 #include <config.h> |
1
|
34 #endif |
|
35 |
1343
|
36 #include <cstring> |
|
37 #include <cstdio> |
|
38 #include <cerrno> |
|
39 #include <cstdlib> |
|
40 #include <cctype> |
|
41 |
1350
|
42 #include <strstream.h> |
|
43 |
|
44 #ifdef HAVE_UNISTD_H |
1230
|
45 #include <sys/types.h> |
1
|
46 #include <unistd.h> |
1343
|
47 #endif |
1350
|
48 |
1230
|
49 #include <fcntl.h> |
1343
|
50 |
1230
|
51 #include <DLList.h> |
|
52 |
456
|
53 #include "dMatrix.h" |
444
|
54 |
1352
|
55 #include "defun.h" |
1
|
56 #include "error.h" |
1352
|
57 #include "file-io.h" |
542
|
58 #include "help.h" |
1352
|
59 #include "input.h" |
|
60 #include "mappers.h" |
|
61 #include "octave-hist.h" |
1
|
62 #include "pager.h" |
1352
|
63 #include "statdefs.h" |
444
|
64 #include "sysdep.h" |
1352
|
65 #include "syswait.h" |
|
66 #include "tree-const.h" |
|
67 #include "utils.h" |
529
|
68 #include "variables.h" |
1
|
69 |
|
70 // keeps a count of args sent to printf or scanf |
|
71 static int fmt_arg_count = 0; |
|
72 |
759
|
73 // double linked list containing relevant information about open files |
|
74 static DLList <file_info> file_list; |
1
|
75 |
240
|
76 file_info::file_info (void) |
1
|
77 { |
334
|
78 file_number = -1; |
529
|
79 file_name = 0; |
|
80 file_fptr = 0; |
|
81 file_mode = 0; |
1
|
82 } |
|
83 |
240
|
84 file_info::file_info (int n, const char *nm, FILE *t, const char *md) |
|
85 { |
334
|
86 file_number = n; |
|
87 file_name = strsave (nm); |
|
88 file_fptr = t; |
|
89 file_mode = strsave (md); |
240
|
90 } |
|
91 |
|
92 file_info::file_info (const file_info& f) |
1
|
93 { |
367
|
94 file_number = f.file_number; |
|
95 file_name = strsave (f.file_name); |
|
96 file_fptr = f.file_fptr; |
|
97 file_mode = strsave (f.file_mode); |
1
|
98 } |
|
99 |
240
|
100 file_info& |
|
101 file_info::operator = (const file_info& f) |
1
|
102 { |
240
|
103 if (this != & f) |
|
104 { |
367
|
105 file_number = f.file_number; |
334
|
106 delete [] file_name; |
|
107 file_name = strsave (f.file_name); |
|
108 file_fptr = f.file_fptr; |
|
109 delete [] file_mode; |
|
110 file_mode = strsave (f.file_mode); |
240
|
111 } |
1
|
112 return *this; |
|
113 } |
|
114 |
240
|
115 file_info::~file_info (void) |
1
|
116 { |
334
|
117 delete [] file_name; |
|
118 delete [] file_mode; |
1
|
119 } |
|
120 |
|
121 int |
240
|
122 file_info::number (void) const |
1
|
123 { |
334
|
124 return file_number; |
1
|
125 } |
|
126 |
240
|
127 const char * |
|
128 file_info::name (void) const |
1
|
129 { |
334
|
130 return file_name; |
1
|
131 } |
|
132 |
|
133 FILE * |
240
|
134 file_info::fptr (void) const |
1
|
135 { |
334
|
136 return file_fptr; |
1
|
137 } |
|
138 |
240
|
139 const char * |
|
140 file_info::mode (void) const |
1
|
141 { |
334
|
142 return file_mode; |
1
|
143 } |
|
144 |
|
145 void |
164
|
146 initialize_file_io (void) |
1
|
147 { |
334
|
148 file_info octave_stdin (0, "stdin", stdin, "r"); |
|
149 file_info octave_stdout (1, "stdout", stdout, "w"); |
|
150 file_info octave_stderr (2, "stderr", stderr, "w"); |
1
|
151 |
334
|
152 file_list.append (octave_stdin); |
|
153 file_list.append (octave_stdout); |
|
154 file_list.append (octave_stderr); |
1
|
155 } |
|
156 |
761
|
157 // Given a file name or number, return a pointer to the corresponding |
|
158 // open file. If the file has not already been opened, return NULL. |
|
159 |
1
|
160 Pix |
164
|
161 return_valid_file (const tree_constant& arg) |
1
|
162 { |
610
|
163 if (arg.is_string ()) |
1
|
164 { |
|
165 Pix p = file_list.first (); |
240
|
166 file_info file; |
896
|
167 int file_count = file_list.length (); |
1
|
168 for (int i = 0; i < file_count; i++) |
|
169 { |
|
170 char *file_name = arg.string_value (); |
|
171 file = file_list (p); |
|
172 if (strcmp (file.name (), file_name) == 0) |
|
173 return p; |
|
174 file_list.next (p); |
|
175 } |
|
176 } |
636
|
177 else |
1
|
178 { |
|
179 double file_num = arg.double_value (); |
636
|
180 |
|
181 if (! error_state) |
1
|
182 { |
1086
|
183 if (D_NINT (file_num) != file_num) |
636
|
184 error ("file number not an integer value"); |
|
185 else |
1
|
186 { |
636
|
187 Pix p = file_list.first (); |
|
188 file_info file; |
896
|
189 int file_count = file_list.length (); |
636
|
190 for (int i = 0; i < file_count; i++) |
|
191 { |
|
192 file = file_list (p); |
|
193 if (file.number () == file_num) |
|
194 return p; |
|
195 file_list.next (p); |
|
196 } |
|
197 error ("no file with that number"); |
1
|
198 } |
|
199 } |
636
|
200 else |
|
201 error ("inapproriate file specifier"); |
|
202 } |
1
|
203 |
529
|
204 return 0; |
1
|
205 } |
|
206 |
|
207 static Pix |
636
|
208 fopen_file_for_user (const char *name, const char *mode, |
370
|
209 const char *warn_for) |
1
|
210 { |
636
|
211 FILE *file_ptr = fopen (name, mode); |
529
|
212 if (file_ptr) |
1
|
213 { |
896
|
214 int file_number = file_list.length () + 1; |
|
215 |
|
216 file_info file (file_number, name, file_ptr, mode); |
1
|
217 file_list.append (file); |
|
218 |
|
219 Pix p = file_list.first (); |
240
|
220 file_info file_from_list; |
896
|
221 int file_count = file_list.length (); |
1
|
222 for (int i = 0; i < file_count; i++) |
|
223 { |
|
224 file_from_list = file_list (p); |
636
|
225 if (strcmp (file_from_list.name (), name) == 0) |
1
|
226 return p; |
|
227 file_list.next (p); |
|
228 } |
|
229 } |
|
230 |
636
|
231 error ("%s: unable to open file `%s'", warn_for, name); |
240
|
232 |
529
|
233 return 0; |
1
|
234 } |
|
235 |
368
|
236 static Pix |
636
|
237 file_io_get_file (const tree_constant& arg, const char *mode, |
368
|
238 const char *warn_for) |
|
239 { |
|
240 Pix p = return_valid_file (arg); |
|
241 |
529
|
242 if (! p) |
368
|
243 { |
610
|
244 if (arg.is_string ()) |
368
|
245 { |
|
246 char *name = arg.string_value (); |
|
247 |
|
248 struct stat buffer; |
|
249 int status = stat (name, &buffer); |
|
250 |
372
|
251 if (status == 0) |
368
|
252 { |
|
253 if ((buffer.st_mode & S_IFREG) == S_IFREG) |
636
|
254 p = fopen_file_for_user (name, mode, warn_for); |
368
|
255 else |
|
256 error ("%s: invalid file type", warn_for); |
|
257 } |
372
|
258 else if (status < 0 && *mode != 'r') |
636
|
259 p = fopen_file_for_user (name, mode, warn_for); |
368
|
260 else |
|
261 error ("%s: can't stat file `%s'", warn_for, name); |
|
262 } |
|
263 else |
|
264 error ("%s: invalid file specifier", warn_for); |
|
265 } |
|
266 |
|
267 return p; |
|
268 } |
1
|
269 |
1181
|
270 static Octave_object |
497
|
271 fclose_internal (const Octave_object& args) |
1
|
272 { |
497
|
273 Octave_object retval; |
1
|
274 |
712
|
275 Pix p = return_valid_file (args(0)); |
1
|
276 |
529
|
277 if (! p) |
1
|
278 return retval; |
|
279 |
240
|
280 file_info file = file_list (p); |
1
|
281 |
|
282 if (file.number () < 3) |
|
283 { |
|
284 warning ("fclose: can't close stdin, stdout, or stderr!"); |
|
285 return retval; |
|
286 } |
|
287 |
|
288 int success = fclose (file.fptr ()); |
|
289 file_list.del (p); |
|
290 |
|
291 if (success == 0) |
636
|
292 retval(0) = 1.0; // succeeded |
1
|
293 else |
|
294 { |
|
295 error ("fclose: error on closing file"); |
636
|
296 retval(0) = 0.0; // failed |
1
|
297 } |
|
298 |
|
299 return retval; |
|
300 } |
|
301 |
1181
|
302 DEFUN ("fclose", Ffclose, Sfclose, 1, 1, |
1230
|
303 "fclose (FILENAME or FILENUM): close a file") |
529
|
304 { |
|
305 Octave_object retval; |
|
306 |
|
307 int nargin = args.length (); |
|
308 |
712
|
309 if (nargin != 1) |
1181
|
310 print_usage ("fclose"); |
529
|
311 else |
1181
|
312 retval = fclose_internal (args); |
529
|
313 |
|
314 return retval; |
|
315 } |
|
316 |
1181
|
317 static Octave_object |
497
|
318 fflush_internal (const Octave_object& args) |
1
|
319 { |
497
|
320 Octave_object retval; |
1
|
321 |
712
|
322 Pix p = return_valid_file (args(0)); |
1
|
323 |
529
|
324 if (! p) |
1
|
325 return retval; |
|
326 |
240
|
327 file_info file = file_list (p); |
1
|
328 |
|
329 if (strcmp (file.mode (), "r") == 0) |
|
330 { |
|
331 warning ("can't flush an input stream"); |
|
332 return retval; |
|
333 } |
|
334 |
|
335 int success = 0; |
240
|
336 |
1
|
337 if (file.number () == 1) |
|
338 flush_output_to_pager (); |
|
339 else |
|
340 success = fflush (file.fptr ()); |
|
341 |
|
342 if (success == 0) |
636
|
343 retval(0) = 1.0; // succeeded |
1
|
344 else |
|
345 { |
|
346 error ("fflush: write error"); |
636
|
347 retval(0) = 0.0; // failed |
1
|
348 } |
|
349 |
|
350 return retval; |
|
351 } |
|
352 |
1181
|
353 DEFUN ("fflush", Ffflush, Sfflush, 1, 1, |
|
354 "fflush (FILENAME or FILENUM): flush buffered data to output file") |
|
355 { |
|
356 Octave_object retval; |
|
357 |
|
358 int nargin = args.length (); |
|
359 |
|
360 if (nargin != 1) |
|
361 print_usage ("fflush"); |
|
362 else |
|
363 retval = fflush_internal (args); |
|
364 |
|
365 return retval; |
|
366 } |
|
367 |
1
|
368 static int |
164
|
369 valid_mode (const char *mode) |
1
|
370 { |
529
|
371 if (mode) |
1
|
372 { |
|
373 char m = mode[0]; |
|
374 if (m == 'r' || m == 'w' || m == 'a') |
|
375 { |
|
376 m = mode[1]; |
|
377 return (m == '\0' || (m == '+' && mode[2] == '\0')); |
|
378 } |
|
379 } |
|
380 return 0; |
|
381 } |
|
382 |
1181
|
383 static Octave_object |
1339
|
384 fgets_internal (const Octave_object& args, int nargin, int nargout, |
|
385 int strip_final_newline = 0) |
1
|
386 { |
497
|
387 Octave_object retval; |
1
|
388 |
712
|
389 Pix p = file_io_get_file (args(0), "r", "fgets"); |
1
|
390 |
529
|
391 if (! p) |
368
|
392 return retval; |
|
393 |
1339
|
394 int length = 0; |
1338
|
395 |
|
396 if (nargin == 2) |
1
|
397 { |
1338
|
398 double dlen = args(1).double_value (); |
|
399 |
|
400 if (error_state) |
|
401 return retval; |
|
402 |
|
403 if (xisnan (dlen)) |
|
404 { |
|
405 error ("fgets: NaN invalid as length"); |
|
406 return retval; |
|
407 } |
|
408 |
1339
|
409 length = NINT (dlen); |
1338
|
410 |
|
411 if ((double) length != dlen) |
|
412 { |
|
413 error ("fgets: length not an integer value"); |
|
414 return retval; |
|
415 } |
|
416 |
1339
|
417 if (length < 0) |
|
418 { |
|
419 error ("fgets: length must be a nonnegative integer"); |
|
420 return retval; |
|
421 } |
1
|
422 } |
1339
|
423 |
|
424 file_info file = file_list (p); |
|
425 FILE *fileptr = file.fptr (); |
|
426 |
|
427 ostrstream buf; |
|
428 int c; |
|
429 int count = 0; |
|
430 int newline_stripped = 0; |
|
431 |
|
432 if (nargin == 1 || length > 0) |
1
|
433 { |
1339
|
434 while ((c = fgetc (fileptr)) != EOF) |
1338
|
435 { |
1339
|
436 count++; |
1338
|
437 if (c == '\n') |
|
438 { |
1339
|
439 if (! strip_final_newline) |
|
440 buf << (char) c; |
|
441 else |
|
442 newline_stripped = 1; |
|
443 |
1338
|
444 break; |
|
445 } |
1339
|
446 else |
|
447 buf << (char) c; |
|
448 |
|
449 if (nargin == 2 && count == length) |
|
450 break; |
1338
|
451 } |
1
|
452 } |
1339
|
453 |
|
454 buf << ends; |
|
455 char *string = buf.str (); |
|
456 |
|
457 if (count) |
1338
|
458 { |
|
459 if (nargout == 2) |
1339
|
460 retval(1) = (double) (count - newline_stripped); |
1338
|
461 |
|
462 retval(0) = string; |
|
463 } |
|
464 else |
|
465 retval(0) = -1.0; |
|
466 |
|
467 delete [] string; |
1
|
468 |
|
469 return retval; |
|
470 } |
|
471 |
1339
|
472 DEFUN ("fgetl", Ffgetl, Sfgetl, 2, 2, |
|
473 "[STRING, LENGTH] = fgetl (FILENAME or FILENUM [, LENGTH])\n\ |
|
474 \n\ |
|
475 read a string from a file") |
|
476 { |
|
477 Octave_object retval; |
|
478 |
|
479 int nargin = args.length (); |
|
480 |
|
481 if (nargin == 1 || nargin == 2) |
|
482 retval = fgets_internal (args, nargin, nargout, 1); |
|
483 else |
|
484 print_usage ("fgetl"); |
|
485 |
|
486 return retval; |
|
487 } |
|
488 |
1181
|
489 DEFUN ("fgets", Ffgets, Sfgets, 2, 2, |
1339
|
490 "[STRING, LENGTH] = fgets (FILENAME or FILENUM [, LENGTH])\n\ |
529
|
491 \n\ |
1181
|
492 read a string from a file") |
529
|
493 { |
|
494 Octave_object retval; |
|
495 |
|
496 int nargin = args.length (); |
|
497 |
1338
|
498 if (nargin == 1 || nargin == 2) |
|
499 retval = fgets_internal (args, nargin, nargout); |
|
500 else |
1181
|
501 print_usage ("fgets"); |
529
|
502 |
|
503 return retval; |
|
504 } |
|
505 |
1181
|
506 static Octave_object |
497
|
507 fopen_internal (const Octave_object& args) |
1
|
508 { |
497
|
509 Octave_object retval; |
1
|
510 Pix p; |
|
511 |
712
|
512 if (! args(0).is_string ()) |
1
|
513 { |
|
514 error ("fopen: file name must be a string"); |
|
515 return retval; |
|
516 } |
|
517 |
712
|
518 p = return_valid_file (args(0)); |
1
|
519 |
529
|
520 if (p) |
1
|
521 { |
240
|
522 file_info file = file_list (p); |
1
|
523 |
636
|
524 retval(0) = (double) file.number (); |
1
|
525 |
|
526 return retval; |
|
527 } |
|
528 |
712
|
529 if (! args(1).is_string ()) |
1
|
530 { |
370
|
531 error ("fopen: file mode must be a string"); |
1
|
532 return retval; |
|
533 } |
|
534 |
712
|
535 char *name = args(0).string_value (); |
|
536 char *mode = args(1).string_value (); |
1
|
537 |
|
538 if (! valid_mode (mode)) |
|
539 { |
|
540 error ("fopen: invalid mode"); |
|
541 return retval; |
|
542 } |
|
543 |
|
544 struct stat buffer; |
|
545 if (stat (name, &buffer) == 0 && (buffer.st_mode & S_IFDIR) == S_IFDIR) |
|
546 { |
|
547 error ("fopen: can't open directory"); |
|
548 return retval; |
|
549 } |
|
550 |
|
551 FILE *file_ptr = fopen (name, mode); |
|
552 |
529
|
553 if (! file_ptr) |
1
|
554 { |
370
|
555 error ("fopen: unable to open file `%s'", name); |
1
|
556 return retval; |
|
557 } |
|
558 |
896
|
559 int file_number = file_list.length () + 1; |
1
|
560 |
896
|
561 file_info file (file_number, name, file_ptr, mode); |
1
|
562 file_list.append (file); |
|
563 |
896
|
564 retval(0) = (double) file_number; |
1
|
565 |
|
566 return retval; |
|
567 } |
|
568 |
1181
|
569 DEFUN ("fopen", Ffopen, Sfopen, 2, 1, |
|
570 "FILENUM = fopen (FILENAME, MODE): open a file\n\ |
|
571 \n\ |
|
572 Valid values for mode include:\n\ |
|
573 \n\ |
|
574 r : open text file for reading\n\ |
|
575 w : open text file for writing; discard previous contents if any\n\ |
|
576 a : append; open or create text file for writing at end of file\n\ |
|
577 r+ : open text file for update (i.e., reading and writing)\n\ |
|
578 w+ : create text file for update; discard previous contents if any\n\ |
|
579 a+ : append; open or create text file for update, writing at end\n\n\ |
|
580 Update mode permits reading from and writing to the same file.") |
529
|
581 { |
|
582 Octave_object retval; |
|
583 |
|
584 int nargin = args.length (); |
|
585 |
1181
|
586 if (nargin != 2) |
|
587 print_usage ("fopen"); |
|
588 else |
|
589 retval = fopen_internal (args); |
529
|
590 |
|
591 return retval; |
|
592 } |
|
593 |
1181
|
594 static Octave_object |
164
|
595 freport_internal (void) |
1
|
596 { |
497
|
597 Octave_object retval; |
1
|
598 Pix p = file_list.first (); |
|
599 |
|
600 ostrstream output_buf; |
|
601 |
|
602 output_buf << "\n number mode name\n\n"; |
896
|
603 |
|
604 int file_count = file_list.length (); |
1
|
605 for (int i = 0; i < file_count; i++) |
|
606 { |
240
|
607 file_info file = file_list (p); |
1
|
608 output_buf.form ("%7d%6s %s\n", file.number (), file.mode (), |
|
609 file.name ()); |
|
610 file_list.next (p); |
|
611 } |
|
612 |
|
613 output_buf << "\n" << ends; |
|
614 maybe_page_output (output_buf); |
|
615 |
|
616 return retval; |
|
617 } |
|
618 |
1181
|
619 DEFUN ("freport", Ffreport, Sfreport, 0, 1, |
|
620 "freport (): list open files and their status") |
|
621 { |
|
622 Octave_object retval; |
|
623 |
|
624 int nargin = args.length (); |
|
625 |
|
626 if (nargin > 0) |
|
627 warning ("freport: ignoring extra arguments"); |
|
628 |
|
629 retval = freport_internal (); |
|
630 |
|
631 return retval; |
|
632 } |
|
633 |
|
634 static Octave_object |
|
635 frewind_internal (const Octave_object& args) |
|
636 { |
|
637 Octave_object retval; |
|
638 |
|
639 Pix p = file_io_get_file (args(0), "a+", "frewind"); |
|
640 |
|
641 if (p) |
|
642 { |
|
643 file_info file = file_list (p); |
|
644 rewind (file.fptr ()); |
|
645 } |
|
646 |
|
647 return retval; |
|
648 } |
|
649 |
712
|
650 DEFUN ("frewind", Ffrewind, Sfrewind, 1, 1, |
529
|
651 "frewind (FILENAME or FILENUM): set file position at beginning of file") |
|
652 { |
|
653 Octave_object retval; |
|
654 |
|
655 int nargin = args.length (); |
|
656 |
712
|
657 if (nargin != 1) |
529
|
658 print_usage ("frewind"); |
|
659 else |
|
660 retval = frewind_internal (args); |
|
661 |
|
662 return retval; |
|
663 } |
|
664 |
1181
|
665 static Octave_object |
506
|
666 fseek_internal (const Octave_object& args) |
1
|
667 { |
497
|
668 Octave_object retval; |
1
|
669 |
506
|
670 int nargin = args.length (); |
|
671 |
712
|
672 Pix p = file_io_get_file (args(0), "a+", "fseek"); |
1
|
673 |
529
|
674 if (! p) |
368
|
675 return retval; |
1
|
676 |
|
677 long origin = SEEK_SET; |
636
|
678 |
712
|
679 double doff = args(1).double_value (); |
636
|
680 |
|
681 if (error_state) |
|
682 return retval; |
|
683 |
1086
|
684 if (xisnan (doff)) |
|
685 { |
|
686 error ("fseek: NaN invalid as offset"); |
|
687 return retval; |
|
688 } |
|
689 |
636
|
690 long offset = NINT (doff); |
|
691 |
|
692 if ((double) offset != doff) |
1
|
693 { |
636
|
694 error ("fseek: offset not an integer value"); |
|
695 return retval; |
|
696 } |
|
697 |
712
|
698 if (nargin == 3) |
636
|
699 { |
712
|
700 double dorig = args(2).double_value (); |
636
|
701 |
|
702 if (error_state) |
|
703 return retval; |
|
704 |
1086
|
705 if (xisnan (dorig)) |
|
706 { |
|
707 error ("fseek: NaN invalid as origin"); |
|
708 return retval; |
|
709 } |
|
710 |
636
|
711 origin = NINT (dorig); |
|
712 |
|
713 if ((double) dorig != origin) |
1
|
714 { |
636
|
715 error ("fseek: origin not an integer value"); |
1
|
716 return retval; |
|
717 } |
|
718 |
763
|
719 if (origin == 0) |
|
720 origin = SEEK_SET; |
|
721 else if (origin == 1) |
1
|
722 origin = SEEK_CUR; |
763
|
723 else if (origin == 2) |
1
|
724 origin = SEEK_END; |
|
725 else |
|
726 { |
636
|
727 error ("fseek: invalid value for origin"); |
|
728 return retval; |
1
|
729 } |
|
730 } |
|
731 |
240
|
732 file_info file = file_list (p); |
1
|
733 int success = fseek (file.fptr (), offset, origin); |
|
734 |
|
735 if (success == 0) |
636
|
736 retval(0) = 1.0; // succeeded |
1
|
737 else |
|
738 { |
|
739 error ("fseek: file error"); |
636
|
740 retval(0) = 0.0; // failed |
1
|
741 } |
|
742 |
|
743 return retval; |
|
744 } |
|
745 |
1181
|
746 DEFUN ("fseek", Ffseek, Sfseek, 3, 1, |
|
747 "fseek (FILENAME or FILENUM, OFFSET [, ORIGIN])\n\ |
|
748 \n\ |
|
749 set file position for reading or writing") |
529
|
750 { |
|
751 Octave_object retval; |
|
752 |
|
753 int nargin = args.length (); |
|
754 |
1181
|
755 if (nargin != 2 && nargin != 3) |
|
756 print_usage ("fseek"); |
529
|
757 else |
1181
|
758 retval = fseek_internal (args); |
529
|
759 |
|
760 return retval; |
|
761 } |
|
762 |
1181
|
763 // Tell current position of file. |
|
764 |
|
765 static Octave_object |
497
|
766 ftell_internal (const Octave_object& args) |
1
|
767 { |
497
|
768 Octave_object retval; |
1
|
769 |
712
|
770 Pix p = file_io_get_file (args(0), "a+", "ftell"); |
1
|
771 |
529
|
772 if (p) |
368
|
773 { |
|
774 file_info file = file_list (p); |
|
775 long offset = ftell (file.fptr ()); |
636
|
776 |
|
777 retval(0) = (double) offset; |
1
|
778 |
368
|
779 if (offset == -1L) |
|
780 error ("ftell: write error"); |
|
781 } |
1
|
782 |
|
783 return retval; |
|
784 } |
|
785 |
1181
|
786 DEFUN ("ftell", Fftell, Sftell, 1, 1, |
|
787 "POSITION = ftell (FILENAME or FILENUM): returns the current file position") |
|
788 { |
|
789 Octave_object retval; |
|
790 |
|
791 int nargin = args.length (); |
|
792 |
|
793 if (nargin != 1) |
|
794 print_usage ("ftell"); |
|
795 else |
|
796 retval = ftell_internal (args); |
|
797 |
|
798 return retval; |
|
799 } |
|
800 |
1
|
801 void |
164
|
802 close_files (void) |
1
|
803 { |
|
804 Pix p = file_list.first (); |
|
805 |
896
|
806 int file_count = file_list.length (); |
1
|
807 for (int i = 0; i < file_count; i++) |
|
808 { |
896
|
809 if (p) |
1
|
810 { |
896
|
811 file_info file = file_list (p); |
|
812 |
|
813 if (i > 2) // do not close stdin, stdout, stderr! |
|
814 { |
|
815 int success = fclose (file.fptr ()); |
|
816 if (success != 0) |
|
817 error ("closing %s", file.name ()); |
|
818 } |
|
819 |
|
820 file_list.del (p); |
1
|
821 } |
896
|
822 else |
|
823 { |
|
824 error ("inconsistent state for internal file list!"); |
|
825 break; |
|
826 } |
1
|
827 } |
|
828 } |
|
829 |
|
830 static int |
497
|
831 process_printf_format (const char *s, const Octave_object& args, |
506
|
832 ostrstream& sb, const char *type) |
1
|
833 { |
|
834 ostrstream fmt; |
|
835 |
506
|
836 int nargin = args.length (); |
|
837 |
1
|
838 fmt << "%"; // do_printf() already blew past this one... |
|
839 |
|
840 int chars_from_fmt_str = 0; |
|
841 |
|
842 again: |
|
843 switch (*s) |
|
844 { |
|
845 case '+': case '-': case ' ': case '0': case '#': |
|
846 chars_from_fmt_str++; |
|
847 fmt << *s++; |
|
848 goto again; |
|
849 |
|
850 case '\0': |
|
851 goto invalid_format; |
|
852 |
|
853 default: |
|
854 break; |
|
855 } |
|
856 |
|
857 if (*s == '*') |
|
858 { |
712
|
859 if (fmt_arg_count > nargin) |
1
|
860 { |
218
|
861 error ("%s: not enough arguments", type); |
1
|
862 return -1; |
|
863 } |
|
864 |
636
|
865 double tmp_len = args(fmt_arg_count++).double_value (); |
|
866 |
1086
|
867 if (error_state || xisnan (tmp_len)) |
1
|
868 { |
218
|
869 error ("%s: `*' must be replaced by an integer", type); |
1
|
870 return -1; |
|
871 } |
|
872 |
636
|
873 fmt << NINT (tmp_len); |
1
|
874 s++; |
|
875 chars_from_fmt_str++; |
|
876 } |
|
877 else |
|
878 { |
|
879 while (*s != '\0' && isdigit (*s)) |
|
880 { |
|
881 chars_from_fmt_str++; |
|
882 fmt << *s++; |
|
883 } |
|
884 } |
|
885 |
|
886 if (*s == '\0') |
|
887 goto invalid_format; |
|
888 |
|
889 if (*s == '.') |
|
890 { |
|
891 chars_from_fmt_str++; |
|
892 fmt << *s++; |
|
893 } |
|
894 |
|
895 if (*s == '*') |
|
896 { |
|
897 if (*(s-1) == '*') |
|
898 goto invalid_format; |
|
899 |
712
|
900 if (fmt_arg_count > nargin) |
1
|
901 { |
218
|
902 error ("%s: not enough arguments", type); |
1
|
903 return -1; |
|
904 } |
|
905 |
636
|
906 double tmp_len = args(fmt_arg_count++).double_value (); |
|
907 |
1086
|
908 if (error_state || xisnan (tmp_len)) |
1
|
909 { |
218
|
910 error ("%s: `*' must be replaced by an integer", type); |
1
|
911 return -1; |
|
912 } |
|
913 |
636
|
914 fmt << NINT (tmp_len); |
1
|
915 s++; |
|
916 chars_from_fmt_str++; |
|
917 } |
|
918 else |
|
919 { |
|
920 while (*s != '\0' && isdigit (*s)) |
|
921 { |
|
922 chars_from_fmt_str++; |
|
923 fmt << *s++; |
|
924 } |
|
925 } |
|
926 |
|
927 if (*s == '\0') |
|
928 goto invalid_format; |
|
929 |
|
930 if (*s != '\0' && (*s == 'h' || *s == 'l' || *s == 'L')) |
|
931 { |
|
932 chars_from_fmt_str++; |
|
933 fmt << *s++; |
|
934 } |
|
935 |
|
936 if (*s == '\0') |
|
937 goto invalid_format; |
|
938 |
712
|
939 if (fmt_arg_count > nargin) |
1
|
940 { |
218
|
941 error ("%s: not enough arguments", type); |
1
|
942 return -1; |
|
943 } |
|
944 |
|
945 switch (*s) |
|
946 { |
|
947 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': |
636
|
948 { |
|
949 double d = args(fmt_arg_count++).double_value (); |
1
|
950 |
1086
|
951 if (error_state || xisnan (d)) |
|
952 goto invalid_conversion; |
|
953 |
636
|
954 int val = NINT (d); |
|
955 |
1086
|
956 if ((double) val != d) |
636
|
957 goto invalid_conversion; |
|
958 else |
|
959 { |
|
960 chars_from_fmt_str++; |
|
961 fmt << *s << ends; |
|
962 char *tmp_fmt = fmt.str (); |
|
963 sb.form (tmp_fmt, val); |
|
964 delete [] tmp_fmt; |
|
965 return chars_from_fmt_str; |
|
966 } |
|
967 } |
1
|
968 |
|
969 case 'e': case 'E': case 'f': case 'g': case 'G': |
636
|
970 { |
|
971 double val = args(fmt_arg_count++).double_value (); |
1
|
972 |
636
|
973 if (error_state) |
|
974 goto invalid_conversion; |
|
975 else |
|
976 { |
|
977 chars_from_fmt_str++; |
|
978 fmt << *s << ends; |
|
979 char *tmp_fmt = fmt.str (); |
|
980 sb.form (tmp_fmt, val); |
|
981 delete [] tmp_fmt; |
|
982 return chars_from_fmt_str; |
|
983 } |
|
984 } |
1
|
985 |
|
986 case 's': |
636
|
987 { |
|
988 char *val = args(fmt_arg_count++).string_value (); |
1
|
989 |
636
|
990 if (error_state) |
|
991 goto invalid_conversion; |
|
992 else |
|
993 { |
|
994 chars_from_fmt_str++; |
|
995 fmt << *s << ends; |
|
996 char *tmp_fmt = fmt.str (); |
|
997 sb.form (tmp_fmt, val); |
|
998 delete [] tmp_fmt; |
|
999 return chars_from_fmt_str; |
|
1000 } |
|
1001 } |
1
|
1002 |
|
1003 case 'c': |
636
|
1004 { |
|
1005 char *val = args(fmt_arg_count++).string_value (); |
1
|
1006 |
636
|
1007 if (error_state || strlen (val) != 1) |
|
1008 goto invalid_conversion; |
|
1009 else |
|
1010 { |
|
1011 chars_from_fmt_str++; |
|
1012 fmt << *s << ends; |
|
1013 char *tmp_fmt = fmt.str (); |
|
1014 sb.form (tmp_fmt, *val); |
|
1015 delete [] tmp_fmt; |
|
1016 return chars_from_fmt_str; |
|
1017 } |
|
1018 } |
1
|
1019 |
|
1020 default: |
|
1021 goto invalid_format; |
|
1022 } |
|
1023 |
|
1024 invalid_conversion: |
218
|
1025 error ("%s: invalid conversion", type); |
1
|
1026 return -1; |
|
1027 |
|
1028 invalid_format: |
218
|
1029 error ("%s: invalid format", type); |
1
|
1030 return -1; |
|
1031 } |
|
1032 |
761
|
1033 // Formatted printing to a file. |
1
|
1034 |
1181
|
1035 static Octave_object |
506
|
1036 do_printf (const char *type, const Octave_object& args, int nargout) |
1
|
1037 { |
497
|
1038 Octave_object retval; |
712
|
1039 fmt_arg_count = 0; |
1
|
1040 char *fmt; |
240
|
1041 file_info file; |
1
|
1042 |
|
1043 if (strcmp (type, "fprintf") == 0) |
|
1044 { |
712
|
1045 Pix p = file_io_get_file (args(0), "a+", type); |
368
|
1046 |
529
|
1047 if (! p) |
368
|
1048 return retval; |
1
|
1049 |
|
1050 file = file_list (p); |
368
|
1051 |
1
|
1052 if (file.mode () == "r") |
|
1053 { |
|
1054 error ("%s: file is read only", type); |
|
1055 return retval; |
|
1056 } |
368
|
1057 |
712
|
1058 fmt = args(1).string_value (); |
368
|
1059 |
636
|
1060 if (error_state) |
|
1061 { |
|
1062 error ("%s: format must be a string", type); |
|
1063 return retval; |
|
1064 } |
|
1065 |
|
1066 fmt_arg_count += 2; |
1
|
1067 } |
|
1068 else |
|
1069 { |
712
|
1070 fmt = args(0).string_value (); |
636
|
1071 |
|
1072 if (error_state) |
|
1073 { |
|
1074 error ("%s: invalid format string", type); |
|
1075 return retval; |
|
1076 } |
|
1077 |
|
1078 fmt_arg_count++; |
1
|
1079 } |
|
1080 |
1358
|
1081 // Scan fmt for % escapes and print out the arguments. |
1
|
1082 |
|
1083 ostrstream output_buf; |
|
1084 |
|
1085 char *ptr = fmt; |
|
1086 |
|
1087 for (;;) |
|
1088 { |
|
1089 char c; |
|
1090 while ((c = *ptr++) != '\0' && c != '%') |
|
1091 output_buf << c; |
|
1092 |
|
1093 if (c == '\0') |
|
1094 break; |
|
1095 |
|
1096 if (*ptr == '%') |
|
1097 { |
|
1098 ptr++; |
|
1099 output_buf << c; |
|
1100 continue; |
|
1101 } |
|
1102 |
1358
|
1103 // We must be looking at a format specifier. Extract it or |
|
1104 // fail. |
1
|
1105 |
506
|
1106 int status = process_printf_format (ptr, args, output_buf, type); |
1
|
1107 |
|
1108 if (status < 0) |
|
1109 return retval; |
|
1110 |
|
1111 ptr += status; |
|
1112 } |
|
1113 |
|
1114 output_buf << ends; |
|
1115 if (strcmp (type, "printf") == 0 |
|
1116 || (strcmp (type, "fprintf") == 0 && file.number () == 1)) |
|
1117 { |
|
1118 maybe_page_output (output_buf); |
|
1119 } |
|
1120 else if (strcmp (type, "fprintf") == 0) |
|
1121 { |
|
1122 char *msg = output_buf.str (); |
|
1123 int success = fputs (msg, file.fptr ()); |
|
1124 if (success == EOF) |
218
|
1125 warning ("%s: unknown failure writing to file", type); |
1
|
1126 delete [] msg; |
|
1127 } |
|
1128 else if (strcmp (type, "sprintf") == 0) |
|
1129 { |
|
1130 char *msg = output_buf.str (); |
636
|
1131 retval(0) = msg; |
1
|
1132 delete [] msg; |
|
1133 } |
|
1134 |
|
1135 return retval; |
|
1136 } |
|
1137 |
1181
|
1138 DEFUN ("fprintf", Ffprintf, Sfprintf, -1, 1, |
|
1139 "fprintf (FILENAME or FILENUM, FORMAT, ...)") |
|
1140 { |
|
1141 Octave_object retval; |
|
1142 |
|
1143 int nargin = args.length (); |
|
1144 |
|
1145 if (nargin < 2) |
|
1146 print_usage ("fprintf"); |
|
1147 else |
|
1148 retval = do_printf ("fprintf", args, nargout); |
|
1149 |
|
1150 return retval; |
|
1151 } |
|
1152 |
|
1153 // Formatted printing. |
|
1154 |
|
1155 DEFUN ("printf", Fprintf, Sprintf, -1, 1, |
|
1156 "printf (FORMAT, ...)") |
|
1157 { |
|
1158 Octave_object retval; |
|
1159 |
|
1160 int nargin = args.length (); |
|
1161 |
|
1162 if (nargin < 1) |
|
1163 print_usage ("printf"); |
|
1164 else |
|
1165 retval = do_printf ("printf", args, nargout); |
|
1166 |
|
1167 return retval; |
|
1168 } |
|
1169 |
|
1170 // Formatted printing to a string. |
|
1171 |
|
1172 DEFUN ("sprintf", Fsprintf, Ssprintf, -1, 1, |
|
1173 "s = sprintf (FORMAT, ...)") |
|
1174 { |
|
1175 Octave_object retval; |
|
1176 |
|
1177 int nargin = args.length (); |
|
1178 |
|
1179 if (nargin < 1) |
|
1180 print_usage ("sprintf"); |
|
1181 else |
|
1182 retval = do_printf ("sprintf", args, nargout); |
|
1183 |
|
1184 return retval; |
|
1185 } |
|
1186 |
1
|
1187 static int |
368
|
1188 process_scanf_format (const char *s, ostrstream& fmt, |
|
1189 const char *type, int nargout, FILE* fptr, |
497
|
1190 Octave_object& values) |
1
|
1191 { |
|
1192 fmt << "%"; |
|
1193 |
|
1194 int chars_from_fmt_str = 0; |
|
1195 int store_value = 1; |
628
|
1196 int string_width = 0; |
1
|
1197 int success = 1; |
|
1198 |
|
1199 if (*s == '*') |
|
1200 { |
|
1201 store_value = 0; |
|
1202 s++; |
|
1203 chars_from_fmt_str++; |
|
1204 } |
|
1205 |
|
1206 if (isdigit (*s)) |
|
1207 { |
|
1208 ostrstream str_number; |
|
1209 while (*s != '\0' && isdigit (*s)) |
|
1210 { |
|
1211 chars_from_fmt_str++; |
|
1212 str_number << *s; |
|
1213 fmt << *s++; |
|
1214 } |
|
1215 str_number << ends; |
|
1216 char *number = str_number.str (); |
|
1217 string_width = atoi (number); |
|
1218 delete [] number; |
|
1219 } |
|
1220 |
|
1221 if (*s == '\0') |
|
1222 goto invalid_format; |
|
1223 |
|
1224 if (*s != '\0' && (*s == 'h' || *s == 'l' || *s == 'L')) |
|
1225 { |
|
1226 chars_from_fmt_str++; |
|
1227 s++; |
|
1228 } |
|
1229 |
|
1230 if (*s == '\0') |
|
1231 goto invalid_format; |
|
1232 |
1358
|
1233 // Even if we don't have a place to store them, attempt to convert |
|
1234 // everything specified by the format string. |
1
|
1235 |
712
|
1236 if (fmt_arg_count > (nargout ? nargout : 1)) |
368
|
1237 store_value = 0; |
1
|
1238 |
|
1239 switch (*s) |
|
1240 { |
|
1241 case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': |
|
1242 { |
|
1243 chars_from_fmt_str++; |
|
1244 fmt << *s << ends; |
|
1245 int temp; |
|
1246 char *str = fmt.str (); |
|
1247 success = fscanf (fptr, str, &temp); |
|
1248 delete [] str; |
|
1249 if (success > 0 && store_value) |
636
|
1250 values(fmt_arg_count++) = (double) temp; |
1
|
1251 } |
|
1252 break; |
628
|
1253 |
1
|
1254 case 'e': case 'E': case 'f': case 'g': case 'G': |
|
1255 { |
|
1256 chars_from_fmt_str++; |
|
1257 fmt << 'l' << *s << ends; |
|
1258 double temp; |
|
1259 char *str = fmt.str (); |
|
1260 success = fscanf (fptr, str, &temp); |
|
1261 delete [] str; |
|
1262 if (success > 0 && store_value) |
636
|
1263 values(fmt_arg_count++) = temp; |
1
|
1264 } |
|
1265 break; |
628
|
1266 |
1
|
1267 case 's': |
|
1268 { |
|
1269 if (string_width < 1) |
|
1270 { |
1358
|
1271 // XXX FIXME XXX -- The code below is miscompiled on the |
|
1272 // Alpha with gcc 2.6.0, so that string_width is never |
|
1273 // incremented, even though reading the data works |
|
1274 // correctly. One fix is to use a fixed-size buffer... |
628
|
1275 // string_width = 8192; |
|
1276 |
1
|
1277 string_width = 0; |
|
1278 long original_position = ftell (fptr); |
628
|
1279 |
1
|
1280 int c; |
|
1281 |
636
|
1282 while ((c = getc (fptr)) != EOF && isspace (c)) |
1
|
1283 ; // Don't count leading whitespace. |
|
1284 |
|
1285 if (c != EOF) |
|
1286 string_width++; |
|
1287 |
|
1288 for (;;) |
|
1289 { |
|
1290 c = getc (fptr); |
636
|
1291 if (c != EOF && ! isspace (c)) |
1
|
1292 string_width++; |
|
1293 else |
|
1294 break; |
|
1295 } |
|
1296 |
|
1297 fseek (fptr, original_position, SEEK_SET); |
|
1298 } |
|
1299 chars_from_fmt_str++; |
636
|
1300 char temp [string_width+1]; |
1
|
1301 fmt << *s << ends; |
|
1302 char *str = fmt.str (); |
|
1303 success = fscanf (fptr, str, temp); |
|
1304 delete [] str; |
636
|
1305 temp[string_width] = '\0'; |
628
|
1306 if (success > 0 && store_value) |
636
|
1307 values(fmt_arg_count++) = temp; |
1
|
1308 } |
|
1309 break; |
628
|
1310 |
1
|
1311 case 'c': |
|
1312 { |
|
1313 if (string_width < 1) |
|
1314 string_width = 1; |
|
1315 chars_from_fmt_str++; |
636
|
1316 char temp [string_width+1]; |
1
|
1317 memset (temp, '\0', string_width+1); |
|
1318 fmt << *s << ends; |
|
1319 char *str = fmt.str (); |
|
1320 success = fscanf (fptr, str, temp); |
|
1321 delete [] str; |
|
1322 temp[string_width] = '\0'; |
|
1323 if (success > 0 && store_value) |
636
|
1324 values(fmt_arg_count++) = temp; |
1
|
1325 } |
|
1326 break; |
628
|
1327 |
1
|
1328 default: |
|
1329 goto invalid_format; |
|
1330 } |
|
1331 |
368
|
1332 if (success > 0) |
1
|
1333 return chars_from_fmt_str; |
368
|
1334 else if (success == 0) |
218
|
1335 warning ("%s: invalid conversion", type); |
1
|
1336 else if (success == EOF) |
|
1337 { |
|
1338 if (strcmp (type, "fscanf") == 0) |
218
|
1339 warning ("%s: end of file reached before final conversion", type); |
1
|
1340 else if (strcmp (type, "sscanf") == 0) |
218
|
1341 warning ("%s: end of string reached before final conversion", type); |
1
|
1342 else if (strcmp (type, "scanf") == 0) |
218
|
1343 warning ("%s: end of input reached before final conversion", type); |
1
|
1344 } |
|
1345 else |
|
1346 { |
|
1347 invalid_format: |
218
|
1348 warning ("%s: invalid format", type); |
1
|
1349 } |
|
1350 |
|
1351 return -1; |
|
1352 } |
|
1353 |
761
|
1354 // Formatted reading from a file. |
|
1355 |
1181
|
1356 static Octave_object |
506
|
1357 do_scanf (const char *type, const Octave_object& args, int nargout) |
1
|
1358 { |
497
|
1359 Octave_object retval; |
529
|
1360 char *scanf_fmt = 0; |
|
1361 char *tmp_file = 0; |
1
|
1362 int tmp_file_open = 0; |
529
|
1363 FILE *fptr = 0; |
240
|
1364 file_info file; |
1
|
1365 |
|
1366 fmt_arg_count = 0; |
|
1367 |
|
1368 if (strcmp (type, "scanf") != 0) |
|
1369 { |
712
|
1370 scanf_fmt = args(1).string_value (); |
636
|
1371 |
|
1372 if (error_state) |
1
|
1373 { |
|
1374 error ("%s: format must be a string", type); |
|
1375 return retval; |
|
1376 } |
|
1377 } |
|
1378 |
|
1379 int doing_fscanf = (strcmp (type, "fscanf") == 0); |
|
1380 |
|
1381 if (doing_fscanf) |
|
1382 { |
712
|
1383 Pix p = file_io_get_file (args(0), "r", type); |
368
|
1384 |
529
|
1385 if (! p) |
368
|
1386 return retval; |
1
|
1387 |
|
1388 file = file_list (p); |
|
1389 |
|
1390 if (strcmp (file.mode (), "w") == 0 || strcmp (file.mode (), "a") == 0) |
|
1391 { |
|
1392 error ("%s: this file is opened for writing only", type); |
|
1393 return retval; |
|
1394 } |
|
1395 |
|
1396 fptr = file.fptr (); |
|
1397 } |
|
1398 |
712
|
1399 if ((! fptr && args(0).is_string ()) |
368
|
1400 || (doing_fscanf && file.number () == 0)) |
1
|
1401 { |
|
1402 char *string; |
|
1403 |
|
1404 if (strcmp (type, "scanf") == 0) |
712
|
1405 scanf_fmt = args(0).string_value (); |
1
|
1406 |
|
1407 if (strcmp (type, "scanf") == 0 |
|
1408 || (doing_fscanf && file.number () == 0)) |
|
1409 { |
1358
|
1410 // XXX FIXME XXX -- this should probably be possible for |
|
1411 // more than just stdin/stdout pairs, using a list of output |
|
1412 // streams to flush. The list could be created with a |
|
1413 // function like iostream's tie(). |
870
|
1414 |
|
1415 flush_output_to_pager (); |
|
1416 |
1
|
1417 string = gnu_readline (""); |
870
|
1418 |
1
|
1419 if (string && *string) |
|
1420 maybe_save_history (string); |
|
1421 } |
|
1422 else |
712
|
1423 string = args(0).string_value (); |
1
|
1424 |
637
|
1425 tmp_file = octave_tmp_file_name (); |
1
|
1426 |
|
1427 fptr = fopen (tmp_file, "w+"); |
529
|
1428 if (! fptr) |
1
|
1429 { |
|
1430 error ("%s: error opening temporary file", type); |
|
1431 return retval; |
|
1432 } |
|
1433 tmp_file_open = 1; |
|
1434 unlink (tmp_file); |
|
1435 |
529
|
1436 if (! string) |
776
|
1437 { |
|
1438 error ("%s: no string to scan", type); |
|
1439 return retval; |
|
1440 } |
1
|
1441 |
|
1442 int success = fputs (string, fptr); |
|
1443 fflush (fptr); |
|
1444 rewind (fptr); |
|
1445 |
|
1446 if (success < 0) |
|
1447 { |
|
1448 error ("%s: trouble writing temporary file", type); |
|
1449 fclose (fptr); |
|
1450 return retval; |
|
1451 } |
|
1452 } |
|
1453 else if (! doing_fscanf) |
|
1454 { |
|
1455 error ("%s: first argument must be a string", type); |
|
1456 return retval; |
|
1457 } |
|
1458 |
1358
|
1459 // Scan scanf_fmt for % escapes and assign the arguments. |
1
|
1460 |
636
|
1461 retval.resize (nargout); |
1
|
1462 |
|
1463 char *ptr = scanf_fmt; |
|
1464 |
|
1465 for (;;) |
|
1466 { |
|
1467 ostrstream fmt; |
|
1468 char c; |
|
1469 while ((c = *ptr++) != '\0' && c != '%') |
|
1470 fmt << c; |
|
1471 |
|
1472 if (c == '\0') |
|
1473 break; |
|
1474 |
|
1475 if (*ptr == '%') |
|
1476 { |
|
1477 ptr++; |
|
1478 fmt << c; |
|
1479 continue; |
|
1480 } |
|
1481 |
1358
|
1482 // We must be looking at a format specifier. Extract it or |
|
1483 // fail. |
1
|
1484 |
368
|
1485 int status = process_scanf_format (ptr, fmt, type, nargout, |
|
1486 fptr, retval); |
1
|
1487 |
|
1488 if (status < 0) |
|
1489 { |
|
1490 if (fmt_arg_count == 0) |
497
|
1491 retval.resize (0); |
1
|
1492 break; |
|
1493 } |
|
1494 |
|
1495 ptr += status; |
|
1496 } |
|
1497 |
|
1498 if (tmp_file_open) |
|
1499 fclose (fptr); |
|
1500 |
|
1501 return retval; |
|
1502 } |
|
1503 |
1181
|
1504 DEFUN ("fscanf", Ffscanf, Sfscanf, 2, -1, |
|
1505 "[A, B, C, ...] = fscanf (FILENAME or FILENUM, FORMAT)") |
|
1506 { |
|
1507 Octave_object retval; |
|
1508 |
|
1509 int nargin = args.length (); |
|
1510 |
|
1511 if (nargin != 1 && nargin != 2) |
|
1512 print_usage ("fscanf"); |
|
1513 else |
|
1514 retval = do_scanf ("fscanf", args, nargout); |
|
1515 |
|
1516 return retval; |
|
1517 } |
|
1518 |
|
1519 // Formatted reading. |
|
1520 |
|
1521 DEFUN ("scanf", Fscanf, Sscanf, 1, -1, |
|
1522 "[A, B, C, ...] = scanf (FORMAT)") |
|
1523 { |
|
1524 Octave_object retval; |
|
1525 |
|
1526 int nargin = args.length (); |
|
1527 |
|
1528 if (nargin != 1) |
|
1529 print_usage ("scanf"); |
|
1530 else |
|
1531 retval = do_scanf ("scanf", args, nargout); |
|
1532 |
|
1533 return retval; |
|
1534 } |
|
1535 |
|
1536 // Formatted reading from a string. |
|
1537 |
|
1538 DEFUN ("sscanf", Fsscanf, Ssscanf, 2, -1, |
|
1539 "[A, B, C, ...] = sscanf (STRING, FORMAT)") |
|
1540 { |
|
1541 Octave_object retval; |
|
1542 |
|
1543 int nargin = args.length (); |
|
1544 |
|
1545 if (nargin != 2) |
|
1546 print_usage ("sscanf"); |
|
1547 else |
|
1548 retval = do_scanf ("sscanf", args, nargout); |
|
1549 |
|
1550 return retval; |
|
1551 } |
|
1552 |
761
|
1553 // Find out how many elements are left to read. |
|
1554 |
444
|
1555 static long |
471
|
1556 num_items_remaining (FILE *fptr, char *type) |
444
|
1557 { |
471
|
1558 size_t size; |
|
1559 |
|
1560 if (strcasecmp (type, "uchar") == 0) |
|
1561 size = sizeof (u_char); |
|
1562 else if (strcasecmp (type, "char") == 0) |
|
1563 size = sizeof (char); |
|
1564 else if (strcasecmp (type, "short") == 0) |
|
1565 size = sizeof (short); |
|
1566 else if (strcasecmp (type, "ushort") == 0) |
|
1567 size = sizeof (u_short); |
|
1568 else if (strcasecmp (type, "int") == 0) |
|
1569 size = sizeof (int); |
|
1570 else if (strcasecmp (type, "uint") == 0) |
|
1571 size = sizeof (u_int); |
|
1572 else if (strcasecmp (type, "long") == 0) |
|
1573 size = sizeof (long); |
|
1574 else if (strcasecmp (type, "ulong") == 0) |
|
1575 size = sizeof (u_long); |
|
1576 else if (strcasecmp (type, "float") == 0) |
|
1577 size = sizeof (float); |
|
1578 else if (strcasecmp (type, "double") == 0) |
|
1579 size = sizeof (double); |
|
1580 else |
|
1581 return 0; |
|
1582 |
444
|
1583 long curr_pos = ftell (fptr); |
|
1584 |
|
1585 fseek (fptr, 0, SEEK_END); |
|
1586 long end_of_file = ftell (fptr); |
|
1587 |
471
|
1588 fseek (fptr, curr_pos, SEEK_SET); |
444
|
1589 |
|
1590 long len = end_of_file - curr_pos; |
|
1591 |
471
|
1592 return len / size; |
444
|
1593 } |
|
1594 |
761
|
1595 // Read binary data from a file. |
|
1596 // |
|
1597 // [data, count] = fread (fid, size, 'precision') |
|
1598 // |
|
1599 // fid : the file id from fopen |
|
1600 // size : the size of the matrix or vector or scaler to read |
|
1601 // |
|
1602 // n : reads n elements of a column vector |
|
1603 // inf : reads to the end of file (default) |
|
1604 // [m, n] : reads enough elements to fill the matrix |
|
1605 // the number of columns can be inf |
|
1606 // |
|
1607 // precision : type of the element. Can be: |
|
1608 // |
|
1609 // char, uchar, schar, short, ushort, int, uint, |
|
1610 // long, ulong, float, double |
|
1611 // |
|
1612 // Default is uchar. |
|
1613 // |
|
1614 // data : output data |
|
1615 // count : number of elements read |
|
1616 |
1181
|
1617 static Octave_object |
506
|
1618 fread_internal (const Octave_object& args, int nargout) |
444
|
1619 { |
497
|
1620 Octave_object retval; |
444
|
1621 |
506
|
1622 int nargin = args.length (); |
|
1623 |
712
|
1624 Pix p = file_io_get_file (args(0), "r", "fread"); |
444
|
1625 |
529
|
1626 if (! p) |
444
|
1627 return retval; |
|
1628 |
1358
|
1629 // Get type and number of bytes per element to read. |
|
1630 |
444
|
1631 char *prec = "uchar"; |
712
|
1632 if (nargin > 2) |
444
|
1633 { |
712
|
1634 prec = args(2).string_value (); |
636
|
1635 |
|
1636 if (error_state) |
444
|
1637 { |
|
1638 error ("fread: precision must be a specified as a string"); |
|
1639 return retval; |
|
1640 } |
|
1641 } |
|
1642 |
|
1643 file_info file = file_list (p); |
471
|
1644 |
|
1645 FILE *fptr = file.fptr (); |
444
|
1646 |
1358
|
1647 // Set up matrix to read into. If specified in arguments use that |
|
1648 // number, otherwise read everyting left in file. |
444
|
1649 |
471
|
1650 double dnr = 0.0; |
|
1651 double dnc = 0.0; |
|
1652 int nr; |
|
1653 int nc; |
444
|
1654 |
712
|
1655 if (nargin > 1) |
444
|
1656 { |
712
|
1657 if (args(1).is_scalar_type ()) |
444
|
1658 { |
712
|
1659 dnr = args(1).double_value (); |
636
|
1660 |
|
1661 if (error_state) |
|
1662 return retval; |
|
1663 |
471
|
1664 dnc = 1.0; |
444
|
1665 } |
636
|
1666 else |
444
|
1667 { |
712
|
1668 ColumnVector tmp = args(1).vector_value (); |
444
|
1669 |
636
|
1670 if (error_state || tmp.length () != 2) |
471
|
1671 { |
|
1672 error ("fread: invalid size specification\n"); |
|
1673 return retval; |
|
1674 } |
636
|
1675 |
|
1676 dnr = tmp.elem (0); |
|
1677 dnc = tmp.elem (1); |
471
|
1678 } |
|
1679 |
|
1680 if ((xisinf (dnr)) && (xisinf (dnc))) |
444
|
1681 { |
471
|
1682 error ("fread: number of rows and columns cannot both be infinite"); |
444
|
1683 return retval; |
|
1684 } |
|
1685 |
471
|
1686 if (xisinf (dnr)) |
|
1687 { |
1086
|
1688 if (xisnan (dnc)) |
|
1689 { |
|
1690 error ("fread: NaN invalid as the number of columns"); |
|
1691 return retval; |
|
1692 } |
|
1693 else |
|
1694 { |
|
1695 nc = NINT (dnc); |
|
1696 int n = num_items_remaining (fptr, prec); |
|
1697 nr = n / nc; |
|
1698 if (n > nr * nc) |
|
1699 nr++; |
|
1700 } |
471
|
1701 } |
|
1702 else if (xisinf (dnc)) |
|
1703 { |
1086
|
1704 if (xisnan (dnr)) |
|
1705 { |
|
1706 error ("fread: NaN invalid as the number of rows"); |
|
1707 return retval; |
|
1708 } |
|
1709 else |
|
1710 { |
|
1711 nr = NINT (dnr); |
|
1712 int n = num_items_remaining (fptr, prec); |
|
1713 nc = n / nr; |
|
1714 if (n > nc * nr) |
|
1715 nc++; |
|
1716 } |
471
|
1717 } |
|
1718 else |
|
1719 { |
1086
|
1720 if (xisnan (dnr)) |
|
1721 { |
|
1722 error ("fread: NaN invalid as the number of rows"); |
|
1723 return retval; |
|
1724 } |
|
1725 else |
|
1726 nr = NINT (dnr); |
|
1727 |
|
1728 if (xisnan (dnc)) |
|
1729 { |
|
1730 error ("fread: NaN invalid as the number of columns"); |
|
1731 return retval; |
|
1732 } |
|
1733 else |
|
1734 nc = NINT (dnc); |
471
|
1735 } |
444
|
1736 } |
|
1737 else |
|
1738 { |
1358
|
1739 // No size parameter, read what's left of the file. |
|
1740 |
471
|
1741 nc = 1; |
|
1742 int n = num_items_remaining (fptr, prec); |
|
1743 nr = n / nc; |
|
1744 if (n > nr * nc) |
|
1745 nr++; |
444
|
1746 } |
|
1747 |
|
1748 Matrix m (nr, nc, octave_NaN); |
|
1749 |
1358
|
1750 // Read data. |
444
|
1751 |
471
|
1752 int count = m.read (fptr, prec); |
444
|
1753 |
|
1754 if (nargout > 1) |
636
|
1755 retval(1) = (double) count; |
444
|
1756 |
636
|
1757 retval(0) = m; |
444
|
1758 |
|
1759 return retval; |
|
1760 } |
|
1761 |
1181
|
1762 DEFUN ("fread", Ffread, Sfread, 3, 2, |
|
1763 "[DATA, COUNT] = fread (FILENUM, SIZE, PRECISION)\n\ |
529
|
1764 \n\ |
1181
|
1765 Reads data in binary form of type PRECISION from a file.\n\ |
529
|
1766 \n\ |
|
1767 FILENUM : file number from fopen\n\ |
1181
|
1768 SIZE : size specification for the Data matrix\n\ |
529
|
1769 PRECISION : type of data to read, valid types are\n\ |
|
1770 \n\ |
1230
|
1771 \"char\" \"schar\" \"short\" \"int\" \"long\" \"float\"\n\ |
|
1772 \"double\" \"uchar\" \"ushort\" \"uint\" \"ulong\"\n\ |
529
|
1773 \n\ |
1181
|
1774 DATA : matrix in which the data is stored\n\ |
|
1775 COUNT : number of elements read") |
529
|
1776 { |
|
1777 Octave_object retval; |
|
1778 |
|
1779 int nargin = args.length (); |
|
1780 |
1181
|
1781 if (nargin < 1 || nargin > 3) |
|
1782 print_usage ("fread"); |
529
|
1783 else |
1181
|
1784 retval = fread_internal (args, nargout); |
529
|
1785 |
|
1786 return retval; |
|
1787 } |
|
1788 |
761
|
1789 // Write binary data to a file. |
|
1790 // |
|
1791 // count = fwrite (fid, data, 'precision') |
|
1792 // |
|
1793 // fid : file id from fopen |
|
1794 // Data : data to be written |
|
1795 // precision : type of output element. Can be: |
|
1796 // |
|
1797 // char, uchar, schar, short, ushort, int, uint, |
|
1798 // long, float, double |
|
1799 // |
|
1800 // Default is uchar. |
|
1801 // |
|
1802 // count : the number of elements written |
|
1803 |
1181
|
1804 static Octave_object |
506
|
1805 fwrite_internal (const Octave_object& args, int nargout) |
444
|
1806 { |
497
|
1807 Octave_object retval; |
444
|
1808 |
506
|
1809 int nargin = args.length (); |
|
1810 |
761
|
1811 Pix p = file_io_get_file (args(0), "a+", "fwrite"); |
444
|
1812 |
529
|
1813 if (! p) |
444
|
1814 return retval; |
|
1815 |
1358
|
1816 // Get type and number of bytes per element to read. |
|
1817 |
444
|
1818 char *prec = "uchar"; |
712
|
1819 if (nargin > 2) |
444
|
1820 { |
761
|
1821 prec = args(2).string_value (); |
636
|
1822 |
|
1823 if (error_state) |
444
|
1824 { |
|
1825 error ("fwrite: precision must be a specified as a string"); |
|
1826 return retval; |
|
1827 } |
|
1828 } |
|
1829 |
|
1830 file_info file = file_list (p); |
|
1831 |
761
|
1832 Matrix m = args(1).matrix_value (); |
444
|
1833 |
636
|
1834 if (! error_state) |
|
1835 { |
|
1836 int count = m.write (file.fptr (), prec); |
444
|
1837 |
636
|
1838 retval(0) = (double) count; |
|
1839 } |
444
|
1840 |
|
1841 return retval; |
|
1842 } |
|
1843 |
1181
|
1844 DEFUN ("fwrite", Ffwrite, Sfwrite, 3, 1, |
|
1845 "COUNT = fwrite (FILENUM, DATA, PRECISION)\n\ |
|
1846 \n\ |
|
1847 Writes data to a file in binary form of size PRECISION\n\ |
|
1848 \n\ |
|
1849 FILENUM : file number from fopen\n\ |
|
1850 DATA : matrix of elements to be written\n\ |
|
1851 PRECISION : type of data to read, valid types are\n\ |
|
1852 \n\ |
1230
|
1853 \"char\" \"schar\" \"short\" \"int\" \"long\" \"float\"\n\ |
|
1854 \"double\" \"uchar\" \"ushort\" \"uint\" \"ulong\"\n\ |
1181
|
1855 \n\ |
|
1856 COUNT : number of elements written") |
|
1857 { |
|
1858 Octave_object retval; |
|
1859 |
|
1860 int nargin = args.length (); |
|
1861 |
|
1862 if (nargin < 2 || nargin > 3) |
|
1863 print_usage ("fwrite"); |
|
1864 else |
|
1865 retval = fwrite_internal (args, nargout); |
|
1866 |
|
1867 return retval; |
|
1868 } |
|
1869 |
|
1870 // Check for an EOF condition on a file opened by fopen. |
|
1871 // |
|
1872 // eof = feof (fid) |
|
1873 // |
|
1874 // fid : file id from fopen |
|
1875 // eof : non zero for an end of file condition |
|
1876 |
|
1877 static Octave_object |
|
1878 feof_internal (const Octave_object& args, int nargout) |
|
1879 { |
|
1880 Octave_object retval; |
|
1881 |
|
1882 Pix p = return_valid_file (args(0)); |
|
1883 |
|
1884 if (! p) |
|
1885 return retval; |
|
1886 |
|
1887 file_info file = file_list (p); |
|
1888 |
|
1889 retval(0) = (double) feof (file.fptr ()); |
|
1890 |
|
1891 return retval; |
|
1892 } |
|
1893 |
712
|
1894 DEFUN ("feof", Ffeof, Sfeof, 1, 1, |
529
|
1895 "ERROR = feof (FILENAME or FILENUM)\n\ |
|
1896 \n\ |
|
1897 Returns a non zero value for an end of file condition for the\n\ |
|
1898 file specified by FILENAME or FILENUM from fopen") |
|
1899 { |
|
1900 Octave_object retval; |
|
1901 |
|
1902 int nargin = args.length (); |
|
1903 |
712
|
1904 if (nargin != 1) |
529
|
1905 print_usage ("feof"); |
|
1906 else |
|
1907 retval = feof_internal (args, nargout); |
|
1908 |
|
1909 return retval; |
|
1910 } |
|
1911 |
1181
|
1912 // Check for an error condition on a file opened by fopen. |
761
|
1913 // |
1181
|
1914 // [message, errnum] = ferror (fid) |
761
|
1915 // |
1181
|
1916 // fid : file id from fopen |
|
1917 // message : system error message |
|
1918 // errnum : error number |
761
|
1919 |
1181
|
1920 static Octave_object |
|
1921 ferror_internal (const Octave_object& args, int nargout) |
444
|
1922 { |
497
|
1923 Octave_object retval; |
444
|
1924 |
1049
|
1925 Pix p = return_valid_file (args(0)); |
444
|
1926 |
529
|
1927 if (! p) |
444
|
1928 return retval; |
|
1929 |
|
1930 file_info file = file_list (p); |
|
1931 |
1181
|
1932 int ierr = ferror (file.fptr ()); |
|
1933 |
|
1934 if (nargout > 1) |
|
1935 retval(1) = (double) ierr; |
|
1936 |
|
1937 retval(0) = strsave (strerror (ierr)); |
529
|
1938 |
|
1939 return retval; |
|
1940 } |
|
1941 |
712
|
1942 DEFUN ("ferror", Fferror, Sferror, 1, 1, |
529
|
1943 "ERROR = ferror (FILENAME or FILENUM)\n\ |
|
1944 \n\ |
|
1945 Returns a non zero value for an error condition on the\n\ |
|
1946 file specified by FILENAME or FILENUM from fopen") |
|
1947 { |
|
1948 Octave_object retval; |
|
1949 |
|
1950 int nargin = args.length (); |
|
1951 |
712
|
1952 if (nargin != 1) |
529
|
1953 print_usage ("ferror"); |
|
1954 else |
|
1955 retval = ferror_internal (args, nargout); |
444
|
1956 |
|
1957 return retval; |
|
1958 } |
|
1959 |
1230
|
1960 static Octave_object |
|
1961 popen_internal (const Octave_object& args) |
|
1962 { |
|
1963 Octave_object retval; |
|
1964 |
|
1965 if (! args(0).is_string ()) |
|
1966 { |
|
1967 error ("popen: file name must be a string"); |
|
1968 return retval; |
|
1969 } |
|
1970 |
|
1971 char *name = args(0).string_value (); |
|
1972 |
|
1973 Pix p = return_valid_file (name); |
|
1974 |
|
1975 if (p) |
|
1976 { |
|
1977 file_info file = file_list (p); |
|
1978 |
|
1979 retval(0) = (double) file.number (); |
|
1980 |
|
1981 return retval; |
|
1982 } |
|
1983 |
|
1984 if (! args(1).is_string ()) |
|
1985 { |
|
1986 error ("popen: file mode must be a string"); |
|
1987 return retval; |
|
1988 } |
|
1989 |
|
1990 char *mode = args(1).string_value (); |
|
1991 |
|
1992 if (mode[1] || (mode[0] != 'w' && mode[0] != 'r')) |
|
1993 { |
|
1994 error ("popen: invalid mode, must be either \"r\" or \"w\"."); |
|
1995 return retval; |
|
1996 } |
|
1997 |
|
1998 struct stat buffer; |
|
1999 if (stat (name, &buffer) == 0 && (buffer.st_mode & S_IFDIR) == S_IFDIR) |
|
2000 { |
|
2001 error ("popen: can't open directory"); |
|
2002 return retval; |
|
2003 } |
|
2004 |
|
2005 FILE *file_ptr = popen (name, mode); |
|
2006 |
|
2007 if (! file_ptr) |
|
2008 { |
|
2009 error ("popen: unable to start process `%s'", name); |
|
2010 return retval; |
|
2011 } |
|
2012 |
|
2013 int number = file_list.length () + 1; |
|
2014 |
|
2015 file_info file (number, name, file_ptr, mode); |
|
2016 file_list.append (file); |
|
2017 |
|
2018 retval(0) = (double) number; |
|
2019 |
|
2020 return retval; |
|
2021 } |
|
2022 |
|
2023 DEFUN ("popen", Fpopen, Spopen, 2, 1, |
|
2024 "FILENUM = popen (FILENAME, MODE)\n\ |
|
2025 \n\ |
|
2026 start a process and create a pipe. Valid values for mode are:\n\ |
|
2027 \n\ |
|
2028 \"r\" : connect stdout of process to pipe\n\ |
|
2029 \"w\" : connect stdin of process to pipe") |
|
2030 { |
|
2031 Octave_object retval; |
|
2032 |
|
2033 int nargin = args.length (); |
|
2034 |
|
2035 if (nargin != 2) |
|
2036 print_usage ("popen"); |
|
2037 else |
|
2038 retval = popen_internal (args); |
|
2039 |
|
2040 return retval; |
|
2041 } |
|
2042 |
|
2043 static Octave_object |
|
2044 pclose_internal (const Octave_object& args) |
|
2045 { |
|
2046 Octave_object retval; |
|
2047 |
|
2048 Pix p = return_valid_file (args(0)); |
|
2049 |
|
2050 if (! p) |
|
2051 return retval; |
|
2052 |
|
2053 file_info file = file_list (p); |
|
2054 |
|
2055 if (file.number () < 3) |
|
2056 { |
|
2057 warning ("pclose: can't close stdin, stdout, or stderr!"); |
|
2058 return retval; |
|
2059 } |
|
2060 |
|
2061 int success = pclose (file.fptr ()); |
|
2062 file_list.del (p); |
|
2063 |
|
2064 if (success == 0) |
|
2065 retval(0) = 1.0; // succeeded |
|
2066 else |
|
2067 { |
|
2068 error ("pclose: error on closing file"); |
|
2069 retval(0) = 0.0; // failed |
|
2070 } |
|
2071 |
|
2072 return retval; |
|
2073 } |
|
2074 |
|
2075 DEFUN ("pclose", Fpclose, Spclose, 1, 1, |
|
2076 "pclose (FILENAME or FILENUM)\n\ |
|
2077 \n\ |
|
2078 Close a pipe and terminate the associated process") |
|
2079 { |
|
2080 Octave_object retval; |
|
2081 |
|
2082 int nargin = args.length (); |
|
2083 |
|
2084 if (nargin != 1) |
|
2085 print_usage ("pclose"); |
|
2086 else |
|
2087 retval = pclose_internal (args); |
|
2088 |
|
2089 return retval; |
|
2090 } |
|
2091 |
|
2092 static Octave_object |
|
2093 execute_internal (const Octave_object& args, int nargout) |
|
2094 { |
|
2095 Octave_object retval (3, tree_constant (-1.0)); |
|
2096 |
|
2097 pid_t pid = 0; |
|
2098 int stdin_pipe[2]; |
|
2099 int stdout_pipe[2]; |
|
2100 FILE *stdin_file; |
|
2101 FILE *stdout_file; |
|
2102 int new_stdin; |
|
2103 int new_stdout; |
|
2104 |
|
2105 if (! args(0).is_string ()) |
|
2106 { |
|
2107 error ("execute: file name must be a string"); |
|
2108 return retval; |
|
2109 } |
|
2110 |
|
2111 char *name = args(0).string_value (); |
|
2112 |
|
2113 if (pipe (stdin_pipe) || pipe (stdout_pipe)) |
|
2114 { |
|
2115 error ("execute: pipe creation failed"); |
|
2116 return retval; |
|
2117 } |
|
2118 |
|
2119 pid = fork (); |
|
2120 |
|
2121 if (pid < (pid_t) 0) |
|
2122 { |
|
2123 error ("execute: fork failed - can't create child process"); |
|
2124 return retval; |
|
2125 } |
|
2126 |
|
2127 if (pid == (pid_t) 0) |
|
2128 { |
|
2129 close (stdin_pipe[1]); |
|
2130 close (stdout_pipe[0]); |
|
2131 |
|
2132 dup2 (stdin_pipe[0], STDIN_FILENO); |
|
2133 close (stdin_pipe[0]); |
|
2134 |
|
2135 dup2 (stdout_pipe[1], STDOUT_FILENO); |
|
2136 close (stdout_pipe[1]); |
|
2137 |
|
2138 if (execlp (name, name, 0) == -1) |
|
2139 error ("execute: unable to start process `%s'", name); |
|
2140 |
|
2141 exit (0); |
|
2142 return 0.0; |
|
2143 } |
|
2144 else |
|
2145 { |
|
2146 close (stdin_pipe[0]); |
|
2147 close (stdout_pipe[1]); |
|
2148 |
|
2149 stdout_file = fdopen (stdout_pipe[0], "r"); |
|
2150 stdin_file = fdopen (stdin_pipe[1], "w"); |
|
2151 |
|
2152 if (fcntl (fileno (stdout_file), F_SETFL, O_NONBLOCK) < 0) |
|
2153 { |
|
2154 error ("execute: error setting file mode"); |
|
2155 return retval; |
|
2156 } |
|
2157 |
|
2158 new_stdin = file_list.length () + 1; |
|
2159 new_stdout = new_stdin + 1; |
|
2160 |
|
2161 file_info new_stdin_file_ptr (new_stdin, name, stdin_file, "w"); |
|
2162 file_info new_stdout_file_ptr (new_stdout, name, stdout_file, "r"); |
|
2163 |
|
2164 file_list.append (new_stdin_file_ptr); |
|
2165 file_list.append (new_stdout_file_ptr); |
|
2166 } |
|
2167 |
|
2168 retval(2) = (double) pid; |
|
2169 retval(1) = (double) new_stdout; |
|
2170 retval(0) = (double) new_stdin; |
|
2171 |
|
2172 return retval; |
|
2173 } |
|
2174 |
|
2175 DEFUN ("execute", Fexecute, Sexecute, 1, 3, |
|
2176 "[STDIN, STDOUT, PID] = execute (COMMAND)\n\ |
|
2177 \n\ |
|
2178 Start a program and redirect its stdin to STDIN and its stdout to STDOUT") |
|
2179 { |
|
2180 Octave_object retval; |
|
2181 |
|
2182 int nargin = args.length (); |
|
2183 |
|
2184 if (nargin != 1) |
|
2185 print_usage ("execute"); |
|
2186 else |
|
2187 retval = execute_internal (args, nargout); |
|
2188 |
|
2189 return retval; |
|
2190 } |
|
2191 |
|
2192 static Octave_object |
|
2193 sync_system_internal (const Octave_object& args) |
|
2194 { |
|
2195 Octave_object retval (1, tree_constant (-1.0)); |
|
2196 |
|
2197 if (! args(0).is_string ()) |
|
2198 { |
|
2199 error ("sync_system: file name must be a string"); |
|
2200 return retval; |
|
2201 } |
|
2202 |
|
2203 char *name = args(0).string_value (); |
|
2204 |
|
2205 retval (0) = (double) system (name); |
|
2206 return retval; |
|
2207 } |
|
2208 |
|
2209 DEFUN ("sync_system", Fsync_system, Ssync_system, 1, 1, |
|
2210 "RETCODE = sync_system (FILENAME)\n\ |
|
2211 \n\ |
|
2212 Start a program and wait until it terminates") |
|
2213 { |
|
2214 Octave_object retval; |
|
2215 |
|
2216 int nargin = args.length (); |
|
2217 |
|
2218 if (nargin != 1) |
|
2219 print_usage ("sync_system"); |
|
2220 else |
|
2221 retval = sync_system_internal (args); |
|
2222 |
|
2223 return retval; |
|
2224 } |
|
2225 |
|
2226 static Octave_object |
|
2227 async_system_internal (const Octave_object& args) |
|
2228 { |
|
2229 Octave_object retval (1, tree_constant (-1.0)); |
|
2230 pid_t pid; |
|
2231 |
|
2232 if (! args(0).is_string ()) |
|
2233 { |
|
2234 error ("async_system: file name must be a string"); |
|
2235 return retval; |
|
2236 } |
|
2237 |
|
2238 char *name = args(0).string_value (); |
|
2239 |
|
2240 pid = fork (); |
|
2241 |
|
2242 if (pid < 0) |
|
2243 { |
|
2244 error ("async_system: fork failed -- can't create child process"); |
|
2245 return retval; |
|
2246 } |
|
2247 |
|
2248 if (pid == 0) |
|
2249 { |
|
2250 system (name); |
|
2251 exit (0); |
|
2252 retval (0) = 0.0; |
|
2253 return retval; |
|
2254 } |
|
2255 else |
|
2256 { |
|
2257 retval (0) = (double) pid; |
|
2258 return retval; |
|
2259 } |
|
2260 } |
|
2261 |
|
2262 DEFUN ("async_system", Fasync_system, Sasync_system, 1, 1, |
|
2263 "PID = async_system (FILENAME)\n\ |
|
2264 \n\ |
|
2265 Create a new process and start FILENAME") |
|
2266 { |
|
2267 Octave_object retval; |
|
2268 |
|
2269 int nargin = args.length (); |
|
2270 |
|
2271 if (nargin != 1) |
|
2272 print_usage ("async_system"); |
|
2273 else |
|
2274 retval = async_system_internal (args); |
|
2275 |
|
2276 return retval; |
|
2277 } |
|
2278 |
|
2279 static Octave_object |
|
2280 waitpid_internal (const Octave_object& args) |
|
2281 { |
|
2282 Octave_object retval (1, tree_constant (-1.0)); |
|
2283 |
|
2284 double pid_num = args(0).double_value (); |
|
2285 |
|
2286 if (! error_state) |
|
2287 { |
|
2288 if (D_NINT (pid_num) != pid_num) |
|
2289 error ("waitpid: PID must be an integer value"); |
|
2290 else |
|
2291 { |
|
2292 pid_t pid = (pid_t) pid_num; |
|
2293 |
|
2294 int options = 0; |
|
2295 |
|
2296 if (args.length () == 2) |
|
2297 { |
|
2298 double options_num = args(1).double_value (); |
|
2299 |
|
2300 if (! error_state) |
|
2301 { |
|
2302 if (D_NINT (options_num) != options_num) |
|
2303 error ("waitpid: PID must be an integer value"); |
|
2304 else |
|
2305 { |
|
2306 options = NINT (options_num); |
|
2307 if (options < 0 || options > 3) |
|
2308 error ("waitpid: invalid OPTIONS value specified"); |
|
2309 } |
|
2310 } |
|
2311 } |
|
2312 |
|
2313 if (! error_state) |
|
2314 retval (0) = (double) waitpid (pid, 0, options); |
|
2315 } |
|
2316 } |
|
2317 |
|
2318 return retval; |
|
2319 } |
|
2320 |
|
2321 DEFUN ("waitpid", Fwaitpid, Swaitpid, 1, 1, |
|
2322 "STATUS = waitpid (PID, OPTIONS)\n\ |
|
2323 \n\ |
|
2324 wait for process PID to terminate\n\ |
|
2325 \n\ |
|
2326 PID can be:\n\ |
|
2327 \n\ |
|
2328 -1 : wait for any child process\n\ |
|
2329 0 : wait for any child process whose process group ID is equal to\n\ |
|
2330 that of the Octave interpreter process.\n\ |
|
2331 > 0 : wait for termination of the child process with ID PID.\n\ |
|
2332 \n\ |
|
2333 OPTIONS is:\n\ |
|
2334 \n\ |
|
2335 0 : wait until signal is received or a child process exits (this\n\ |
|
2336 is the default if the OPTIONS argument is missing) \n\ |
|
2337 1 : do not hang if status is not immediately available\n\ |
|
2338 2 : report the status of any child processes that are\n\ |
|
2339 stopped, and whose status has not yet been reported\n\ |
|
2340 since they stopped\n\ |
|
2341 3 : implies both 1 and 2\n\ |
|
2342 \n\ |
|
2343 STATUS is:\n\ |
|
2344 \n\ |
|
2345 -1 : if an error occured\n\ |
|
2346 > 0 : the process ID of the child process that exited") |
|
2347 { |
|
2348 Octave_object retval; |
|
2349 |
|
2350 int nargin = args.length (); |
|
2351 |
|
2352 if (nargin == 1 || nargin == 2) |
|
2353 retval = waitpid_internal (args); |
|
2354 else |
|
2355 print_usage ("waitpid"); |
|
2356 |
|
2357 return retval; |
|
2358 } |
|
2359 |
|
2360 static Octave_object |
|
2361 mkfifo_internal (const Octave_object& args) |
|
2362 { |
|
2363 Octave_object retval (1, tree_constant (-1.0)); |
|
2364 |
|
2365 if (! args(0).is_string ()) |
|
2366 { |
|
2367 error ("mkfifo: file name must be a string"); |
|
2368 return retval; |
|
2369 } |
|
2370 |
|
2371 char *name = args(0).string_value (); |
|
2372 |
|
2373 if (! args(1).is_scalar_type ()) |
|
2374 { |
|
2375 error ("mkfifo: MODE must be an integer"); |
|
2376 return retval; |
|
2377 } |
|
2378 |
|
2379 long mode = (long) args(1).double_value (); |
|
2380 |
|
2381 retval (0) = (double) mkfifo (name, mode); |
|
2382 |
|
2383 return retval; |
|
2384 } |
|
2385 |
|
2386 DEFUN ("mkfifo", Fmkfifo, Smkfifo, 2, 1, |
|
2387 "STATUS = mkfifo (NAME, MODE)\n\ |
|
2388 \n\ |
|
2389 Create a FIFO special file named NAME with file mode MODE\n\ |
|
2390 \n\ |
|
2391 STATUS is:\n\ |
|
2392 \n\ |
|
2393 != 0 : if mkfifo failed\n\ |
|
2394 0 : if the FIFO special file could be created") |
|
2395 { |
|
2396 Octave_object retval; |
|
2397 |
|
2398 int nargin = args.length (); |
|
2399 |
|
2400 if (nargin != 2) |
|
2401 print_usage ("mkfifo"); |
|
2402 else |
|
2403 retval = mkfifo_internal (args); |
|
2404 |
|
2405 return retval; |
|
2406 } |
|
2407 |
|
2408 static Octave_object |
|
2409 unlink_internal (const Octave_object& args) |
|
2410 { |
|
2411 Octave_object retval; |
|
2412 |
|
2413 if (! args(0).is_string ()) |
|
2414 { |
|
2415 error ("unlink: file name must be a string"); |
|
2416 retval (0) = -1.0; |
|
2417 return retval; |
|
2418 } |
|
2419 |
|
2420 char *name = args(0).string_value (); |
|
2421 |
|
2422 retval (0) = (double) unlink (name); |
|
2423 |
|
2424 return retval; |
|
2425 } |
|
2426 |
|
2427 DEFUN ("unlink", Funlink, Sunlink, 1, 1, |
|
2428 "STATUS = unlink (NAME)\n\ |
|
2429 \n\ |
|
2430 Delete the file NAME\n\ |
|
2431 \n\ |
|
2432 STATUS is:\n\ |
|
2433 \n\ |
|
2434 != 0 : if unlink failed\n\ |
|
2435 0 : if the file could be successfully deleted") |
|
2436 { |
|
2437 Octave_object retval; |
|
2438 |
|
2439 int nargin = args.length (); |
|
2440 |
|
2441 if (nargin != 1) |
|
2442 print_usage ("unlink"); |
|
2443 else |
|
2444 retval = unlink_internal (args); |
|
2445 |
|
2446 return retval; |
|
2447 } |
|
2448 |
444
|
2449 /* |
1
|
2450 ;;; Local Variables: *** |
|
2451 ;;; mode: C++ *** |
|
2452 ;;; page-delimiter: "^/\\*" *** |
|
2453 ;;; End: *** |
|
2454 */ |