home *** CD-ROM | disk | FTP | other *** search
- /*
- * grid - print grid on standard output
- */
-
- #ifndef lint
- static char _cpyrgt[] = "Copyright 1989 Howard Lee Gayle";
- #endif lint
-
- /*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 1,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
- #include <stdio.h>
- #include <howard/port.h>
- #include <howard/version.h>
- #include <howard/usage.h>
-
- MAINVER ("@(#)$Header: grid.c,v 1.4 89/08/21 11:03:01 howard Exp $");
- USAGE ("number-of-rows number-of-columns");
-
- #include <howard/malf.h>
- #include <howard/registers.i>
-
- #define MIN_ROW 1 /* minimum row number */
- #define MAX_ROW 9999 /* maximum row number */
- #define MIN_COL 11 /* minimum column number */
- #define MAX_COL 9999 /* maximum column number */
-
- /* decpad - convert integer to string, pad with minus */
-
- PRIVATE void decpad (s, i)
- R1 char *s; /* output string */
- R2 int i; /* integer */
-
- /* Function:
- * Convert i to a right-justified string, padded on the left with '-'
- * characters to exactly 4 positions plus a NUL terminator.
- * Algorithm:
- * Call sprintf. Change leading blanks to '-'.
- * Notes:
- * 1) s must point to a buffer of at least 5 bytes. Overflow is not
- * checked.
- */
-
- {
- SPRINTF (s, "%4d", i);
- while (*s == ' ')
- *s++ = '-';
- }
-
- /* main - main function */
-
- PUBLIC int main (argc, argv)
- int argc; /* Number of arguments.*/
- R6 bStrT *argv; /* Points to array of argument strings.*/
-
- /* Function:
- *
- * Algorithm:
- * * Initialize rb. For each line, compute lb, then output lb and rb.
- * Notes:
- *
- */
-
- {
- R1 int i; /* general purpose loop variable */
- R2 char *cp; /* for setting up rb[] */
- R3 int nr; /* number of rows */
- R4 int nc; /* number of columns */
- R5 int nc10; /* highest column number (divided by 10)
- * to appear on grid. */
- static char lb[6] = "<"; /* buffer for columns 1 .. 5 of each line */
- static char rb[MAX_COL - 3]; /* buffer for columns 6 .. nc of
- * output grid. These do not change from
- * line to line. */
-
- if (argc != 3) usage();
- nr = mra2i (argv[1], NULBSTR, FALSE, S("Number of rows"), MIN_ROW, MAX_ROW,
- (bStrT *) NULL);
- nc = mra2i (argv[2], NULBSTR, FALSE, S("Number of columns"), MIN_COL, MAX_COL,
- (bStrT *) NULL);
- cp = rb;
- *cp++ = '-';
- *cp++ = '-';
- *cp++ = '-';
- *cp++ = '-';
- *cp++ = '1';
- nc10 = (nc - 1) / 10;
- for (i = 2; i <= nc10; ++i)
- {
- *cp++ = '-';
- *cp++ = '-';
- *cp++ = '-';
- *cp++ = '-';
- *cp++ = '+';
- *cp++ = '-';
- decpad (cp, i);
- cp += 4;
- }
- i *= 10;
- for (i -= 9; i != nc; ++i)
- *cp++ = ((i % 5) ? '-' : '+');
- *cp++ = '>';
- *cp++ = '\n';
- *cp = 0;
- cp = rb;
- for (i = 1; i <= nr; ++i)
- {
- decpad (&lb[1], i);
- FPUTS (lb, stdout);
- FPUTS (cp, stdout);
- }
- mfflush (stdout, S("Standard Output"));
- exit (SUCCESS);
-
- #ifdef lint
- return (SUCCESS);
- #endif
- }
-