home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / slackwar / a / util / util-lin.10 / util-lin / util-linux-1.10 / tunelp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-27  |  3.3 KB  |  161 lines

  1. /***********************************************************************\
  2. *    Copyright (C) 1992 by Michael K. Johnson, johnsonm@stolaf.edu    *
  3. *                                    *
  4. *    This file is placed under the conditions of the GNU public    *
  5. *    license, version 2, or any later version.  See file COPYING    *
  6. *    for information on distribution conditions.            *
  7. \***********************************************************************/
  8.  
  9. #include<unistd.h>
  10. #include<stdio.h>
  11. #include<fcntl.h>
  12. #include<linux/lp.h>
  13. #include<linux/fs.h>
  14. #include<sys/ioctl.h>
  15. #include<sys/stat.h>
  16. #include<sys/types.h>
  17. #include<malloc.h>
  18. #include<string.h>
  19.  
  20. struct command {
  21.   long op;
  22.   long val;
  23.   struct command *next;
  24. };
  25.  
  26.  
  27.  
  28.  
  29. void print_usage(char *progname) {
  30.   printf("Usage: %s <device> [ -i <IRQ> | -t <TIME> | -c <CHARS> | -w <WAIT> | -a [on|off]]\n",
  31.      progname);
  32.   exit (1);
  33. }
  34.  
  35.  
  36.  
  37.  
  38.  
  39. void *mylloc(long size) {
  40.   void *ptr;
  41.   if(!(ptr = (void*)malloc(size))) {
  42.     perror("malloc error");
  43.     exit(2);
  44.   }
  45.   return ptr;
  46. }
  47.  
  48.  
  49.  
  50. long get_val(char *val) {
  51.   long ret;
  52.   if (!(sscanf(val, "%d", &ret) == 1)) {
  53.     perror("sscanf error");
  54.     exit(3);
  55.   }
  56.   return ret;
  57. }
  58.  
  59.  
  60. long get_onoff(char *val) {
  61.   if (!strncasecmp("on", val, 2))
  62.     return 1;
  63.   return 0;
  64. }
  65.  
  66.  
  67.  
  68. int main (int argc, char ** argv) {
  69.   int c, fd, irq;
  70.   char *progname;
  71.   char *filename;
  72.   struct stat statbuf;
  73.   struct command *cmds, *cmdst;
  74.  
  75.  
  76.   progname = argv[0];
  77.   if (argc < 2) print_usage(progname);
  78.   if ((argc % 2) != 0) print_usage(progname);
  79.  
  80.   filename = strdup(argv[1]);
  81.   fd = open(filename, O_WRONLY, 0);
  82.   if (fd < 0) {
  83.     perror(argv[1]);
  84.     return -1;
  85.   }
  86.  
  87.   fstat(fd, &statbuf);
  88.  
  89.   if((!S_ISCHR(statbuf.st_mode)) || (MAJOR(statbuf.st_rdev) != 6 )
  90.      || (MINOR(statbuf.st_rdev) > 3)) {
  91.     printf("%s: %s not an lp device.\n", argv[0], argv[1]);
  92.     print_usage(progname);
  93.   }
  94.  
  95.   cmdst = cmds = mylloc(sizeof(struct command));
  96.   cmds->next = 0;
  97.  
  98.   while ((c = getopt(argc, argv, "t:c:w:a:i:h")) != EOF) {
  99.     switch (c) {
  100.     case 'h':
  101.       print_usage(progname);
  102.       break;
  103.     case 'i':
  104.       cmds->op = LPSETIRQ;
  105.       cmds->val = get_val(optarg);
  106.       cmds->next = mylloc(sizeof(struct command));
  107.       cmds = cmds->next; cmds->next = 0;
  108.       break;
  109.     case 't':
  110.       cmds->op = LPTIME;
  111.       cmds->val = get_val(optarg);
  112.       cmds->next = mylloc(sizeof(struct command));
  113.       cmds = cmds->next; cmds->next = 0;
  114.       break;
  115.     case 'c':
  116.       cmds->op = LPCHAR;
  117.       cmds->val = get_val(optarg);
  118.       cmds->next = mylloc(sizeof(struct command));
  119.       cmds = cmds->next; cmds->next = 0;
  120.       break;
  121.     case 'w':
  122.       cmds->op = LPWAIT;
  123.       cmds->val = get_val(optarg);
  124.       cmds->next = mylloc(sizeof(struct command));
  125.       cmds = cmds->next; cmds->next = 0;
  126.       break;
  127.     case 'a':
  128.       cmds->op = LPABORT;
  129.       cmds->val = get_onoff(optarg);
  130.       cmds->next = mylloc(sizeof(struct command));
  131.       cmds = cmds->next; cmds->next = 0;
  132.       break;
  133.     default: print_usage(progname);
  134.     }
  135.   }
  136.  
  137.  
  138.   cmds = cmdst;
  139.   while (cmds->next) {
  140.     if (ioctl(fd, cmds->op, cmds->val) < 0)
  141.       perror("tunelp: ioctl");
  142.     cmdst = cmds;
  143.     cmds = cmds->next;
  144.     free(cmdst);
  145.   }
  146.  
  147.   irq = ioctl(fd, LPGETIRQ);
  148.   if (irq == -1) {
  149.     perror("LPGETIRQ error");
  150.     exit(4);
  151.   }
  152.   if (irq)
  153.     printf("%s using IRQ %d\n", filename, irq);
  154.   else
  155.     printf("%s using polling\n", filename);
  156.  
  157.   close(fd);
  158.  
  159.   return 0;
  160. }
  161.