linux下c/c++编程--学习笔记(头文件)

一些常用头文件
功能描述: 

获取当前内核名称和其它信息。
用法: 
#include <sys/utsname.h>
extern int uname (struct utsname *__name) __THROW;

参数: 
__name:指向存放系统信息的缓冲区,原型如下
struct utsname
  { char sysname[_UTSNAME_SYSNAME_LENGTH];//当前操作系统名
   char nodename[_UTSNAME_NODENAME_LENGTH];//网络上的名称
   char release[_UTSNAME_RELEASE_LENGTH];//当前发布级别
   char version[_UTSNAME_VERSION_LENGTH];//当前发布版本
   char machine[_UTSNAME_MACHINE_LENGTH];//当前硬件体系类型
#if _UTSNAME_DOMAIN_LENGTH - 0
    /* Name of the domain of this node on the network.  */
# ifdef __USE_GNU
    char domainname[_UTSNAME_DOMAIN_LENGTH]; //当前域名
# else
    char __domainname[_UTSNAME_DOMAIN_LENGTH];
# endif
#endif
  };
返回说明: 
成功执行时,返回0。失败返回-1,errno被设为EFAULT,表示buf无效。

//
 #include <unistd.h>
  是POSIX标准定义的unix类系统定义符号常量的头文件,包含了许多UNIX系统服务的函数原型,例如read函数、write函数和getpid函数
  #ifndef _UNISTD_H
  #define _UNISTD_H
  #include <features.h>
  unistd.h含有的常量与函数:
  ssize_t read(int, void *, size_t);
  int unlink(const char *);
  ssize_t write(int, const void *, size_t);
  int usleep(useconds_t);
  unsigned sleep(unsigned);
  int access(const char *, int);
  unsigned alarm(unsigned);
  int chdir(const char *);
  int chown(const char *, uid_t, gid_t);
  int close(int);
  size_t confstr(int, char *, size_t);
  void _exit(int);
  pid_t fork(void);
  NULL // Null pointer
  SEEK_CUR // Set file offset to current plus offset.
  SEEK_END // Set file offset to EOF plus offset.
  SEEK_SET // Set file offset to offset.

//
 #include <sys/ty pes.h>

基本系统数据类型

是Unix/Linux系统的基本系统数据类型的头文件,含有size_t,time_t,pid_t等类型。

NAME
       sys pes.h - data types

SYNOPSIS
       #include <sys pes.h>

DESCRIPTION
       The <sys pes.h> header shall include definitions for at least the following types:

       blkcnt_t
              Used for file block counts.

       blksize_t
              Used for block sizes.

       clock_t
              Used for system times in clock ticks or CLOCKS_PER_SEC; see <time.h> .

       clockid_t
              Used for clock ID type in the clock and timer functions.

       dev_t  Used for device IDs.

       fsblkcnt_t
              Used for file system block counts.

       fsfilcnt_t
              Used for file system file counts.

       gid_t  Used for group IDs.

       id_t   Used as a general identifier; can be used to contain at least a pid_t, uid_t, or gid_t.

       ino_t  Used for file serial numbers.

       key_t  Used for XSI interprocess communication.

       mode_t Used for some file attributes.

       nlink_t
              Used for link counts.

       off_t  Used for file sizes.

       pid_t  Used for process IDs and process group IDs.

       size_t Used for sizes of objects.

       ssize_t
              Used for a count of bytes or an error indication.

       suseconds_t
              Used for time in microseconds.

       time_t Used for time in seconds.

       timer_t
              Used for timer ID returned by timer_create().

       trace_attr_t
              Used to identify a trace stream attributes object.

       trace_event_id_t
              Used to identify a trace event type.

       trace_event_set_t
              Used to identify a trace event type set.

       trace_id_t
              Used to identify a trace stream.

       uid_t  Used for user IDs.

       useconds_t
              Used for time in microseconds.

       All of the types shall be defined as arithmetic types of an appropriate length, with the following exceptions:

       key_t
       Additionally:

        * mode_t shall be an integer type.

        * nlink_t, uid_t, gid_t, and id_t shall be integer types.

        * blkcnt_t and off_t shall be signed integer types.

        * fsblkcnt_t, fsfilcnt_t,   and ino_t shall be defined as unsigned integer types.

        * size_t shall be an unsigned integer type.

        * blksize_t, pid_t, and ssize_t shall be signed integer types.

        * time_t and clock_t shall be integer or real-floating types. 




