Browse Source

Document functions in the util module

master
Damien Goutte-Gattat 5 years ago
parent
commit
ead2d91da4
  1. 37
      src/util.c

37
src/util.c

@ -28,6 +28,13 @@
#include <sys/stat.h>
#include <unistd.h>
/**
* Checks if the specified file exists.
*
* @param path The filename to check.
*
* @return 0 if the file exists, or -1 (check errno for details).
*/
int
file_exists(const char *path)
{
@ -41,6 +48,14 @@ file_exists(const char *path)
return stat(path, &st);
}
/**
* Gets the size of the specified file.
*
* @param f The file object.
*
* @return The file's size, or -1 if an error occured
* (check errno for details).
*/
long
get_file_size(FILE *f)
{
@ -59,6 +74,18 @@ get_file_size(FILE *f)
return s;
}
/**
* Reads a file into memory.
*
* @param filename The path to the file to read.
* @param len The address where the file's size will be stored
* (may be NULL).
* @param max Do not read the file if its size exceeds this value;
* if zero, always read the file no matter its size.
*
* @return A newly allocated buffer containing the file's data, or
* NULL if an error occured (check errno for details).
*/
unsigned char *
read_file(const char *filename, size_t *len, size_t max)
{
@ -94,6 +121,16 @@ read_file(const char *filename, size_t *len, size_t max)
return blob;
}
/**
* Writes a buffer into a file.
*
* @param filename The path to the file to write.
* @param contents The buffer to write.
* @param len The size of the \c contents buffer.
*
* @return The number of bytes written, or -1 if an error occured
* (check errno for details).
*/
int
write_file(const char *filename, const unsigned char *contents, size_t len)
{

Loading…
Cancel
Save