home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (C) 1989 by David W. Trissel
- *
- * Not derived from licensed software.
- *
- * Permission is granted to freely use, copy, modify, and redistribute
- * this software, provided that no attempt is made to gain profit from it,
- * the author is not construed to be liable for any results of using the
- * software, alterations are clearly marked as such, and this notice is
- * not modified.
- *
- */
- /* All rights reserved. */
-
- /* layersize - update BSD Sun Unix with window size information */
-
- #include <stdio.h>
- #include <errno.h>
- #include <sys/ioctl.h>
-
- extern int sys_nerr;
- extern char *sys_errlist[];
-
- static void gotsyserr(/* char * */);
- static void goterr(/* char * */);
- static int getnumber(/* char * */);
-
-
- /* main() - update BSD window size */
-
- main(ac, av)
- int ac; /* argument count */
- char **av; /* argument vector */
- {
- struct winsize wsize; /* window size structure for ioctl() */
- char *ap; /* argument scan pointer */
- int lines; /* new lines value */
- int cols; /* new columns value */
-
- if (--ac != 2)
- goterr("Missing lines and column options");
-
- /* get window size (actually do this to set xpixel and ypixel values) */
- if (ioctl(0, TIOCGWINSZ, &wsize) == -1)
- gotsyserr("No window support in host"); /* terminate with message */
-
- /* scan looking for -l and -c line and column numeric sizes */
- lines = cols = 0; /* reset values */
- while (ac > 0)
- { ap = *++av; /* point to next argument string */
- if (ac-- > 0 && *ap == '-') /* if option ... */
- switch (ap[1])
- { case 'l': /* lines */
- lines = getnumber(&ap[2]);
- break;
-
- case 'c': /* columns */
- cols = getnumber(&ap[2]);
- break;
-
- default:
- goterr("Usupported option"); /* unsupported option */
- break;
-
- } /* end '-' argument */
- else
- goterr("Unsupported parameter"); /* unsupported parameter */
-
- } /* end while argument vector scan */
-
- /* must have both lines and columns */
- if (lines == 0 || cols == 0)
- goterr("Must specify both lines and columns");
-
- wsize.ws_col = cols; /* set columns */
- wsize.ws_row = lines; /* set lines */
- /* update the kernel */
- if (ioctl(0, TIOCSWINSZ, &wsize) == -1)
- gotsyserr("Failed to update window size"); /* didn't go */
-
- }
-
-
- /* goterr() - issue error and terminate */
-
- static void
- goterr(msg)
- char *msg; /* error message string */
- {
- printf("%s\n", msg); /* send error message to user */
- exit(1); /* terminate with error */
-
- } /* goterr() */
-
-
- /* gotsyserror() - system error return */
-
- static void
- gotsyserr(msg)
- char *msg; /* error string */
- {
- if (errno > 0 && errno < sys_nerr)
- printf("%s: %s\n", msg, sys_errlist[errno]);
- else
- printf("%s: Error %d\n", msg, errno);
-
- exit(1); /* exit with failure */
-
- } /* gotsyserr() */
-
-
- /* getnumber() - parse option number */
-
- static int
- getnumber(str)
- char *str; /* start of option string */
- {
- int n; /* number being built */
-
- if (str == NULL)
- goterr("Invalid numeric in option");
-
- /* skip any leading delimiters */
- while (*str && (*str == ' ' || *str == '\t'))
- str++;
-
- for(n=0; *str && *str >= '0' && *str <= '9'; str++)
- n = n*10 + *str - '0'; /* add next digit in */
-
- /* make sure number terminates legally */
- switch (*str)
- { case '\0':
- case ' ':
- case '\t':
- case '\n':
- if (n <= 0 || n > 200)
- goterr("Number out of range");
- break; /* these are OK */
-
- default:
- goterr("Invalid numeric in option");
-
- } /* end switch */
-
- return ( n ); /* return the number */
-
- } /* getnumber() */
-