-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.h
89 lines (73 loc) · 2.02 KB
/
utils.h
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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_TOKENS 64
/*
* Checks if user wishes to exit interactive mode.
*
* Parameters:
* input: user input
*
* Returns:
* 1 if input is "exit", 0 otherwise
*/
int exit_check(char* input);
/*
* Strips leading and trailing whitespace from user input, in-place.
*
* Parameters:
* str: user input
*/
void strip_whitespace(char* str);
/*
* Tokenizes user input, for use with execv. Caller is responsible for freeing memory,
* function free_tokens() is provided for this purpose.
*
* Parameters:
* str: user input
*
* Returns:
* tokens: array of string tokens, where the first token is the command and the rest are args
*/
char** tokenize(char* str);
/*
* Frees memory allocated for tokens by tokenize().
*
* Parameters:
* tokens: array of string tokens
*/
void free_tokens(char** tokens);
/*
* Combines an executable name with a path to create a full path to the executable.
* Caller is responsible for freeing memory allocated for return value.
*
* Parameters:
* path: path to use, i.e. /usr/bin/
* cmd: executable name, i.e. ls
*/
char* get_full_path(char* path, char* cmd);
/*
* Checks if a file is executable.
*
* Parameters:
* full_path: full path to file, i.e. /usr/bin/ls
*
* Returns:
* 1 if file is executable, 0 otherwise
*/
int is_executable(char* full_path);
/*
* Searches for an executable in a list of paths.
* Caller is responsible for freeing memory allocated for return value.
*
* Parameters:
* paths: list of paths to search, i.e. {"/bin", "/usr/bin", NULL}
* tokens: array of string tokens, where the first token is the command and the rest are args
* for example, {"ls", "-l", NULL}
*
* Returns:
* full_path: full path to executable, i.e. /usr/bin/ls
*/
char* get_exec_path(char** paths, char* cmd);