Skip to content

Commit 8c9cee6

Browse files
committed
Reduce cut&paste in hs_get_h_errno: macro __HS_GET_H_ERRNO
1 parent de12926 commit 8c9cee6

File tree

1 file changed

+20
-39
lines changed

1 file changed

+20
-39
lines changed

cbits/hs_resolv.h

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@
3434
# error broken invariant
3535
#endif
3636

37+
/* Macro to calculate error code returned by hs_get_h_errno */
38+
#define __HS_GET_H_ERRNO(h_errno) \
39+
switch(h_errno) \
40+
{ \
41+
case HOST_NOT_FOUND: return 1; \
42+
case NO_DATA: return 2; \
43+
case NO_RECOVERY: return 3; \
44+
case TRY_AGAIN: return 4; \
45+
default: return -1; \
46+
}
47+
3748
#if USE_RES_NQUERY
3849

3950
inline static int
@@ -92,39 +103,16 @@ hs_res_close(struct __res_state *s)
92103

93104
#if defined(HAVE_DECL_H_ERRNO)
94105

95-
#if defined(HAVE_STRUCT___RES_STATE_RES_H_ERRNO)
96-
97106
inline static int
98107
hs_get_h_errno(struct __res_state *s)
99108
{
109+
#if defined(HAVE_STRUCT___RES_STATE_RES_H_ERRNO)
100110
assert(s);
101-
102-
switch(s->res_h_errno)
103-
{
104-
case HOST_NOT_FOUND: return 1;
105-
case NO_DATA: return 2;
106-
case NO_RECOVERY: return 3;
107-
case TRY_AGAIN: return 4;
108-
default: return -1;
109-
}
110-
}
111-
111+
__HS_GET_H_ERRNO(s->res_h_errno)
112112
#else
113-
114-
inline static int
115-
hs_get_h_errno(struct __res_state *s)
116-
{
117-
switch(h_errno)
118-
{
119-
case HOST_NOT_FOUND: return 1;
120-
case NO_DATA: return 2;
121-
case NO_RECOVERY: return 3;
122-
case TRY_AGAIN: return 4;
123-
default: return -1;
124-
}
125-
}
126-
113+
__HS_GET_H_ERRNO(h_errno)
127114
#endif
115+
}
128116

129117
#else
130118

@@ -134,9 +122,9 @@ hs_get_h_errno(struct __res_state *s)
134122
return -1;
135123
}
136124

137-
#endif
125+
#endif /* HAVE_DECL_H_ERRNO */
138126

139-
#else
127+
#else /* USE_RES_NQUERY */
140128

141129
/* use non-reentrant API */
142130

@@ -194,14 +182,7 @@ hs_res_close(void *s)
194182
inline static int
195183
hs_get_h_errno(void *s)
196184
{
197-
switch(h_errno)
198-
{
199-
case HOST_NOT_FOUND: return 1;
200-
case NO_DATA: return 2;
201-
case NO_RECOVERY: return 3;
202-
case TRY_AGAIN: return 4;
203-
default: return -1;
204-
}
185+
__HS_GET_H_ERRNO(h_errno)
205186
}
206187

207188
#else
@@ -212,8 +193,8 @@ hs_get_h_errno(void *s)
212193
return -1;
213194
}
214195

215-
#endif
196+
#endif /* HAVE_DECL_H_ERRNO */
216197

217-
#endif
198+
#endif /* USE_RES_NQUERY */
218199

219200
#endif /* HS_RESOLV_H */

0 commit comments

Comments
 (0)