[問題] strerror 跟 hstrerror 有何不同

看板C_and_CPP (C/C++)作者 ( )時間13年前 (2012/12/01 00:42), 編輯推噓0(0014)
留言14則, 3人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) GNU GCC/G++ 問題(Question): 今天看朋友的程式 才知道有 hstrerror 這個函式, 而我朋友傳入 h_errno 給他 不過 man page 寫的讓我很困惑, The (obsolete) hstrerror() function takes an error number (typically h_errno) and returns the corresponding message string. 這邊的 obsolete 到底是指什麼呢? http://ftp.suacommunity.com/man/3/strerror.3.html 然後這裡寫的更讓我困惑@@ The hstrerror(3) call is a #define for strerror(3), provided for convenience in working with sockets. 所以這兩個根本沒有差嗎? 可是好像 h_errno, errno 是不同變數, hstrerror 跟 strerror 也是不同函數. 所以這兩個函數有差嗎? 印 socket 相關的錯誤訊息 (如 gethostbyname 錯誤) 應該使用 hstrerror 嗎? man page 裡面又寫 RETURN VALUE The gethostbyname() and gethostbyaddr() functions return the hos- tent structure or a NULL pointer if an error occurs. On error, the h_errno variable holds an error number. When non-NULL, the return value may point at static data, see the notes below. 所以我使用 errno 就夠了? 謝謝! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.166.45.226

12/01 14:03, , 1F
文件寫得很清楚,hstrerror()只是一個使用#define去轉呼
12/01 14:03, 1F

12/01 14:06, , 2F
叫strerror的macro而已,而你問的"obsolete"是指現在來說
12/01 14:06, 2F

12/01 14:06, , 3F
hstrerror這個function已經不建議用了. 為什麼會用macro
12/01 14:06, 3F

12/01 14:07, , 4F
define的方式把hstrerror轉呼叫到strerror呢? 你好好想
12/01 14:07, 4F

12/01 14:08, , 5F
想,其實這是為了讓一些舊的程式是使用hstrerror的,能夠
12/01 14:08, 5F

12/01 14:09, , 6F
在不改原始碼的情況下,能夠被compile成功. 總而言之, 這
12/01 14:09, 6F

12/01 14:10, , 7F
個hstrerror算是有歷史的東西,但後來處理error number的
12/01 14:10, 7F

12/01 14:10, , 8F
部份就統一用strerror了,hstrerror前面的"h"其實可能在
12/01 14:10, 8F

12/01 14:12, , 9F
當時是for hostxxx相關的function用的. 所以用errno 和
12/01 14:12, 9F

12/01 14:12, , 10F
strerror就好了!
12/01 14:12, 10F

12/01 18:31, , 11F
了解了, 謝謝你!
12/01 18:31, 11F

12/01 18:42, , 12F
因為我在我裝的 include 檔中, hstrerror 宣告成 extern,
12/01 18:42, 12F

12/01 18:42, , 13F
印出來也跟 strerror 不同位置, 所以很困惑
12/01 18:42, 13F

12/02 09:53, , 14F
obsolete == 「過時」 比 deprecated 更過時
12/02 09:53, 14F
文章代碼(AID): #1GkE7VqL (C_and_CPP)
文章代碼(AID): #1GkE7VqL (C_and_CPP)