学习笔记2009年3月24日

Unistd.h

From Wikipedia, the free encyclopedia

Jump to: navigation, search

unistd.h is the C header which contains various UNIX-related functions such as fork and various pipe operations. It is not part of the C standard library but is part of the POSIX standard - so should be present on all UNIX systems, such as Linux, BSD and Solaris. However, it is not present on Windows systems which do not support, for example, forking.

Cygwin and mingw provide their own versions of unistd.h and translation libraries allowing you to use its functions on Windows.

 

 External Links

unistd.h on opengroup

 

dirent.h

From Wikipedia, the free encyclopedia

Jump to: navigation, search
C POSIX Library headers
cpio.h
dirent.h
fcntl.h
grp.h
pwd.h
sys/ipc.h
sys/msg.h
sys/sem.h
sys/stat.h
sys/time.h
sys/types.h
sys/utsname.h
sys/wait.h
tar.h
termios.h
unistd.h
utime.h

dirent.h is the header in the C POSIX library for the C programming language that contains constructs that facilitate directory traversing. The function is not part of the C standard, but is considered "pseudo-standard" and is reliably portable between platforms.

Contents

<script type="text/javascript"></script>

Member functions

NameNotes
int closedir(DIR* dirp)Closes the directory stream referred to by dirp. Upon return, dirp may no longer point to an accessible object of the type DIR. If a file descriptor is used to implement type DIR, that file descriptor will be closed. Upon successful completion, closedir() returns 0. Otherwise, -1 is returned and errno is set to indicate the error.

errno Errors: EBADF means dirp does not refer to an open directory stream, EINTR means the function was interrupted by a signal.

DIR* opendir(const char* dirname)Opens a directory stream corresponding to the directory named by dirname. The directory stream is positioned at the first entry. If the type DIR is implemented using a file descriptor, applications will only be able to open up to a total of OPEN_MAX files and directories. Upon successful completion, opendir() returns a pointer to an object of type DIR. Otherwise, a null pointer is returned and errno is set to indicate the error.

errno Errors: EACCES means the search permission is denied for the component of the path prefix of dirname or read permission is denied for dirname. ELOOP means too many symbolic links were encountered in resolving path. ENAMETOOLONG means the length of the dirname argument exceeds PATH_MAX, or a pathname component is longer than NAME_MAX. ENOENT means a component of dirname does not name an existing directory or dirname is an empty string. ENOTDIR means a component of dirname is not a directory. EMFILE means OPEN_MAX file descriptors are currently open in the calling process. ENAMETOOLONG means the pathname resolution of a symbolic link produced an intermediate result whose length exceeds PATH_MAX. ENFILE means there are too many files are currently open in the system.

struct dirent* readdir(DIR* dirp)Returns a pointer to a structure representing the directory entry at the current position in the directory stream specified by the argument dirp, and positions the directory stream at the next entry. It returns a null pointer upon reaching the end of the directory stream. If entries for dot or dot-dot exist, one entry will be returned for dot and one entry will be returned for dot-dot; otherwise they will not be returned. When an error is encountered, a null pointer is returned and errno is set to indicate the error. When the end of the directory is encountered, a null pointer is returned and errno is not changed.

errno Errors: EOVERFLOW means one of the values in the structure to be returned cannot be represented correctly. EBADF means dirp does not refer to an open directory stream. ENOENT means the current position of the directory stream is invalid.

int readdir_r(DIR* dirp, struct dirent* entry, struct dirent** result)Initialises entry to represent the directory entry at the current position in dirp, store a pointer to this structure at the location referenced by result, and positions the directory stream at the next entry. The storage pointed to by entry will be large enough for a dirent with an array of char d_name member containing at least NAME_MAX plus one elements. On successful return, the pointer returned at *result will have the same value as the argument entry. Upon reaching the end of the directory stream, this pointer will have the value NULL.

errno Errors: EBADF means dirp does not refer to an open directory stream.

void rewinddir(DIR* dirp)Resets the position of the directory stream to which dirp refers to the beginning of the directory. It also causes the directory stream to refer to the current state of the corresponding directory, as a call to opendir() would have done. If dirp does not refer to a directory stream, the effect is undefined.
void seekdir(DIR* dirp, long int loc)Sets the position of the next readdir() operation on the directory stream specified by dirp to the position specified by loc. The value of loc should have been returned from an earlier call to telldir(). The new position reverts to the one associated with the directory stream when telldir() was performed. If the value of loc was not obtained from an earlier call to telldir() or if a call to rewinddir() occurred between the call to telldir() and the call to seekdir(), the results of subsequent calls to readdir() are unspecified.
long int telldir(DIR*)Obtains the current location associated with the directory stream specified by dirp. If the most recent operation on the directory stream was a seekdir(), the directory position returned from the telldir() is the same as that supplied as a loc argument for seekdir(). Upon successful completion, telldir() returns the current location of the specified directory stream.

 Member constants

Constants defined in the stdio.h header include:

NameNotes
NAME_MAXThe maximum length of the char array d_name.

 Member types

Data types defined in the dirent.h header include:

  • DIR - A structure representing a directory stream. Its structure is not defined by POSIX, and is usually opaque to users.
  • struct dirent - A structure with the following members:
  • ino_t d_ino - file serial number
  • char d_name[] - name of entry (will not exceed a size of NAME_MAX)
  • In addition, struct dirent may contain the following members, depending on the platform:
  • off_t d_off - file offset
  • unsigned short int d_reclen - length of name
  • unsigned short int d_namlen - length of name (alternative to d_reclen)
  • unsigned char d_type - type of file

 

Example

A short example of dirent.h usage is:

 

 

 Advanced Programming in the Unix

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值