#include #include #include #include #include #include void usage (char * progname) { fprintf (stderr, "usage: %s [ -h ] [ -d char ] [ -c num ] [ -o file ] file ...\n", progname); exit (1); } #define USAGEIF(X) if ((X)) { usage (argv[0]); } int main(int argc, char ** argv) { char delim = '\t'; FILE * in = stdin; FILE * out = stdout; int * cols = NULL; int * sums; int ncols = 0; int i, more, ret, col; opterr = 0; while (1) { int ch = getopt (argc, argv, "ho:d:c:"); char tmp; if (ch == -1) { USAGEIF (cols == NULL); break; } switch (ch) { case 'h': USAGEIF(1); break; case 'o': USAGEIF(out != stdout); out = fopen (optarg, "w"); USAGEIF (out == NULL); break; case 'd': USAGEIF (delim != '\t'); USAGEIF (strlen (optarg) != 1); delim = optarg[0]; USAGEIF (delim == '\n'); break; case 'c': ret = sscanf (optarg, "%d%c", &col, &tmp); USAGEIF (ret != 1); USAGEIF (col <= 0); if (cols) { cols = realloc (cols, (ncols+1) * sizeof (int)); } else { cols = malloc (sizeof (int)); } assert (cols); cols[ncols] = col; ncols++; break; default: USAGEIF(1); } } if (argv[optind]) { USAGEIF(argv[optind+1]); in = fopen (argv[optind], "r"); USAGEIF (in == NULL); } sums = malloc (ncols * sizeof (int)); assert (sums); for (i=0; i