3
$Id: prosfs.c,v 1.16 2001/05/09 03:15:11 cph Exp $
5
Copyright (c) 1987-2001 Massachusetts Institute of Technology
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or (at
10
your option) any later version.
12
This program is distributed in the hope that it will be useful, but
13
WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
23
/* Primitives to perform file-system operations. */
31
extern int EXFUN (OS_channel_copy,
33
Tchannel source_channel,
34
Tchannel destination_channel));
35
extern void EXFUN (OS_file_copy, (CONST char *, CONST char *));
37
#define STRING_RESULT(expression) \
39
CONST char * result = (expression); \
43
: (char_pointer_to_string ((unsigned char *) result))); \
46
DEFINE_PRIMITIVE ("FILE-EXISTS?", Prim_file_exists_p, 1, 1,
47
"Return #T iff FILENAME refers to an existing file.\n\
48
Return #F if the file doesn't exist.\n\
49
Return zero if it's a symbolic link that points to a nonexisting file.\n\
50
Signal an error if the file's existence is indeterminate.")
54
enum file_existence result = (OS_file_existence_test (STRING_ARG (1)));
56
((result == file_doesnt_exist)
58
: (result == file_does_exist)
64
DEFINE_PRIMITIVE ("FILE-EXISTS-DIRECT?", Prim_file_exists_direct_p, 1, 1,
65
"Return #T iff FILENAME refers to an existing file.\n\
66
Return #F if the file doesn't exist.\n\
67
Return zero if it's a symbolic link.\n\
68
Signal an error if the file's existence is indeterminate.")
72
enum file_existence result
73
= (OS_file_existence_test_direct (STRING_ARG (1)));
75
((result == file_doesnt_exist)
77
: (result == file_does_exist)
83
DEFINE_PRIMITIVE ("FILE-TYPE-DIRECT", Prim_file_type_direct, 1, 1,
84
"Return type of FILE, as an exact non-negative integer.\n\
85
Don't indirect through symbolic links.")
89
enum file_type t = (OS_file_type_direct (STRING_ARG (1)));
91
((t == file_type_nonexistent)
93
: (ulong_to_integer ((unsigned long) t)));
97
DEFINE_PRIMITIVE ("FILE-TYPE-INDIRECT", Prim_file_type_indirect, 1, 1,
98
"Return type of FILE, as an exact non-negative integer.\n\
99
Indirect through symbolic links.")
101
PRIMITIVE_HEADER (1);
103
enum file_type t = (OS_file_type_indirect (STRING_ARG (1)));
105
((t == file_type_nonexistent)
107
: (ulong_to_integer ((unsigned long) t)));
111
DEFINE_PRIMITIVE ("FILE-ACCESS", Prim_file_access, 2, 2,
112
"Return #T iff FILENAME exists and is accessible according to MODE.\n\
113
MODE is an integer between 0 and 7 inclusive, bitwise encoded:\n\
114
4 ==> file is readable;\n\
115
2 ==> file is writable;\n\
116
1 ==> file is executable.")
118
PRIMITIVE_HEADER (2);
121
(OS_file_access ((STRING_ARG (1)), (arg_index_integer (2, 8)))));
124
DEFINE_PRIMITIVE ("FILE-DIRECTORY?", Prim_file_directory_p, 1, 1,
125
"Return #T iff FILENAME refers to an existing directory.\n\
126
Otherwise #F is returned, meaning either that FILENAME doesn't exist\n\
127
or that it isn't a directory.")
129
PRIMITIVE_HEADER (1);
130
PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT (OS_file_directory_p (STRING_ARG (1))));
133
DEFINE_PRIMITIVE ("FILE-SOFT-LINK?", Prim_file_soft_link_p, 1, 1,
134
"Iff FILENAME refers to an existing soft link, return the link contents.\n\
135
Otherwise #F is returned, meaning either that FILENAME doesn't exist\n\
136
or that it isn't a soft link.")
138
PRIMITIVE_HEADER (1);
139
STRING_RESULT (OS_file_soft_link_p (STRING_ARG (1)));
142
DEFINE_PRIMITIVE ("FILE-REMOVE", Prim_file_remove, 1, 1,
143
"Delete file FILENAME.\n\
144
If FILENAME is a soft link, the link is deleted.")
146
PRIMITIVE_HEADER (1);
147
OS_file_remove (STRING_ARG (1));
148
PRIMITIVE_RETURN (UNSPECIFIC);
151
DEFINE_PRIMITIVE ("FILE-REMOVE-LINK", Prim_file_remove_link, 1, 1,
152
"If file FILENAME is a link to another file (hard or soft), remove it.")
154
PRIMITIVE_HEADER (1);
155
OS_file_remove_link (STRING_ARG (1));
156
PRIMITIVE_RETURN (UNSPECIFIC);
159
DEFINE_PRIMITIVE ("FILE-RENAME", Prim_file_rename, 2, 2,
160
"Rename file FROM-NAME to TO-NAME.")
162
PRIMITIVE_HEADER (2);
163
OS_file_rename ((STRING_ARG (1)), (STRING_ARG (2)));
164
PRIMITIVE_RETURN (UNSPECIFIC);
167
DEFINE_PRIMITIVE ("FILE-LINK-HARD", Prim_file_link_hard, 2, 2,
168
"Create a hard link from file FROM-NAME to file TO-NAME.\n\
169
TO-NAME becomes another name for the file FROM-NAME.")
171
PRIMITIVE_HEADER (2);
172
OS_file_link_hard ((STRING_ARG (1)), (STRING_ARG (2)));
173
PRIMITIVE_RETURN (UNSPECIFIC);
176
DEFINE_PRIMITIVE ("FILE-LINK-SOFT", Prim_file_link_soft, 2, 2,
177
"Create a soft link from file FROM-NAME to file TO-NAME.\n\
178
TO-NAME becomes a soft link containing the string FROM-NAME.")
180
PRIMITIVE_HEADER (2);
181
OS_file_link_soft ((STRING_ARG (1)), (STRING_ARG (2)));
182
PRIMITIVE_RETURN (UNSPECIFIC);
185
DEFINE_PRIMITIVE ("LINK-FILE", Prim_link_file, 3, 3,
186
"This is an obsolete primitive. Use `file-link-hard' or `file-link-soft'.\n\
187
Create a new name for file FROM-NAME, called TO-NAME.\n\
188
If third arg HARD? is #F, a soft link is created;\n\
189
otherwise a hard link is created.")
191
PRIMITIVE_HEADER (3);
193
CONST char * from_name = (STRING_ARG (1));
194
CONST char * to_name = (STRING_ARG (2));
195
if ((ARG_REF (3)) != SHARP_F)
196
OS_file_link_hard (from_name, to_name);
198
OS_file_link_soft (from_name, to_name);
200
PRIMITIVE_RETURN (UNSPECIFIC);
203
#ifndef FILE_COPY_BUFFER_LENGTH
204
#define FILE_COPY_BUFFER_LENGTH 8192
208
DEFUN (OS_channel_copy, (source_length, source_channel, destination_channel),
209
off_t source_length AND
210
Tchannel source_channel AND
211
Tchannel destination_channel)
213
char buffer [FILE_COPY_BUFFER_LENGTH];
214
off_t transfer_length =
215
((source_length > (sizeof (buffer))) ? (sizeof (buffer)) : source_length);
217
while (source_length > 0)
220
(OS_channel_read (source_channel, buffer, transfer_length));
225
if ((OS_channel_write (destination_channel, buffer, nread)) <
230
source_length -= nread;
231
if (source_length < (sizeof (buffer)))
232
transfer_length = source_length;
237
DEFINE_PRIMITIVE ("FILE-COPY", Prim_file_copy, 2, 2,
238
"Make a new copy of the file FROM-NAME, called TO-NAME.")
240
PRIMITIVE_HEADER (2);
241
OS_file_copy ((STRING_ARG (1)), (STRING_ARG (2)));
242
PRIMITIVE_RETURN (UNSPECIFIC);
245
DEFINE_PRIMITIVE ("DIRECTORY-MAKE", Prim_directory_make, 1, 1,
246
"Create a new directory, called NAME.")
248
PRIMITIVE_HEADER (1);
249
OS_directory_make (STRING_ARG (1));
250
PRIMITIVE_RETURN (UNSPECIFIC);
253
DEFINE_PRIMITIVE ("DIRECTORY-DELETE", Prim_directory_delete, 1, 1,
254
"Delete directory called NAME.")
256
PRIMITIVE_HEADER (1);
257
OS_directory_delete (STRING_ARG (1));
258
PRIMITIVE_RETURN (UNSPECIFIC);
261
DEFINE_PRIMITIVE ("FILE-TOUCH", Prim_file_touch, 1, 1,
262
"Given a file name, change the times of the file to the current time.\n\
263
If the file does not exist, create it.\n\
264
Both the access time and modification time are changed.\n\
265
Return #F if the file existed and its time was modified.\n\
266
Otherwise the file did not exist and it was created.")
268
PRIMITIVE_HEADER (1);
270
(BOOLEAN_TO_OBJECT (OS_file_touch ((CONST char *) (STRING_ARG (1)))));
273
DEFINE_PRIMITIVE ("NEW-DIRECTORY-OPEN", Prim_new_directory_open, 1, 1,
274
"Open the directory NAME for reading, returning a directory number.")
276
PRIMITIVE_HEADER (1);
277
PRIMITIVE_RETURN (long_to_integer (OS_directory_open (STRING_ARG (1))));
281
DEFUN (arg_directory_index, (argument), unsigned int argument)
283
long index = (arg_integer (argument));
284
if (! (OS_directory_valid_p (index)))
285
error_bad_range_arg (argument);
289
DEFINE_PRIMITIVE ("NEW-DIRECTORY-CLOSE", Prim_new_directory_close, 1, 1,
292
PRIMITIVE_HEADER (1);
293
OS_directory_close (arg_directory_index (1));
294
PRIMITIVE_RETURN (UNSPECIFIC);
297
DEFINE_PRIMITIVE ("NEW-DIRECTORY-READ", Prim_new_directory_read, 1, 1,
298
"Read and return a filename from DIRECTORY, or #F if no more files.")
300
PRIMITIVE_HEADER (1);
301
STRING_RESULT (OS_directory_read (arg_directory_index (1)));
304
DEFINE_PRIMITIVE ("NEW-DIRECTORY-READ-MATCHING", Prim_new_directory_read_match, 2, 2,
305
"Read and return a filename from DIRECTORY.\n\
306
The filename must begin with the STRING.\n\
307
Return #F if there are no more matching files in the directory.")
309
PRIMITIVE_HEADER (2);
311
(OS_directory_read_matching ((arg_directory_index (1)), (STRING_ARG (2))));