Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.sbin/envstat - no point in allocating memory to hold com...
details: https://anonhg.NetBSD.org/src/rev/071daff19fd6
branches: trunk
changeset: 783293:071daff19fd6
user: christos <christos%NetBSD.org@localhost>
date: Thu Dec 13 20:06:42 2012 +0000
description:
- no point in allocating memory to hold command line arguments.
- allocate memory inside the function used.
diffstat:
usr.sbin/envstat/envstat.c | 54 ++++++++++++++++++---------------------------
1 files changed, 22 insertions(+), 32 deletions(-)
diffs (150 lines):
diff -r d31a5e9bbe2c -r 071daff19fd6 usr.sbin/envstat/envstat.c
--- a/usr.sbin/envstat/envstat.c Thu Dec 13 19:38:40 2012 +0000
+++ b/usr.sbin/envstat/envstat.c Thu Dec 13 20:06:42 2012 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: envstat.c,v 1.92 2012/12/13 19:31:25 christos Exp $ */
+/* $NetBSD: envstat.c,v 1.93 2012/12/13 20:06:42 christos Exp $ */
/*-
* Copyright (c) 2007, 2008 Juan Romero Pardines.
@@ -27,7 +27,7 @@
#include <sys/cdefs.h>
#ifndef lint
-__RCSID("$NetBSD: envstat.c,v 1.92 2012/12/13 19:31:25 christos Exp $");
+__RCSID("$NetBSD: envstat.c,v 1.93 2012/12/13 20:06:42 christos Exp $");
#endif /* not lint */
#include <stdio.h>
@@ -112,7 +112,7 @@
static int send_dictionary(FILE *);
static int find_sensors(prop_array_t, const char *, dvprops_t);
static void print_sensors(void);
-static int check_sensors(char *);
+static int check_sensors(const char *);
static int usage(void);
static int sysmonfd; /* fd of /dev/sysmon */
@@ -132,17 +132,13 @@
while ((c = getopt(argc, argv, "c:Dd:fIi:klrSs:Tw:Wx")) != -1) {
switch (c) {
case 'c': /* configuration file */
- configfile = strdup(optarg);
- if (configfile == NULL)
- err(EXIT_FAILURE, "strdup");
+ configfile = optarg;
break;
case 'D': /* list registered devices */
flags |= ENVSYS_DFLAG;
break;
case 'd': /* show sensors of a specific device */
- mydevname = strdup(optarg);
- if (mydevname == NULL)
- err(EXIT_FAILURE, "strdup");
+ mydevname = optarg;
break;
case 'f': /* display temperature in Farenheit */
flags |= ENVSYS_FFLAG;
@@ -171,9 +167,7 @@
flags |= ENVSYS_SFLAG;
break;
case 's': /* only show specified sensors */
- sensors = strdup(optarg);
- if (sensors == NULL)
- err(EXIT_FAILURE, "strdup");
+ sensors = optarg;
break;
case 'T': /* make statistics */
flags |= ENVSYS_TFLAG;
@@ -281,10 +275,6 @@
rval = parse_dictionary(sysmonfd);
}
- if (sensors)
- free(sensors);
- if (mydevname)
- free(mydevname);
(void)prog_close(sysmonfd);
return rval ? EXIT_FAILURE : EXIT_SUCCESS;
@@ -438,15 +428,8 @@
}
/* print sensors now */
- if (sensors) {
- char *str = strdup(sensors);
- if (!str) {
- rval = ENOMEM;
- goto out;
- }
- rval = check_sensors(str);
- free(str);
- }
+ if (sensors)
+ rval = check_sensors(sensors);
if ((flags & ENVSYS_LFLAG) == 0 && (flags & ENVSYS_DFLAG) == 0)
print_sensors();
if (interval)
@@ -659,30 +642,33 @@
}
static int
-check_sensors(char *str)
+check_sensors(const char *str)
{
sensor_t sensor = NULL;
- char *dvstring, *sstring, *p, *last;
+ char *dvstring, *sstring, *p, *last, *s;
bool sensor_found = false;
+ if ((s = strdup(str)) == NULL)
+ return errno;
+
/*
* Parse device name and sensor description and find out
* if the sensor is valid.
*/
- for ((p = strtok_r(str, ",", &last)); p;
+ for ((p = strtok_r(s, ",", &last)); p;
(p = strtok_r(NULL, ",", &last))) {
/* get device name */
dvstring = strtok(p, ":");
if (dvstring == NULL) {
warnx("missing device name");
- return EINVAL;
+ goto out;
}
/* get sensor description */
sstring = strtok(NULL, ":");
if (sstring == NULL) {
warnx("missing sensor description");
- return EINVAL;
+ goto out;
}
SIMPLEQ_FOREACH(sensor, &sensors_list, entries) {
@@ -698,17 +684,21 @@
if (sensor_found == false) {
warnx("unknown sensor `%s' for device `%s'",
sstring, dvstring);
- return EINVAL;
+ goto out;
}
sensor_found = false;
}
/* check if all sensors were ok, and error out if not */
SIMPLEQ_FOREACH(sensor, &sensors_list, entries)
- if (sensor->visible)
+ if (sensor->visible) {
+ free(s);
return 0;
+ }
warnx("no sensors selected to display");
+out:
+ free(s);
return EINVAL;
}
Home |
Main Index |
Thread Index |
Old Index