Skip to content

Commit a7bb1e1

Browse files
committed
Refactored xhost
1 parent d2fe416 commit a7bb1e1

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

tools/xhost.c

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
* @brief Modify hosts file, add or remove entries.
88
*/
99

10-
#define _XUTILS_DEBUG
11-
1210
#include "xstd.h"
1311
#include "xfs.h"
1412
#include "str.h"
@@ -23,6 +21,7 @@
2321

2422
typedef struct {
2523
xbool_t bAdd;
24+
xbool_t bAppend;
2625
xbool_t bRemove;
2726
xbool_t bVerbose;
2827
xbool_t bNewLine;
@@ -56,15 +55,15 @@ static void XHost_Usage(const char *pName)
5655
for (i = 0; i < nLength; i++) sWhiteSpace[i] = ' ';
5756
sWhiteSpace[nLength] = 0;
5857

59-
printf("Usage: %s [-a <addr>] [-c] [-u] [-r]\n", pName);
60-
printf(" %s [-n <host>] [-d] [-l] [-v] [-h]\n\n", sWhiteSpace);
58+
printf("Usage: %s [-a <address>] [-n <hostname>]\n", pName);
59+
printf(" %s [-c] [-u] [-r] [-d] [-l] [-v] [-h]\n\n", sWhiteSpace);
6160

6261
printf("Options are:\n");
6362
printf(" -a <address> # IP address\n");
64-
printf(" -n <host> # Host name\n");
65-
printf(" -c # Comment host entry\n");
66-
printf(" -u # Uncomment host entry\n");
67-
printf(" -r # Remove host entry\n");
63+
printf(" -n <hostname> # Host name\n");
64+
printf(" -c # Comment entry\n");
65+
printf(" -u # Uncomment entry\n");
66+
printf(" -r # Remove entry\n");
6867
printf(" -l # Insert new line before entry\n");
6968
printf(" -d # Display /etc/hosts file\n");
7069
printf(" -v # Enable verbose logging\n");
@@ -102,7 +101,9 @@ static int XHost_ParseArgs(xhost_args_t *pArgs, int argc, char *argv[])
102101
xbool_t bHaveHost = xstrused(pArgs->sHost);
103102
xbool_t bModify = pArgs->bRemove || pArgs->bComment || pArgs->bUncomment;
104103

105-
if (!bModify && !pArgs->bDisplay && (!bHaveAddress || !bHaveHost)) return XSTDERR;
104+
pArgs->bAppend = !bModify && bHaveAddress && bHaveHost;
105+
if (pArgs->bVerbose) xlog_enable(XLOG_DEBUG);
106+
if (!pArgs->bAppend && !bModify) pArgs->bDisplay = XTRUE;
106107
if (bModify && !bHaveAddress && !bHaveHost) return XSTDERR;
107108

108109
return XSTDOK;
@@ -117,7 +118,7 @@ static int XHost_InitContext(xhost_ctx_t *pCtx)
117118
XASSERT((XFile_Open(&pCtx->file, XHOST_FILE_PATH, "r", NULL) >= 0),
118119
xthrowe("Failed to open hosts file"));
119120

120-
XASSERT_CALL((XString_Init(&pCtx->hosts, XLINE_MAX, XFALSE) >= 0),
121+
XASSERT_CALL((XString_Init(&pCtx->hosts, XSTDNON, XFALSE) >= 0),
121122
XFile_Close, &pCtx->file, xthrowe("Failed alloc hosts file buffer"));
122123

123124
return XSTDOK;
@@ -134,7 +135,7 @@ static int XHost_Write(xstring_t *pString)
134135
xfile_t file;
135136
if (XFile_Open(&file, XHOST_FILE_PATH, "cwt", NULL) < 0)
136137
{
137-
xloge("Failed to open hosts file: %s", XSTRERR);
138+
xloge("Failed to open hosts file for writing: %s", XSTRERR);
138139
return XSTDERR;
139140
}
140141

@@ -205,7 +206,7 @@ static int XHost_AddEntry(xhost_ctx_t *pCtx, xbool_t bNewLine)
205206
pCtx->sAddr, pCtx->sHost) >= 0),
206207
xthrowe("Failed to append new host entry"));
207208

208-
XASSERT((XHost_Write(&pCtx->hosts) >= 0), xthrowe("Failed to write hosts file"));
209+
XASSERT((XHost_Write(&pCtx->hosts) >= 0), XSTDERR);
209210
xlogd("Added new entry: %s %s", pCtx->sAddr, pCtx->sHost);
210211
}
211212

@@ -218,7 +219,13 @@ static int XHost_DisplayHosts()
218219
XASSERT((XPath_LoadBuffer(XHOST_FILE_PATH, &buffer) > 0),
219220
xthrow("Failed to load hosts file (%s)", XSTRERR));
220221

221-
if (buffer.pData != NULL) xlog("%s", buffer.pData);
222+
if (buffer.pData != NULL && buffer.nUsed > 0)
223+
{
224+
if (buffer.pData[buffer.nUsed-1] == '\n')
225+
buffer.pData[--buffer.nUsed] = XSTR_NUL;
226+
227+
xlog("%s", buffer.pData);
228+
}
222229

223230
XByteBuffer_Clear(&buffer);
224231
return XSTDNON;
@@ -256,7 +263,7 @@ static int XHost_RemoveEntry(xhost_ctx_t *pCtx, xbool_t bComment)
256263

257264
if (nCount)
258265
{
259-
XASSERT_CALL((XHost_Write(&pCtx->hosts) >= 0), XString_Clear, &pCtx->hosts, XSTDERR);
266+
XASSERT((XHost_Write(&pCtx->hosts) >= 0), XSTDERR);
260267
xlogd("%s entres: %d", bComment ? "Commented" : "Removed", nCount);
261268
}
262269

@@ -315,23 +322,17 @@ int main(int argc, char *argv[])
315322
return XSTDERR;
316323
}
317324

318-
if (args.bVerbose)
319-
{
320-
xlog_enable(XLOG_DEBUG);
321-
xlog_indent(XTRUE);
322-
}
323-
324325
xhost_ctx_t ctx;
325326
int nStatus = 0;
326327

327328
XASSERT((XHost_InitContext(&ctx) > 0), xthrowe("Failed to init context"));
328329
xstrncpy(ctx.sAddr, sizeof(ctx.sAddr), args.sAddress);
329330
xstrncpy(ctx.sHost, sizeof(ctx.sHost), args.sHost);
330331

331-
if (args.bRemove) nStatus = XHost_RemoveEntry(&ctx, XFALSE);
332+
if (args.bAppend) nStatus = XHost_AddEntry(&ctx, args.bNewLine);
333+
else if (args.bRemove) nStatus = XHost_RemoveEntry(&ctx, XFALSE);
332334
else if (args.bComment) nStatus = XHost_RemoveEntry(&ctx, XTRUE);
333335
else if (args.bUncomment) nStatus = XHost_UncommentEntry(&ctx);
334-
else nStatus = XHost_AddEntry(&ctx, args.bNewLine);
335336
if (!nStatus && args.bDisplay) XHost_DisplayHosts();
336337

337338
XHost_ClearContext(&ctx);

0 commit comments

Comments
 (0)