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