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