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