Home | History | Annotate | Line # | Download | only in envstat
envstat.c revision 1.22.2.1
      1  1.22.2.1       riz /*	$NetBSD: envstat.c,v 1.22.2.1 2007/10/15 05:09:58 riz Exp $ */
      2       1.1      groo 
      3       1.1      groo /*-
      4       1.1      groo  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5       1.1      groo  * All rights reserved.
      6       1.1      groo  *
      7       1.1      groo  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1      groo  * by Bill Squier.
      9       1.1      groo  *
     10       1.1      groo  * Redistribution and use in source and binary forms, with or without
     11       1.1      groo  * modification, are permitted provided that the following conditions
     12       1.1      groo  * are met:
     13       1.1      groo  * 1. Redistributions of source code must retain the above copyright
     14       1.1      groo  *    notice, this list of conditions and the following disclaimer.
     15       1.1      groo  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1      groo  *    notice, this list of conditions and the following disclaimer in the
     17       1.1      groo  *    documentation and/or other materials provided with the distribution.
     18       1.1      groo  * 3. All advertising materials mentioning features or use of this software
     19       1.1      groo  *    must display the following acknowledgement:
     20       1.1      groo  *        This product includes software developed by the NetBSD
     21       1.1      groo  *        Foundation, Inc. and its contributors.
     22       1.1      groo  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.1      groo  *    contributors may be used to endorse or promote products derived
     24       1.1      groo  *    from this software without specific prior written permission.
     25       1.1      groo  *
     26       1.1      groo  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.1      groo  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.1      groo  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.1      groo  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.1      groo  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.1      groo  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.1      groo  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.1      groo  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.1      groo  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.1      groo  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.1      groo  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1      groo  */
     38       1.1      groo 
     39       1.1      groo #include <sys/cdefs.h>
     40       1.1      groo #ifndef lint
     41  1.22.2.1       riz __RCSID("$NetBSD: envstat.c,v 1.22.2.1 2007/10/15 05:09:58 riz Exp $");
     42       1.1      groo #endif
     43       1.1      groo 
     44       1.1      groo #include <fcntl.h>
     45       1.1      groo #include <stdio.h>
     46       1.1      groo #include <stdlib.h>
     47       1.1      groo #include <string.h>
     48       1.1      groo #include <unistd.h>
     49       1.3   thorpej #include <err.h>
     50       1.8  augustss #include <paths.h>
     51       1.1      groo 
     52      1.12  christos #define ENVSYSUNITNAMES
     53       1.1      groo #include <sys/envsys.h>
     54       1.1      groo #include <sys/ioctl.h>
     55       1.1      groo 
     56       1.7  augustss int main(int, char **);
     57      1.12  christos void listsensors(envsys_basic_info_t *, size_t);
     58      1.12  christos size_t numsensors(int);
     59      1.12  christos void fillsensors(int, envsys_tre_data_t *, envsys_basic_info_t *, size_t);
     60      1.12  christos size_t longestname(envsys_basic_info_t *, size_t);
     61      1.12  christos int marksensors(envsys_basic_info_t *, int *, char *, size_t);
     62      1.12  christos int strtosnum(envsys_basic_info_t *, const char *, size_t);
     63      1.12  christos void header(size_t, int, envsys_basic_info_t *, const int * const, size_t);
     64      1.12  christos void values(size_t, int, envsys_tre_data_t *, const int * const, size_t);
     65       1.7  augustss void usage(void);
     66      1.19       mrg void printrow(int *, envsys_tre_data_t *, envsys_basic_info_t *, int, int,
     67      1.19       mrg 	      size_t);
     68       1.1      groo 
     69       1.6  explorer int rflag = 0;
     70       1.6  explorer 
     71       1.1      groo int
     72       1.7  augustss main(int argc, char **argv)
     73       1.1      groo {
     74      1.12  christos 	int c, fd, ls, celsius;
     75      1.12  christos 	size_t ns;
     76      1.12  christos 	size_t interval, width, headrep, headcnt;
     77       1.1      groo 	envsys_tre_data_t *etds;
     78       1.1      groo 	envsys_basic_info_t *ebis;
     79       1.1      groo 	int *cetds;
     80       1.1      groo 	char *sensors;
     81       1.3   thorpej 	const char *dev;
     82       1.1      groo 
     83       1.1      groo 	fd = -1;
     84       1.1      groo 	ls = 0;
     85       1.1      groo 	celsius = 1;
     86       1.1      groo 	interval = 0;
     87       1.1      groo 	width = 0;
     88       1.1      groo 	sensors = NULL;
     89       1.1      groo 	headrep = 22;
     90       1.1      groo 
     91      1.22       wiz 	while ((c = getopt(argc, argv, "fi:ln:rs:w:")) != -1) {
     92       1.1      groo 		switch(c) {
     93       1.6  explorer 		case 'r':
     94      1.18       mrg 			rflag = 1;
     95       1.6  explorer 			break;
     96       1.1      groo 		case 'i':	/* wait time between displays */
     97       1.1      groo 			interval = atoi(optarg);
     98       1.1      groo 			break;
     99       1.1      groo 		case 'w':	/* minimum column width */
    100       1.1      groo 			width = atoi(optarg);
    101       1.1      groo 			break;
    102       1.1      groo 		case 'l':	/* list sensor names */
    103       1.1      groo 			ls = 1;
    104       1.1      groo 			break;
    105       1.1      groo 		case 'n':	/* repeat header every headrep lines */
    106       1.1      groo 			headrep = atoi(optarg);
    107       1.1      groo 			break;
    108       1.1      groo 		case 's':	/* restrict display to named sensors */
    109       1.1      groo 			sensors = (char *)malloc(strlen(optarg) + 1);
    110       1.1      groo 			if (sensors == NULL)
    111       1.1      groo 				exit(1);
    112       1.1      groo 			strcpy(sensors, optarg);
    113       1.1      groo 			break;
    114       1.1      groo 		case 'f':	/* display temp in degF */
    115       1.1      groo 			celsius = 0;
    116       1.1      groo 			break;
    117       1.1      groo 		case '?':
    118       1.1      groo 		default:
    119       1.1      groo 			usage();
    120       1.1      groo 			/* NOTREACHED */
    121       1.1      groo 		}
    122       1.1      groo 	}
    123       1.1      groo 
    124       1.3   thorpej 	if (optind < argc)
    125       1.3   thorpej 		dev = argv[optind];
    126       1.3   thorpej 	else
    127       1.3   thorpej 		dev = _PATH_SYSMON;
    128       1.1      groo 
    129       1.3   thorpej 	if ((fd = open(dev, O_RDONLY)) == -1)
    130       1.3   thorpej 		err(1, "unable to open %s", dev);
    131       1.1      groo 
    132       1.1      groo 	/*
    133       1.1      groo 	 * Determine number of sensors, allocate and fill arrays with
    134       1.1      groo 	 * initial information.  Determine column width
    135       1.1      groo 	 */
    136      1.12  christos 	if ((ns = numsensors(fd)) == 0)
    137      1.12  christos 		errx(1, "No sensors found");
    138       1.3   thorpej 
    139       1.1      groo 	cetds = (int *)malloc(ns * sizeof(int));
    140       1.1      groo 	etds = (envsys_tre_data_t *)malloc(ns * sizeof(envsys_tre_data_t));
    141       1.1      groo 	ebis = (envsys_basic_info_t *)malloc(ns * sizeof(envsys_basic_info_t));
    142       1.1      groo 
    143       1.3   thorpej 	if ((cetds == NULL) || (etds == NULL) || (ebis == NULL))
    144      1.12  christos 		err(1, "Out of memory");
    145       1.1      groo 
    146      1.12  christos 	fillsensors(fd, etds, ebis, ns);
    147       1.1      groo 
    148       1.1      groo 	if (ls) {
    149       1.1      groo 		listsensors(ebis, ns);
    150       1.1      groo 		exit(0);
    151       1.1      groo 	}
    152       1.1      groo 
    153       1.1      groo 
    154       1.1      groo #define MAX(x, y)  (((x) > (y)) ? (x) : (y))
    155       1.1      groo 	if (!width) {
    156       1.1      groo 		width = longestname(ebis, ns);
    157       1.1      groo 		width = MAX(((79 - ns) / ns), width);
    158       1.1      groo 	}
    159       1.1      groo 
    160       1.1      groo 	/* Mark which sensor numbers are to be displayed */
    161       1.1      groo 	if (marksensors(ebis, cetds, sensors, ns) == -1)
    162       1.1      groo 		exit(1);
    163       1.1      groo 
    164       1.6  explorer 	if (rflag) {
    165      1.19       mrg 		if (interval == 0) {
    166      1.19       mrg 			printrow(cetds, etds, ebis, ns, celsius, width);
    167      1.19       mrg 			exit(0);
    168      1.19       mrg 		}
    169       1.6  explorer 
    170      1.19       mrg 		while (1) {
    171      1.19       mrg 			printrow(cetds, etds, ebis, ns, celsius, width);
    172      1.19       mrg 			sleep(interval);
    173      1.19       mrg 			fillsensors(fd, etds, ebis, ns);
    174       1.6  explorer 			printf("\n");
    175       1.6  explorer 		}
    176       1.6  explorer 	}
    177       1.6  explorer 
    178       1.6  explorer 
    179       1.6  explorer 
    180       1.1      groo 	/* If we didn't specify an interval value, print the sensors once */
    181       1.1      groo 	if (!interval) {
    182       1.1      groo 		if (headrep)
    183       1.1      groo 			header(width, celsius, ebis, cetds, ns);
    184       1.1      groo 		values(width, celsius, etds, cetds, ns);
    185       1.1      groo 
    186       1.1      groo 		exit (0);
    187       1.1      groo 	}
    188       1.1      groo 
    189       1.1      groo 	headcnt = 0;
    190       1.1      groo 	if (headrep)
    191       1.1      groo 		header(width, celsius, ebis, cetds, ns);
    192       1.1      groo 
    193       1.1      groo         for (;;) {
    194       1.1      groo 		values(width, celsius, etds, cetds, ns);
    195       1.1      groo 		if (headrep && (++headcnt == headrep)) {
    196       1.1      groo 			headcnt = 0;
    197       1.1      groo 			header(width, celsius, ebis, cetds, ns);
    198       1.1      groo 		}
    199       1.1      groo 
    200       1.1      groo 		sleep(interval);
    201       1.1      groo 
    202      1.12  christos 		fillsensors(fd, etds, ebis, ns);
    203       1.1      groo 	}
    204       1.1      groo 
    205       1.1      groo 	/* NOTREACHED */
    206       1.1      groo 	return (0);
    207       1.1      groo }
    208       1.1      groo 
    209      1.19       mrg /*
    210      1.19       mrg  * row wise processing
    211      1.19       mrg  */
    212      1.19       mrg void
    213      1.19       mrg printrow(int *cetds, envsys_tre_data_t *etds, envsys_basic_info_t *ebis,
    214      1.19       mrg 	 int ns, int celsius, size_t width)
    215      1.19       mrg {
    216      1.19       mrg 	int i;
    217      1.19       mrg 
    218      1.19       mrg 	for (i = 0 ; i < ns ; i++) {
    219      1.19       mrg 		if (cetds[i] == 0)
    220      1.19       mrg 			continue;
    221      1.19       mrg 
    222      1.19       mrg 		if ((etds[i].validflags & ENVSYS_FCURVALID) == 0)
    223      1.19       mrg 			continue;
    224      1.19       mrg 
    225      1.19       mrg 		if (ebis[i].units == ENVSYS_INDICATOR &&
    226      1.19       mrg 		    etds[i].cur.data_s == 0)
    227      1.19       mrg 			continue;
    228      1.19       mrg 
    229      1.19       mrg 		printf("%*.*s", (int)width, (int)width, ebis[i].desc);
    230      1.19       mrg 		/* different units need some magic */
    231      1.19       mrg 		switch (ebis[i].units)
    232      1.19       mrg 		{
    233  1.22.2.1       riz 		case ENVSYS_DRIVE:
    234  1.22.2.1       riz 			printf(": drive %s",
    235  1.22.2.1       riz 			    envsysdrivestatus[etds[i].cur.data_us]);
    236      1.19       mrg 		case ENVSYS_INDICATOR:
    237      1.19       mrg 			break;
    238      1.19       mrg 		case ENVSYS_INTEGER:
    239      1.19       mrg 			printf(": %10d", etds[i].cur.data_s);
    240      1.19       mrg 			break;
    241      1.19       mrg 		case ENVSYS_STEMP: {
    242      1.19       mrg 			double temp = (etds[i].cur.data_s / 1000000.0)
    243      1.19       mrg 			    - 273.15;
    244      1.19       mrg 			if (celsius)
    245      1.19       mrg 				printf(": %10.3f degC", temp);
    246      1.19       mrg 			else {
    247      1.19       mrg 				temp = (9.0 / 5.0) * temp + 32.0;
    248      1.19       mrg 				printf(": %10.3f degF", temp);
    249      1.19       mrg 			}
    250      1.19       mrg 			break;
    251      1.19       mrg 		}
    252      1.19       mrg 		case ENVSYS_SFANRPM:
    253      1.19       mrg 			printf(": %10u RPM", etds[i].cur.data_us);
    254      1.19       mrg 			break;
    255      1.19       mrg 		default:
    256      1.19       mrg 			printf(": %10.3f %s",
    257      1.19       mrg 			    etds[i].cur.data_s / 1000000.0,
    258      1.19       mrg 			    envsysunitnames[ebis[i].units]);
    259      1.19       mrg 			break;
    260      1.19       mrg 		}
    261      1.19       mrg 
    262      1.19       mrg 		if (etds[i].validflags & ENVSYS_FFRACVALID) {
    263      1.19       mrg 			printf(" (%5.2f%%)",
    264      1.19       mrg 			    (etds[i].cur.data_s * 100.0) /
    265      1.19       mrg 			    etds[i].max.data_s);
    266      1.19       mrg 		}
    267      1.19       mrg 
    268      1.19       mrg 		printf("\n");
    269      1.19       mrg 	}
    270      1.19       mrg 
    271      1.19       mrg }
    272       1.1      groo 
    273       1.1      groo /*
    274       1.1      groo  * pre:  cetds[i] != 0 iff sensor i should appear in the output
    275       1.1      groo  * post: a column header line is displayed on stdout
    276       1.1      groo  */
    277       1.1      groo void
    278      1.12  christos header(size_t width, int celsius, envsys_basic_info_t *ebis,
    279      1.12  christos        const int * const cetds, size_t ns)
    280       1.1      groo {
    281       1.1      groo 	int i;
    282       1.1      groo 	const char *s;
    283       1.1      groo 
    284       1.1      groo 	/* sensor names */
    285       1.1      groo 	for (i = 0; i < ns; ++i)
    286       1.1      groo 		if (cetds[i])
    287       1.1      groo 			printf(" %*.*s", (int)width, (int)width, ebis[i].desc);
    288       1.1      groo 
    289       1.1      groo 	printf("\n");
    290       1.1      groo 
    291       1.1      groo 	/* units */
    292       1.1      groo 	for (i = 0; i < ns; ++i)
    293       1.1      groo 		if (cetds[i]) {
    294       1.1      groo 			if ((ebis[i].units == ENVSYS_STEMP) &&
    295       1.1      groo 			    !celsius)
    296       1.1      groo 				s = "degF";
    297       1.6  explorer 			else if (ebis[i].units >= ENVSYS_NSENSORS)
    298      1.12  christos 				s = envsysunitnames[ENVSYS_NSENSORS];
    299       1.1      groo 			else
    300      1.12  christos 				s = envsysunitnames[ebis[i].units];
    301       1.6  explorer 
    302       1.1      groo 			printf(" %*.*s", (int)width, (int)width, s);
    303       1.1      groo 		}
    304       1.1      groo 	printf("\n");
    305       1.1      groo }
    306       1.1      groo 
    307       1.1      groo void
    308      1.12  christos values(size_t width, int celsius, envsys_tre_data_t *etds,
    309      1.12  christos        const int * const cetds, size_t ns)
    310       1.1      groo {
    311       1.1      groo 	int i;
    312       1.1      groo 	double temp;
    313       1.6  explorer 
    314       1.1      groo 	for (i = 0; i < ns; ++i)
    315       1.1      groo 		if (cetds[i]) {
    316       1.1      groo 
    317       1.1      groo 			/* * sensors without valid data */
    318       1.1      groo 			if ((etds[i].validflags & ENVSYS_FCURVALID) == 0) {
    319       1.1      groo 				printf(" %*.*s", (int)width, (int)width, "*");
    320       1.1      groo 				continue;
    321       1.1      groo 			}
    322       1.1      groo 
    323       1.1      groo 			switch(etds[i].units) {
    324  1.22.2.1       riz 			case ENVSYS_DRIVE:
    325  1.22.2.1       riz 				printf(" %*.*s", (int)width, (int)width,
    326  1.22.2.1       riz 				    envsysdrivestatus[etds[i].cur.data_us]);
    327  1.22.2.1       riz 				break;
    328       1.6  explorer 			case ENVSYS_INDICATOR:
    329       1.6  explorer 				printf(" %*.*s", (int)width, (int)width,
    330       1.6  explorer 				    etds[i].cur.data_us ? "ON" : "OFF");
    331       1.6  explorer 				break;
    332       1.1      groo 			case ENVSYS_STEMP:
    333       1.1      groo 				temp = (etds[i].cur.data_us / 1000000.0) -
    334      1.17     soren 				    273.15;
    335       1.1      groo 				if (!celsius)
    336       1.1      groo 					temp = (9.0 / 5.0) * temp + 32.0;
    337      1.13    martin 				printf(" %*.2f", (int)width, temp);
    338       1.1      groo 				break;
    339       1.1      groo 			case ENVSYS_SFANRPM:
    340      1.13    martin 				printf(" %*u", (int)width, etds[i].cur.data_us);
    341       1.1      groo 				break;
    342       1.1      groo 			default:
    343      1.13    martin 				printf(" %*.2f", (int)width, etds[i].cur.data_s /
    344       1.1      groo 				       1000000.0);
    345       1.1      groo 				break;
    346       1.1      groo 			}
    347       1.1      groo 		}
    348       1.1      groo 	printf("\n");
    349       1.1      groo }
    350       1.1      groo 
    351       1.1      groo 
    352       1.1      groo /*
    353       1.1      groo  * post: displays usage on stderr
    354       1.1      groo  */
    355       1.1      groo void
    356       1.7  augustss usage(void)
    357       1.1      groo {
    358       1.5       cgd 
    359      1.20       wiz 	(void)fprintf(stderr, "usage: %s [-fr] [-s s1,s2,...]", getprogname());
    360      1.12  christos 	(void)fprintf(stderr, " [-i interval] [-n headrep] [-w width]");
    361      1.12  christos 	(void)fprintf(stderr, " [device]\n");
    362      1.12  christos 	(void)fprintf(stderr, "       %s -l [device]\n", getprogname());
    363       1.1      groo 	exit(1);
    364       1.1      groo }
    365       1.1      groo 
    366       1.1      groo 
    367       1.1      groo /*
    368       1.1      groo  * post: a list of sensor names supported by the device is displayed on stdout
    369       1.1      groo  */
    370       1.1      groo void
    371      1.12  christos listsensors(envsys_basic_info_t *ebis, size_t ns)
    372       1.1      groo {
    373       1.1      groo 	int i;
    374       1.1      groo 
    375       1.1      groo 	for (i = 0; i < ns; ++i)
    376       1.1      groo 		if (ebis[i].validflags & ENVSYS_FVALID)
    377       1.1      groo 			printf("%s\n", ebis[i].desc);
    378       1.1      groo }
    379       1.1      groo 
    380       1.6  explorer 
    381       1.1      groo /*
    382       1.1      groo  * pre:  fd contains a valid file descriptor of an envsys(4) supporting device
    383       1.1      groo  * post: returns the number of valid sensors provided by the device
    384       1.1      groo  *       or -1 on error
    385       1.1      groo  */
    386      1.12  christos size_t
    387       1.7  augustss numsensors(int fd)
    388       1.1      groo {
    389       1.1      groo 	int count = 0, valid = 1;
    390       1.1      groo 	envsys_tre_data_t etd;
    391       1.1      groo 	etd.sensor = 0;
    392       1.1      groo 
    393       1.1      groo 	while (valid) {
    394      1.12  christos 		if (ioctl(fd, ENVSYS_GTREDATA, &etd) == -1)
    395      1.12  christos 			err(1, "Can't get sensor data");
    396       1.1      groo 
    397       1.1      groo 		valid = etd.validflags & ENVSYS_FVALID;
    398       1.1      groo 		if (valid)
    399       1.1      groo 			++count;
    400       1.1      groo 
    401       1.1      groo 		++etd.sensor;
    402       1.1      groo 	}
    403       1.1      groo 
    404       1.1      groo 	return count;
    405       1.1      groo }
    406       1.1      groo 
    407       1.1      groo /*
    408       1.1      groo  * pre:  fd contains a valid file descriptor of an envsys(4) supporting device
    409       1.1      groo  *       && ns is the number of sensors
    410       1.1      groo  *       && etds and ebis are arrays of sufficient size
    411       1.1      groo  * post: returns 0 and etds and ebis arrays are filled with sensor info
    412       1.1      groo  *       or returns -1 on failure
    413       1.1      groo  */
    414      1.12  christos void
    415      1.12  christos fillsensors(int fd, envsys_tre_data_t *etds, envsys_basic_info_t *ebis,
    416      1.12  christos     size_t ns)
    417       1.1      groo {
    418       1.1      groo 	int i;
    419       1.1      groo 
    420       1.1      groo 	for (i = 0; i < ns; ++i) {
    421       1.1      groo 		ebis[i].sensor = i;
    422       1.1      groo 		if (ioctl(fd, ENVSYS_GTREINFO, &ebis[i]) == -1)
    423      1.12  christos 			err(1, "Can't get sensor info for sensor %d", i);
    424       1.1      groo 
    425       1.1      groo 		etds[i].sensor = i;
    426       1.1      groo 		if (ioctl(fd, ENVSYS_GTREDATA, &etds[i]) == -1)
    427      1.12  christos 			err(1, "Can't get sensor data for sensor %d", i);
    428       1.1      groo 	}
    429       1.1      groo }
    430       1.1      groo 
    431       1.1      groo 
    432       1.1      groo /*
    433       1.1      groo  * post: returns the strlen() of the longest sensor name
    434       1.1      groo  */
    435      1.12  christos size_t
    436      1.12  christos longestname(envsys_basic_info_t *ebis, size_t ns)
    437       1.1      groo {
    438      1.12  christos 	size_t i, maxlen, cur;
    439       1.1      groo 
    440       1.1      groo 	maxlen = 0;
    441       1.1      groo 
    442       1.1      groo 	for (i = 0; i < ns; ++i) {
    443       1.1      groo 		cur = strlen(ebis[i].desc);
    444       1.1      groo 		if (cur > maxlen)
    445       1.1      groo 			maxlen = cur;
    446       1.1      groo 	}
    447       1.1      groo 
    448       1.1      groo 	return maxlen;
    449       1.1      groo }
    450       1.1      groo 
    451       1.1      groo /*
    452       1.1      groo  * post: returns 0 and cetds[i] != 0 iff sensor i should appear in the output
    453       1.1      groo  *       or returns -1
    454       1.1      groo  */
    455       1.1      groo int
    456      1.12  christos marksensors(envsys_basic_info_t *ebis, int *cetds, char *sensors, size_t ns)
    457       1.1      groo {
    458      1.12  christos 	size_t i;
    459       1.1      groo 	char *s;
    460       1.1      groo 
    461       1.1      groo 	if (sensors == NULL) {
    462       1.1      groo 		/* No sensors specified, include them all */
    463       1.1      groo 		for (i = 0; i < ns; ++i)
    464       1.1      groo 			cetds[i] = 1;
    465       1.1      groo 
    466       1.1      groo 		return 0;
    467       1.1      groo 	}
    468       1.1      groo 
    469       1.1      groo 	/* Assume no sensors in display */
    470       1.1      groo 	memset(cetds, 0, ns * sizeof(int));
    471       1.1      groo 
    472       1.1      groo 	s = strtok(sensors, ",");
    473       1.1      groo 	while (s != NULL) {
    474      1.12  christos 		int snum;
    475      1.12  christos 		if ((snum = strtosnum(ebis, s, ns)) != -1)
    476      1.12  christos 			cetds[snum] = 1;
    477       1.1      groo 		else {
    478      1.12  christos 			warnx("Unknown sensor %s", s);
    479       1.1      groo 			return (-1);
    480       1.1      groo 		}
    481       1.1      groo 
    482       1.1      groo 		s = strtok(NULL, ",");
    483       1.1      groo 	}
    484       1.1      groo 
    485       1.1      groo 	/* Check if we have at least one sensor selected for output */
    486       1.1      groo 	for (i = 0; i < ns; ++i)
    487       1.1      groo 		if (cetds[i] == 1)
    488       1.1      groo 			return (0);
    489       1.1      groo 
    490      1.12  christos 	warnx("No sensors selected for display");
    491       1.1      groo 	return (-1);
    492       1.1      groo }
    493       1.6  explorer 
    494       1.1      groo 
    495       1.1      groo /*
    496       1.1      groo  * returns -1 if s is not a valid sensor name for the device
    497       1.1      groo  *       or the sensor number of a sensor which has that name
    498       1.1      groo  */
    499       1.1      groo int
    500      1.12  christos strtosnum(envsys_basic_info_t *ebis, const char *s, size_t ns)
    501       1.1      groo {
    502      1.12  christos 	size_t i;
    503       1.1      groo 
    504       1.1      groo 	for (i = 0; i < ns; ++i) {
    505       1.1      groo 		if((ebis[i].validflags & ENVSYS_FVALID) &&
    506       1.1      groo 		   !strcmp(s, ebis[i].desc))
    507       1.1      groo 			return ebis[i].sensor;
    508       1.1      groo 	}
    509       1.1      groo 
    510       1.1      groo 	return -1;
    511       1.1      groo }
    512