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