C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
futil.c File Reference

General utility functions. More...

#include <cm.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <pwd.h>
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
#include <wait.h>
Include dependency graph for futil.c:

Go to the source code of this file.

Macros

#define LF_LNK   1
#define LF_DIR   2
#define LF_REG   4

Functions

bool lf_find (const char *base_path, const char *re, const char *ere, int max_depth, int flags)
 Find files in a directory matching a regular expression.
bool lf_process (const char *base_path, regex_t *compiled_re, regex_t *compiled_ere, int depth, int max_depth, int flags)
 logic for lf_find()
size_t strip_ansi (char *d, char *s)
 Strips ANSI SGR escape sequences (ending in 'm') from string s to d.
int a_toi (char *s, bool *a_toi_error)
 a safer alternative to atoi() for converting ASCII strings to integers.
bool chrep (char *s, char old_chr, char new_chr)
 Replaces all occurrences of old_chr in s with new_chr in place.
size_t trim (char *s)
 Trims leading and trailing spaces from string s in place.
size_t rtrim (char *s)
 Trims trailing spaces from string s in place.
bool stripz_quotes (char *s)
 removes leading and trailing double quotes if present
bool strip_quotes (char *s)
 removes leading and trailing double quotes if present
bool str_to_bool (const char *s)
 Converts String to boolean true or false.
int str_to_args (char **argv, char *arg_str, int max_args)
 Converts a string into an array of argument strings.
double str_to_double (char *s)
 converts string to double
bool str_to_lower (char *s)
 Converts a string to lowercase.
bool str_to_upper (char *s)
 Converts a string to uppercase.
size_t strz (char *s)
 Terminates string at new line or carriage return.
size_t strnz (char *s, size_t max_len)
 terminates string at New Line, Carriage Return, or max_len
size_t strnlf (char *s, size_t max_len)
 terminates string with line feed
bool str_subc (char *d, char *s, char ReplaceChr, char *Withstr, int l)
 Replaces "ReplaceChr" in "s" with "Withstr" in "d" won't copy more than "l" bytes to "d" Replaces all occurrences of a character in a string with another string, copying the result to a destination buffer.
char * rep_substring (const char *org_s, const char *tgt_s, const char *rep_s)
 Replace all occurrences of "tgt_s" in "org_s" with "rep_s".
bool normalize_file_spec (char *fs)
 replace backslashes with forward lashes
bool file_spec_path (char *fp, char *fs)
 extracts the path component of a file specification
bool file_spec_name (char *fn, char *fs)
 extracts the file name component of a file specification
bool verify_file (char *in_spec, int imode)
 Verifies that the file specified by "in_spec" exists and is accessible with the permissions specified by "imode".
bool verify_dir (char *spec, int imode)
 Verifies that the directory specified by "spec" exists and is accessible with the permissions specified by "imode".
bool locate_file_in_path (char *file_spec, char *file_name)
 Locates a file in the system PATH.
size_t canonicalize_file_spec (char *spec)
 Removes quotes and trims at first space.
bool is_directory (const char *path)
 Checks if the given path is a directory.
bool is_valid_regex (const char *pattern)
 Checks if the given regular expression pattern is valid.
size_t ssnprintf (char *buf, size_t buf_size, const char *format,...)
 ssnprintf was designed to be a safer alternative to snprintf.
size_t strnz__cpy (char *d, const char *s, size_t max_len)
 safer alternative to strncpy
size_t strnz__cat (char *d, const char *s, size_t max_len)
 safer alternative to strncat
size_t string_cpy (String *dest, const String *src)
 Copy src String to dest String, allocating additional memory for dest String if necessary.
size_t string_cat (String *dest, const String *src)
 Concatenates src String to dest String, allocating additional memory for dest String if necessary.
size_t string_ncat (String *dest, const String *src, size_t n)
 Concatenates up to n characters from src String to dest String, allocating additional memory for dest String if necessary.
size_t string_ncpy (String *dest, const String *src, size_t n)
 copies up to n characters from src String to dest String, allocating additional memory for dest String if necessary
String to_string (const char *s)
 String functions provide a simple string library to facilitate string manipulation in C, allowing developers to easily create, copy, concatenate, and free strings without having to manage memory manually.
String mk_string (size_t l)
 Create a String struct with a dynamically allocated string.
String free_string (String string)
 Free the dynamically allocated String.
void destroy_argv (int argc, char **argv)
 Deallocates memory allocated for argument strings in argv.
char * strnz_dup (char *s, size_t l)
 Allocates memory for and duplicates string s up to length l or until line feed or carriage return.
