@@ -40,6 +40,12 @@ typedef struct {
4040 xfile_t file ;
4141} xhost_ctx_t ;
4242
43+ typedef struct {
44+ char sAddr [XLINE_MAX ];
45+ char sHost [XLINE_MAX ];
46+ char sComment [XLINE_MAX ];
47+ } xhost_entry_t ;
48+
4349extern char * optarg ;
4450
4551static void XHost_Usage (const char * pName )
@@ -150,19 +156,102 @@ static int XHost_Write(xstring_t *pString)
150156 return XSTDNON ;
151157}
152158
159+ static void XHost_RemoveTailSpace (char * sEntry , size_t nLength )
160+ {
161+ if (nLength == 0 ) return ;
162+ int nPosit = (int )nLength - 1 ;
163+
164+ while (nPosit >= 0 &&
165+ (isspace ((unsigned char )sEntry [nPosit ]) ||
166+ sEntry [nPosit ] == '\n' )) nPosit -- ;
167+
168+ if (nPosit >= 0 ) sEntry [nPosit + 1 ] = XSTR_NUL ;
169+ }
170+
171+ static xbool_t XHost_ParseEntry (xhost_entry_t * pEntry , const char * pLine )
172+ {
173+ pEntry -> sAddr [0 ] = XSTR_NUL ;
174+ pEntry -> sHost [0 ] = XSTR_NUL ;
175+ pEntry -> sComment [0 ] = XSTR_NUL ;
176+
177+ // Skip empty spaces
178+ int nPosit = 0 ;
179+ while (pLine [nPosit ] && isspace ((unsigned char )pLine [nPosit ])) nPosit ++ ;
180+ if (!pLine [nPosit ]) return XFALSE ;
181+
182+ if (pLine [nPosit ] == '#' )
183+ {
184+ while (pLine [nPosit ] && (pLine [nPosit ] == '#' ||
185+ isspace ((unsigned char )pLine [nPosit ]))) nPosit ++ ;
186+
187+ if (!pLine [nPosit ]) return XFALSE ;
188+ xstrncpy (pEntry -> sComment , sizeof (pEntry -> sComment ), & pLine [nPosit ]);
189+ XHost_RemoveTailSpace (pEntry -> sComment , strlen (pEntry -> sComment ));
190+ return XTRUE ;
191+ }
192+
193+ // Get IP addr
194+ int nEnd = nPosit ;
195+ while (pLine [nEnd ] && !isspace ((unsigned char )pLine [nEnd ])) nEnd ++ ;
196+ if (!pLine [nPosit ]) return XFALSE ;
197+ xstrncpy (pEntry -> sAddr , sizeof (pEntry -> sAddr ), & pLine [nPosit ]);
198+ pEntry -> sAddr [nEnd - nPosit ] = XSTR_NUL ;
199+
200+ // Get host name
201+ nPosit = nEnd ;
202+ while (pLine [nPosit ] && isspace ((unsigned char )pLine [nPosit ])) nPosit ++ ;
203+ if (!pLine [nPosit ]) return XFALSE ;
204+ xstrncpy (pEntry -> sHost , sizeof (pEntry -> sHost ), & pLine [nPosit ]);
205+
206+ nPosit = 0 ;
207+ while (pEntry -> sHost [nPosit ] &&
208+ pEntry -> sHost [nPosit ] != '\n' &&
209+ pEntry -> sHost [nPosit ] != '#' ) nPosit ++ ;
210+
211+ if (pEntry -> sHost [nPosit ] == '#' )
212+ {
213+ pEntry -> sHost [nPosit ++ ] = XSTR_NUL ;
214+ while (pEntry -> sHost [nPosit ] && isspace ((unsigned char )pEntry -> sHost [nPosit ])) nPosit ++ ;
215+ xstrncpy (pEntry -> sComment , sizeof (pEntry -> sComment ), & pEntry -> sHost [nPosit ]);
216+
217+ nPosit = 0 ;
218+ while (pEntry -> sComment [nPosit ] && pEntry -> sComment [nPosit ] != '\n' ) nPosit ++ ;
219+ if (pEntry -> sComment [nPosit ] == '\n' ) pEntry -> sComment [nPosit ] = XSTR_NUL ;
220+ }
221+
222+ if (pEntry -> sHost [nPosit ] == '\n' ) pEntry -> sHost [nPosit ] = XSTR_NUL ;
223+ XHost_RemoveTailSpace (pEntry -> sAddr , strlen (pEntry -> sAddr ));
224+ XHost_RemoveTailSpace (pEntry -> sHost , strlen (pEntry -> sHost ));
225+ XHost_RemoveTailSpace (pEntry -> sComment , strlen (pEntry -> sComment ));
226+
227+ return XTRUE ;
228+ }
229+
153230static xbool_t XHost_SearchEntry (xhost_ctx_t * pCtx )
154231{
155232 xlogd_wn ("Checking entry: %s" , pCtx -> sLine );
156233
157- if (xstrused (pCtx -> sHost ) && xstrused (pCtx -> sAddr ))
234+ xhost_entry_t entry ;
235+ XHost_ParseEntry (& entry , pCtx -> sLine );
236+
237+ if (!xstrused (entry .sAddr ) ||
238+ !xstrused (entry .sHost )) return XFALSE ;
239+
240+ char sSearchLine [XLINE_MAX ];
241+ xstrncpyf (sSearchLine , sizeof (sSearchLine ), "%s %s" , pCtx -> sAddr , pCtx -> sHost );
242+
243+ xhost_entry_t search ;
244+ XHost_ParseEntry (& search , sSearchLine );
245+
246+ if (xstrused (search .sHost ) && xstrused (search .sAddr ))
158247 {
159- if (xstrsrc (pCtx -> sLine , pCtx -> sHost ) >= 0 &&
160- xstrsrc (pCtx -> sLine , pCtx -> sAddr ) >= 0 ) return XTRUE ;
248+ if (xstrsrc (entry . sHost , search . sHost ) >= 0 &&
249+ xstrsrc (entry . sAddr , search . sAddr ) >= 0 ) return XTRUE ;
161250 }
162- else
251+ else
163252 {
164- if (xstrused (pCtx -> sHost ) && xstrsrc (pCtx -> sLine , pCtx -> sHost ) >= 0 ) return XTRUE ;
165- if (xstrused (pCtx -> sAddr ) && xstrsrc (pCtx -> sLine , pCtx -> sAddr ) >= 0 ) return XTRUE ;
253+ if (xstrused (search . sHost ) && xstrsrc (entry . sHost , search . sHost ) >= 0 ) return XTRUE ;
254+ if (xstrused (search . sAddr ) && xstrsrc (entry . sAddr , search . sAddr ) >= 0 ) return XTRUE ;
166255 }
167256
168257 return XFALSE ;
@@ -175,10 +264,7 @@ static int XHost_AddEntry(xhost_ctx_t *pCtx, xbool_t bNewLine)
175264
176265 while (XFile_GetLine (& pCtx -> file , pCtx -> sLine , sizeof (pCtx -> sLine )) > 0 )
177266 {
178- int nPosit = 0 ;
179- while (pCtx -> sLine [nPosit ] && isspace ((unsigned char )pCtx -> sLine [nPosit ])) nPosit ++ ;
180-
181- if (pCtx -> sLine [nPosit ] && pCtx -> sLine [nPosit ] != '#' && XHost_SearchEntry (pCtx ))
267+ if (XHost_SearchEntry (pCtx ))
182268 {
183269 xlogd_wn ("Found entry: %s" , pCtx -> sLine );
184270 bFound = XTRUE ;
@@ -215,33 +301,44 @@ static int XHost_AddEntry(xhost_ctx_t *pCtx, xbool_t bNewLine)
215301
216302static int XHost_DisplayHosts ()
217303{
218- xbyte_buffer_t buffer ;
219- XASSERT ((XPath_LoadBuffer (XHOST_FILE_PATH , & buffer ) > 0 ),
220- xthrow ("Failed to load hosts file (%s)" , XSTRERR ));
304+ xhost_ctx_t ctx ;
305+ XASSERT ((XHost_InitContext (& ctx ) > 0 ), xthrowe ("Failed to init context" ));
221306
222- if ( buffer . pData != NULL && buffer . nUsed > 0 )
307+ while ( XFile_GetLine ( & ctx . file , ctx . sLine , sizeof ( ctx . sLine )) > 0 )
223308 {
224- if ( buffer . pData [ buffer . nUsed - 1 ] == '\n' )
225- buffer . pData [ -- buffer . nUsed ] = XSTR_NUL ;
309+ xhost_entry_t entry ;
310+ XHost_ParseEntry ( & entry , ctx . sLine ) ;
226311
227- xlog ("%s" , buffer .pData );
312+ if (xstrused (entry .sAddr ) && xstrused (entry .sHost ))
313+ {
314+ XString_Append (& ctx .hosts , "%s%s%s %s" ,
315+ XSTR_CLR_CYAN , entry .sAddr ,
316+ XSTR_FMT_RESET , entry .sHost );
317+
318+ if (!xstrused (entry .sComment )) XString_Append (& ctx .hosts , "\n" );
319+ else XString_Append (& ctx .hosts , " %s# %s%s\n" , XSTR_FMT_DIM , entry .sComment , XSTR_FMT_RESET );
320+ }
321+ else if (!xstrused (entry .sComment )) XString_Append (& ctx .hosts , "\n" );
322+ else XString_Append (& ctx .hosts , "%s# %s%s\n" , XSTR_FMT_DIM , entry .sComment , XSTR_FMT_RESET );
323+ }
324+
325+ if (ctx .hosts .nLength )
326+ {
327+ xlog_useheap (XTRUE );
328+ xlog ("%s" , ctx .hosts .pData );
228329 }
229330
230- XByteBuffer_Clear ( & buffer );
331+ XHost_ClearContext ( & ctx );
231332 return XSTDNON ;
232333}
233334
234335static int XHost_RemoveEntry (xhost_ctx_t * pCtx , xbool_t bComment )
235336{
236337 int nCount = 0 ;
237- int nPosit = 0 ;
238338
239339 while (XFile_GetLine (& pCtx -> file , pCtx -> sLine , sizeof (pCtx -> sLine )) > 0 )
240340 {
241- nPosit = 0 ;
242- while (pCtx -> sLine [nPosit ] && isspace ((unsigned char )pCtx -> sLine [nPosit ])) nPosit ++ ;
243-
244- if (pCtx -> sLine [nPosit ] && pCtx -> sLine [nPosit ] != '#' && XHost_SearchEntry (pCtx ))
341+ if (XHost_SearchEntry (pCtx ))
245342 {
246343 xlogd_wn ("Found entry: %s" , pCtx -> sLine );
247344
0 commit comments