Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

4


Files and Director ies


4.1 Introduction


In the previous chapter we covered the basic functions that perform I/O. The
discussion centered on I/O for regular files—opening a file, and reading or writing a
file. We’ll now look at additional features of the file system and the properties of a file.
We’ll start with thestatfunctions and go through each member of thestatstructure,
looking at all the attributes of a file. In this process, we’ll also describe each of the
functions that modify these attributes: change the owner,change the permissions, and
so on.We’ll also look in moredetail at the structure of a UNIX file system and symbolic
links. Wefinish this chapter with the functions that operate on directories, and we
develop a function that descends through a directory hierarchy.

4.2 stat,fstat,fstatat,and lstatFunctions


The discussion in this chapter centers on the fourstatfunctions and the information
they return.
#include <sys/stat.h>
int stat(const char *restrictpathname,struct stat *restrict buf);
int fstat(intfd,struct stat *buf);
int lstat(const char *restrictpathname,struct stat *restrictbuf);
int fstatat(intfd,const char *restrictpathname,
struct stat *restrict buf,intflag);
All four return: 0 if OK,−1 on error

93
Free download pdf