tmin.c - numtools - perform numerical operations on vectors and matrices in unix pipes
 (HTM) git clone git://src.adamsgaard.dk/numtools
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       tmin.c (1340B)
       ---
            1 #include <err.h>
            2 #include <stdio.h>
            3 #include <stdlib.h>
            4 #include <unistd.h>
            5 #include <limits.h>
            6 
            7 #include "util.h"
            8 
            9 static void
           10 usage(void)
           11 {
           12         errx(1, "usage: max [-d delimstr] [-n] [-p prec]");
           13 }
           14 
           15 int
           16 main(int argc, char *argv[])
           17 {
           18         int ch, prec = 17, finalnl = 1;
           19         size_t i = 0, nf = 0, nr = 0, linesize = 0;
           20         char *line = NULL, *data = NULL, *delimstr = "\t";
           21         const char *errstr;
           22         double val, *vals = NULL;
           23 
           24         if (pledge("stdio", NULL) == -1)
           25                 err(2, "pledge");
           26 
           27         while ((ch = getopt(argc, argv, "d:np:")) != -1) {
           28                 switch (ch) {
           29                 case 'd':
           30                         delimstr = optarg;
           31                         break;
           32                 case 'n':
           33                         finalnl = 0;
           34                         break;
           35                 case 'p':
           36                         prec = strtonum(optarg, -10, INT_MAX, &errstr);
           37                         if (errstr != NULL)
           38                                 errx(1, "bad precision value, %s: %s", errstr, optarg);
           39                         break;
           40                 default:
           41                         usage();
           42                 }
           43         }
           44         argc -= optind;
           45         /*argv += optind;*/
           46         if (argc > 0)
           47                 usage();
           48 
           49         while (getline(&line, &linesize, stdin) > 0) {
           50                 if (nr == 0)
           51                         if ((nf = allocarr(&vals, line, linesize)) == 0)
           52                                 errx(1, "no fields in input");
           53                 data = line;
           54                 for (i = 0; i < nf; i++) {
           55                         if (!scannextval(&data, &val))
           56                                 errx(1, "could not parse line %ld, field %ld", nr + 1, i + 1);
           57                         if (nr == 0 || val < vals[i])
           58                                 vals[i] = val;
           59                 }
           60                 nr++;
           61         }
           62         printfarr(delimstr, prec, vals, nf);
           63         if (finalnl)
           64                 putchar('\n');
           65 
           66         free(line);
           67         free(vals);
           68 
           69         return 0;
           70 }