home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / layers.zoo / layers.2 / layersize.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-11  |  3.5 KB  |  147 lines

  1. /*             Copyright (C) 1989 by David W. Trissel 
  2.  *
  3.  *  Not derived from licensed software.
  4.  *
  5.  * Permission is granted to freely use, copy, modify, and redistribute
  6.  * this software, provided that no attempt is made to gain profit from it,
  7.  * the author is not construed to be liable for any results of using the
  8.  * software, alterations are clearly marked as such, and this notice is
  9.  * not modified.
  10.  *
  11.  */
  12.                 /*         All rights reserved.        */
  13.  
  14.         /* layersize - update BSD Sun Unix with window size information */
  15.  
  16. #include <stdio.h>
  17. #include <errno.h>
  18. #include <sys/ioctl.h>
  19.  
  20. extern int sys_nerr;
  21. extern char    *sys_errlist[];
  22.  
  23. static void gotsyserr(/* char * */);
  24. static void goterr(/* char * */);
  25. static int    getnumber(/* char * */);
  26.  
  27.  
  28.                         /* main() - update BSD window size */
  29.  
  30. main(ac, av)
  31. int            ac;                            /* argument count */
  32. char        **av;                        /* argument vector */
  33. {
  34.     struct winsize wsize;                /* window size structure for ioctl() */
  35.     char        *ap;                    /* argument scan pointer */
  36.     int            lines;                    /* new lines value */
  37.     int            cols;                    /* new columns value */
  38.  
  39.     if (--ac != 2)
  40.         goterr("Missing lines and column options");
  41.  
  42.     /* get window size (actually do this to set xpixel and ypixel values) */
  43.     if (ioctl(0, TIOCGWINSZ, &wsize) == -1)
  44.         gotsyserr("No window support in host"); /* terminate with message */
  45.  
  46.     /* scan looking for -l and -c line and column numeric sizes */
  47.     lines = cols = 0;                    /* reset values */
  48.     while (ac > 0)
  49.       {    ap = *++av;                        /* point to next argument string */
  50.         if (ac-- > 0 && *ap == '-')        /* if option ... */
  51.         switch (ap[1])
  52.         { case 'l':        /* lines */
  53.             lines = getnumber(&ap[2]);
  54.             break;
  55.  
  56.           case 'c':        /* columns */
  57.             cols = getnumber(&ap[2]);
  58.             break;
  59.  
  60.           default:
  61.             goterr("Usupported option"); /* unsupported option */
  62.             break;
  63.  
  64.         } /* end '-' argument */
  65.         else
  66.             goterr("Unsupported parameter"); /* unsupported parameter */
  67.  
  68.       } /* end while argument vector scan */
  69.             
  70.     /* must have both lines and columns */
  71.     if (lines == 0 || cols == 0)
  72.         goterr("Must specify both lines and columns");
  73.  
  74.     wsize.ws_col = cols;                /* set columns */
  75.     wsize.ws_row = lines;                /* set lines */
  76.     /* update the kernel */
  77.     if (ioctl(0, TIOCSWINSZ, &wsize) == -1)
  78.         gotsyserr("Failed to update window size"); /* didn't go */
  79.  
  80.  
  81.  
  82.                 /* goterr() - issue error and terminate */
  83.  
  84. static void
  85. goterr(msg)
  86. char        *msg;                        /* error message string */
  87. {
  88.     printf("%s\n", msg);                /* send error message to user */
  89.     exit(1);                            /* terminate with error */
  90.  
  91. } /* goterr() */
  92.  
  93.  
  94.                 /* gotsyserror() - system error return */
  95.  
  96. static void
  97. gotsyserr(msg)
  98. char        *msg;                        /* error string */
  99. {
  100.     if (errno > 0 && errno < sys_nerr)
  101.         printf("%s: %s\n", msg, sys_errlist[errno]);
  102.     else
  103.         printf("%s: Error %d\n", msg, errno);
  104.  
  105.     exit(1);                            /* exit with failure */
  106.  
  107. } /* gotsyserr() */
  108.  
  109.  
  110.                     /* getnumber() - parse option number */
  111.  
  112. static int
  113. getnumber(str)
  114. char        *str;                        /* start of option string */
  115. {
  116.     int            n;                        /* number being built */
  117.  
  118.     if (str == NULL)
  119.         goterr("Invalid numeric in option");
  120.  
  121.     /* skip any leading delimiters */
  122.     while (*str && (*str == ' ' || *str == '\t'))
  123.         str++;
  124.  
  125.     for(n=0; *str && *str >= '0' && *str <= '9'; str++)
  126.         n = n*10 + *str - '0';            /* add next digit in */
  127.  
  128.     /* make sure number terminates legally */
  129.     switch (*str)
  130.     { case '\0':
  131.       case ' ':
  132.       case '\t':
  133.       case '\n':
  134.         if (n <= 0 || n > 200)
  135.             goterr("Number out of range");
  136.         break;                            /* these are OK */
  137.  
  138.       default:
  139.         goterr("Invalid numeric in option");
  140.  
  141.     } /* end switch */
  142.  
  143.     return ( n );                        /* return the number */
  144.  
  145. } /* getnumber() */
  146.