-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cf25e73
commit 7b94927
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |