styling fixes

This commit is contained in:
Luxferre
2026-08-21 11:56:48 +03:00
parent e89bfb86d3
commit 86082d67c0
+40 -40
View File
@@ -45,10 +45,10 @@ static pthread_mutex_t dir_mx = PTHREAD_MUTEX_INITIALIZER;
static void push_dir(const char *path) {
DirJob *j = malloc(sizeof *j);
if (j == NULL)
if(j == NULL)
return;
j->path = strdup(path);
if (j->path == NULL) {
if(j->path == NULL) {
free(j);
return;
}
@@ -66,10 +66,10 @@ static void die(const char *msg) {
static char *pop_dir(void) {
pthread_mutex_lock(&dir_mx);
DirJob *j = dir_stack;
if (j != NULL)
if(j != NULL)
dir_stack = j->next;
pthread_mutex_unlock(&dir_mx);
if (j == NULL)
if(j == NULL)
return NULL;
char *p = j->path;
free(j);
@@ -88,14 +88,14 @@ static int scan_fd(int fd, ScanBufs *sb) {
size_t carryn = 0;
int hit = 0;
for (;;) {
for(;;) {
ssize_t r = read(fd, buf + carryn, IO_BUFSZ);
if (r < 0) {
if (errno == EINTR)
if(r < 0) {
if(errno == EINTR)
continue;
break;
}
if (r == 0)
if(r == 0)
break; /* EOF */
size_t wn = carryn + (size_t)r;
@@ -105,17 +105,17 @@ static int scan_fd(int fd, ScanBufs *sb) {
* before the first NUL it is still a valid text match. */
void *nul = memchr(buf, '\0', wn);
size_t text_end = (nul != NULL) ? (size_t)((const char *)nul - buf) : wn;
if (text_end >= needle_len) {
if (memmem(buf, text_end, needle, needle_len) != NULL) {
if(text_end >= needle_len) {
if(memmem(buf, text_end, needle, needle_len) != NULL) {
hit = 1;
break;
}
}
if (nul != NULL)
if(nul != NULL)
break; /* binary file: no further scanning */
/* No NUL and no match yet: carry the tail across chunks. */
if (wn >= needle_len)
if(wn >= needle_len)
carryn = needle_len - 1;
else
carryn = wn;
@@ -135,9 +135,9 @@ static void report_match(const char *path) {
static void scan_file(const char *path, ScanBufs *sb) {
int fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd < 0)
if(fd < 0)
return; /* unreadable entries are silently skipped */
if (scan_fd(fd, sb))
if(scan_fd(fd, sb))
report_match(path);
close(fd);
}
@@ -148,39 +148,39 @@ static void *worker(void *arg) {
ScanBufs sb;
sb.buf = malloc(IO_BUFSZ + needle_len);
sb.carry = malloc(needle_len);
if (sb.buf == NULL || sb.carry == NULL) {
if(sb.buf == NULL || sb.carry == NULL) {
free(sb.buf);
free(sb.carry);
return NULL;
}
char child[PATH_MAX];
for (;;) {
for(;;) {
char *dir = pop_dir();
if (dir == NULL)
if(dir == NULL)
break;
DIR *d = opendir(dir);
if (d != NULL) {
if(d != NULL) {
struct dirent *ent;
while ((ent = readdir(d)) != NULL) {
if (strcmp(ent->d_name, ".") == 0 ||
while((ent = readdir(d)) != NULL) {
if(strcmp(ent->d_name, ".") == 0 ||
strcmp(ent->d_name, "..") == 0)
continue;
int n = snprintf(child, sizeof child, "%s/%s", dir, ent->d_name);
if (n < 0 || (size_t)n >= sizeof child)
if(n < 0 || (size_t)n >= sizeof child)
continue;
if (ent->d_type == DT_REG) {
if(ent->d_type == DT_REG) {
scan_file(child, &sb);
} else if (ent->d_type == DT_DIR) {
} else if(ent->d_type == DT_DIR) {
push_dir(child);
} else if (ent->d_type == DT_LNK) {
} else if(ent->d_type == DT_LNK) {
continue; /* do not follow symlinks: avoids cycles */
} else {
struct stat st;
if (lstat(child, &st) != 0)
if(lstat(child, &st) != 0)
continue;
if (S_ISDIR(st.st_mode))
if(S_ISDIR(st.st_mode))
push_dir(child);
else if (S_ISREG(st.st_mode))
else if(S_ISREG(st.st_mode))
scan_file(child, &sb);
}
}
@@ -194,39 +194,39 @@ static void *worker(void *arg) {
}
int main(int argc, char **argv) {
if (argc < 2 || argc > 3) {
if(argc < 2 || argc > 3) {
fprintf(stderr, "usage: %s SEARCH_TERM [FILE_OR_DIRECTORY]\n", argv[0]);
return 2;
}
needle = argv[1];
needle_len = strlen(needle);
if (needle_len == 0) {
if(needle_len == 0) {
fprintf(stderr, "gg: empty search term\n");
return 2;
}
char cwd[PATH_MAX];
const char *root;
if (argc == 3) {
if(argc == 3) {
struct stat st;
if (stat(argv[2], &st) != 0) {
if(stat(argv[2], &st) != 0) {
fprintf(stderr, "gg: %s: %s\n", argv[2], strerror(errno));
return 2;
}
if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode)) {
if(!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode)) {
fprintf(stderr, "gg: %s: not a regular file or directory\n", argv[2]);
return 2;
}
root = argv[2];
} else {
if (getcwd(cwd, sizeof cwd) == NULL)
if(getcwd(cwd, sizeof cwd) == NULL)
die("getcwd");
root = cwd;
}
struct stat st;
if (stat(root, &st) != 0) {
if(stat(root, &st) != 0) {
fprintf(stderr, "gg: %s: %s\n", root, strerror(errno));
return 2;
}
@@ -235,7 +235,7 @@ int main(int argc, char **argv) {
* parallel: each worker pops a directory, scans its regular files inline,
* and pushes any sub-directories it finds. This overlaps the (metadata)
* walk with the (data) scan across all online CPUs. */
if (S_ISREG(st.st_mode)) {
if(S_ISREG(st.st_mode)) {
scan_file(root, &(ScanBufs){ malloc(IO_BUFSZ + needle_len),
malloc(needle_len) });
} else {
@@ -243,20 +243,20 @@ int main(int argc, char **argv) {
}
long nproc = sysconf(_SC_NPROCESSORS_ONLN);
if (nproc < 1)
if(nproc < 1)
nproc = 1;
int nth = (int)nproc;
if (nth > 256)
if(nth > 256)
nth = 256;
pthread_t t[256];
int started = 0;
for (int i = 0; i < nth; i++) {
if (pthread_create(&t[i], NULL, worker, NULL) != 0)
for(int i = 0; i < nth; i++) {
if(pthread_create(&t[i], NULL, worker, NULL) != 0)
break;
started++;
}
for (int i = 0; i < started; i++)
for(int i = 0; i < started; i++)
pthread_join(t[i], NULL);
return found_any ? 0 : 1;