bool strnfill (char *s, char c, int n)
 Fills string s with character c n.
bool expand_tilde (char *path, int path_maxlen)
 Replace Leading Tilde With Home Directory.
bool trim_path (char *dir)
 Trims trailing spaces and slashes from directory path in place.
bool trim_ext (char *buf, char *filename)
 trims the file extension from "filename" and copies the result to "buf"
bool base_name (char *buf, char *path)
 Returns the base name of a file specification.
bool dir_name (char *buf, char *path)
 Returns the directory name of a file specification.
bool mk_dir (char *dir)
 If directory doesn't exist, make it.
int segmentation_fault ()
 Function to intentionally cause a segmentation fault for testing purposes.

Variables

char earg_str [MAXLEN]
int eargc
char * eargv [MAXARGS]
char errmsg [MAXLEN]
error_info_t error_info
error_source_t error_source
int wait_timeout

Detailed Description

General utility functions.

Author
Bill Waller Copyright (c) 2025 MIT License billx.nosp@m.wall.nosp@m.er@gm.nosp@m.ail..nosp@m.com
Date
2026-02-09

Definition in file futil.c.

Macro Definition Documentation

◆ LF_DIR

#define LF_DIR   2

Definition at line 37 of file futil.c.

◆ LF_LNK

#define LF_LNK   1

Definition at line 36 of file futil.c.

◆ LF_REG

#define LF_REG   4

Definition at line 38 of file futil.c.

Function Documentation

◆ mk_string()

String mk_string ( size_t l)

Create a String struct with a dynamically allocated string.

Parameters
llength of string to create including null terminator
Returns
String struct
Note
The returned String struct contains a dynamically allocated string of he specified length
See also
free_string
Note
the caller is responsible for calling free_string to free the allocated memory.

Definition at line 1396 of file futil.c.

1397 {
1398 if (l == 0) {
1399 String str;
1400 str.l = 0;
1401 str.s = nullptr;
1402 return str;
1403 }
1404 String str;
1405 str.l = l + 1;
1406 str.s = (char *)malloc(str.l);
1407 str.s[0] = '\0';
1408 return str;
The String structure represents a string object with a pointer to the string and its allocated length...
Definition cm.h:584
size_t l
Definition cm.h:586
char * s
Definition cm.h:585

References String::l, and String::s.

◆ rtrim()

size_t rtrim ( char * s)

Trims trailing spaces from string s in place.

Parameters
s- string to trim
Returns
length of trimmed string

Definition at line 102 of file futil.c.

102 {
103 if (s == nullptr || *s == '\0')
104 return 0;
105 char *p = s;
106 char *d = s;
107 while (*p != '\0')
108 *d++ = *p++;
109 while (*(d - 1) == ' ' && d > s)
110 d--;
111 *d = '\0';
112 return (size_t)(d - s);
113}

Referenced by fmt_line().

Here is the caller graph for this function:

◆ str_to_bool()

bool str_to_bool ( const char * s)

Converts String to boolean true or false.

Parameters
s- string to convert
Returns
boolean true or false

Definition at line 649 of file futil.c.

650 {
651 if (s == nullptr || *s == '\0')
652 return false;
653 switch (s[0]) {
654 case 't':
655 case 'T':
656 case 'y':
657 case 'Y':
658 case '1':
659 return true;
660 case 'o':
661 case 'O':
662 switch (s[1]) {
663 case 'n':
664 case 'N':
665 return true;
666 default:
667 break;
668 }
669 default:
670 break;
671 }
672 return false;

Referenced by parse_config().

Here is the caller graph for this function:

Variable Documentation

◆ earg_str

char earg_str[MAXLEN]

earg - general use argument string general use argument string, for external commands or error messages

Definition at line 40 of file futil.c.

Referenced by enter_file_spec(), and menu_cmd_processor().

◆ eargc

int eargc

general use argument count, for external commands or error messages

Definition at line 41 of file futil.c.

Referenced by display_pick_help(), enter_file_spec(), form_engine(), form_process(), and menu_cmd_processor().

◆ eargv

char* eargv[MAXARGS]

eargv - argument vector for external commands, or error messages

Definition at line 42 of file futil.c.

Referenced by display_pick_help(), enter_file_spec(), and menu_cmd_processor().

◆ errmsg

char errmsg[MAXLEN]

Global variables for error reporting

Definition at line 84 of file futil.c.

◆ error_info

error_info_t error_info

Definition at line 96 of file futil.c.

◆ error_source

error_source_t error_source

Definition at line 97 of file futil.c.

◆ wait_timeout

int wait_timeout