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 <stdint.h>
#include <argp.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <ifaddrs.h>
#include <pwd.h>
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#include <wait.h>
Include dependency graph for futil.c:

Go to the source code of this file.

Macros

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

Functions

unsigned long a_to_ul (const char *str)
 Converts a string to an unsigned long long integer, with support for suffixes K, M, and G for kilobytes, megabytes, and gigabytes respectively.
int a_toi (char *s, bool *a_toi_error)
 a safer alternative to atoi() for converting ASCII strings to integers.
bool base_name (char *buf, char *path)
 Returns the base name of a file specification.
size_t canonicalize_file_spec (char *spec)
 Removes quotes and trims at first space.
bool chrep (char *s, char old_chr, char new_chr)
 Replaces all occurrences of old_chr in s with new_chr in place.
int destroy_argv (int argc, char **argv)
 Deallocates memory allocated for argument strings in argv.
bool dir_name (char *buf, char *path)
 Returns the directory name of a file specification.
bool expand_tilde (char *str, int path_maxlen)
 Replaces "~/" in string with the user's home directory.
char * fdname (int fd, char *out_path)
 Retrieves the file path associated with a given file descriptor.
bool file_spec_name (char *file_name, char *fs)
 extracts the file name component of a file specification
bool file_spec_path (char *fp, char *fs)
 extracts the path component of a file specification
char * fill_field (char *accept_s, char *display_s, char fill_char, int flen)
char * format_local_timestamp (time_t t, char *buf, size_t n)
 Formats a time_t as an ISO 8601 string in local time.
String free_string (String string)
 Free the dynamically allocated String.
bool get_argp_doc_by_name (char *comment, const struct argp_option *options, const char *key_name)
 Retrieves the documentation string for a given key name from an argp options array.
char * get_ip_addresses (char *ip_str, int maxlen)
 Retrieves the IP addresses of the local machine and formats them into a string.
char * get_local_timestamp ()
 Returns the current local time as an ISO 8601 formatted string.
char * get_user_str (char *user_str, size_t maxlen)
 Retrieves the current user's name and UID, and formats it into a string.
bool is_directory (const char *path)
 Checks if the given path is a directory.
bool is_hex_str (char *str, int len)
 Validates that a string consists of exactly len hexadecimal digits.
bool is_newer (char *control, char *fut)
 Checks if the file specified by "fut" is newer than the file specified by "control".
bool is_symlink_to_dir (const char *path)
 Checks if the given path is a symbolic link to a directory.
bool is_valid_date (int yyyy, int mm, int dd)
 Check if a given date is valid, including leap years.
bool is_valid_regex (const char *pattern)
 Checks if the given regular expression pattern is valid.
bool is_valid_time (int hh, int mm, int ss)
 Check if a given time is valid.
char * iso8601_time (char *buf, int n, time_t *t, bool local)
 Formats a struct tm as an ISO 8601 string.
void left_justify (char *s)
 Left justify string by removing leading spaces.
bool locate_file_in_path (char *file_spec, char *file_name)
 Locates a file in the system PATH.
bool mk_dir (char *dir)
 If directory doesn't exist, make it.
String mk_string (size_t l)
 Create a String struct with a dynamically allocated string.
bool normalize_file_spec (char *fs)
 replace backslashes with forward lashes
void numeric (char *d, char *s)
 Extract numeric characters from source string to destination string.
void open_cmenu_log ()
 Open new C-Menu log file.
bool parse_local_timestamp (const char *s, time_t *out)
 Parses an ISO 8601 timestamp string in local time and converts it to time_t.
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".
void right_justify (char *, int)
 Right justify string by removing trailing spaces and adding leadingspaces.
size_t rtrim (char *s)
 Trims trailing spaces from string s in place.
int segmentation_fault ()
 Function to intentionally cause a segmentation fault for testing purposes.
size_t ssnprintf (char *buf, size_t buf_size, const char *format,...)
 ssnprintf was designed to be a safer alternative to snprintf.
char * stdio_fdnames (char *stdio_str, char *id)
char * stdio_names (char *stdio_str, char *id)
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.
int str_to_args (char **argv, char *arg_str, int max_args)
 Converts a string into an array of argument strings.
bool str_to_bool (const char *s)
 Converts String to boolean true or false.
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 string_cat (String *dest, const String *src)
 Concatenates src String to dest String, allocating additional memory for dest String if necessary.
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_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
size_t strip_ansi (char *d, char *s)
 Strips ANSI SGR escape sequences (ending in 'm') from string s to d.
