10 int buffered_write(
char** buffer,
size_t* buffer_size,
char* data,
size_t size) {
11 if (*buffer == NULL) {
12 *buffer = calloc(size,
sizeof(
char));
14 *buffer = realloc(*buffer, *buffer_size + size);
17 memcpy(*buffer + *buffer_size, data, size);
23 int read_while(
const int fd,
char* buffer,
size_t to_read) {
26 while (total != to_read) {
27 ssize_t readed = read(fd, buffer + total, to_read - total);
38 int write_while(
const int fd,
const char* buffer,
size_t to_write) {
41 while (total != to_write) {
42 ssize_t written = write(fd, buffer + total, to_write - total);
54 char* parse_path(
const char* path,
char* current_file_name) {
55 if (strcmp(path,
"/") == 0) {
56 strcpy(current_file_name,
"/");
60 char* first_slash_position = strchr(path,
'/');
62 if (first_slash_position == NULL) {
63 fprintf(stderr,
"Incorrect path in parse_path function. Abort!\n");
67 uint16_t path_length = strlen(path);
69 char* second_slash_position = strchr(first_slash_position + 1,
'/');
70 uint16_t current_file_name_length = 0;
72 if (second_slash_position == NULL) {
73 current_file_name_length = path_length - 1;
75 current_file_name_length = second_slash_position - first_slash_position - 1;
78 if (current_file_name_length == 0) {
79 fprintf(stderr,
"Incorrect path in parse_path function. Abort!\n");
83 memcpy(current_file_name, first_slash_position + 1, current_file_name_length);
84 current_file_name[current_file_name_length + 1] =
'\0';
86 return second_slash_position;
89 bool split_path(
const char* path,
char* parent_path,
char* dirname) {
90 uint16_t path_length = strlen(path);
92 char buffer[buffer_length];
93 memcpy(buffer, path, path_length);
94 if (buffer[path_length - 1] !=
'/') {
95 buffer[path_length] =
'\0';
97 buffer[path_length - 1] =
'\0';
99 path_length = strlen(buffer);
101 if (buffer[0] !=
'/' || path_length == 0) {
102 fprintf(stderr,
"Incorrect path to split. Abort!\n");
106 char* slash_pos = strchr(buffer + 1,
'/');
107 char* last_slash_pos = buffer;
109 while (slash_pos != NULL) {
110 last_slash_pos = slash_pos;
111 slash_pos = strchr(slash_pos + 1,
'/');
114 memcpy(parent_path, buffer, last_slash_pos - buffer + 1);
115 parent_path[last_slash_pos - buffer + 1] =
'\0';
116 memcpy(dirname, last_slash_pos + 1, buffer_length - strlen(parent_path));
118 if (strlen(dirname) == 0) {
126 fgets(command_buffer, buffer_size, stdin);
127 uint32_t lenght = strlen(command_buffer);
128 command_buffer[lenght - 1] =
'\0';
132 char* first_space = strchr(command_buffer,
' ');
134 if (first_space == NULL) {
135 strcpy(command, command_buffer);
137 uint16_t command_length = first_space - command_buffer;
138 memcpy(command, command_buffer, command_length);
139 command[command_length] =
'\0';
142 return first_space == NULL ? NULL : first_space + 1;
146 int fd = open(path_to_file, O_RDONLY);
148 fprintf(stderr,
"Can't open file. Abort!");
152 FILE* file = fdopen(fd,
"rb");
155 fprintf(stderr,
"Can't open file. Abort!");
161 if (fstat(fd, &stat) == -1) {
162 fprintf(stderr,
"Can't open file. Abort!");
168 ssize_t size = stat.st_size;
Contains some useful methods.
int read_while(int fd, char *buffer, size_t to_read)
Properly reading from memory.
int write_while(int fd, const char *buffer, size_t to_write)
Properly writing to memory.
char * parse_command(char *command_buffer, char *command)
Parse command Parse command to command and args. Put command to arg command and return position of fi...
ssize_t get_file_size(const char *path_to_file)
Get size of file.
void read_command_from_stdin(char *command_buffer, size_t buffer_size)
Read line from stdin.
int buffered_write(char **buffer, size_t *buffer_size, char *data, size_t size)