lazysphere/lib/lslib.h

445 lines
12 KiB
C
Raw Permalink Normal View History

2023-05-16 23:15:01 +00:00
/**
* file: lslib.h
* author: Tyler Murphy
*/
2023-05-06 04:39:44 +00:00
#ifndef SHARED_H
#define SHARED_H
#include <stdint.h>
#include <stdio.h>
2023-05-15 01:43:02 +00:00
#include <sys/stat.h>
#include <pwd.h>
2023-05-16 23:15:01 +00:00
/* create define flags to be used with color anscii codes */
#define ANSCII "\x1b[" /* anscii start escape code */
#define NEXT ";" /* anscii seperator */
2023-05-15 01:43:02 +00:00
#define RESET "0"
#define BOLD "1"
#define NORMAL "3"
#define BACKGROUND "4"
#define HIGHLIGHT "9"
#define BLACK "0"
#define RED "1"
#define GREEN "2"
#define YELLOW "3"
#define BLUE "4"
#define MAGENTA "5"
#define TURQUOISE "6"
#define WHITE "7"
2023-05-16 23:15:01 +00:00
#define COLOR "m" /* color anscii code */
2023-05-15 01:43:02 +00:00
2023-05-16 23:15:01 +00:00
/**
* Define bool cause c89 dont have it
*/
2023-05-15 01:43:02 +00:00
typedef uint8_t bool;
#define true 1
#define false 0
2023-05-16 23:15:01 +00:00
/* mark argument unused */
2023-05-15 01:43:02 +00:00
#define UNUSED(x) (void)(x)
2023-05-16 23:15:01 +00:00
/*
* Flags to return from parse_args fn pointers
*/
#define ARG_UNUSED 0 /* state that the next char* wasnt used */
#define ARG_USED 1 /* state that the next char* was used */
#define ARG_IGNORE 2 /* stop parsing arguments */
#define ARG_INVALID 3 /* print error message and die */
/*
* Die is argument is null
* @param arg argument to check
*/
2023-05-15 01:43:02 +00:00
void check_arg (char* arg);
2023-05-16 23:15:01 +00:00
/**
* Print help message with lazysphere title then exit
* @param help fn pointer to call to display fn specific help msg
*/
2023-05-15 01:43:02 +00:00
void global_help(void (*help)(void));
2023-05-16 23:15:01 +00:00
/**
* Parse arguments and only check for --help message otherwise do nothing
* @param argc argument count
* @param argv argument data
* @param help fn pointer to call and display fn specific help msg
*/
2023-05-15 01:43:02 +00:00
void parse_help (int argc, char** argv, void (*help)(void));
2023-05-16 23:15:01 +00:00
/**
* Parse arguments with given short and long arg pointers, and call help fn if --help supplied
* @param argv argument count
* @param argv argument data
* @param help fn pointer to call and display fn specific help msg
* @param short_arg fn pointer to check char after a -, 2nd char* is the next unparsed argv for something like -s [str]
* @param long_arg fn pointer to check string after a --, 2nd char* is the next unparsed argv for something like --string [str]
* @returns argc where arguments ended
*/
int parse_args (int argc, char** argv, void (*help)(void), int (*short_arg)(char, char*), int (*long_arg)(char*, char*));
2023-05-15 01:43:02 +00:00
2023-05-16 23:15:01 +00:00
/*
* Open a file with a given path and type, and print error if failed
* @param path the path to the file
* @param type the mode to open the file with
* @return the file pointer if opened, NULL if failed
*/
2023-05-15 01:43:02 +00:00
FILE* get_file_s(const char* path, const char* type);
2023-05-16 23:15:01 +00:00
/*
* Open a file with a given path and type, and print error and die if failed
* @param path the path to the file
* @param type the mode to open the file with
* @return the file pointer
*/
2023-05-15 01:43:02 +00:00
FILE* get_file(const char* path, const char* type);
2023-05-16 23:15:01 +00:00
/**
* Open a tty and error and die if failed
* @return the file descripter to the tty
*/
2023-05-15 01:43:02 +00:00
int get_tty(void);
2023-05-16 23:15:01 +00:00
/**
* Open a tty stream with the given mode and error and die if failed
* @param type the mode to open the tty with
* @return the file pointer to the tty
*/
FILE* get_tty_stream(char* type);
2023-05-15 01:43:02 +00:00
2023-05-16 23:15:01 +00:00
/**
* Get a reference to the first path buffer
* @return the pointer to the first path buffer
*/
2023-05-15 01:43:02 +00:00
char* get_path_buffer(void);
2023-05-16 23:15:01 +00:00
/**
* Push a string to the first path buffer, a / as added before string if it doesnt allready exist at the end
* @param stirng the local file path to push to the first path buffer
* @return the integer index to revert to then done
*/
2023-05-15 01:43:02 +00:00
int push_path_buffer(const char* string);
2023-05-16 23:15:01 +00:00
/**
* Revert the first path buffer to the index provided by push_path_buffer
* @param i the index to revert to
*/
2023-05-15 01:43:02 +00:00
void pop_path_buffer(int i);
2023-05-16 23:15:01 +00:00
/**
* Get a reference to the second path buffer
* @return the pointer to the second path buffer
*/
2023-05-15 01:43:02 +00:00
char* get_path_buffer_2(void);
2023-05-16 23:15:01 +00:00
/**
* Push a string to the second path buffer, a / as added before string if it doesnt allready exist at the end
* @param stirng the local file path to push to the first path buffer
* @return the integer index to revert to then done
*/
int push_path_buffer_2(const char* string);
2023-05-15 01:43:02 +00:00
2023-05-16 23:15:01 +00:00
/**
* Revert the second path buffer to the index provided by push_path_buffer_2
* @param i the index to revert to
*/
void pop_path_buffer_2(int i);
2023-05-15 01:43:02 +00:00
2023-05-16 23:15:01 +00:00
/**
* Get a base 10 number from a string and error and die if failed
* @param text the string that contains the number
* @return the parsed integer if successfull
*/
2023-05-15 01:43:02 +00:00
long int get_number(const char* text);
2023-05-16 23:15:01 +00:00
/**
* Get a blkm (1024m, 38b, 26G) from a string and error and die if failed
* @param text the string that contains the blkm
* @return the blkm in bytes if successfull
*/
2023-05-15 01:43:02 +00:00
long int get_blkm(const char* text);
2023-05-16 23:15:01 +00:00
/**
* Get a mode_t from a string and error and die if failed
* @param text the string that contains the mode_t
* @return the mode_t if successfull
*/
2023-05-15 01:43:02 +00:00
mode_t get_mode(const char* text);
2023-05-16 23:15:01 +00:00
/**
* Check if two strings are exactly equal
* @param a null terminated string a
* @param b null terminated string b
* @returns true if the two strings are exactly equal else false
*/
2023-05-15 01:43:02 +00:00
bool streql(const char* a, const char* b);
2023-05-16 23:15:01 +00:00
/**
* Check if a string has a given prefix
* @param pre the null terminated prefix
* @param str the null terminated string
* @returns true if str is prefixed with pre else false
*/
2023-05-15 01:43:02 +00:00
bool prefix(const char* pre, const char* str);
2023-05-16 23:15:01 +00:00
/**
* Put a human redable file size (1024M) into buf with the given byte count
* @param bytes the byte count
* @param buf a char buf of length 5 to contain the parsed size
*/
2023-05-15 01:43:02 +00:00
void print_file_size(size_t bytes, char buf[5]);
2023-05-16 23:15:01 +00:00
/**
* Put a human redable date into buf with the given mills since unix epoch
* @param mills mills since the unix epoch
* @param buf a char buf of length 13 to contain the parsed date
*/
2023-05-15 01:43:02 +00:00
void print_date_time(time_t mills, char buf[13]);
2023-05-16 23:15:01 +00:00
/**
* Print a given file path to stdout or (standard input) if '-'
* @param path the file path
*/
2023-05-15 01:43:02 +00:00
void print_file_path(char* path);
2023-05-16 23:15:01 +00:00
/**
* Nuke a given string in memory
* @param the stirng to nuke
*/
void nuke_str(char* type);
/**
* Checks if a given character is printable symbol
* @returns if its printable
*/
bool printable_char(char c);
/**
* Checks if a directory name is . or ..
* @param path the directory name
* @return if a directory is one of the . or .. directorys
*/
2023-05-15 01:43:02 +00:00
bool is_dot_dir(const char* path);
2023-05-16 23:15:01 +00:00
/**
* Get the last component of a path
* @param path the path to parse
* @return a const reference to the last component
*/
const char* get_last_component(const char* path);
2023-05-15 01:43:02 +00:00
2023-05-16 23:15:01 +00:00
/**
* Die
*/
2023-05-15 01:43:02 +00:00
void die (void);
2023-05-16 23:15:01 +00:00
/**
* Print error message to stdout
* @param format the format string
* @param ... the rest of the arguments
*/
2023-05-15 01:43:02 +00:00
__attribute__ ((__format__(printf, 1, 2)))
void error_s(const char* format, ...);
2023-05-16 23:15:01 +00:00
/**
* Print error message to stdout and die
* @param format the format string
* @param ... the rest of the arguments
*/
2023-05-15 01:43:02 +00:00
__attribute__ ((__format__(printf, 1, 2)))
void error(const char* format, ...);
2023-05-16 23:15:01 +00:00
/**
* Output message as the given command
* @param format the format string
* @param ... the rest of the arguments
*/
2023-05-15 01:43:02 +00:00
__attribute__ ((__format__(printf, 1, 2)))
void output(const char* format, ...);
2023-05-16 23:15:01 +00:00
/**
* Refine the regex as a ADT
*/
2023-05-15 01:43:02 +00:00
typedef struct regex_t* re_t;
2023-05-16 23:15:01 +00:00
/**
* Compile a given regex pattern
* @param pattern the regex to compile
* @return the compiled regex
*/
2023-05-15 01:43:02 +00:00
re_t re_compile(const char* pattern);
2023-05-16 23:15:01 +00:00
/**
* Match a given regex pattern with the string
* @param pattern the pattern to match with
* @param text the text to match
* @param matchlength a pointer to how much was matched
* @return the index where the match was found, -1 if nothing matched
*/
2023-05-15 01:43:02 +00:00
int re_matchp(re_t pattern, const char* text, int* matchlength);
2023-05-16 23:15:01 +00:00
/**
* Compile and match a given regex pattern with the string
* @param pattern the pattern to compile and match with
* @param text the text to match
* @param matchlength a pointer to how much was matched
* @return the index where the match was found, -1 if nothing matched
*/
int re_match(const char* pattern, const char* text, int* matchlength);
2023-05-15 01:43:02 +00:00
2023-05-16 23:15:01 +00:00
/**
* A struct containing a stack
*/
2023-05-15 01:43:02 +00:00
struct Stack {
2023-05-16 23:15:01 +00:00
size_t size; /* The size of the stack */
size_t capacity; /* The capacity of the stack */
void* data; /* THe data stored in the stack */
2023-05-15 01:43:02 +00:00
};
2023-05-16 23:15:01 +00:00
/**
* Initalize a stack with a given size
* @param stack a allocated stack struct to inatalize
* @param size the initial stack capacity
*/
2023-05-15 01:43:02 +00:00
void stack_init(struct Stack* stack, size_t size);
2023-05-16 23:15:01 +00:00
/**
* Push data to the stack
* @param stack the stack to push to
* @param data the data to push
* @param len the length of the data to push
*/
2023-05-15 01:43:02 +00:00
void stack_push(struct Stack* stack, void* data, size_t len);
2023-05-16 23:15:01 +00:00
/**
* Pop the data from the stack (move the pointer back by len doesn't delete)
* @param stack the stack to pop from
* @param len the length of the data to pop
* @return the pointer to the data popped (not yet destroyed until pushed)
*/
2023-05-15 01:43:02 +00:00
void* stack_pop(struct Stack* stack, size_t len);
2023-05-16 23:15:01 +00:00
/**
* Free a stacks allocated memory
* @param stack the stack to free
*/
2023-05-15 01:43:02 +00:00
void stack_free(struct Stack* stack);
2023-05-16 23:15:01 +00:00
/**
* Wrapper function to push a int to the stack
* @param stack the stack to push to
* @param value the int to push
*/
2023-05-15 01:43:02 +00:00
void stack_push_int(struct Stack* stack, int value);
2023-05-16 23:15:01 +00:00
/**
* Wrapper function to pop a int from the stack
* @param stack the stack to pop from
* @param value the int pointer to pop
* @returns if a int was successfully popped
*/
bool stack_pop_int(struct Stack* stack, int* value);
2023-05-15 01:43:02 +00:00
2023-05-16 23:15:01 +00:00
/* The default shell if no shell is provided */
2023-05-15 01:43:02 +00:00
#define DEFAULT_SHELL "/bin/sh"
2023-05-16 23:15:01 +00:00
/* The default PATH env variable */
#define DEFAULT_PATH "/sbin:/usr/sbin:/bin:/usr/bin"
2023-05-15 01:43:02 +00:00
2023-05-16 23:15:01 +00:00
/**
* Setup the environment variables for a given user
* @param shell the shell to set
* @param new_env if to update the env variables according to pw
* @param clear_env if to remove the old env variables
* @param pw the user to update from
*/
void setup_environment(const char* shell, bool new_env, bool clear_env, const struct passwd *pw);
/**
* Exec a shell using the current environment, function will not return
* @param shell the shell to execute
* @param loginshell if to set the shell as login
* @param additional_args additional arguments to pass to the shell command
*/
void exec_shell(const char* shell, bool loginshell, const char** additional_args);
#define PASSWORD_INVALID 0 /* if the password was invalid */
#define PASSWORD_VALID 1 /* if the password was valid */
#define PASSWORD_EMPTY 2 /* if the user has an empty password */
/**
* Check if a given plaintext password matches the users /etc/shadow hash
* @param pw the user to check
* @param plaintext the plaintext password to check
* @return if the password was invalid, valid, or empty
*/
int check_password(const struct passwd* pw, char* plaintext);
/**
* Prompt the user for their password and then check the given password
* @param pw the user to check
* @param prompt the pompt to give to the user
* @return if the password was invalid, valid, or empty
*/
int prompt_password(const struct passwd* pw, char* prompt);
/**
* Set UID and GID to a given user
* @param user to change to
*/
2023-05-15 01:43:02 +00:00
void change_identity(const struct passwd* pw);
2023-05-16 23:15:01 +00:00
/**
* Alloc memory and die if failed
* @param size the amount of memory to allocate in bytes
* @return the pointer to the allocated data
*/
2023-05-15 01:43:02 +00:00
void* xalloc(size_t size);
2023-05-16 23:15:01 +00:00
/**
* Realloc memory and die if failed
* @param ptr the pointer to realloc
* @param size the new size of the data
* @return the pointer to the reallocated data
*/
void* xrealloc(void* ptr, size_t size);
2023-05-15 14:57:33 +00:00
2023-05-16 23:15:01 +00:00
/**
* Alloc memory and zero it, die if failed
* @param size the amouint of memory to allocate in bytes
* @return the zeroed pointer to the allocated data
*/
void* xzalloc(size_t size);
2023-05-15 14:57:33 +00:00
2023-05-16 23:15:01 +00:00
/**
* Set the env, die if failed
* @param name the key to set
* @param value to value to set the key to
*/
void xsetenv(const char* name, const char* value);
/**
* Set the uid, die if failed
* @param uid the uid to set to
*/
void xsetuid(uid_t uid);
2023-05-15 14:57:33 +00:00
2023-05-16 23:15:01 +00:00
/**
* Set the gid, die if failed
* @param gid the gid to set to
*/
void xsetgid(gid_t gid);
2023-05-06 04:39:44 +00:00
2023-05-16 23:15:01 +00:00
/**
* Get pw by name, die if failed
* @param name the name of the user to get
* @return the pw of the given user
*/
struct passwd* xgetpwnam(char* name);
2023-05-06 04:39:44 +00:00
2023-05-16 23:15:01 +00:00
#endif /* SHARED_H */