home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / slackwar / a / util / util-lin.2 / util-lin / util-linux-2.2 / sys-utils / tunelp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-22  |  6.1 KB  |  249 lines

  1. /****************************************************************************\
  2. *    Copyright (C) 1992 by Michael K. Johnson, johnsonm@nigel.vnet.net    *
  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. /* tunelp.c,v 1.1.1.1 1995/02/22 19:09:12 faith Exp
  10.  * tunelp.c,v
  11.  * Revision 1.1.1.1  1995/02/22  19:09:12  faith
  12.  * Imported sources
  13.  *
  14.  * Revision 1.5  1995/01/13  10:33:43  johnsonm
  15.  * Chris's changes for new ioctl numbers and backwards compatibility
  16.  * and the reset ioctl.
  17.  *
  18.  * Revision 1.4  1995/01/03  17:42:14  johnsonm
  19.  * -s isn't supposed to take an argument; removed : after s in getopt...
  20.  *
  21.  * Revision 1.3  1995/01/03  07:36:49  johnsonm
  22.  * Fixed typo
  23.  *
  24.  * Revision 1.2  1995/01/03  07:33:44  johnsonm
  25.  * revisions for lp driver updates in Linux 1.1.76
  26.  *
  27.  *
  28.  */
  29.  
  30. #include<unistd.h>
  31. #include<stdio.h>
  32. #include<fcntl.h>
  33. #include<linux/lp.h>
  34. #include<linux/fs.h>
  35. #include<sys/ioctl.h>
  36. #include<sys/stat.h>
  37. #include<sys/types.h>
  38. #include<malloc.h>
  39. #include<string.h>
  40. #include<errno.h>
  41.  
  42. struct command {
  43.   long op;
  44.   long val;
  45.   struct command *next;
  46. };
  47.  
  48.  
  49.  
  50.  
  51. void print_usage(char *progname) {
  52.   printf("Usage: %s <device> [ -i <IRQ> | -t <TIME> | -c <CHARS> | -w <WAIT> | \n"
  53.          "          -a [on|off] | -o [on|off] | -C [on|off] | -q [on|off] | -s ]\n", progname);
  54.   exit (1);
  55. }
  56.  
  57.  
  58.  
  59.  
  60.  
  61. void *mylloc(long size) {
  62.   void *ptr;
  63.   if(!(ptr = (void*)malloc(size))) {
  64.     perror("malloc error");
  65.     exit(2);
  66.   }
  67.   return ptr;
  68. }
  69.  
  70.  
  71.  
  72. long get_val(char *val) {
  73.   long ret;
  74.   if (!(sscanf(val, "%d", &ret) == 1)) {
  75.     perror("sscanf error");
  76.     exit(3);
  77.   }
  78.   return ret;
  79. }
  80.  
  81.  
  82. long get_onoff(char *val) {
  83.   if (!strncasecmp("on", val, 2))
  84.     return 1;
  85.   return 0;
  86. }
  87.  
  88.  
  89.  
  90. int main (int argc, char ** argv) {
  91.   int c, fd, irq, status, show_irq, offset = 0, retval;
  92.   char *progname;
  93.   char *filename;
  94.   struct stat statbuf;
  95.   struct command *cmds, *cmdst;
  96.  
  97.  
  98.   progname = argv[0];
  99.   if (argc < 2) print_usage(progname);
  100.  
  101.   filename = strdup(argv[1]);
  102.   fd = open(filename, O_WRONLY|O_NONBLOCK, 0);
  103.   /* Need to open O_NONBLOCK in case ABORTOPEN is already set and
  104.      printer is off or off-line or in an error condition.  Otherwise
  105.      we would abort... */
  106.   if (fd < 0) {
  107.     perror(argv[1]);
  108.     return -1;
  109.   }
  110.  
  111.   fstat(fd, &statbuf);
  112.  
  113.   if((!S_ISCHR(statbuf.st_mode)) || (MAJOR(statbuf.st_rdev) != 6 )
  114.      || (MINOR(statbuf.st_rdev) > 3)) {
  115.     printf("%s: %s not an lp device.\n", argv[0], argv[1]);
  116.     print_usage(progname);
  117.   }
  118.  
  119.   cmdst = cmds = mylloc(sizeof(struct command));
  120.   cmds->next = 0;
  121.  
  122.   show_irq = 1;
  123.   while ((c = getopt(argc, argv, "t:c:w:a:i:ho:C:sq:r")) != EOF) {
  124.     switch (c) {
  125.     case 'h':
  126.       print_usage(progname);
  127.       break;
  128.     case 'i':
  129.       cmds->op = LPSETIRQ;
  130.       cmds->val = get_val(optarg);
  131.       cmds->next = mylloc(sizeof(struct command));
  132.       cmds = cmds->next; cmds->next = 0;
  133.       break;
  134.     case 't':
  135.       cmds->op = LPTIME;
  136.       cmds->val = get_val(optarg);
  137.       cmds->next = mylloc(sizeof(struct command));
  138.       cmds = cmds->next; cmds->next = 0;
  139.       break;
  140.     case 'c':
  141.       cmds->op = LPCHAR;
  142.       cmds->val = get_val(optarg);
  143.       cmds->next = mylloc(sizeof(struct command));
  144.       cmds = cmds->next; cmds->next = 0;
  145.       break;
  146.     case 'w':
  147.       cmds->op = LPWAIT;
  148.       cmds->val = get_val(optarg);
  149.       cmds->next = mylloc(sizeof(struct command));
  150.       cmds = cmds->next; cmds->next = 0;
  151.       break;
  152.     case 'a':
  153.       cmds->op = LPABORT;
  154.       cmds->val = get_onoff(optarg);
  155.       cmds->next = mylloc(sizeof(struct command));
  156.       cmds = cmds->next; cmds->next = 0;
  157.       break;
  158.     case 'q':
  159.       if (get_onoff(optarg)) {
  160.         show_irq=1;
  161.       } else {
  162.         show_irq=0;
  163.       }
  164. #ifdef LPGETSTATUS
  165.     case 'o':
  166.       cmds->op = LPABORTOPEN;
  167.       cmds->val = get_onoff(optarg);
  168.       cmds->next = mylloc(sizeof(struct command));
  169.       cmds = cmds->next; cmds->next = 0;
  170.       break;
  171.     case 'C':
  172.       cmds->op = LPCAREFUL;
  173.       cmds->val = get_onoff(optarg);
  174.       cmds->next = mylloc(sizeof(struct command));
  175.       cmds = cmds->next; cmds->next = 0;
  176.       break;
  177.     case 's':
  178.       show_irq = 0;
  179.       cmds->op = LPGETSTATUS;
  180.       cmds->val = 0;
  181.       cmds->next = mylloc(sizeof(struct command));
  182.       cmds = cmds->next; cmds->next = 0;
  183.       break;
  184. #endif
  185. #ifdef LPRESET
  186.     case 'r':
  187.       cmds->op = LPRESET;
  188.       cmds->val = 0;
  189.       cmds->next = mylloc(sizeof(struct command));
  190.       cmds = cmds->next; cmds->next = 0;
  191.       break;
  192. #endif
  193.     default: print_usage(progname);
  194.     }
  195.   }
  196.  
  197.   /* Allow for binaries compiled under a new kernel to work on the old ones */
  198.   if (LPGETIRQ >= 0x0600 && ioctl(fd, LPGETIRQ) < 0 && errno == EINVAL)
  199.     offset = 0x0600;    /* We don't understand the new ioctls */
  200.  
  201.   cmds = cmdst;
  202.   while (cmds->next) {
  203. #ifdef LPGETSTATUS
  204.     if (cmds->op == LPGETSTATUS) {
  205.       status = 0xdeadbeef;
  206.       retval = ioctl(fd, LPGETSTATUS - offset, &status);
  207.       if (retval < 0)
  208.           perror("LPGETSTATUS error");
  209.       else {
  210.         if (status == 0xdeadbeef)    /* a few 1.1.7x kernels will do this */
  211.           status = retval;
  212.     printf("%s status is %d", filename, status);
  213.     if (!(status & LP_PBUSY)) printf(", busy");
  214.     if (!(status & LP_PACK)) printf(", ready");
  215.     if ((status & LP_POUTPA)) printf(", out of paper");
  216.     if ((status & LP_PSELECD)) printf(", on-line");
  217.     if (!(status & LP_PERRORP)) printf(", error");
  218.     printf("\n");
  219.       }
  220.     } else
  221. #endif /* LPGETSTATUS */
  222.     if (ioctl(fd, cmds->op - offset, cmds->val) < 0) {
  223.       perror("tunelp: ioctl");
  224.     }
  225.     cmdst = cmds;
  226.     cmds = cmds->next;
  227.     free(cmdst);
  228.   }
  229.  
  230.   if (show_irq) {
  231.     irq = 0xdeadbeef;
  232.     retval = ioctl(fd, LPGETIRQ - offset, &irq);
  233.     if (retval == -1) {
  234.       perror("LPGETIRQ error");
  235.       exit(4);
  236.     }
  237.     if (irq == 0xdeadbeef)        /* up to 1.1.77 will do this */
  238.       irq = retval;
  239.     if (irq)
  240.       printf("%s using IRQ %d\n", filename, irq);
  241.     else
  242.       printf("%s using polling\n", filename);
  243.   }
  244.  
  245.   close(fd);
  246.  
  247.   return 0;
  248. }
  249.