SIO包是什么
SIO包是什么
首先SIO包并不是一個標準庫中的函數集合,這是CSAPP中為了方便樣例講解而創建的一個代碼庫中的一些函數的集合,我們的linux中默認肯定是沒有的 需要我們自己去導入,怎么導入呢,其實很簡單,就是把代碼拉下來放到/usr/include這個目錄當中,然后當我們需要使用時就可以當做一般的函數庫來使用了
這是csapp.h的源碼
這是csapp.c的源碼
SIO包中的函數
平時能用的上的函數就是這些
ssize_t Sio_putl(long v); ssize_t Sio_puts(char s[]) void Sio_error(char s[])Sio_putl
打印一個數字
Sio_puts
打印一個字符串
Sio_error
打印一條錯誤消息并終止程序
SIO包中的函數的作用
你一定很奇怪為什么我們有封裝好的標準輸入輸出函數 為什么還需要這樣幾個奇怪的函數呢 這就牽扯到了另外一個問題
編寫安全的信號處理函數
SIO包的源碼
/************************************************************** The Sio (Signal-safe I/O) package - simple reentrant output* functions that are safe for signal handlers.*************************************************************//* Private sio functions *//* $begin sioprivate */ /* sio_reverse - Reverse a string (from K&R) */ static void sio_reverse(char s[]) {int c, i, j;for (i = 0, j = strlen(s)-1; i < j; i++, j--) {c = s[i];s[i] = s[j];s[j] = c;} }/* sio_ltoa - Convert long to base b string (from K&R) */ static void sio_ltoa(long v, char s[], int b) {int c, i = 0;int neg = v < 0;if (neg)v = -v;do { s[i++] = ((c = (v % b)) < 10) ? c + '0' : c - 10 + 'a';} while ((v /= b) > 0);if (neg)s[i++] = '-';s[i] = '\0';sio_reverse(s); }/* sio_strlen - Return length of string (from K&R) */ static size_t sio_strlen(char s[]) {int i = 0;while (s[i] != '\0')++i;return i; } /* $end sioprivate *//* Public Sio functions */ /* $begin siopublic */ssize_t sio_puts(char s[]) /* Put string */ {return write(STDOUT_FILENO, s, sio_strlen(s)); //line:csapp:siostrlen }ssize_t sio_putl(long v) /* Put long */ {char s[128];sio_ltoa(v, s, 10); /* Based on K&R itoa() */ //line:csapp:sioltoareturn sio_puts(s); }void sio_error(char s[]) /* Put error message and exit */ {sio_puts(s);_exit(1); //line:csapp:sioexit } /* $end siopublic *//******************************** Wrappers for the SIO routines******************************/ ssize_t Sio_putl(long v) {ssize_t n;if ((n = sio_putl(v)) < 0)sio_error("Sio_putl error");return n; }ssize_t Sio_puts(char s[]) {ssize_t n;if ((n = sio_puts(s)) < 0)sio_error("Sio_puts error");return n; }void Sio_error(char s[]) {sio_error(s); }總結
- 上一篇: 三层交换机配置MSTP协议详解【华为eN
- 下一篇: 尚硅谷jenkins