//
#i nclude<sys/stat.h>
int stat(const char *restrict pathname,struct stat *restrict buf);
int fstat(int fields,struct stat *buf);
int lstat(const char *restrict pathname,struct stat *restrict buf);


返回值:若成功则返回0,失败则返回-1


一旦给出pathname,stat函数就返回与此命名文件有关的信息结构,fstat函数获取已在描述符fields上打开文件的有关信息。
lstat函数类似于stat.但是当命名的文件是一个符号链接时,lstat返回该符号链接的有关信息,而不是由该符号链接引用文件
的信息。第二个参数buf是指针,它指向一个我们必须提供的结构,这些函数填写由buf指向的结构。该结构的实际定义可能随实现
有所不同.
   struct stat{
mode_t st_mode; //文件类型和权限信息
ino_t st_ino; //i结点标识
dev_t st_dev; //device number (file system)
dev_t st_rdev; //device number for special files
nlink_t st_nlink; //符号链接数
uid_t st_uid; //用户ID
gid_t st_gid; //组ID
off_t st_size; //size in bytes,for regular files
time_t st_st_atime; //最后一次访问的时间
time_t st_mtime; //文件内容最后一次被更改的时间
time_t st_ctime; //文件结构最后一次被更改的时间
blksize_t st_blksize; //best I/O block size
blkcnt_t st_blocks; //number of disk blocks allocated
};
文件类型:
普通文件,目录文件,块特殊文件,字符特殊文件,套接字,FIFO,符号链接.
文件类型信息包含在stat结构的st_mode成员中,可以用如下的宏确定文件类型,这些宏是stat结构中的st_mode成员.
S_ISREG();S_ISDIR();S_ISCHR();S_ISBLK();S_ISFIFO();S_ISLNK();S_ISSOCK()


示例:
     #i nclude<iostream>
     int main(int argc,char* argv[])
     {
         int i;
        struct stat buf;
        char * ptr;
       
        for(i=1;i<argc;i++)
         {
            if(lstat(argv[i],&buf)<0)
               {
                 perror(”错误原因是:”);
                 continue;
               }


            if (S_ISREG(buf.st_mode))
                ptr=”普通文件”;
            if (S_ISDIR(buf.st_mode))
                ptr=”目录”;
           
            //……and so on…
           
           cout<<”参数为:”<<argv[i]<<”的标识是一个”<<ptr<<endl;
         }
        exit(0);
     }


pwd.h - password structure
SYNOPSIS

#include <pwd.h>

DESCRIPTION

The <pwd.h> header shall provide a definition for struct passwd, which shall include at least the following members:

char    *pw_name   User's login name. 
uid_t    pw_uid    Numerical user ID. 
gid_t    pw_gid    Numerical group ID. 
char    *pw_dir    Initial working directory. 
char    *pw_shell  Program to use as shell. 

The gid_t and uid_t types shall be defined as described in <sys/types.h> .

The following shall be declared as functions and may also be defined as macros. Function prototypes shall be provided.

struct passwd *getpwnam(const char *);
struct passwd *getpwuid(uid_t);
[TSF][Option Start]
int            getpwnam_r(const char *, struct passwd *, char *,
                   size_t, struct passwd **);
int            getpwuid_r(uid_t, struct passwd *, char *,
                   size_t, struct passwd **);
[Option End]
[XSI][Option Start]
void           endpwent(void);
struct passwd *getpwent(void);
void           setpwent(void);
[Option End]

/
NAME
errno.h - system error numbers
SYNOPSIS

#include <errno.h>

DESCRIPTION
[CX]  [Option Start] Some of the functionality described on this reference page extends the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of IEEE Std 1003.1-2001 defers to the ISO C standard.  [Option End]

