2
3
4
5
6
7
8
13#define LF_VERSION_MAJOR 0
14#define LF_VERSION_MINOR 3
15#define LF_VERSION_PATCH 3
16#define LF_VERSION "v0.3.3"
34#if __STDC_VERSION__
< 202311L
42#define CACHE_LINE_SIZE 64
45#define MAX_PATH_LEN PATH_MAX
47#define DIR_BUF_SIZE 65536
48#define QUEUE_NODES (1024
* 1024
)
49#define PATH_BYTES (32
* 1024
* 1024
)
50#define MAX_LOG_REC_LEN 1024
51#define MAX_LOG_SIZE (1024
* 1024
)
55#if defined(__has_c_attribute
)
56#if __has_c_attribute
(gnu::format)
57#define ATTR_FORMAT(archetype, fmt_idx, first_arg)
58 [[gnu::format(archetype, fmt_idx, first_arg)]]
63#if defined(__GNUC__) || defined(__clang__)
64#define ATTR_FORMAT(archetype, fmt_idx, first_arg)
65 __attribute__((format(archetype, fmt_idx, first_arg)))
67#define ATTR_FORMAT(archetype, fmt_idx,
72#ifdef __USE_GNU_GETTEXT
74#define _(String) gettext(String)
75#define gettext_noop(String) String
76#define N_(String) gettext_noop(String)
78#define _(String) (String)
79#define N_(String) String
80#define textdomain(Domain)
81#define bindtextdomain(Package, Directory)
86#define ANSI_RESET "\033[0m"
87#define AS_STRINGS(NAME) #NAME,
88#define AS_ENUM(NAME) NAME,
89#define LOG_LEVEL_LIST(X)
111#define TIMESTAMP_STYLE(X)
117}
__attribute((packed)) tss_e;
118#define ANSI_COLOR_LIST(X)
140}
__attribute((packed)) sgr_t;
141#define MAX_TIMESTAMP_LEN 64
154 __typeof__(a) _a = (a);
155 __typeof__(a) _b = (b);
180 __typeof__(x) _a = (x);
186 (__c >= 'A' && __c <= 'Z') ? (__c + ('a' - 'A')) : __c;
191 (__c >= 'a' && __c <= 'z') ? (__c - ('a' - 'A')) : __c;
193#define to_uppercase(c)
194 if (c >= 'a' && c <= 'z')
196#define to_lowercase(c)
197 if (c >= 'A' && c <= 'Z')
254
255
256
257
258
259
260
261
262
263
264
265
266
269
270
271
272
273
274
275
276
277
278
279
280#define DEFAULTSHELL "/bin/bash"
282#define S_QUIET 0x2000
284
285
286
287
288
289
290
291
295 rc = atexit(end_pgm);
297 fprintf(stderr
, "\nCannot set exit function\n");
301#define to_uppercase(c)
302 if (c >= 'a' && c <= 'z')
307#define NO_PARENT UINT32_MAX
447#define print_file_type(mask, lf_type, dt_type, name)
449 fprintf(stderr
, "%c %08b (%3d) %08b (%2d) %s\n",
450 (mask & lf_type) ? '*' : ' ', lf_type, lf_type, dt_type, dt_type,
457unsigned long a_to_ul(
const char *);
458int a_toi(
char *,
bool *);
468size_t
ssnprintf(
char *, size_t,
const char *, ...);
469size_t
strnz__cpy(
char *,
const char *, size_t);
470size_t
strnz__cat(
char *,
const char *, size_t);
471size_t
strnz__pcat(
char *d, size_t pos,
const char *s, size_t max_len);
472char *
strerror__r(
int errnum,
char *buf, size_t buflen);
478void debug_out(GlobalContext *__G,
int,
char **);
479bool init_lf(GlobalContext *__G,
int,
char **);
482void *
finder(GlobalContext *__G, ThreadLocal *tl);
483int print_file(GlobalContext *__G, ThreadLocal *tl);
485bool append_output_buffer(GlobalContext *__G, OutputBuffer *output,
const char *path, size_t path_len,
bool append_slash);
490bool mpmc_enqueue(GlobalContext *__G, QueueNode *node,
const char *path, size_t len);
493bool mpmc_dequeue(GlobalContext *__G, uint_fast32_t *dequeue_pos);
496size_t
flush_ch(GlobalContext *__G, ThreadLocal *tl);
501char *
rep_substring(
const char *org_s,
const char *tgt_s,
const char *rep_s);
502size_t
vssnprintf(
char *buf, size_t buf_size,
const char *format, va_list args);
503size_t
ssnprintf(
char *buf, size_t buf_size,
const char *format, ...);
505unsigned long a_to_ul(
const char *str);
506int a_toi(
char *s,
bool *a_toi_error);
507size_t
strnz__cpy(
char *d,
const char *s, size_t max_len);
508size_t
strnz__cat(
char *d,
const char *s, size_t max_len);
511void build_path(ThreadLocal *__tl,
char *file_name);
515void log_open(GlobalContext *__G, LogCtx *log_ctx, size_t thread_id);
517void log_write(
const ll_e level, GlobalContext *__G, LogCtx *log_ctx,
const char *file,
518 const char *func,
const int line,
const char *fmt, ...);
519#define lf_log(level, fmt, ...)
521 log_write(level, __G, log_ctx, __FILE__, __func__, __LINE__,
size_t strnz__pcat(char *d, size_t pos, const char *s, size_t max_len)
char * rep_substring(const char *, const char *, const char *) __attribute__((nonnull(1
Replace all occurrences of "tgt_s" in "org_s" with "rep_s".
#define MAX_TIMESTAMP_LEN
#define ATTR_FORMAT(archetype, fmt_idx, first_arg)
char * path_data_init(GlobalContext *__G, LogCtx *log_ctx)
bool mpmc_enqueue_staged(GlobalContext *, ThreadLocal *__tl)
#define lf_log(level, fmt,...)
#define OUTPUT_BUFFER_BYTES
#define TIMESTAMP_STYLE(X)
void path_data_destroy(GlobalContext *__G)
size_t flush_ch(GlobalContext *__G, ThreadLocal *tl)
#define print_file_type(mask, lf_type, dt_type, name)
#define LOG_LEVEL_LIST(X)
void finalize_dir_node(GlobalContext *__G, size_t node_offset)
#define MAX_LOG_RECORD_LEN
#define ANSI_COLOR_LIST(X)
void * worker(void *arg)
Worker thread function for processing directory nodes.
void coders_timestamp(LogCtx *log_ctx)
Generate a timestamp string for logging, including both current time and interval since last log entr...
TerminationStatus termination_status
bool global_arena_create(GlobalContext *__G)
Create a queue arena for managing directory traversal tasks.
void initialize_timer(GlobalContext *__G, LogCtx *log_ctx)
Initialize the timer for logging, capturing the start time and setting up interval tracking.
#define WRITE_STR(p, str)
unsigned char const lf_type_mask[15]
void debug_out(GlobalContext *__G, int argc, char **argv)
Display everything and the kitchen sink on stderr.
bool append_output_buffer(GlobalContext *__G, OutputBuffer *output_buffer, const char *path, size_t path_len, bool append_slash)
Append a path to the output_buffer, flushing if necessary.
void initialize_thread_timer(GlobalContext *__G, LogCtx *log_ctx)
Initialize the timer for a thread, capturing the start time and setting up interval tracking.
void * finder(GlobalContext *__G, ThreadLocal *__tl)
Traverse a directory and process its entries.
void build_path(ThreadLocal *__tl, char *file_name)
Build the full path for a file or directory entry.
void iso8601_timestamp(GlobalContext *__G, LogCtx *log_ctx)
Generate an ISO 8601 timestamp string.
bool mpmc_enqueue(GlobalContext *__G, QueueNode *node, const char *path, size_t len)
int sort_lf_output(GlobalContext *__G, int argc, char **argv)
Sort the output of the lf command using the system's sort utility.
bool mpmc_dequeue(GlobalContext *__G, uint_fast32_t *dequeue_pos)
Dequeue a node from the MPMC queue.
void log_write(const ll_e level, GlobalContext *__G, LogCtx *log_ctx, const char *file, const char *func, const int line, const char *fmt,...)
Write a log message to the log file and/or display.
void push_root_node(GlobalContext *__G, dev_t dev, ino_t ino)
Push the root directory node into the queue arena.
bool init_lf(GlobalContext *__G, int argc, char **argv)
Initialize the file search process.
void global_arena_destroy(GlobalContext *__G)
Destroy the queue arena and free associated resources.
bool mpmc_bulk_enqueue(GlobalContext *__G, ThreadLocal *__tl)
Bulk enqueue multiple nodes into the MPMC queue.
void flush_output_buffer(GlobalContext *__G, OutputBuffer *output_buffer)
Flush the output_buffer to stdout in a thread-safe manner.
void log_open(GlobalContext *__G, LogCtx *log_ctx, size_t thread_id)
Open the log file for writing.
bool is_dirsys(const char *)
int print_file(GlobalContext *__G, ThreadLocal *__tl)
Print the path of a file or directory entry based on filtering criteria.
bool is_hidden(const char *)
const char * argp_program_version
const char * argp_program_bug_address
int main(int argc, char **argv)
Capture the current terminal settings for later restoration.
size_t strnz__cpy(char *, const char *, size_t)
safer alternative to strncpy
bool parse_local_timestamp(const char *, time_t *)
Parses an ISO 8601 timestamp string in local time and converts it to time_t.
char * get_local_timestamp()
Returns the current local time as an ISO 8601 formatted string.
bool is_directory(const char *)
Check if a given path is a directory.
size_t ssnprintf(char *, size_t, const char *,...)
Custom safe string formatting utility (Variadic version).
int str_to_args(char **, char *, uint)
Converts a string into an array of argument strings.
bool is_valid_regex(const char *)
Check if a given path is a regular file.
bool expand_tilde(char *, uint)
Expand a tilde (~) in a file path to the user's home directory.
size_t strnz__cat(char *, const char *, size_t)
safer alternative to strncat
char * get_ip_addresses(char *, uint)
Retrieves the IP addresses of the local machine and formats them into a string.
bool is_symlink_to_dir(const char *)
Check if a given path is a symbolic link that points to a directory.
int a_toi(char *, bool *)
a safer alternative to atoi() for converting ASCII strings to integers.
char * strerror__r(int errnum, char *buf, size_t buflen)
A thread-safe alternative to strerror() that writes the error message corresponding to errnum into th...
size_t vssnprintf(char *buf, size_t buf_size, const char *format, va_list args)
Custom safe string formatting utility (va_list version).
char * format_local_timestamp(time_t, char *, size_t)
Formats a time_t as an ISO 8601 string in local time.
bool is_newer(char *, char *)
Checks if the file specified by "fut" is newer than the file specified by "control".
char * get_user_str(char *, size_t)
Retrieves the current user's name and UID, and formats it into a string.
unsigned long a_to_ul(const char *)
Convert a string to an unsigned long with optional size suffixes.
char record[MAX_LOG_REC_LEN]
char time_buf[MAX_TIMESTAMP_LEN]
uint_fast32_t child_dirs_remaining
uint_fast32_t path_offset
char data[OUTPUT_BUFFER_BYTES]
uint_fast32_t child_dirs_remaining
OutputBuffer * output_buffer
unsigned char effective_type
uint_fast32_t parent_path_len
uint_fast32_t cum_path_offset
uint_fast32_t path_offset
unsigned char include_types
atomic_size_t log_error_count
atomic_uint_fast32_t dequeue_pos
atomic_uint_fast32_t queue_idx
atomic_uint_fast32_t shut_down
atomic_uint_fast32_t active_tasks
pthread_mutex_t output_mutex
atomic_uint_fast32_t sequence[QUEUE_NODES]
atomic_uint_fast32_t thread_id
char log_record[MAX_LOG_RECORD_LEN]
unsigned char suppress_types
unsigned char include_perms
atomic_uint_fast32_t enqueue_pos
atomic_size_t error_count
char log_file_spec[MAX_PATH_LEN]
atomic_uint_fast32_t path_offset