mirror of
https://github.com/team-infusion-developers/android_hardware_samsung.git
synced 2024-11-06 21:55:41 +00:00
lights_helper: I/O API cleanup
* buf => buffer for consistency * explicitly cast strtol retval to int * close(NULL) is a no-op, remove superfluous checking * replace variying buffer sizes by INT_MAX_STRLEN * always return signed errno codes Change-Id: I972e33e2435ec7d68a8ffdc5ab88a14d8005ff55
This commit is contained in:
parent
d483dd7898
commit
235c298767
1 changed files with 21 additions and 22 deletions
|
@ -33,40 +33,39 @@
|
||||||
* @param path The absolute path string.
|
* @param path The absolute path string.
|
||||||
* @return The Integer with decimal base, -1 or errno on error.
|
* @return The Integer with decimal base, -1 or errno on error.
|
||||||
*/
|
*/
|
||||||
int read_int(char const *path)
|
static int read_int(char const *path)
|
||||||
{
|
{
|
||||||
int fd, len;
|
int fd, len;
|
||||||
int num_bytes = 10;
|
int ret = 0;
|
||||||
char buf[11];
|
const int INT_MAX_STRLEN = 10;
|
||||||
int ret;
|
char buffer[INT_MAX_STRLEN+1];
|
||||||
|
|
||||||
fd = open(path, O_RDONLY);
|
fd = open(path, O_RDONLY);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
ret = errno;
|
ret = -errno;
|
||||||
ALOGE("%s: failed to open %s (%s)", __func__, path, strerror(errno));
|
ALOGE("%s: failed to open %s (%s)", __func__, path, strerror(errno));
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
len = read(fd, buf, num_bytes - 1);
|
len = read(fd, buffer, INT_MAX_STRLEN-1);
|
||||||
if (len < 0) {
|
if (len < 0) {
|
||||||
ret = errno;
|
ret = -errno;
|
||||||
ALOGE("%s: failed to read from %s (%s)", __func__, path, strerror(errno));
|
ALOGE("%s: failed to read from %s (%s)", __func__, path, strerror(errno));
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
buf[len] = '\0';
|
buffer[len] = '\0';
|
||||||
|
|
||||||
// decimal base
|
/* now parse the integer from buffer */
|
||||||
char *endptr = NULL;
|
char *endptr = NULL;
|
||||||
ret = strtol(buf, &endptr, 10);
|
ret = (int) strtol(buffer, &endptr, 10);
|
||||||
if (ret == 0 && endptr == NULL) {
|
if (ret == 0 && endptr == NULL) {
|
||||||
ret = -1;
|
ret = -1;
|
||||||
ALOGE("%s: failed to extract number from string %s", __func__, buf);
|
ALOGE("%s: failed to extract number from string %s", __func__, buffer);
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
if (fd >= 0)
|
|
||||||
close(fd);
|
close(fd);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -78,29 +77,29 @@ exit:
|
||||||
* @param value The Integer value to be written.
|
* @param value The Integer value to be written.
|
||||||
* @return 0 on success, errno on error.
|
* @return 0 on success, errno on error.
|
||||||
*/
|
*/
|
||||||
int write_int(char const *path, const int value)
|
static int write_int(char const *path, const int value)
|
||||||
{
|
{
|
||||||
int fd, len;
|
int fd, len, num_bytes;
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
const int INT_MAX_STRLEN = 10;
|
||||||
|
char buffer[INT_MAX_STRLEN+1];
|
||||||
|
|
||||||
fd = open(path, O_WRONLY);
|
fd = open(path, O_WRONLY);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
ret = errno;
|
ret = -errno;
|
||||||
ALOGE("%s: failed to open %s (%s)", __func__, path, strerror(errno));
|
ALOGE("%s: failed to open %s (%s)", __func__, path, strerror(errno));
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
char buffer[20];
|
num_bytes = sprintf(buffer, "%d", value);
|
||||||
int bytes = sprintf(buffer, "%d", value);
|
len = write(fd, buffer, num_bytes);
|
||||||
len = write(fd, buffer, bytes);
|
|
||||||
if (len < 0) {
|
if (len < 0) {
|
||||||
ret = errno;
|
ret = -errno;
|
||||||
ALOGE("%s: failed to write to %s (%s)", __func__, path, strerror(errno));
|
ALOGE("%s: failed to write to %s (%s)", __func__, path, strerror(errno));
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
if (fd >= 0)
|
|
||||||
close(fd);
|
close(fd);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue