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