Skip to content

Commit

Permalink
include: Add sysfs_helpers.h
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiLux authored and prashantpaddune committed Apr 11, 2019
1 parent cf25e73 commit 7b94927
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions include/linux/sysfs_helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* Sysfs helper functions
*
* Author: Andrei F. <luxneb@gmail.com>
* Derived from function implementation from Gokhan Moral
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*/

static inline int read_into(int *container, int size, const char *buf, size_t count)
{
int i, j, t, n;
i=0; j=0; t=0; n=0;

for(j = 0; j < size; j++)
*(container + j) = 0;

for(j = 0; i < count; i++) {
char c = buf[i];
if(c >= '0' && c <= '9') {
if(t < (j + 1))
t = j + 1;
if(t > size)
return -EINVAL;
*(container + j) *= 10;
*(container + j) += (c - '0');
} else if(c == ' ' || c == '\t' || c == '\n' ) {
if(*(container + j) != 0) {
if(n) {
*(container + j) *= -1;
n = 0;
}
j++;
}
} else if(c == '-') {
n = 1;
} else
break;
}

if(n)
*(container + j) *= -1;

return t;
}

#define sanitize_min_max(val, min, max) \
if(val < min) \
val = min; \
if(val > max) \
val = max;

0 comments on commit 7b94927

Please sign in to comment.