summaryrefslogtreecommitdiff
path: root/lib/lslib.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/lslib.h')
-rw-r--r--lib/lslib.h374
1 files changed, 337 insertions, 37 deletions
diff --git a/lib/lslib.h b/lib/lslib.h
index 0836e44..e31f5b1 100644
--- a/lib/lslib.h
+++ b/lib/lslib.h
@@ -1,3 +1,8 @@
+/**
+ * file: lslib.h
+ * author: Tyler Murphy
+ */
+
#ifndef SHARED_H
#define SHARED_H
@@ -6,9 +11,9 @@
#include <sys/stat.h>
#include <pwd.h>
-
-#define ANSCII "\x1b["
-#define NEXT ";"
+/* create define flags to be used with color anscii codes */
+#define ANSCII "\x1b[" /* anscii start escape code */
+#define NEXT ";" /* anscii seperator */
#define RESET "0"
#define BOLD "1"
@@ -26,119 +31,414 @@
#define TURQUOISE "6"
#define WHITE "7"
-#define COLOR "m"
-
+#define COLOR "m" /* color anscii code */
+/**
+ * Define bool cause c89 dont have it
+ */
typedef uint8_t bool;
#define true 1
#define false 0
-
+/* mark argument unused */
#define UNUSED(x) (void)(x)
-#define ARG_UNUSED 0
-#define ARG_USED 1
-#define ARG_IGNORE 2
-#define ARG_INVALID 3
+/*
+ * 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
+ */
void check_arg (char* arg);
+
+/**
+ * Print help message with lazysphere title then exit
+ * @param help fn pointer to call to display fn specific help msg
+ */
void global_help(void (*help)(void));
+
+/**
+ * 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
+ */
void parse_help (int argc, char** argv, void (*help)(void));
-int parse_args (int argc, char** argv, void (*help)(void), int (*short_arg)(char, char*), int (*long_arg)(char*, char*));
+/**
+ * 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*));
+/*
+ * 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
+ */
FILE* get_file_s(const char* path, const char* type);
+
+/*
+ * 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
+ */
FILE* get_file(const char* path, const char* type);
+
+/**
+ * Open a tty and error and die if failed
+ * @return the file descripter to the tty
+ */
int get_tty(void);
-FILE* get_tty_stream(char* type);
-void nuke_str(char* type);
+/**
+ * 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);
+/**
+ * Get a reference to the first path buffer
+ * @return the pointer to the first path buffer
+ */
char* get_path_buffer(void);
+
+/**
+ * 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
+ */
int push_path_buffer(const char* string);
+
+/**
+ * Revert the first path buffer to the index provided by push_path_buffer
+ * @param i the index to revert to
+ */
void pop_path_buffer(int i);
+/**
+ * Get a reference to the second path buffer
+ * @return the pointer to the second path buffer
+ */
char* get_path_buffer_2(void);
-int push_path_buffer_2(const char* string);
-void pop_path_buffer_2(int i);
+/**
+ * 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);
+/**
+ * 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);
+/**
+ * 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
+ */
long int get_number(const char* text);
+
+/**
+ * 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
+ */
long int get_blkm(const char* text);
+
+/**
+ * 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
+ */
mode_t get_mode(const char* text);
+/**
+ * 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
+ */
bool streql(const char* a, const char* b);
+
+/**
+ * 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
+ */
bool prefix(const char* pre, const char* str);
+/**
+ * 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
+ */
void print_file_size(size_t bytes, char buf[5]);
+
+/**
+ * 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
+ */
void print_date_time(time_t mills, char buf[13]);
+
+/**
+ * Print a given file path to stdout or (standard input) if '-'
+ * @param path the file path
+ */
void print_file_path(char* path);
+
+/**
+* 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
+ */
bool is_dot_dir(const char* path);
-const char* get_last_component(const char* path);
+/**
+ * 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);
+/**
+* Die
+*/
void die (void);
+/**
+ * Print error message to stdout
+ * @param format the format string
+ * @param ... the rest of the arguments
+ */
__attribute__ ((__format__(printf, 1, 2)))
void error_s(const char* format, ...);
+/**
+ * Print error message to stdout and die
+ * @param format the format string
+ * @param ... the rest of the arguments
+ */
__attribute__ ((__format__(printf, 1, 2)))
void error(const char* format, ...);
+/**
+ * Output message as the given command
+ * @param format the format string
+ * @param ... the rest of the arguments
+ */
__attribute__ ((__format__(printf, 1, 2)))
void output(const char* format, ...);
-
+/**
+ * Refine the regex as a ADT
+ */
typedef struct regex_t* re_t;
+
+/**
+ * Compile a given regex pattern
+ * @param pattern the regex to compile
+ * @return the compiled regex
+ */
re_t re_compile(const char* pattern);
+
+/**
+ * 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
+ */
int re_matchp(re_t pattern, const char* text, int* matchlength);
-int re_match(const char* pattern, const char* text, int* matchlength);
+/**
+ * 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);
+/**
+ * A struct containing a stack
+ */
struct Stack {
- size_t size;
- size_t capacity;
- void* data;
+ size_t size; /* The size of the stack */
+ size_t capacity; /* The capacity of the stack */
+ void* data; /* THe data stored in the stack */
};
+/**
+ * Initalize a stack with a given size
+ * @param stack a allocated stack struct to inatalize
+ * @param size the initial stack capacity
+ */
void stack_init(struct Stack* stack, size_t size);
+
+/**
+ * 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
+ */
void stack_push(struct Stack* stack, void* data, size_t len);
+
+/**
+ * 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)
+ */
void* stack_pop(struct Stack* stack, size_t len);
+
+/**
+ * Free a stacks allocated memory
+ * @param stack the stack to free
+ */
void stack_free(struct Stack* stack);
+/**
+ * Wrapper function to push a int to the stack
+ * @param stack the stack to push to
+ * @param value the int to push
+ */
void stack_push_int(struct Stack* stack, int value);
-bool stack_pop_int(struct Stack* stack, int* value);
+/**
+ * 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);
+/* The default shell if no shell is provided */
#define DEFAULT_SHELL "/bin/sh"
+
+/* The default PATH env variable */
#define DEFAULT_PATH "/sbin:/usr/sbin:/bin:/usr/bin"
-void setup_environment(const char *shell, bool new_env, bool clear_env, const struct passwd *pw);
-void exec_shell(const char *shell, bool loginshell, const char **additional_args);
+/**
+ * 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);
-int check_password(const struct passwd *pw, char* plaintext);
-int prompt_password(const struct passwd *pw, char* prompt);
+#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);
-void change_identity(const struct passwd* pw);
+/**
+ * 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
+ */
+void change_identity(const struct passwd* pw);
+/**
+ * Alloc memory and die if failed
+ * @param size the amount of memory to allocate in bytes
+ * @return the pointer to the allocated data
+ */
void* xalloc(size_t size);
+
+/**
+ * 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);
-void* xzalloc(size_t size);
-void xsetenv(const char *name, const char *value);
-void xsetuid(uid_t uid);
-void xsetgid(gid_t gid);
-struct passwd* xgetpwnam(char* name);
+/**
+ * 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);
-char* crypt(const char* plaintext, const char* pw_pass);
+/**
+ * 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);
-#define PASSWORD_INVALID 0
-#define PASSWORD_VALID 1
-#define PASSWORD_EMPTY 2
+/**
+ * Set the gid, die if failed
+ * @param gid the gid to set to
+ */
+void xsetgid(gid_t gid);
+/**
+ * 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);
-#endif
+#endif /* SHARED_H */