4 * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
23 * @desc Helper functions for working with files
32 #include "file-helper.h"
37 int fwrite_str(const char *path, const char *str)
39 _cleanup_fclose_ FILE *f = NULL;
46 ret_value_errno_msg_if(!f, -errno,
47 "Fail to open file %s", path);
51 ret_value_errno_msg_if(ret == EOF, -(errno ?: EIO),
52 "Fail to write file");
54 return RESOURCED_ERROR_NONE;
57 int fwrite_int(const char *path, const int number)
59 _cleanup_free_ char *digit_buf = NULL;
62 ret = asprintf(&digit_buf, "%d", number);
63 ret_value_errno_msg_if(ret < 0, -ENOMEM,
66 return fwrite_str(path, digit_buf);
69 int fwrite_uint(const char *path, const u_int32_t number)
71 _cleanup_free_ char *digit_buf = NULL;
74 ret = asprintf(&digit_buf, "%u", number);
75 ret_value_errno_msg_if(ret < 0, -ENOMEM,
78 return fwrite_str(path, digit_buf);
81 int fwrite_ulong(const char *path, const unsigned long number)
83 _cleanup_free_ char *digit_buf = NULL;
86 ret = asprintf(&digit_buf, "%lu", number);
87 ret_value_errno_msg_if(ret < 0, -ENOMEM,
90 return fwrite_str(path, digit_buf);
93 int fwrite_ulonglong(const char *path, const unsigned long long number)
95 _cleanup_free_ char *digit_buf = NULL;
98 ret = asprintf(&digit_buf, "%llu", number);
99 ret_value_errno_msg_if(ret < 0, -ENOMEM,
102 return fwrite_str(path, digit_buf);
105 int fread_str(const char *path, char **str)
107 _cleanup_fclose_ FILE *f = NULL;
110 f = fopen(path, "r");
111 ret_value_errno_msg_if(!f, -errno,
112 "Fail to open %s file.", path);
114 ret = fgets(*str, LINE_MAX, f);
115 ret_value_errno_msg_if(!ret, -ENODATA,
116 "Fail to read file\n");
119 return RESOURCED_ERROR_NONE;
122 int fread_nth_int(const char *path, size_t n, int32_t *number)
124 _cleanup_fclose_ FILE *f = NULL;
129 f = fopen(path, "r");
130 ret_value_errno_msg_if(!f, -errno,
131 "Fail to open %s file.", path);
134 for (i = 0; i <= n; i++) {
135 ret = fscanf(f, "%d", &t);
136 ret_value_errno_msg_if(ret == EOF, -(errno ?: ENOENT),
137 "Fail to read file\n");
141 return RESOURCED_ERROR_NONE;
144 int fread_nth_uint(const char *path, size_t n, u_int32_t *number)
146 _cleanup_fclose_ FILE *f = NULL;
151 f = fopen(path, "r");
152 ret_value_errno_msg_if(!f, -errno,
153 "Fail to open %s file.", path);
156 for (i = 0; i <= n; i++) {
157 ret = fscanf(f, "%u", &t);
158 ret_value_errno_msg_if(ret == EOF, -(errno ?: ENOENT),
159 "Fail to read file\n");
163 return RESOURCED_ERROR_NONE;
166 int fread_nth_ulong(const char *path, size_t n, unsigned long *number)
168 _cleanup_fclose_ FILE *f = NULL;
173 f = fopen(path, "r");
174 ret_value_errno_msg_if(!f, -errno,
175 "Fail to open %s file.", path);
178 for (i = 0; i <= n; i++) {
179 ret = fscanf(f, "%lu", &t);
180 ret_value_errno_msg_if(ret == EOF, -(errno ?: ENOENT),
181 "Fail to read file\n");
185 return RESOURCED_ERROR_NONE;
188 int fread_nth_ulonglong(const char *path, size_t n, unsigned long long *number)
190 _cleanup_fclose_ FILE *f = NULL;
192 unsigned long long t;
195 f = fopen(path, "r");
196 ret_value_errno_msg_if(!f, -errno,
197 "Fail to open %s file.", path);
200 for (i = 0; i <= n; i++) {
201 ret = fscanf(f, "%llu", &t);
202 ret_value_errno_msg_if(ret == EOF, -(errno ?: ENOENT),
203 "Fail to read file\n");
207 return RESOURCED_ERROR_NONE;