Merge tag 'upstream/0.2.1'
[debian/yforth] / filee.c
1 /* yForth? - A Forth interpreter written in ANSI C
2  * Copyright (C) 2012 Luca Padovani
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  * ------------------------------------------------------------------------
17  * Module name: filee.c
18  * Abstract:    File extension word set
19  */
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <malloc.h>
24 #include <errno.h>
25 #include "yforth.h"
26 #include "file.h"
27 #include "filee.h"
28
29 /**************************************************************************/
30 /* VARIABLES **************************************************************/
31 /**************************************************************************/
32
33 extern Char file_name[];
34
35 /**************************************************************************/
36 /* WORDS ******************************************************************/
37 /**************************************************************************/
38
39 void _file_status() {
40         register FILE *f;
41         get_file_name();
42         f = fopen(file_name, "rb");
43         *--sp = 0;
44         if (f) {
45                 *--sp = 0;
46                 fclose(f);
47         } else *--sp = errno;
48 }
49
50 void _flush_file() {
51         register FILE *f = (FILE *) *sp;
52         if (fflush(f)) sp[0] = errno;
53         else sp[0] = 0;
54 }
55
56 void _rename_file() {
57         register Char *file_name2;
58         get_file_name();
59         file_name2 = (Char *) malloc(strlen(file_name) + 1);
60         if (file_name2) {
61                 strcpy(file_name2, file_name);
62                 get_file_name();
63                 if (rename(file_name, file_name2)) *--sp = errno;
64                 else *--sp = 0;
65                 free(file_name2);
66         } else *--sp = errno;
67 }
68
69