Skip to content

Commit 838fabb

Browse files
committed
Added executable filter in xsearch
1 parent 677e0e9 commit 838fabb

File tree

6 files changed

+37
-7
lines changed

6 files changed

+37
-7
lines changed

src/sys/search.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void XSearch_InitEntry(xsearch_entry_t *pEntry)
3232
pEntry->nSize = 0;
3333
pEntry->nGID = 0;
3434
pEntry->nUID = 0;
35+
pEntry->nMode = 0;
3536
}
3637

3738
void XSearch_CreateEntry(xsearch_entry_t *pEntry, const char *pName, const char *pPath, xstat_t *pStat)
@@ -50,6 +51,7 @@ void XSearch_CreateEntry(xsearch_entry_t *pEntry, const char *pName, const char
5051
pEntry->nSize = pStat->st_size;
5152
pEntry->nGID = pStat->st_gid;
5253
pEntry->nUID = pStat->st_uid;
54+
pEntry->nMode = pStat->st_mode;
5355
}
5456

5557
#ifndef _WIN32
@@ -362,8 +364,14 @@ static XSTATUS XSearch_CheckCriteria(xsearch_t *pSearch, const char *pPath, cons
362364

363365
if (pSearch->nFileTypes)
364366
{
365-
xfile_type_t eType = XFile_GetType(pStat->st_mode);
366-
if (!XFILE_CHECK_FL(pSearch->nFileTypes, eType)) return XSTDNON;
367+
if (XFILE_CHECK_FL(pSearch->nFileTypes, XF_EXEC) &&
368+
!XFile_IsExec(pStat->st_mode)) return XSTDNON;
369+
370+
if (pSearch->nFileTypes & ~XF_EXEC)
371+
{
372+
xfile_type_t eType = XFile_GetType(pStat->st_mode);
373+
if (!XFILE_CHECK_FL(pSearch->nFileTypes, eType)) return XSTDNON;
374+
}
367375
}
368376

369377
if (xstrused(pSearch->sName))

src/sys/search.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ typedef struct XSearchEntry {
2828
char sPerm[XPERM_MAX];
2929
char sLine[XLINE_MAX];
3030
xfile_type_t eType;
31+
xmode_t nMode;
3132
size_t nLinkCount;
3233
uint32_t nGID;
3334
uint32_t nUID;

src/sys/xfs.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,23 @@ xfile_type_t XFile_GetType(xmode_t nMode)
460460
return 0; // Unknown file format
461461
}
462462

463+
xbool_t XFile_IsExec(xmode_t nMode)
464+
{
465+
#ifdef S_IXUSR
466+
if (nMode & S_IXUSR) return XTRUE;
467+
#endif
468+
#ifdef S_IXGRP
469+
if (nMode & S_IXGRP) return XTRUE;
470+
#endif
471+
#ifdef S_IXOTH
472+
if (nMode & S_IXOTH) return XTRUE;
473+
#endif
474+
#ifdef _WIN32
475+
if (nMode & _S_IEXEC) return XTRUE;
476+
#endif
477+
return XFALSE;
478+
}
479+
463480
char XFile_GetTypeChar(xfile_type_t eType)
464481
{
465482
switch (eType)

src/sys/xfs.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ typedef enum {
3838
XF_REGULAR = (1 << 3),
3939
XF_SYMLINK = (1 << 4),
4040
XF_SOCKET = (1 << 5),
41-
XF_PIPE = (1 << 6)
41+
XF_PIPE = (1 << 6),
42+
XF_EXEC = (1 << 7)
4243
} xfile_type_t;
4344

4445
typedef struct XFile {
@@ -99,6 +100,7 @@ int XFile_Print(xfile_t *pFile, const char *pFmt, ...);
99100

100101
xfile_type_t XFile_GetType(xmode_t nMode);
101102
char XFile_GetTypeChar(xfile_type_t eType);
103+
xbool_t XFile_IsExec(xmode_t nMode);
102104

103105
int XFile_GetStats(xfile_t *pFile);
104106
int XFile_Copy(xfile_t *pIn, xfile_t *pOut);

src/xver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#define XUTILS_VERSION_MAX 2
1414
#define XUTILS_VERSION_MIN 6
15-
#define XUTILS_BUILD_NUMBER 46
15+
#define XUTILS_BUILD_NUMBER 47
1616

1717
#ifdef __cplusplus
1818
extern "C" {

tools/xsrc.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ static int XSearch_GetFileTypes(const char *pTypes)
7575
case 'l': nTypes |= XF_SYMLINK; break;
7676
case 'p': nTypes |= XF_PIPE; break;
7777
case 's': nTypes |= XF_SOCKET; break;
78+
case 'x': nTypes |= XF_EXEC; break;
7879
default:
7980
{
8081
xloge("Invalid file type");
@@ -146,7 +147,8 @@ void XSearch_Usage(const char *pName)
146147
printf(" %sf%s: regular file\n", XSTR_CLR_CYAN, XSTR_FMT_RESET);
147148
printf(" %sl%s: symbolic link\n", XSTR_CLR_CYAN, XSTR_FMT_RESET);
148149
printf(" %sp%s: pipe\n", XSTR_CLR_CYAN, XSTR_FMT_RESET);
149-
printf(" %ss%s: socket\n\n", XSTR_CLR_CYAN, XSTR_FMT_RESET);
150+
printf(" %ss%s: socket\n", XSTR_CLR_CYAN, XSTR_FMT_RESET);
151+
printf(" %sx%s: executable\n\n", XSTR_CLR_CYAN, XSTR_FMT_RESET);
150152

151153
printf("Notes:\n");
152154
printf(" 1) <file_name> option is supporting wildcard character: '%s*%s'\n", XSTR_FMT_BOLD, XSTR_FMT_RESET);
@@ -160,7 +162,7 @@ void XSearch_Usage(const char *pName)
160162
printf("%s[xutils@examples]$ %s -rvd / -t lf -f \"*.log\" -p rwxrwxrwx%s\n\n", XSTR_FMT_BOLD, pName, XSTR_FMT_RESET);
161163

162164
printf("%sRecursive search of every .cpp and .java file in the \"/opt\" directory%s\n", XSTR_FMT_DIM, XSTR_FMT_RESET);
163-
printf("%sthat contains the case insensitive text \"socket\" and verbose output:%s\n", XSTR_FMT_DIM, XSTR_FMT_RESET);
165+
printf("%sthat contains the case insensitive text \"test\" and verbose output:%s\n", XSTR_FMT_DIM, XSTR_FMT_RESET);
164166
printf("%s[xutils@examples]$ %s -rvd /opt -f \"*.cpp;*.java\" -ig test%s\n\n", XSTR_FMT_BOLD, pName, XSTR_FMT_RESET);
165167
}
166168

@@ -302,7 +304,7 @@ static int XSearch_ParseArgs(xsearch_args_t *pArgs, int argc, char *argv[])
302304

303305
static void XSearch_ColorizeEntry(xsearch_t *pSearch, char *pOutput, size_t nSize, xsearch_entry_t *pEntry)
304306
{
305-
xbool_t bIsExec = pEntry->sPerm[XPERM_LEN-1] == 'x' ? XTRUE : XFALSE;
307+
xbool_t bIsExec = XFile_IsExec(pEntry->nMode);
306308
char *pColor = XSTR_EMPTY;
307309
char *pBack = XSTR_EMPTY;
308310
char *pFmt = XSTR_EMPTY;

0 commit comments

Comments
 (0)