bool strip_quotes (char *s)
 removes leading and trailing double quotes if present
bool stripz_quotes (char *s)
 removes leading and trailing double quotes if present
size_t strnlf (char *s, size_t max_len)
 terminates string with line feed
size_t strnz (char *s, size_t max_len)
 terminates string at New Line, Carriage Return, or max_len
size_t strnz__cat (char *d, const char *s, size_t max_len)
 safer alternative to strncat
size_t strnz__cpy (char *d, const char *s, size_t max_len)
 safer alternative to strncpy
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.
size_t strz (char *s)
 Terminates string at new line or carriage return.
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.
size_t trim (char *s)
 Trims leading and trailing spaces from string s in place.
bool trim_ext (char *buf, char *filename)
 trims the file extension from "filename" and copies the result to "buf"
bool trim_path (char *dir)
 Trims trailing spaces and slashes from directory path in place.
bool unstr_hex_clr (char *dst, char *str)
 Validates that a string is a hex color code in the format "#RRGGBB".
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 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".
void write_cmenu_log (char *msg)
 Write message to C-Menu log file with timestamp.
void write_cmenu_log_nt (char *msg)
 Write message to C-Menu log file without timestamp.

Variables

int cmenu_log_fd
char earg_str [MAXLEN]
int eargc
char * eargv [MAXARGS]
char errmsg [MAXLEN]
error_info_t error_info
error_source_t error_source
char stdio_names_str [MAXLEN]
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 44 of file futil.c.

◆ LF_LNK

#define LF_LNK   1

Definition at line 43 of file futil.c.

◆ LF_REG

#define LF_REG   4

Definition at line 45 of file futil.c.

Function Documentation

◆ fill_field()

char * fill_field ( char * accept_s,
char * display_s,
char fill_char,
int flen )

Definition at line 1748 of file futil.c.

