Ext
 All Files Functions
utils.c
1 
3 #include <string.h>
4 #include <stdio.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include "utils.h"
9 
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));
13  } else {
14  *buffer = realloc(*buffer, *buffer_size + size);
15  }
16 
17  memcpy(*buffer + *buffer_size, data, size);
18  *buffer_size += size;
19 
20  return size;
21 }
22 
23 int read_while(const int fd, char* buffer, size_t to_read) {
24  size_t total = 0;
25 
26  while (total != to_read) {
27  ssize_t readed = read(fd, buffer + total, to_read - total);
28  if (readed == -1) {
29  return readed;
30  }
31 
32  total += readed;
33  }
34 
35  return total;
36 }
37 
38 int write_while(const int fd, const char* buffer, size_t to_write) {
39  size_t total = 0;
40 
41  while (total != to_write) {
42  ssize_t written = write(fd, buffer + total, to_write - total);
43 
44  if (written == -1) {
45  return written;
46  }
47 
48  total += written;
49  }
50 
51  return total;
52 }
53 
54 char* parse_path(const char* path, char* current_file_name) {
55  if (strcmp(path, "/") == 0) {
56  strcpy(current_file_name, "/");
57  return NULL;
58  }
59 
60  char* first_slash_position = strchr(path, '/');
61 
62  if (first_slash_position == NULL) {
63  fprintf(stderr, "Incorrect path in parse_path function. Abort!\n");
64  exit(EXIT_FAILURE);
65  }
66 
67  uint16_t path_length = strlen(path);
68 
69  char* second_slash_position = strchr(first_slash_position + 1, '/');
70  uint16_t current_file_name_length = 0;
71 
72  if (second_slash_position == NULL) {
73  current_file_name_length = path_length - 1;
74  } else {
75  current_file_name_length = second_slash_position - first_slash_position - 1;
76  }
77 
78  if (current_file_name_length == 0) {
79  fprintf(stderr, "Incorrect path in parse_path function. Abort!\n");
80  exit(EXIT_FAILURE);
81  }
82 
83  memcpy(current_file_name, first_slash_position + 1, current_file_name_length);
84  current_file_name[current_file_name_length + 1] = '\0';
85 
86  return second_slash_position;
87 }
88 
89 bool split_path(const char* path, char* parent_path, char* dirname) {
90  uint16_t path_length = strlen(path);
91 
92  char buffer[buffer_length];
93  memcpy(buffer, path, path_length);
94  if (buffer[path_length - 1] != '/') {
95  buffer[path_length] = '\0';
96  } else {
97  buffer[path_length - 1] = '\0';
98  }
99  path_length = strlen(buffer);
100 
101  if (buffer[0] != '/' || path_length == 0) {
102  fprintf(stderr, "Incorrect path to split. Abort!\n");
103  return false;
104  }
105 
106  char* slash_pos = strchr(buffer + 1, '/');
107  char* last_slash_pos = buffer;
108 
109  while (slash_pos != NULL) {
110  last_slash_pos = slash_pos;
111  slash_pos = strchr(slash_pos + 1, '/');
112  }
113 
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));
117 
118  if (strlen(dirname) == 0) {
119  return false;
120  }
121 
122  return true;
123 }
124 
125 void read_command_from_stdin(char* command_buffer, size_t buffer_size) {
126  fgets(command_buffer, buffer_size, stdin);
127  uint32_t lenght = strlen(command_buffer);
128  command_buffer[lenght - 1] = '\0';
129 }
130 
131 char* parse_command(char* command_buffer, char* command) {
132  char* first_space = strchr(command_buffer, ' ');
133 
134  if (first_space == NULL) {
135  strcpy(command, command_buffer);
136  } else {
137  uint16_t command_length = first_space - command_buffer;
138  memcpy(command, command_buffer, command_length);
139  command[command_length] = '\0';
140  }
141 
142  return first_space == NULL ? NULL : first_space + 1;
143 }
144 
145 ssize_t get_file_size(const char* path_to_file) {
146  int fd = open(path_to_file, O_RDONLY);
147  if (fd == -1) {
148  fprintf(stderr, "Can't open file. Abort!");
149  return -1;
150  }
151 
152  FILE* file = fdopen(fd, "rb");
153 
154  if (file == NULL) {
155  fprintf(stderr, "Can't open file. Abort!");
156  close(fd);
157  return -1;
158  }
159 
160  struct stat stat;
161  if (fstat(fd, &stat) == -1) {
162  fprintf(stderr, "Can't open file. Abort!");
163  fclose(file);
164  close(fd);
165  return -1;
166  }
167 
168  ssize_t size = stat.st_size;
169  fclose(file);
170  close(fd);
171  return size;
172 }
Contains some useful methods.
int read_while(int fd, char *buffer, size_t to_read)
Properly reading from memory.
Definition: utils.c:23
int write_while(int fd, const char *buffer, size_t to_write)
Properly writing to memory.
Definition: utils.c:38
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...
Definition: utils.c:131
ssize_t get_file_size(const char *path_to_file)
Get size of file.
Definition: utils.c:145
void read_command_from_stdin(char *command_buffer, size_t buffer_size)
Read line from stdin.
Definition: utils.c:125
int buffered_write(char **buffer, size_t *buffer_size, char *data, size_t size)
Definition: utils.c:10