[CX] [Option Start] The ISO C standard only requires the symbols [EDOM], [EILSEQ], and [ERANGE] to be defined. [Option End]

The <errno.h> header shall provide a declaration for errno and give positive values for the following symbolic constants. Their values shall be unique except as noted below.

[E2BIG]
Argument list too long.
[EACCES]
Permission denied.
[EADDRINUSE]
Address in use.
[EADDRNOTAVAIL]
Address not available.
[EAFNOSUPPORT]
Address family not supported.
[EAGAIN]
Resource unavailable, try again (may be the same value as [EWOULDBLOCK]).
[EALREADY]
Connection already in progress.
[EBADF]
Bad file descriptor.
[EBADMSG]
Bad message.
[EBUSY]
Device or resource busy.
[ECANCELED]
Operation canceled.
[ECHILD]
No child processes.
[ECONNABORTED]
Connection aborted.
[ECONNREFUSED]
Connection refused.
[ECONNRESET]
Connection reset.
[EDEADLK]
Resource deadlock would occur.
[EDESTADDRREQ]
Destination address required.
[EDOM]
Mathematics argument out of domain of function.
[EDQUOT]
Reserved.
[EEXIST]
File exists.
[EFAULT]
Bad address.
[EFBIG]
File too large.
[EHOSTUNREACH]
Host is unreachable.
[EIDRM]
Identifier removed.
[EILSEQ]
Illegal byte sequence.
[EINPROGRESS]
Operation in progress.
[EINTR]
Interrupted function.
[EINVAL]
Invalid argument.
[EIO]
I/O error.
[EISCONN]
Socket is connected.
[EISDIR]
Is a directory.
[ELOOP]
Too many levels of symbolic links.
[EMFILE]
Too many open files.
[EMLINK]
Too many links.
[EMSGSIZE]
Message too large.
[EMULTIHOP]
Reserved.
[ENAMETOOLONG]
Filename too long.
[ENETDOWN]
Network is down.
[ENETRESET]
Connection aborted by network.
[ENETUNREACH]
Network unreachable.
[ENFILE]
Too many files open in system.
[ENOBUFS]
No buffer space available.
[ENODATA]
[XSR]  [Option Start] No message is available on the STREAM head read queue.  [Option End]
[ENODEV]
No such device.
[ENOENT]
No such file or directory.
[ENOEXEC]
Executable file format error.
[ENOLCK]
No locks available.
[ENOLINK]
Reserved.
[ENOMEM]
Not enough space.
[ENOMSG]
No message of the desired type.
[ENOPROTOOPT]
Protocol not available.
[ENOSPC]
No space left on device.
[ENOSR]
[XSR]  [Option Start] No STREAM resources.  [Option End]
[ENOSTR]
[XSR]  [Option Start] Not a STREAM.  [Option End]
[ENOSYS]
Function not supported.
[ENOTCONN]
The socket is not connected.
[ENOTDIR]
Not a directory.
[ENOTEMPTY]
Directory not empty.
[ENOTSOCK]
Not a socket.
[ENOTSUP]
Not supported.
[ENOTTY]
Inappropriate I/O control operation.
[ENXIO]
No such device or address.
[EOPNOTSUPP]
Operation not supported on socket.
[EOVERFLOW]
Value too large to be stored in data type.
[EPERM]
Operation not permitted.
[EPIPE]
Broken pipe.
[EPROTO]
Protocol error.
[EPROTONOSUPPORT]
Protocol not supported.
[EPROTOTYPE]
Protocol wrong type for socket.
[ERANGE]
Result too large.
[EROFS]
Read-only file system.
[ESPIPE]
Invalid seek.
[ESRCH]
No such process.
[ESTALE]
Reserved.
[ETIME]
[XSR]  [Option Start] Stream  ioctl() timeout.  [Option End]
[ETIMEDOUT]
Connection timed out.
[ETXTBSY]
Text file busy.
[EWOULDBLOCK]
Operation would block (may be the same value as [EAGAIN]).
[EXDEV]
Cross-device link.



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值