summaryrefslogtreecommitdiff
path: root/lib/lslib.h
blob: e31f5b1b602e45239d8e9c22a1fc9e52919abca8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/**
 * file: lslib.h
 * author: Tyler Murphy
 */

#ifndef SHARED_H
#define SHARED_H

#include <stdint.h>
#include <stdio.h>
#include <sys/stat.h>
#include <pwd.h>

/* 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"

#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"

#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)

/*
 * 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));

/**
 * 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);

/**
 * 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);

/**
 * 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);

/**
 * 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);

/**
 * 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;        /* 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); 

/**
 * 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"

/**
 * 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
 */
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);

/**
 * 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);

/**
 * 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);

/**
 * 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 /* SHARED_H */