forked from bminor/glibc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesys.texi
2200 lines (1797 loc) · 77.9 KB
/
filesys.texi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@node File System Interface, Pipes and FIFOs, Low-Level I/O, Top
@chapter File System Interface
This chapter describes the GNU C library's functions for manipulating
files. Unlike the input and output functions described in
@ref{I/O on Streams} and @ref{Low-Level I/O}, these
functions are concerned with operating on the files themselves, rather
than on their contents.
Among the facilities described in this chapter are functions for
examining or modifying directories, functions for renaming and deleting
files, and functions for examining and setting file attributes such as
access permissions and modification times.
@menu
* Working Directory:: This is used to resolve relative
file names.
* Accessing Directories:: Finding out what files a directory
contains.
* Hard Links:: Adding alternate names to a file.
* Symbolic Links:: A file that ``points to'' a file name.
* Deleting Files:: How to delete a file, and what that means.
* Renaming Files:: Changing a file's name.
* Creating Directories:: A system call just for creating a directory.
* File Attributes:: Attributes of individual files.
* Making Special Files:: How to create special files.
* Temporary Files:: Naming and creating temporary files.
@end menu
@node Working Directory
@section Working Directory
@cindex current working directory
@cindex working directory
@cindex change working directory
Each process has associated with it a directory, called its @dfn{current
working directory} or simply @dfn{working directory}, that is used in
the resolution of relative file names (@pxref{File Name Resolution}).
When you log in and begin a new session, your working directory is
initially set to the home directory associated with your login account
in the system user database. You can find any user's home directory
using the @code{getpwuid} or @code{getpwnam} functions; see @ref{User
Database}.
Users can change the working directory using shell commands like
@code{cd}. The functions described in this section are the primitives
used by those commands and by other programs for examining and changing
the working directory.
@pindex cd
Prototypes for these functions are declared in the header file
@file{unistd.h}.
@pindex unistd.h
@comment unistd.h
@comment POSIX.1
@deftypefun {char *} getcwd (char *@var{buffer}, size_t @var{size})
The @code{getcwd} function returns an absolute file name representing
the current working directory, storing it in the character array
@var{buffer} that you provide. The @var{size} argument is how you tell
the system the allocation size of @var{buffer}.
The GNU library version of this function also permits you to specify a
null pointer for the @var{buffer} argument. Then @code{getcwd}
allocates a buffer automatically, as with @code{malloc}
(@pxref{Unconstrained Allocation}). If the @var{size} is greater than
zero, then the buffer is that large; otherwise, the buffer is as large
as necessary to hold the result.
The return value is @var{buffer} on success and a null pointer on failure.
The following @code{errno} error conditions are defined for this function:
@table @code
@item EINVAL
The @var{size} argument is zero and @var{buffer} is not a null pointer.
@item ERANGE
The @var{size} argument is less than the length of the working directory
name. You need to allocate a bigger array and try again.
@item EACCES
Permission to read or search a component of the file name was denied.
@end table
@end deftypefun
Here is an example showing how you could implement the behavior of GNU's
@w{@code{getcwd (NULL, 0)}} using only the standard behavior of
@code{getcwd}:
@smallexample
char *
gnu_getcwd ()
@{
int size = 100;
char *buffer = (char *) xmalloc (size);
while (1)
@{
char *value = getcwd (buffer, size);
if (value != 0)
return buffer;
size *= 2;
free (buffer);
buffer = (char *) xmalloc (size);
@}
@}
@end smallexample
@noindent
@xref{Malloc Examples}, for information about @code{xmalloc}, which is
not a library function but is a customary name used in most GNU
software.
@comment unistd.h
@comment BSD
@deftypefun {char *} getwd (char *@var{buffer})
This is similar to @code{getcwd}, but has no way to specify the size of
the buffer. The GNU library provides @code{getwd} only
for backwards compatibility with BSD.
The @var{buffer} argument should be a pointer to an array at least
@code{PATH_MAX} bytes long (@pxref{Limits for Files}). In the GNU
system there is no limit to the size of a file name, so this is not
necessarily enough space to contain the directory name. That is why
this function is deprecated.
@end deftypefun
@comment unistd.h
@comment POSIX.1
@deftypefun int chdir (const char *@var{filename})
This function is used to set the process's working directory to
@var{filename}.
The normal, successful return value from @code{chdir} is @code{0}. A
value of @code{-1} is returned to indicate an error. The @code{errno}
error conditions defined for this function are the usual file name
syntax errors (@pxref{File Name Errors}), plus @code{ENOTDIR} if the
file @var{filename} is not a directory.
@end deftypefun
@node Accessing Directories
@section Accessing Directories
@cindex accessing directories
@cindex reading from a directory
@cindex directories, accessing
The facilities described in this section let you read the contents of a
directory file. This is useful if you want your program to list all the
files in a directory, perhaps as part of a menu.
@cindex directory stream
The @code{opendir} function opens a @dfn{directory stream} whose
elements are directory entries. You use the @code{readdir} function on
the directory stream to retrieve these entries, represented as
@w{@code{struct dirent}} objects. The name of the file for each entry is
stored in the @code{d_name} member of this structure. There are obvious
parallels here to the stream facilities for ordinary files, described in
@ref{I/O on Streams}.
@menu
* Directory Entries:: Format of one directory entry.
* Opening a Directory:: How to open a directory stream.
* Reading/Closing Directory:: How to read directory entries from the stream.
* Simple Directory Lister:: A very simple directory listing program.
* Random Access Directory:: Rereading part of the directory
already read with the same stream.
* Scanning Directory Content:: Get entries for user selected subset of
contents in given directory.
* Simple Directory Lister Mark II:: Revised version of the program.
@end menu
@node Directory Entries
@subsection Format of a Directory Entry
@pindex dirent.h
This section describes what you find in a single directory entry, as you
might obtain it from a directory stream. All the symbols are declared
in the header file @file{dirent.h}.
@comment dirent.h
@comment POSIX.1
@deftp {Data Type} {struct dirent}
This is a structure type used to return information about directory
entries. It contains the following fields:
@table @code
@item char d_name[]
This is the null-terminated file name component. This is the only
field you can count on in all POSIX systems.
@item ino_t d_fileno
This is the file serial number. For BSD compatibility, you can also
refer to this member as @code{d_ino}. In the GNU system and most POSIX
systems, for most files this the same as the @code{st_ino} member that
@code{stat} will return for the file. @xref{File Attributes}.
@item unsigned char d_namlen
This is the length of the file name, not including the terminating null
character. Its type is @code{unsigned char} because that is the integer
type of the appropriate size
@item unsigned char d_type
This is the type of the file, possibly unknown. The following constants
are defined for its value:
@table @code
@item DT_UNKNOWN
The type is unknown. On some systems this is the only value returned.
@item DT_REG
A regular file.
@item DT_DIR
A directory.
@item DT_FIFO
A named pipe, or FIFO. @xref{FIFO Special Files}.
@item DT_SOCK
A local-domain socket. @c !!! @xref{Local Domain}.
@item DT_CHR
A character device.
@item DT_BLK
A block device.
@end table
This member is a BSD extension. Each value except DT_UNKNOWN
corresponds to the file type bits in the @code{st_mode} member of
@code{struct statbuf}. These two macros convert between @code{d_type}
values and @code{st_mode} values:
@deftypefun int IFTODT (mode_t @var{mode})
This returns the @code{d_type} value corresponding to @var{mode}.
@end deftypefun
@deftypefun mode_t DTTOIF (int @var{dirtype})
This returns the @code{st_mode} value corresponding to @var{dirtype}.
@end deftypefun
@end table
This structure may contain additional members in the future.
When a file has multiple names, each name has its own directory entry.
The only way you can tell that the directory entries belong to a
single file is that they have the same value for the @code{d_fileno}
field.
File attributes such as size, modification times, and the like are part
of the file itself, not any particular directory entry. @xref{File
Attributes}.
@end deftp
@node Opening a Directory
@subsection Opening a Directory Stream
@pindex dirent.h
This section describes how to open a directory stream. All the symbols
are declared in the header file @file{dirent.h}.
@comment dirent.h
@comment POSIX.1
@deftp {Data Type} DIR
The @code{DIR} data type represents a directory stream.
@end deftp
You shouldn't ever allocate objects of the @code{struct dirent} or
@code{DIR} data types, since the directory access functions do that for
you. Instead, you refer to these objects using the pointers returned by
the following functions.
@comment dirent.h
@comment POSIX.1
@deftypefun {DIR *} opendir (const char *@var{dirname})
The @code{opendir} function opens and returns a directory stream for
reading the directory whose file name is @var{dirname}. The stream has
type @code{DIR *}.
If unsuccessful, @code{opendir} returns a null pointer. In addition to
the usual file name errors (@pxref{File Name Errors}), the
following @code{errno} error conditions are defined for this function:
@table @code
@item EACCES
Read permission is denied for the directory named by @code{dirname}.
@item EMFILE
The process has too many files open.
@item ENFILE
The entire system, or perhaps the file system which contains the
directory, cannot support any additional open files at the moment.
(This problem cannot happen on the GNU system.)
@end table
The @code{DIR} type is typically implemented using a file descriptor,
and the @code{opendir} function in terms of the @code{open} function.
@xref{Low-Level I/O}. Directory streams and the underlying
file descriptors are closed on @code{exec} (@pxref{Executing a File}).
@end deftypefun
@node Reading/Closing Directory
@subsection Reading and Closing a Directory Stream
@pindex dirent.h
This section describes how to read directory entries from a directory
stream, and how to close the stream when you are done with it. All the
symbols are declared in the header file @file{dirent.h}.
@comment dirent.h
@comment POSIX.1
@deftypefun {struct dirent *} readdir (DIR *@var{dirstream})
This function reads the next entry from the directory. It normally
returns a pointer to a structure containing information about the file.
This structure is statically allocated and can be rewritten by a
subsequent call.
@strong{Portability Note:} On some systems, @code{readdir} may not
return entries for @file{.} and @file{..}, even though these are always
valid file names in any directory. @xref{File Name Resolution}.
If there are no more entries in the directory or an error is detected,
@code{readdir} returns a null pointer. The following @code{errno} error
conditions are defined for this function:
@table @code
@item EBADF
The @var{dirstream} argument is not valid.
@end table
@code{readdir} is not thread safe. Multiple threads using
@code{readdir} on the same @var{dirstream} may overwrite the return
value. Use @code{readdir_r} when this is critical.
@end deftypefun
@comment dirent.h
@comment GNU
@deftypefun int readdir_r (DIR *@var{dirstream}, struct *@var{entry}, struct **@var{result})
This function is the reentrant version of @code{readdir}. Like
@code{readdir} it returns the next entry from the directory. But to
prevent conflicts for simultaneously running threads the result is not
stored in some internal memory. Instead the argument @var{entry} has to
point to a place where the result is stored.
The return value is @code{0} in case the next entry was read
successfully. In this case a pointer to the result is returned in
*@var{result}. It is not required that *@var{result} is the same as
@var{entry}. If something goes wrong while executing @code{readdir_r}
the function returns @code{-1}. The @code{errno} variable is set like
described for @code{readdir}.
@strong{Portability Note:} On some systems, @code{readdir_r} may not
return a terminated string as the file name even if no @code{d_reclen}
element is available in @code{struct dirent} and the file name as the
maximal allowed size. Modern systems all have the @code{d_reclen} field
and on old systems multi threading is not critical. In any case, there
is no such problem with the @code{readdir} function so that even on
systems without @code{d_reclen} field one could use multiple threads by
using external locking.
@end deftypefun
@comment dirent.h
@comment POSIX.1
@deftypefun int closedir (DIR *@var{dirstream})
This function closes the directory stream @var{dirstream}. It returns
@code{0} on success and @code{-1} on failure.
The following @code{errno} error conditions are defined for this
function:
@table @code
@item EBADF
The @var{dirstream} argument is not valid.
@end table
@end deftypefun
@node Simple Directory Lister
@subsection Simple Program to List a Directory
Here's a simple program that prints the names of the files in
the current working directory:
@smallexample
@include dir.c.texi
@end smallexample
The order in which files appear in a directory tends to be fairly
random. A more useful program would sort the entries (perhaps by
alphabetizing them) before printing them; see
@ref{Scanning Directory Content} and @ref{Array Sort Function}.
@node Random Access Directory
@subsection Random Access in a Directory Stream
@pindex dirent.h
This section describes how to reread parts of a directory that you have
already read from an open directory stream. All the symbols are
declared in the header file @file{dirent.h}.
@comment dirent.h
@comment POSIX.1
@deftypefun void rewinddir (DIR *@var{dirstream})
The @code{rewinddir} function is used to reinitialize the directory
stream @var{dirstream}, so that if you call @code{readdir} it
returns information about the first entry in the directory again. This
function also notices if files have been added or removed to the
directory since it was opened with @code{opendir}. (Entries for these
files might or might not be returned by @code{readdir} if they were
added or removed since you last called @code{opendir} or
@code{rewinddir}.)
@end deftypefun
@comment dirent.h
@comment BSD
@deftypefun off_t telldir (DIR *@var{dirstream})
The @code{telldir} function returns the file position of the directory
stream @var{dirstream}. You can use this value with @code{seekdir} to
restore the directory stream to that position.
@end deftypefun
@comment dirent.h
@comment BSD
@deftypefun void seekdir (DIR *@var{dirstream}, off_t @var{pos})
The @code{seekdir} function sets the file position of the directory
stream @var{dirstream} to @var{pos}. The value @var{pos} must be the
result of a previous call to @code{telldir} on this particular stream;
closing and reopening the directory can invalidate values returned by
@code{telldir}.
@end deftypefun
@node Scanning Directory Content
@subsection Scanning the Content of a Directory
A higher-level interface to the directory handling functions is the
@code{scandir} function. With its help one can select a subset of the
entries in a directory, possibly sort them and get as the result a list
of names.
@deftypefun int scandir (const char *@var{dir}, struct dirent ***@var{namelist}, int (*@var{selector}) (struct dirent *), int (*@var{cmp}) (const void *, const void *))
The @code{scandir} function scans the contents of the directory selected
by @var{dir}. The result in @var{namelist} is an array of pointers to
structure of type @code{struct dirent} which describe all selected
directory entries and which is allocated using @code{malloc}. Instead
of always getting all directory entries returned, the user supplied
function @var{selector} can be used to decide which entries are in the
result. Only the entries for which @var{selector} returns a nonzero
value are selected.
Finally the entries in the @var{namelist} are sorted using the user
supplied function @var{cmp}. The arguments of the @var{cmp} function
are of type @code{struct dirent **}. I.e., one cannot directly use the
@code{strcmp} or @code{strcoll} function; see the functions
@code{alphasort} and @code{versionsort} below.
The return value of the function gives the number of entries placed in
@var{namelist}. If it is @code{-1} an error occurred and the global
variable @code{errno} contains more information on the error.
@end deftypefun
As said above the fourth argument to the @code{scandir} function must be
a pointer to a sorting function. For the convenience of the programmer
the GNU C library contains implementations of functions which are very
helpful for this purpose.
@deftypefun int alphasort (const void *@var{a}, const void *@var{b})
The @code{alphasort} function behaves like the @code{strcmp} function
(@pxref{String/Array Comparison}). The difference is that the arguments
are not string pointers but instead they are of type
@code{struct dirent **}.
Return value of is less than, equal to, or greater than zero depending
on the order of the two entries @var{a} and @var{b}.
@end deftypefun
@deftypefun int versionsort (const void *@var{a}, const void *@var{b})
The @code{versionsort} function is like @code{alphasort}, excepted that it
uses the @code{strverscmp} function internally.
@end deftypefun
@node Simple Directory Lister Mark II
@subsection Simple Program to List a Directory, Mark II
Here is a revised version of the directory lister found above
(@pxref{Simple Directory Lister}). Using the @code{scandir} function we
can avoid using the functions which directly work with the directory
contents. After the call the found entries are available for direct
used.
@smallexample
@include dir2.c.texi
@end smallexample
Please note the simple selector function for this example. Since
we want to see all directory entries we always return @code{1}.
@node Hard Links
@section Hard Links
@cindex hard link
@cindex link, hard
@cindex multiple names for one file
@cindex file names, multiple
In POSIX systems, one file can have many names at the same time. All of
the names are equally real, and no one of them is preferred to the
others.
To add a name to a file, use the @code{link} function. (The new name is
also called a @dfn{hard link} to the file.) Creating a new link to a
file does not copy the contents of the file; it simply makes a new name
by which the file can be known, in addition to the file's existing name
or names.
One file can have names in several directories, so the the organization
of the file system is not a strict hierarchy or tree.
In most implementations, it is not possible to have hard links to the
same file in multiple file systems. @code{link} reports an error if you
try to make a hard link to the file from another file system when this
cannot be done.
The prototype for the @code{link} function is declared in the header
file @file{unistd.h}.
@pindex unistd.h
@comment unistd.h
@comment POSIX.1
@deftypefun int link (const char *@var{oldname}, const char *@var{newname})
The @code{link} function makes a new link to the existing file named by
@var{oldname}, under the new name @var{newname}.
This function returns a value of @code{0} if it is successful and
@code{-1} on failure. In addition to the usual file name errors
(@pxref{File Name Errors}) for both @var{oldname} and @var{newname}, the
following @code{errno} error conditions are defined for this function:
@table @code
@item EACCES
You are not allowed to write the directory in which the new link is to
be written.
@ignore
Some implementations also require that the existing file be accessible
by the caller, and use this error to report failure for that reason.
@end ignore
@item EEXIST
There is already a file named @var{newname}. If you want to replace
this link with a new link, you must remove the old link explicitly first.
@item EMLINK
There are already too many links to the file named by @var{oldname}.
(The maximum number of links to a file is @w{@code{LINK_MAX}}; see
@ref{Limits for Files}.)
@item ENOENT
The file named by @var{oldname} doesn't exist. You can't make a link to
a file that doesn't exist.
@item ENOSPC
The directory or file system that would contain the new link is full
and cannot be extended.
@item EPERM
In the GNU system and some others, you cannot make links to directories.
Many systems allow only privileged users to do so. This error
is used to report the problem.
@item EROFS
The directory containing the new link can't be modified because it's on
a read-only file system.
@item EXDEV
The directory specified in @var{newname} is on a different file system
than the existing file.
@item EIO
A hardware error occurred while trying to read or write the to filesystem.
@end table
@end deftypefun
@node Symbolic Links
@section Symbolic Links
@cindex soft link
@cindex link, soft
@cindex symbolic link
@cindex link, symbolic
The GNU system supports @dfn{soft links} or @dfn{symbolic links}. This
is a kind of ``file'' that is essentially a pointer to another file
name. Unlike hard links, symbolic links can be made to directories or
across file systems with no restrictions. You can also make a symbolic
link to a name which is not the name of any file. (Opening this link
will fail until a file by that name is created.) Likewise, if the
symbolic link points to an existing file which is later deleted, the
symbolic link continues to point to the same file name even though the
name no longer names any file.
The reason symbolic links work the way they do is that special things
happen when you try to open the link. The @code{open} function realizes
you have specified the name of a link, reads the file name contained in
the link, and opens that file name instead. The @code{stat} function
likewise operates on the file that the symbolic link points to, instead
of on the link itself.
By contrast, other operations such as deleting or renaming the file
operate on the link itself. The functions @code{readlink} and
@code{lstat} also refrain from following symbolic links, because their
purpose is to obtain information about the link. So does @code{link},
the function that makes a hard link---it makes a hard link to the
symbolic link, which one rarely wants.
Prototypes for the functions listed in this section are in
@file{unistd.h}.
@pindex unistd.h
@comment unistd.h
@comment BSD
@deftypefun int symlink (const char *@var{oldname}, const char *@var{newname})
The @code{symlink} function makes a symbolic link to @var{oldname} named
@var{newname}.
The normal return value from @code{symlink} is @code{0}. A return value
of @code{-1} indicates an error. In addition to the usual file name
syntax errors (@pxref{File Name Errors}), the following @code{errno}
error conditions are defined for this function:
@table @code
@item EEXIST
There is already an existing file named @var{newname}.
@item EROFS
The file @var{newname} would exist on a read-only file system.
@item ENOSPC
The directory or file system cannot be extended to make the new link.
@item EIO
A hardware error occurred while reading or writing data on the disk.
@ignore
@comment not sure about these
@item ELOOP
There are too many levels of indirection. This can be the result of
circular symbolic links to directories.
@item EDQUOT
The new link can't be created because the user's disk quota has been
exceeded.
@end ignore
@end table
@end deftypefun
@comment unistd.h
@comment BSD
@deftypefun int readlink (const char *@var{filename}, char *@var{buffer}, size_t @var{size})
The @code{readlink} function gets the value of the symbolic link
@var{filename}. The file name that the link points to is copied into
@var{buffer}. This file name string is @emph{not} null-terminated;
@code{readlink} normally returns the number of characters copied. The
@var{size} argument specifies the maximum number of characters to copy,
usually the allocation size of @var{buffer}.
If the return value equals @var{size}, you cannot tell whether or not
there was room to return the entire name. So make a bigger buffer and
call @code{readlink} again. Here is an example:
@smallexample
char *
readlink_malloc (char *filename)
@{
int size = 100;
while (1)
@{
char *buffer = (char *) xmalloc (size);
int nchars = readlink (filename, buffer, size);
if (nchars < size)
return buffer;
free (buffer);
size *= 2;
@}
@}
@end smallexample
@c @group Invalid outside example.
A value of @code{-1} is returned in case of error. In addition to the
usual file name errors (@pxref{File Name Errors}), the following
@code{errno} error conditions are defined for this function:
@table @code
@item EINVAL
The named file is not a symbolic link.
@item EIO
A hardware error occurred while reading or writing data on the disk.
@end table
@c @end group
@end deftypefun
@node Deleting Files
@section Deleting Files
@cindex deleting a file
@cindex removing a file
@cindex unlinking a file
You can delete a file with the functions @code{unlink} or @code{remove}.
Deletion actually deletes a file name. If this is the file's only name,
then the file is deleted as well. If the file has other names as well
(@pxref{Hard Links}), it remains accessible under its other names.
@comment unistd.h
@comment POSIX.1
@deftypefun int unlink (const char *@var{filename})
The @code{unlink} function deletes the file name @var{filename}. If
this is a file's sole name, the file itself is also deleted. (Actually,
if any process has the file open when this happens, deletion is
postponed until all processes have closed the file.)
@pindex unistd.h
The function @code{unlink} is declared in the header file @file{unistd.h}.
This function returns @code{0} on successful completion, and @code{-1}
on error. In addition to the usual file name errors
(@pxref{File Name Errors}), the following @code{errno} error conditions are
defined for this function:
@table @code
@item EACCES
Write permission is denied for the directory from which the file is to be
removed, or the directory has the sticky bit set and you do not own the file.
@item EBUSY
This error indicates that the file is being used by the system in such a
way that it can't be unlinked. For example, you might see this error if
the file name specifies the root directory or a mount point for a file
system.
@item ENOENT
The file name to be deleted doesn't exist.
@item EPERM
On some systems, @code{unlink} cannot be used to delete the name of a
directory, or can only be used this way by a privileged user.
To avoid such problems, use @code{rmdir} to delete directories.
(In the GNU system @code{unlink} can never delete the name of a directory.)
@item EROFS
The directory in which the file name is to be deleted is on a read-only
file system, and can't be modified.
@end table
@end deftypefun
@comment unistd.h
@comment POSIX.1
@deftypefun int rmdir (const char *@var{filename})
@cindex directories, deleting
@cindex deleting a directory
The @code{rmdir} function deletes a directory. The directory must be
empty before it can be removed; in other words, it can only contain
entries for @file{.} and @file{..}.
In most other respects, @code{rmdir} behaves like @code{unlink}. There
are two additional @code{errno} error conditions defined for
@code{rmdir}:
@table @code
@item ENOTEMPTY
@itemx EEXIST
The directory to be deleted is not empty.
@end table
These two error codes are synonymous; some systems use one, and some use
the other. The GNU system always uses @code{ENOTEMPTY}.
The prototype for this function is declared in the header file
@file{unistd.h}.
@pindex unistd.h
@end deftypefun
@comment stdio.h
@comment ISO
@deftypefun int remove (const char *@var{filename})
This is the @w{ISO C} function to remove a file. It works like
@code{unlink} for files and like @code{rmdir} for directories.
@code{remove} is declared in @file{stdio.h}.
@pindex stdio.h
@end deftypefun
@node Renaming Files
@section Renaming Files
The @code{rename} function is used to change a file's name.
@cindex renaming a file
@comment stdio.h
@comment ISO
@deftypefun int rename (const char *@var{oldname}, const char *@var{newname})
The @code{rename} function renames the file name @var{oldname} with
@var{newname}. The file formerly accessible under the name
@var{oldname} is afterward accessible as @var{newname} instead. (If the
file had any other names aside from @var{oldname}, it continues to have
those names.)
The directory containing the name @var{newname} must be on the same
file system as the file (as indicated by the name @var{oldname}).
One special case for @code{rename} is when @var{oldname} and
@var{newname} are two names for the same file. The consistent way to
handle this case is to delete @var{oldname}. However, POSIX requires
that in this case @code{rename} do nothing and report success---which is
inconsistent. We don't know what your operating system will do.
If the @var{oldname} is not a directory, then any existing file named
@var{newname} is removed during the renaming operation. However, if
@var{newname} is the name of a directory, @code{rename} fails in this
case.
If the @var{oldname} is a directory, then either @var{newname} must not
exist or it must name a directory that is empty. In the latter case,
the existing directory named @var{newname} is deleted first. The name
@var{newname} must not specify a subdirectory of the directory
@code{oldname} which is being renamed.
One useful feature of @code{rename} is that the meaning of the name
@var{newname} changes ``atomically'' from any previously existing file
by that name to its new meaning (the file that was called
@var{oldname}). There is no instant at which @var{newname} is
nonexistent ``in between'' the old meaning and the new meaning. If
there is a system crash during the operation, it is possible for both
names to still exist; but @var{newname} will always be intact if it
exists at all.
If @code{rename} fails, it returns @code{-1}. In addition to the usual
file name errors (@pxref{File Name Errors}), the following
@code{errno} error conditions are defined for this function:
@table @code
@item EACCES
One of the directories containing @var{newname} or @var{oldname}
refuses write permission; or @var{newname} and @var{oldname} are
directories and write permission is refused for one of them.
@item EBUSY
A directory named by @var{oldname} or @var{newname} is being used by
the system in a way that prevents the renaming from working. This includes
directories that are mount points for filesystems, and directories
that are the current working directories of processes.
@item ENOTEMPTY
@itemx EEXIST
The directory @var{newname} isn't empty. The GNU system always returns
@code{ENOTEMPTY} for this, but some other systems return @code{EEXIST}.
@item EINVAL
The @var{oldname} is a directory that contains @var{newname}.
@item EISDIR
The @var{newname} names a directory, but the @var{oldname} doesn't.
@item EMLINK
The parent directory of @var{newname} would have too many links.
@item ENOENT
The file named by @var{oldname} doesn't exist.
@item ENOSPC
The directory that would contain @var{newname} has no room for another
entry, and there is no space left in the file system to expand it.
@item EROFS
The operation would involve writing to a directory on a read-only file
system.
@item EXDEV
The two file names @var{newname} and @var{oldnames} are on different
file systems.
@end table
@end deftypefun
@node Creating Directories
@section Creating Directories
@cindex creating a directory
@cindex directories, creating
@pindex mkdir
Directories are created with the @code{mkdir} function. (There is also
a shell command @code{mkdir} which does the same thing.)
@c !!! umask
@comment sys/stat.h
@comment POSIX.1
@deftypefun int mkdir (const char *@var{filename}, mode_t @var{mode})
The @code{mkdir} function creates a new, empty directory whose name is
@var{filename}.
The argument @var{mode} specifies the file permissions for the new
directory file. @xref{Permission Bits}, for more information about
this.
A return value of @code{0} indicates successful completion, and
@code{-1} indicates failure. In addition to the usual file name syntax
errors (@pxref{File Name Errors}), the following @code{errno} error
conditions are defined for this function:
@table @code
@item EACCES
Write permission is denied for the parent directory in which the new
directory is to be added.
@item EEXIST
A file named @var{filename} already exists.
@item EMLINK
The parent directory has too many links.
Well-designed file systems never report this error, because they permit
more links than your disk could possibly hold. However, you must still
take account of the possibility of this error, as it could result from
network access to a file system on another machine.
@item ENOSPC
The file system doesn't have enough room to create the new directory.
@item EROFS
The parent directory of the directory being created is on a read-only
file system, and cannot be modified.
@end table
To use this function, your program should include the header file
@file{sys/stat.h}.
@pindex sys/stat.h
@end deftypefun
@node File Attributes
@section File Attributes
@pindex ls
When you issue an @samp{ls -l} shell command on a file, it gives you
information about the size of the file, who owns it, when it was last
modified, and the like. This kind of information is called the
@dfn{file attributes}; it is associated with the file itself and not a
particular one of its names.
This section contains information about how you can inquire about and
modify these attributes of files.
@menu
* Attribute Meanings:: The names of the file attributes,
and what their values mean.
* Reading Attributes:: How to read the attributes of a file.
* Testing File Type:: Distinguishing ordinary files,
directories, links...
* File Owner:: How ownership for new files is determined,
and how to change it.
* Permission Bits:: How information about a file's access
mode is stored.
* Access Permission:: How the system decides who can access a file.
* Setting Permissions:: How permissions for new files are assigned,
and how to change them.
* Testing File Access:: How to find out if your process can
access a file.
* File Times:: About the time attributes of a file.
@end menu
@node Attribute Meanings
@subsection What the File Attribute Values Mean
@cindex status of a file
@cindex attributes of a file
@cindex file attributes
When you read the attributes of a file, they come back in a structure
called @code{struct stat}. This section describes the names of the
attributes, their data types, and what they mean. For the functions
to read the attributes of a file, see @ref{Reading Attributes}.
The header file @file{sys/stat.h} declares all the symbols defined
in this section.
@pindex sys/stat.h
@comment sys/stat.h
@comment POSIX.1
@deftp {Data Type} {struct stat}
The @code{stat} structure type is used to return information about the
attributes of a file. It contains at least the following members:
@table @code
@item mode_t st_mode
Specifies the mode of the file. This includes file type information
(@pxref{Testing File Type}) and the file permission bits
(@pxref{Permission Bits}).
@item ino_t st_ino
The file serial number, which distinguishes this file from all other