[vlc-commits] linsys_sdi: simplify and fix sysfs usage

Rémi Denis-Courmont git at videolan.org
Sun Aug 23 09:16:07 CEST 2015


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun Aug 23 10:05:57 2015 +0300| [4e84bd2cef4e41cd074cd5a6e99fa18923acc545] | committer: Rémi Denis-Courmont

linsys_sdi: simplify and fix sysfs usage

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=4e84bd2cef4e41cd074cd5a6e99fa18923acc545
---

 modules/access/linsys/linsys_sdi.c |   49 ++++++++++++++----------------------
 1 file changed, 19 insertions(+), 30 deletions(-)

diff --git a/modules/access/linsys/linsys_sdi.c b/modules/access/linsys/linsys_sdi.c
index d2f9184..c479ad9 100644
--- a/modules/access/linsys/linsys_sdi.c
+++ b/modules/access/linsys/linsys_sdi.c
@@ -33,6 +33,7 @@
 #include <sys/types.h>
 #include <fcntl.h>
 #include <errno.h>
+#include <limits.h>
 
 #include <vlc_common.h>
 #include <vlc_plugin.h>
@@ -1660,47 +1661,35 @@ static int HandleSDBuffer( demux_t *p_demux, uint8_t *p_buffer,
 
 static int ReadULSysfs( const char *psz_fmt, unsigned int i_link )
 {
-    char psz_file[MAXLEN], psz_data[MAXLEN];
-    char *psz_tmp;
-    int i_fd;
-    ssize_t i_ret;
+    char psz_file[MAXLEN];
     unsigned int i_data;
 
-    snprintf( psz_file, sizeof(psz_file) - 1, psz_fmt, i_link );
+    snprintf( psz_file, sizeof(psz_file), psz_fmt, i_link );
 
-    if ( (i_fd = vlc_open( psz_file, O_RDONLY )) < 0 )
-        return i_fd;
-
-    i_ret = read( i_fd, psz_data, sizeof(psz_data) );
-    close( i_fd );
-
-    if ( i_ret < 0 )
-        return i_ret;
-
-    i_data = strtoul( psz_data, &psz_tmp, 0 );
-    if ( *psz_tmp != '\n' )
+    FILE *stream = vlc_fopen( psz_file, "rt" );
+    if( stream == NULL )
         return -1;
 
-    return i_data;
+    int ret = fscanf( stream, "%u", &i_data );
+    fclose( stream );
+
+    return (ret == 1 && i_data <= INT_MAX) ? (int)i_data : -1;
 }
 
-static ssize_t WriteULSysfs( const char *psz_fmt, unsigned int i_link,
-                             unsigned int i_buf )
+static int WriteULSysfs( const char *psz_fmt, unsigned int i_link,
+                         unsigned int i_buf )
 {
-    char psz_file[MAXLEN], psz_data[MAXLEN];
-    int i_fd;
-    ssize_t i_ret;
-
-    snprintf( psz_file, sizeof(psz_file) -1, psz_fmt, i_link );
+    char psz_file[MAXLEN];
 
-    snprintf( psz_data, sizeof(psz_data) -1, "%u\n", i_buf );
+    snprintf( psz_file, sizeof(psz_file), psz_fmt, i_link );
 
-    if ( (i_fd = vlc_open( psz_file, O_WRONLY )) < 0 )
-        return i_fd;
+    FILE *stream = vlc_fopen( psz_file, "wt" );
+    if( stream == NULL )
+        return -1;
 
-    i_ret = write( i_fd, psz_data, strlen(psz_data) + 1 );
-    close( i_fd );
-    return i_ret;
+    int ret = fprintf( stream, "%u\n", i_buf );
+    fclose( stream );
+    return ret;
 }
 
 static int InitCapture( demux_t *p_demux )



More information about the vlc-commits mailing list