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