Add unit(in variable) & fix bugs
[platform/core/system/resourced.git] / src / common / file-helper.c
1 /*
2  * resourced
3  *
4  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20
21 /*
22  * @file file-helper.c
23  * @desc Helper functions for working with files
24  */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <limits.h>
31
32 #include "file-helper.h"
33 #include "trace.h"
34 #include "macro.h"
35 #include "util.h"
36
37 int fwrite_str(const char *path, const char *str)
38 {
39         _cleanup_fclose_ FILE *f = NULL;
40         int ret;
41
42         assert(path);
43         assert(str);
44
45         f = fopen(path, "w");
46         ret_value_errno_msg_if(!f, -errno,
47                                "Fail to open file %s", path);
48
49         errno = 0;
50         ret = fputs(str, f);
51         ret_value_errno_msg_if(ret == EOF, -(errno ?: EIO),
52                                "Fail to write file");
53
54         return RESOURCED_ERROR_NONE;
55 }
56
57 int fwrite_int(const char *path, const int number)
58 {
59         _cleanup_free_ char *digit_buf = NULL;
60         int ret;
61
62         ret = asprintf(&digit_buf, "%d", number);
63         ret_value_errno_msg_if(ret < 0, -ENOMEM,
64                                "sprintf failed\n");
65
66         return fwrite_str(path, digit_buf);
67 }
68
69 int fwrite_uint(const char *path, const u_int32_t number)
70 {
71         _cleanup_free_ char *digit_buf = NULL;
72         int ret;
73
74         ret = asprintf(&digit_buf, "%u", number);
75         ret_value_errno_msg_if(ret < 0, -ENOMEM,
76                                "sprintf failed\n");
77
78         return fwrite_str(path, digit_buf);
79 }
80
81 int fwrite_ulong(const char *path, const unsigned long number)
82 {
83         _cleanup_free_ char *digit_buf = NULL;
84         int ret;
85
86         ret = asprintf(&digit_buf, "%lu", number);
87         ret_value_errno_msg_if(ret < 0, -ENOMEM,
88                                "sprintf failed\n");
89
90         return fwrite_str(path, digit_buf);
91 }
92
93 int fwrite_ulonglong(const char *path, const unsigned long long number)
94 {
95         _cleanup_free_ char *digit_buf = NULL;
96         int ret;
97
98         ret = asprintf(&digit_buf, "%llu", number);
99         ret_value_errno_msg_if(ret < 0, -ENOMEM,
100                                "sprintf failed\n");
101
102         return fwrite_str(path, digit_buf);
103 }
104
105 int fread_str(const char *path, char **str)
106 {
107         _cleanup_fclose_ FILE *f = NULL;
108         char *ret;
109
110         f = fopen(path, "r");
111         ret_value_errno_msg_if(!f, -errno,
112                         "Fail to open %s file.", path);
113
114         ret = fgets(*str, LINE_MAX, f);
115         ret_value_errno_msg_if(!ret, -ENODATA,
116                         "Fail to read file\n");
117
118         *str = ret;
119         return RESOURCED_ERROR_NONE;
120 }
121
122 int fread_nth_int(const char *path, size_t n, int32_t *number)
123 {
124         _cleanup_fclose_ FILE *f = NULL;
125         size_t i;
126         int32_t t;
127         int ret;
128
129         f = fopen(path, "r");
130         ret_value_errno_msg_if(!f, -errno,
131                                "Fail to open  %s file.", path);
132
133         errno = 0;
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");
138         }
139
140         *number = t;
141         return RESOURCED_ERROR_NONE;
142 }
143
144 int fread_nth_uint(const char *path, size_t n, u_int32_t *number)
145 {
146         _cleanup_fclose_ FILE *f = NULL;
147         size_t i;
148         u_int32_t t;
149         int ret;
150
151         f = fopen(path, "r");
152         ret_value_errno_msg_if(!f, -errno,
153                                "Fail to open %s file.", path);
154
155         errno = 0;
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");
160         }
161
162         *number = t;
163         return RESOURCED_ERROR_NONE;
164 }
165
166 int fread_nth_ulong(const char *path, size_t n, unsigned long *number)
167 {
168         _cleanup_fclose_ FILE *f = NULL;
169         size_t i;
170         unsigned long t;
171         int ret;
172
173         f = fopen(path, "r");
174         ret_value_errno_msg_if(!f, -errno,
175                                "Fail to open %s file.", path);
176
177         errno = 0;
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");
182         }
183
184         *number = t;
185         return RESOURCED_ERROR_NONE;
186 }
187
188 int fread_nth_ulonglong(const char *path, size_t n, unsigned long long *number)
189 {
190         _cleanup_fclose_ FILE *f = NULL;
191         size_t i;
192         unsigned long long t;
193         int ret;
194
195         f = fopen(path, "r");
196         ret_value_errno_msg_if(!f, -errno,
197                                "Fail to open %s file.", path);
198
199         errno = 0;
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");
204         }
205
206         *number = t;
207         return RESOURCED_ERROR_NONE;
208 }