Add unit(in variable) & fix bugs
[platform/core/system/resourced.git] / src / common / file-helper.c
index afd3f94..0d9e0bd 100644 (file)
@@ -90,6 +90,18 @@ int fwrite_ulong(const char *path, const unsigned long number)
        return fwrite_str(path, digit_buf);
 }
 
+int fwrite_ulonglong(const char *path, const unsigned long long number)
+{
+       _cleanup_free_ char *digit_buf = NULL;
+       int ret;
+
+       ret = asprintf(&digit_buf, "%llu", number);
+       ret_value_errno_msg_if(ret < 0, -ENOMEM,
+                              "sprintf failed\n");
+
+       return fwrite_str(path, digit_buf);
+}
+
 int fread_str(const char *path, char **str)
 {
        _cleanup_fclose_ FILE *f = NULL;
@@ -172,3 +184,25 @@ int fread_nth_ulong(const char *path, size_t n, unsigned long *number)
        *number = t;
        return RESOURCED_ERROR_NONE;
 }
+
+int fread_nth_ulonglong(const char *path, size_t n, unsigned long long *number)
+{
+       _cleanup_fclose_ FILE *f = NULL;
+       size_t i;
+       unsigned long long t;
+       int ret;
+
+       f = fopen(path, "r");
+       ret_value_errno_msg_if(!f, -errno,
+                              "Fail to open %s file.", path);
+
+       errno = 0;
+       for (i = 0; i <= n; i++) {
+               ret = fscanf(f, "%llu", &t);
+               ret_value_errno_msg_if(ret == EOF, -(errno ?: ENOENT),
+                                      "Fail to read file\n");
+       }
+
+       *number = t;
+       return RESOURCED_ERROR_NONE;
+}