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