[Title] modify sys_get_str
[platform/hal/backend/emulator/device-emulator.git] / src / device_manager_io.c
1 /*
2  *  device-manager-plugin-maru
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: DongGi Jang <dg0402.jang@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20 */
21
22 /*
23  * 2012-03-05 Dohyung Hong <don.hong@samsung.com> Changed package name for the maru board of emulator
24  */
25
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <fcntl.h>
30 #include <devman_plugin_intf.h>
31 #include <errno.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34
35 #define EXPORT_API  __attribute__((visibility("default")))
36
37 #define BUFF_MAX        255
38 #define SUCCESS         0
39 #define FAIL            1
40
41 int sys_get_node(char *path, char *node)
42 {
43         if (0 >= snprintf(node, strlen(path) + 1, "%s", path))
44                 return -1;
45
46         return 0;
47 }
48
49 int sys_get_int_wo_convert(char *path, int *value)
50 {
51         int fd = -1;
52
53         fd = open(path, O_RDONLY);
54         if (fd == -1) {
55                 return -1;
56         }
57
58         if (0 > read(fd, value, sizeof(int))) {
59                 close(fd);
60                 return -1;
61         }
62         close (fd);
63
64         return 0;
65 }
66
67 int sys_set_int_wo_convert(char *path, int value)
68 {
69         int fd = -1;
70
71         fd = open(path, O_WRONLY);
72         if (fd == -1) {
73                 return -1;
74         }
75
76         if (0 > write(fd, &value, sizeof(int))) {
77                 close(fd);
78                 return -1;
79         }
80         close(fd);
81
82         return 0;
83 }
84
85 static int sys_read_buf(char *file, char *buf)
86 {
87         int fd;
88         int r;
89
90         fd = open(file, O_RDONLY);
91         if (fd == -1) {
92                 return -ENOENT;
93         }
94
95         r = read(fd, buf, BUFF_MAX);
96         if ((r >= 0) && (r < BUFF_MAX))
97                 buf[r] = '\0';
98         else {
99                 return -EIO;
100         }
101
102         close(fd);
103
104         return 0;
105 }
106
107 static int sys_write_buf(char *file, char *buf)
108 {
109         int fd;
110         int r;
111
112         fd = open(file, O_WRONLY);
113         if (fd == -1) {
114                 return -1;
115         }
116
117         r = write(fd, buf, strlen(buf));
118         close(fd);
119         if (r < 0) {
120                 return -1;
121         }
122
123         return 0;
124 }
125
126 int sys_get_int(char *fname, int *val)
127 {
128         char buf[BUFF_MAX];
129
130         if (sys_read_buf(fname, buf) == 0) {
131                 *val = atoi(buf);
132                 return 0;
133         } else {
134                 *val = -1;
135                 return -1;
136         }
137 }
138
139 int sys_get_str(char *fname, char* str)
140 {
141         char buf[BUFF_MAX] = {0};
142
143         if (sys_read_buf(fname, buf) == 0) {
144                 strncpy(str, buf, strlen(buf));
145                 return 0;
146         }
147
148         return -1;
149 }
150
151 int sys_set_int(char *fname, int val)
152 {
153         char buf[BUFF_MAX];
154         int r = -1;
155         snprintf(buf, sizeof(buf), "%d", val);
156
157         if (sys_write_buf(fname, buf) == 0)
158                 r = 0;
159
160         return r;
161 }
162
163 int sys_set_str(char *fname, char *val)
164 {
165         int r = -1;
166
167         if (val != NULL) {
168                 if (sys_write_buf(fname, val) == 0)
169                         r = 0;
170         }
171
172         return r;
173 }