函數(shù)名: fputs
功 能: 送一個字符串到一個流中
頭文件: #include<stdio.h>
用 法: int fputs(char * string, FILE * stream);
unix小知識:對設(shè)備文件 /dev/tty 讀,相當(dāng)于讀鍵盤輸入;對其寫,相當(dāng)于輸出到屏幕
函數(shù)名: open
功 能: 打開一個文件用于讀或?qū)?br>頭文件: #include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
用 法: int open(char * pathname, int how); //how 為 O_RDONLY , O_WRONLY, O_RDWR
函數(shù)名: read
功 能: 從文件中讀
頭文件: #include<unistd.h>
用 法: int read(int fd, void *buf, int nbyte);
函數(shù)名: write
功 能: 寫到一文件中
頭文件: #include<unistd.h>
用 法: int write(int fd, void *buf, int nbyte);
函數(shù)名: close
功 能: 關(guān)閉文件句柄
頭文件: #include<unistd.h>
用 法: int close(int fd);
unix小知識:
1. /var/adm/utmp 保存了所有用戶的登陸信息,其信息結(jié)構(gòu)描述是在/usr/include/utmp.h中描述,可以用UTMP_FILE代指utmp文件的路徑,因為在utmp.h中用宏定義了#define UTMP_FILE "/var/adm/utmp"
2.utmp結(jié)構(gòu)體類型中有個ut_type字段,其含義為用戶是否已登陸,若其值為7(#define USER_PROCESS 7),則代表該用戶已登陸。
3.在/usr/include/time.h中時間類型的定義為(typedef long int time_h;),則隱含時間是個長整型的類型,在unix中有一個函數(shù)ctime()可以將整數(shù)值的時間轉(zhuǎn)化為人們?nèi)粘K褂玫臅r間形式(如: Jun 30 21:49:08 1993)其聲明如下:
函數(shù)名: ctime
功 能: 把日期和時間轉(zhuǎn)換為字符串
頭文件: #include<time.h>
用 法: char *ctime(const time_t *time);
函數(shù)名: creat
功 能: 創(chuàng)建一個新文件,如果這個文件不存在,就創(chuàng)建它,如果已經(jīng)存在,就把它的內(nèi)容清空,把文件的長度設(shè)置為0
頭文件: #include<fcntl.h>
返回值: 返回值是個整數(shù)fd,如果成功則返回文件的描述符,如果失敗則返回-1
用 法: int creat (const char *filename, int permiss);
permiss:為9段的文件允許位(rwxr--r--之類的),可用三個八進制數(shù)代替如(0644,0代表644是個8進制數(shù))
unix小知識:umask命令可以更改文件"新建文件掩碼值",他指定哪些位需要被關(guān)閉.
例如: umask 022(----w--w-),就可以防止程序創(chuàng)建能被同組用戶和其他用戶修改的文件.
函數(shù)名: write
功 能: 將內(nèi)存中的數(shù)據(jù)寫入文件
頭文件: #include<unistd.h>
返回值: 遇到錯誤返回-1 ,成功寫入返回 number written
用 法: int write(int fd, void *buf, int nbyte);
函數(shù)名: lseek
功 能: 移動文件讀/寫指針
頭文件: #include<sys/type.h>
#include<unistd.h>
用 法: long lseek(int fd, off_t offset, int base);
返回值: 遇到錯誤(-1),指針變化前的位置(oldpos)
base:有三種選擇SEEK_SET(文件開始),SEEK_CUR(當(dāng)前位置),SEEK_END(文件結(jié)尾)
unix小知識: 返回值-1表示系統(tǒng)調(diào)用在運行中出了些差錯,一旦檢測到錯誤必須做相應(yīng)的處理
內(nèi)核通過全局變量errno來指明錯誤的類型,每個程序都可以訪問到這個變量,可以通過
man 3 errno來查看出錯編號的含義
函數(shù)名: opendir
功 能: 打開一個目錄
頭文件: #include<sys/types.h>
#include<dirent.h>
用 法: DIR * opendir(const char *name);
函數(shù)名: readdir
功 能: 讀目錄項的內(nèi)容
頭文件: #include<sys/types.h>
#include<dirent.h>
用 法: struct dirent * readdir(DIR * dir);
返回值: 返回一個目錄項的首地址
struct dirent{
ino_t d_ino;
off_t d_off;
unsigned short d_reclen;
unsigned char d_type;
char d_name[256];
};
函數(shù)名: stat
功 能: 得到文件的屬性
頭文件: #include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
用 法: int stat(char *pathname, struct stat *buff);
返回值: -1遇到錯誤;0成功返回
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes文件大小 */
blksize_t st_blksize; /* blocksize for filesystem I/O */
blkcnt_t st_blocks; /* number of blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
函數(shù)名: sscanf
功 能: 執(zhí)行從字符串中的格式化輸入
用 法: int sscanf(char *string, char *format, ... );
unix小知識: 若要找某個數(shù)據(jù)結(jié)構(gòu)的定義,可以在shell下輸入man -k <關(guān)鍵字>
函數(shù)名: getpwuid
功 能: 使用uid(user ID)作為參數(shù)返回一個指向struct passwd的指針
頭文件: #include<sys/types.h>
#include<pwd.h>
用 法: struct passwd * getpwuid(uid_t uid);
返回值: 出錯則返回NULL,成功則返回指向struct passwd的指針
/* The passwd structure. */
struct passwd
{
char *pw_name; /* Username. */
char *pw_passwd; /* Password. */
__uid_t pw_uid; /* User ID. */
__gid_t pw_gid; /* Group ID. */
char *pw_gecos; /* Real name. */
char *pw_dir; /* Home directory. */
char *pw_shell; /* Shell program. */
};
函數(shù)名: getgrgid
功 能: 使用gid(group ID)作為參數(shù)返回一個指向struct group的指針
頭文件: #include<sys/types.h>
#include<grp.h>
用 法: struct group * getgrgid(gid_t gid);
返回值: 出錯則返回NULL,成功則返回指向struct group的指針
/* The group structure. */
struct group
{
char *gr_name; /* Group name. */
char *gr_passwd; /* Password. */
__gid_t gr_gid; /* Group ID. */
char **gr_mem; /* Member list. */
};
函數(shù)名: chmod
功 能: 改變文件的訪問模式(rwxrwxrwx)
頭文件: #include<sys/types.h>
#include<sys/stat.h>
用 法: int chmod(char * path,mode_t mode);
返回值: -1遇到錯誤,0成功返回,此系統(tǒng)調(diào)用不受"新建文件掩碼"(umask)的影響
e.g. : chmod("/tmp/myfile",04764);
函數(shù)名: chown
功 能: 改變文件的訪問模式(rwxrwxrwx)
頭文件: #include<unistd.h>
用 法: int chown(char * path,uid_t owner,gid_t group);
返回值: -1遇到錯誤,0成功返回,如果后兩個參數(shù)都為-1,那么文件所有者和組都不會改變。
文件所有者可以把文件的組改成任何一個他所屬的組。
函數(shù)名: utime
功 能: 修改文件最后修改時間和最后訪問時間
頭文件: #include<sys/time.h>
#include<utime.h>
#include<sys/types.h>
用 法: int utime(char * path,struct utimbuf * newtimes);
返回值: -1遇到錯誤,0成功返回
/* Structure describing file times. */
struct utimbuf
{
__time_t actime; /* Access time. */
__time_t modtime; /* Modification time. */
};
unix小知識: 用touch命令可以修改文件的最后訪問時間和最后修改時間
函數(shù)名: rename
功 能: 修改文件/目錄的名字或移動文件的位置
頭文件: #include<stdio.h>
用 法: int rename(char * old,char * new);
返回值: -1遇到錯誤,0成功返回
tips: 命令mv即調(diào)用此函數(shù)
函數(shù)名: mkdir
功 能: 創(chuàng)建一個目錄
頭文件: #include<sys/stat.h>
#include<sys/types.h>
用 法: int mkdir(const char * pathname,mode_t mode);
返回值: -1遇到錯誤,0成功返回
tips: mode 即為權(quán)限位的掩碼
函數(shù)名: rmdir
功 能: 刪除一個目錄,此目錄必須為空
頭文件: #include<unistd.h>
用 法: int rmdir(const char * path);
返回值: -1遇到錯誤,0成功返回
tips: path 即為目錄名,且這個目錄必須為空。如果這個目錄并未被其他的任何進程占用,它的i節(jié)點和數(shù)據(jù)塊將被釋放。
函數(shù)名: unlink
功 能: 刪除一個鏈接(此處指的是硬鏈接)
頭文件: #include<unistd.h>
用 法: int unlink(const char * path);
返回值: -1遇到錯誤,0成功返回
tips: 命令rm即是調(diào)用此函數(shù)來刪除文件。unlink用來刪除目錄文件中的一個記錄,減少相應(yīng)i節(jié)點的鏈接數(shù)。如果該i節(jié)點的鏈接數(shù)減為0,數(shù)據(jù)塊和i節(jié)點將被釋放。unlink不能被用來刪除目錄。
函數(shù)名: link
功 能: 創(chuàng)建一個文件的新鏈接(此處指的是硬鏈接)
頭文件: #include<unistd.h>
用 法: int link(const char * oldpath,const char * newpath);
返回值: -1遇到錯誤,0成功返回
tips: 命令ln即調(diào)用此函數(shù)。
函數(shù)名: chdir
功 能: 改變所調(diào)用進程的當(dāng)前目錄
頭文件: #include<unistd.h>
用 法: int chdir(const char * path);
返回值: -1遇到錯誤,0成功返回
tips: 命令cd即調(diào)用此函數(shù)。
函數(shù)名: strncpy
功 能: 串拷貝
用 法: char *strncpy(char * dest, char * src, int maxlen);
Note: 此串拷貝只是簡單的按照maxlen規(guī)定從源串拷貝到目的串, 所以需要手動將目的串后加'\0'
函數(shù)名: perror
功 能: 根據(jù)全局變量errno的值,打印一個系統(tǒng)錯誤消息
頭文件: #include<stdio.h>
#include<errno.h> //errno 的定義在此頭文件中
用 法: void perror(const char *s);
返回值: -1遇到錯誤,0成功返回
例如 : 若此時errno的值為10 ,則調(diào)用perror("hello!");后則屏幕輸出如下信息:
hello!: No child processes
函數(shù)名: symlink
功 能: 為原始文件創(chuàng)建一個符號鏈接
頭文件: #include<unistd.h>
用 法: int symlink(const char *path1,const char * path2);
返回值: -1遇到錯誤,0成功返回
NOTE : 注意path2為符號鏈接文件的路徑,path1為源文件的路徑
ln -s 即調(diào)用此函數(shù)
函數(shù)名: readlink
功 能: 讀取符號鏈接的值(其實就是讀取原始文件的路徑字符串)
頭文件: #include<unistd.h>
用 法: int readlink(const char * path,char * buf,size_t bufsiz);
返回值: 若成功則返回放到緩沖buf里的字符個數(shù),若失敗則返回-1
NOTE : 此函數(shù)讀取符號鏈接字串的前bufsiz個字符,到緩沖buf中,且此函數(shù)并不會為保存到buf中的字符串后面添加'\0',需要你自己添加
unix小知識: man 命令可查找到很多有用的東西, 文檔1為命令部分,2為系統(tǒng)調(diào)用部分。3為標(biāo)準(zhǔn)C函數(shù)部分
///////////////////////////////////////////////////////////////////////////////////////
目前還不懂的地方:
1.設(shè)備文件的i節(jié)點存儲的是指向內(nèi)核子程序的指針,而不是文件的大小和存儲列表。內(nèi)核中傳輸設(shè)備數(shù)據(jù)的子程序被稱為設(shè)備驅(qū)動程序。
2.如用ls命令顯示但前終端文件的詳細(xì)信息有如下:
crw--w---- 1 root tty 136, 0 May 16 17:58 /dev/pts/0
其中的“136, 0” 表示的是 從終端進行數(shù)據(jù)傳輸?shù)拇a是在設(shè)備——進程表中編號為136
的子程序。該子程序接受一個整形參數(shù)。在/dev/pts/0 中,參數(shù)是0 。136和0這兩個數(shù)被
稱為設(shè)備的主設(shè)備號和次設(shè)備號。
主設(shè)備號確定處理該設(shè)備實際的子程序,
而從設(shè)備號被作為參數(shù)傳輸?shù)皆撟映绦颉?br>3.每個i節(jié)點編號指向i節(jié)點表中的一個結(jié)構(gòu)。i節(jié)點可以是磁盤文件的也可以是設(shè)備文件的。i節(jié)點的類型被記錄在結(jié)構(gòu)stat的成員變量st_mode的類型區(qū)域中。
磁盤文件的i節(jié)點包含指向數(shù)據(jù)塊的指針。設(shè)備文件的i節(jié)點包含指向內(nèi)核子程序表的指針。主設(shè)備號用于告知從設(shè)備讀取數(shù)據(jù)的那部分代碼的位置。
///////////////////////////////////////////////////////////////////////////////////////
函數(shù)名: fcntl
功 能: 控制文件描述符
頭文件: #include<fcntl.h>
#include<unistd.h>
#include<sys/types.h>
用 法: int fcntl(int fd,int cmd);
int fcntl(int fd,int cmd,long arg);
int fcntl(int fd,int cmd,struct flock * lockp);
參數(shù) : cmd需進行的操作,arg操作的參數(shù),lockp鎖信息
返回值: -1遇到錯誤,other 依操作而定
舉例:
1.關(guān)閉內(nèi)核對fd描述符的緩沖機制
#include<fcntl.h>
int s;
s = fcntl(fd,F_GETFL);
s |= O_SYNC;
result = fcntl(fd,F_SETFL,s);
if( result == 1 )
perror("setting SYNC");
2.自動添加模式(為了避免同一時刻兩個進程對同一文件的操作,也即"競爭",內(nèi)核提供了一個簡單的解決辦法:自動添加模式。當(dāng)文件描述符的O_APPEND位被開啟后,每個對write的調(diào)用自動調(diào)用lseek將內(nèi)容添加到文件末尾。)
#include<fcntl.h>
int s;
s = fcntl(fd,F_GETFL);
s |= O_APPEND;
result = fcntl(fd,F_SETFL,s);
if( result == 1 )
perror("setting APPEND");
else
write(fd,&rec,1);
unix小知識: 用open函數(shù)也可以控制文件描述符如:
fd = open(WTMP_FILE,O_WRONLY | O_APPEND | O_SYNC);
open支持的其他標(biāo)志位:
O_CREAT 如果不存在,創(chuàng)建該文件。
O_TRUNC 如果文件存在,將文件長度置0
O_EXCL O_EXCL標(biāo)志位防止兩個進程創(chuàng)建同樣的文件。如果文件存在且O_EXCL被置位,則返 回-1.
unix命令:
stty命令可以讀取和修改驅(qū)動程序的設(shè)置
stty 顯示終端配置信息
stty -all 顯示所有配置信息
stty -echo 消除鍵盤回顯 stty echo 打開鍵盤回顯
函數(shù)名: tcgetattr
功 能: 讀取tty驅(qū)動程序的屬性
頭文件: #include<termios.h>
#include<unistd.h>
用 法: int tcgetattr(int fd,struct termios * info);
返回值: -1遇到錯誤,0成功返回
NOTE : info指向終端結(jié)構(gòu)的指針
函數(shù)名: tcsetattr
功 能: 設(shè)置tty驅(qū)動程序的屬性
頭文件: #include<termios.h>
#include<unistd.h>
用 法: int tcsetattr(int fd,int when,struct termios * info);
返回值: -1遇到錯誤,0成功返回
NOTE : info指向終端結(jié)構(gòu)的指針,when改變設(shè)置的時間
when的允許值如下:
(1)TCSANOW 立即更新驅(qū)動程序設(shè)置
(2)TCSADRAIN 和 TCSAFLUSH 等等
struct termio
{
unsigned short c_iflag; /* 輸入模式標(biāo)志 */
unsigned short c_oflag; /* 輸出模式標(biāo)志 */
unsigned short c_cflag; /* 控制模式標(biāo)志*/
unsigned short c_lflag; /* local mode flags */
unsigned char c_line; /* line discipline */
unsigned char c_cc[NCC]; /* control characters */
};
小知識: 若要查找termios中的域變量的詳細(xì)用法請參考man tcgetattr
例子 : 以下代碼為一個連接開啟字符回顯
#include<termios.h>
struct termios settings;
tcgetattr(fd,&settings);
settings.c_lflag |= ECHO;
tcsetattr(fd,TCSANOW,&settings);
函數(shù)名: ioctl
功 能: 控制一個設(shè)備
頭文件: #include<sys/ioctl.h>
用 法: int ioctl(int fd,int operation [,arg ...]);
返回值: -1遇到錯誤,other依錯誤而定
NOTE : operation需進行的操作,操作所需的參數(shù)
緩沖和編輯包含規(guī)范處理(canonical processing)。當(dāng)這些特征被啟動時,終端連接被稱為處于規(guī)范模式。
函數(shù)名: strchr
頭文件: #include<string.h>
功 能: 在一個串中查找給定字符的第一個匹配之處
用 法: char * strchr(char *str, char c);
返回值: 返回字符串中第一個匹配字符的地址
函數(shù)名: signal
功 能: 簡單的信號處理
頭文件: #include<signal.h>
用 法: int signal(int signum,void (* action)(int) );
返回值: -1遇到錯誤,prevaction 成功返回
NOTE : SIG_IGN 忽略信號 SIG_DFL 將信號恢復(fù)為默認(rèn)處理
函數(shù)名: alarm
功 能: 設(shè)置發(fā)送信號的計時器
頭文件: #include<unistd.h>
用 法: unsigned old = alarm(unsigned seconds);
返回值: -1遇到錯誤,old 計時器剩余時間
說 明: alarm設(shè)置本進程的計時器到seconds秒后激發(fā)信號。當(dāng)設(shè)定的時間過去后,內(nèi)核發(fā)送SIGALRM到這個進程。如果計時器已被,alarm返回剩余秒數(shù)(注意: 調(diào)用alarm(0)意味著關(guān)掉鬧鐘)
函數(shù)名: pause
功 能: 等待信號
頭文件: #include<unistd.h>
用 法: result = pause();
返回值: 總是-1
***************************************************************************************
* 以下為網(wǎng)絡(luò)函數(shù) *
***************************************************************************************
函數(shù)名: htonl
htons
功 能: 主機字節(jié)序到網(wǎng)絡(luò)字節(jié)序的轉(zhuǎn)化
頭文件: #include<netient/in.h>
用 法: unsigned long int htonl(unsigned long int hostlong);
unsigned short int htons(unsigned short int hostshort);
返回值: 按照網(wǎng)絡(luò)字節(jié)順序的值
函數(shù)名: ntohl
ntohs
功 能: 網(wǎng)絡(luò)字節(jié)序到主機字節(jié)序的轉(zhuǎn)化
頭文件: #include<netient/in.h>
用 法: unsigned long int ntohl(unsigned long int netlong);
unsigned short int ntohs(unsigned short int netshort);
返回值: 按照主機字節(jié)順序的值
函數(shù)名: inet_aton
功 能: 點分十進制字符串到32位的IP地址(此IP地址為網(wǎng)絡(luò)字節(jié)序)
頭文件: #include<arpa/inet.h>
用 法: int inet_aton(const char * cp,struct in_addr * inp);
返回值: 若成功則返回1,出錯則返回0
IP地址結(jié)構(gòu)體的定義:
/* Internet address structure */
struct in_addr {
unsigned int s_addr; /* network byte order(big-endian) */
};
函數(shù)名: inet_ntoa
功 能: 32位的IP地址(此IP地址為網(wǎng)絡(luò)字節(jié)序)到點分十進制字符串
頭文件: #include<arpa/inet.h>
用 法: char * inet_ntoa(strct in_addr in);
返回值: 指向點分十進制的字符串的首地址
函數(shù)名: gethostbyname
功 能: 由主機名(hostname)得到hostent結(jié)構(gòu)體信息的首地址(其定義如下)
頭文件: #include<netdb.h>
用 法: struct hostent * gethostbyname(const char * name);
返回值: 若成功則返回得到的hostent結(jié)構(gòu)的首地址,出錯則返回NULL指針,同時置h_errno
函數(shù)名: gethostbyaddr
功 能: 由32位的IP地址得到hostent結(jié)構(gòu)體信息的首地址(其定義如下)
頭文件: #include<netdb.h>
用 法: struct hostent * gethostbyaddr(const void * addr,int len,0);
返回值: 若成功則返回得到的hostent結(jié)構(gòu)的首地址,出錯則返回NULL指針,同時置h_errno
struct hostent
{
char *h_name; /* Official name of host. */
char **h_aliases; /* Alias list. */
int h_addrtype; /* Host address type. */
int h_length; /* Length of address. */
char **h_addr_list; /* List of addresses from name server. */
};