#include "macro.h"
#include "util.h"
-#define BUF_MAX (BUFSIZ)
-#define BUF_INC_SIZE (512 << 10)
-
int fwrite_str(const char *path, const char *str)
{
_cleanup_fclose_ FILE *f = NULL;
*number = t;
return RESOURCED_ERROR_NONE;
}
-
-/* reads file contents into memory */
-char* cread(const char* path)
-{
- char* text = NULL;
- size_t size = 0;
-
- ssize_t ret;
- char* ptr = text;
- size_t cap = size;
- _cleanup_close_ int fd = -1;
-
- assert(path);
-
- fd = open(path, O_RDONLY);
- if (fd < 0) {
- _E("Failed to open %s: %m", path);
- return NULL;
- }
-
- do {
- /* ensure we have enough space */
- if (cap == 0) {
- ptr = (char*)realloc(text, size + BUF_INC_SIZE);
- if (ptr == NULL) {
- ret = -1;
- break;
- }
-
- text = ptr;
- ptr = text + size;
- cap = BUF_INC_SIZE;
- size += BUF_INC_SIZE;
- }
- ret = read(fd, ptr, cap);
- if (ret == 0) {
- *ptr = 0;
- } else if (ret > 0) {
- cap -= ret;
- ptr += ret;
- } else
- free(text);
- } while (ret > 0);
-
- return (ret < 0 ? NULL : text);
-}
-
-/* like fgets/gets but adjusting contents pointer */
-char* cgets(char** contents)
-{
- if (contents && *contents && **contents) {
- char* bos = *contents; /* begin of string */
- char* eos = strchr(bos, '\n'); /* end of string */
-
- if (eos) {
- *contents = eos + 1;
- *eos = 0;
- } else {
- *contents = NULL;
- }
-
- return bos;
- }
-
- return NULL;
-}
-
-int copy_file(char *dest, char *src)
-{
- _cleanup_fclose_ FILE *fps = NULL;
- _cleanup_fclose_ FILE *fpd = NULL;
- char buf[BUF_MAX];
- size_t size;
-
- fps = fopen(src, "rb");
- if (fps == NULL) {
- _E("Failed to open src file '%s': %m", src);
- return -errno;
- }
-
- fpd = fopen(dest, "wb");
- if (fpd == NULL) {
- _E("Failed to open dest file '%s': %m", dest);
- return -errno;
- }
-
- while ((size = fread(buf, 1, BUF_MAX, fps)))
- fwrite(buf, 1, size, fpd);
-
- return RESOURCED_ERROR_NONE;
-}
return fread_nth_ulong(path, 0, number);
}
-char *cread(const char *path);
-char *cgets(char **contents);
-
-/**
- * @desc copy file from src to dest
- * @param dest- destination file path, src- source file path
- * @return negative value if error
- */
-int copy_file(char *dest, char *src);
-
#endif /*_RESOURCED_FILE_HELPER_H_*/