[hackers] [ubase] Replace fgets() with agetline() || sin

From: <git_AT_suckless.org>
Date: Sat, 14 Jun 2014 14:15:38 +0200

commit 816199471f291e52ca42b1a6aa04deb0043a57a3
Author: sin <sin_AT_2f30.org>
Date: Sat Jun 14 13:13:10 2014 +0100

    Replace fgets() with agetline()

diff --git a/lsmod.c b/lsmod.c
index 8f625b0..7d72d4b 100644
--- a/lsmod.c
+++ b/lsmod.c
_AT_@ -2,6 +2,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include "text.h"
 #include "util.h"
 
 static void parse_modline(char *buf, char **name, char **size,
_AT_@ -18,8 +19,9 @@ main(int argc, char *argv[])
 {
         const char *modfile = "/proc/modules";
         FILE *fp;
- char buf[BUFSIZ];
+ char *buf = NULL;
         char *name, *size, *refcount, *users;
+ size_t bufsize = 0;
         size_t len;
 
         ARGBEGIN {
_AT_@ -35,7 +37,7 @@ main(int argc, char *argv[])
         fp = fopen(modfile, "r");
         if (!fp)
                 eprintf("fopen %s:", modfile);
- while (fgets(buf, sizeof buf, fp)) {
+ while (agetline(&buf, &bufsize, fp) != -1) {
                 parse_modline(buf, &name, &size, &refcount, &users);
                 if (!name || !size || !refcount || !users)
                         eprintf("invalid format: %s
", modfile);
_AT_@ -47,6 +49,7 @@ main(int argc, char *argv[])
         }
         if (ferror(fp))
                 eprintf("%s: read error:", modfile);
+ free(buf);
         fclose(fp);
         return EXIT_SUCCESS;
 }
diff --git a/lsusb.c b/lsusb.c
index 058124a..2bcc4af 100644
--- a/lsusb.c
+++ b/lsusb.c
_AT_@ -2,6 +2,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <limits.h>
+#include "text.h"
 #include "util.h"
 
 static void lsusb(const char *file);
_AT_@ -30,7 +31,8 @@ lsusb(const char *file)
         FILE *fp;
         char *cwd;
         char path[PATH_MAX];
- char buf[BUFSIZ];
+ char *buf = NULL;
+ size_t size = 0;
         unsigned int i = 0, busnum = 0, devnum = 0, pid = 0, vid = 0;
 
         cwd = agetcwd();
_AT_@ -38,7 +40,7 @@ lsusb(const char *file)
         free(cwd);
         if (!(fp = fopen(path, "r")))
                 return;
- while (fgets(buf, sizeof(buf), fp)) {
+ while (agetline(&buf, &size, fp) != -1) {
                 if (sscanf(buf, "BUSNUM=%u
", &busnum) ||
                     sscanf(buf, "DEVNUM=%u
", &devnum) ||
                     sscanf(buf, "PRODUCT=%x/%x/", &pid, &vid))
_AT_@ -51,6 +53,6 @@ lsusb(const char *file)
         }
         if (ferror(fp))
                 eprintf("%s: read error:", path);
+ free(buf);
         fclose(fp);
 }
-
diff --git a/sysctl.c b/sysctl.c
index f6bc141..9c91843 100644
--- a/sysctl.c
+++ b/sysctl.c
_AT_@ -5,6 +5,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "text.h"
 #include "util.h"
 
 static void
_AT_@ -160,8 +161,9 @@ int
 main(int argc, char *argv[])
 {
         FILE *fp;
- char buf[BUFSIZ], *p;
+ char *buf = NULL, *p;
         char *file = NULL;
+ size_t size = 0;
         int i;
         int r = EXIT_SUCCESS;
 
_AT_@ -184,7 +186,7 @@ main(int argc, char *argv[])
                 fp = fopen(file, "r");
                 if (!fp)
                         eprintf("fopen %s:", file);
- while (fgets(buf, sizeof(buf), fp)) {
+ while (agetline(&buf, &size, fp) != -1) {
                         p = buf;
                         for (p = buf; *p == ' ' || *p == ' '; p++)
                                 ;
_AT_@ -202,6 +204,7 @@ main(int argc, char *argv[])
                 }
                 if (ferror(fp))
                         eprintf("%s: read error:", file);
+ free(buf);
                 fclose(fp);
         }
 
Received on Sat Jun 14 2014 - 14:15:38 CEST

This archive was generated by hypermail 2.3.0 : Sat Jun 14 2014 - 14:24:07 CEST