1749 {
1750 char *s = accept_s;
1751 char *d = display_s;
1752 char *e = d + flen;
1753 while (*s != '\0' && d < e)
1754 *d++ = *s++;
1755 while (d < e)
1756 *d++ = fill_char;
1757 *d = '\0';
1758 return display_s;

◆ 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

The returned String struct contains a dynamically allocated string of he specified length

Note
the caller is responsible for calling free_string to free the allocated memory.

Definition at line 1543 of file futil.c.

1544 {
1545 if (l == 0) {
1546 String str;
1547 str.l = 0;
1548 str.s = nullptr;
1549 return str;
1550 }
1551 String str;
1552 str.l = l + 1;
1553 str.s = (char *)malloc(str.l);
1554 str.s[0] = '\0';
1555 return str;
The String structure represents a string object with a pointer to the string and its allocated length...
Definition cm.h:762
size_t l
Definition cm.h:764
char * s
Definition cm.h:763

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 338 of file futil.c.

338 {
339 if (s == nullptr || *s == '\0')
340 return (size_t)0;
341 char *p = s + strlen(s) - 1;
342 while (p >= s && *p == ' ')
343 p--;
344 *(p + 1) = '\0';
345 return (size_t)(p - s + 1);
346}

Referenced by fmt_line(), and picker().

Here is the caller graph for this function:

◆ stdio_fdnames()

char * stdio_fdnames ( char * stdio_str,
char * id )

Definition at line 1089 of file futil.c.

1090 {
1091 if (!stdio_str)
1092 return nullptr;
1093 char buf[MAXLEN] = {'\0'};
1094 ssnprintf(buf, MAXLEN - 1, "%s - ", id);
1095 strnz__cpy(stdio_str, buf, MAXLEN - 1);
1096 strnz__cat(stdio_str, fdname(0, buf), MAXLEN - 1);
1097 strnz__cat(stdio_str, ",", MAXLEN - 1);
1098 strnz__cat(stdio_str, fdname(1, buf), MAXLEN - 1);
1099 strnz__cat(stdio_str, ",", MAXLEN - 1);
1100 strnz__cat(stdio_str, fdname(2, buf), MAXLEN - 1);
1101 strnz__cat(stdio_str, ",", MAXLEN - 1);
1102 strnz__cat(stdio_str, fdname(3, buf), MAXLEN - 1);
1103 strnz__cat(stdio_str, ",", MAXLEN - 1);
1104 strnz__cat(stdio_str, fdname(4, buf), MAXLEN - 1);
1105 strnz__cat(stdio_str, ",", MAXLEN - 1);
1106 strnz__cat(stdio_str, fdname(5, buf), MAXLEN - 1);
1107 strnz__cat(stdio_str, ",", MAXLEN - 1);
1108 strnz__cat(stdio_str, fdname(6, buf), MAXLEN - 1);
1109 strnz__cat(stdio_str, ",", MAXLEN - 1);
1110 strnz__cat(stdio_str, fdname(7, buf), MAXLEN - 1);
1111 strnz__cat(stdio_str, ",", MAXLEN - 1);
1112 strnz__cat(stdio_str, fdname(8, buf), MAXLEN - 1);
1113 return stdio_str;
1114 return stdio_str;
#define MAXLEN
Definition curskeys.c:15
size_t strnz__cpy(char *, const char *, size_t)
safer alternative to strncpy
Definition futil.c:544
size_t ssnprintf(char *, size_t, const char *,...)
ssnprintf was designed to be a safer alternative to snprintf.
Definition futil.c:420
size_t strnz__cat(char *, const char *, size_t)
safer alternative to strncat
Definition futil.c:573
char * fdname(int fd, char *out_path)
Retrieves the file path associated with a given file descriptor.
Definition futil.c:1054

References fdname(), ssnprintf(), strnz__cat(), and strnz__cpy().

Referenced by destroy_pick_view(), form_process(), and view_init_input().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ stdio_names()

char * stdio_names ( char * stdio_str,
char * id )

Definition at line 1064 of file futil.c.

1065 {
1066 if (!stdio_str)
1067 return nullptr;
1068 char buf[MAXLEN] = {'\0'};
1069 char err_str[MAXLEN] = {'\0'};
1070 errno = 0;
1071 ssnprintf(buf, MAXLEN - 1, "%s - ", id);
1072 strnz__cpy(stdio_str, buf, MAXLEN - 1);
1073 strnz__cat(stdio_str, ttyname(0), MAXLEN - 1);
1074 strnz__cat(stdio_str, ", ", MAXLEN - 1);
1075 if (errno)
1076 ssnprintf(err_str, MAXLEN - 1, "Error fd %d: %s\n", 0, strerror(errno));
1077
1078 strnz__cat(stdio_str, ttyname(1), MAXLEN - 1);
1079 strnz__cat(stdio_str, ", ", MAXLEN - 1);
1080 if (errno)
1081 ssnprintf(err_str, MAXLEN - 1, "Error fd %d: %s\n", 1, strerror(errno));
1082
1083 strnz__cat(stdio_str, ttyname(2), MAXLEN - 1);
1084 strnz__cat(stdio_str, ", ", MAXLEN - 1);
1085 if (errno)
1086 ssnprintf(err_str, MAXLEN - 1, "Error fd %d: %s\n", 2, strerror(errno));
1087
1088 return stdio_str;

References ssnprintf(), strnz__cat(), and strnz__cpy().

Referenced by fork_exec(), menu_cmd_processor(), and view_init_input().

Here is the call graph for this function:
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 941 of file futil.c.

942 {
943 if (s == nullptr || *s == '\0')
944 return false;
945 switch (s[0]) {
946 case 't':
947 case 'T':
948 case 'y':
949 case 'Y':
950 case '1':
951 return true;
952 case 'o':
953 case 'O':
954 switch (s[1]) {
955 case 'n':
956 case 'N':
957 return true;
958 default:
959 break;
960 }
961 default:
962 break;
963 }
964 return false;

Referenced by process_config_file().

Here is the caller graph for this function:

Variable Documentation

◆ cmenu_log_fd

int cmenu_log_fd

Definition at line 54 of file futil.c.

Referenced by open_cmenu_log(), write_cmenu_log(), and write_cmenu_log_nt().

◆ earg_str

char earg_str[MAXLEN]

Definition at line 56 of file futil.c.

◆ eargc

int eargc

Definition at line 57 of file futil.c.

◆ eargv

char* eargv[MAXARGS]

Definition at line 58 of file futil.c.

◆ errmsg

char errmsg[MAXLEN]

Global variables for error reporting

Definition at line 138 of file futil.c.

◆ error_info

error_info_t error_info

Definition at line 150 of file futil.c.

◆ error_source

error_source_t error_source

Definition at line 151 of file futil.c.

◆ stdio_names_str

char stdio_names_str[MAXLEN]

◆ wait_timeout

int wait_timeout

Definition at line 152 of file futil.c.

Referenced by form_process(), process_config_file(), and write_config().