Introduction
Introduction Statistics Contact Development Disclaimer Help
tsum.c - numtools - perform numerical operations on vectors and matrices in uni…
git clone git://src.adamsgaard.dk/numtools
Log
Files
Refs
README
LICENSE
---
tsum.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: sum [-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, 0, INT_MAX, &errstr);
37 if (errstr != NULL)
38 errx(1, "bad precision value, %s: %s", e…
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…
57 if (nr == 0)
58 vals[i] = 0.0;
59 vals[i] += val;
60 }
61 nr++;
62 }
63 printfarr(delimstr, prec, vals, nf);
64 if (finalnl)
65 putchar('\n');
66
67 free(line);
68 free(vals);
69
70 return 0;
71 }
You are viewing proxied material from mx1.adamsgaard.dk. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.