]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - sys-utils/tunelp.c
Merge branch 'patch-23' of https://github.com/mariobl/util-linux
[thirdparty/util-linux.git] / sys-utils / tunelp.c
index f59c6dd485923e5691f9cf14fc15e8167dd11ab7..95a21b39eeecbebec1b4b89543fc1916e964f40c 100644 (file)
@@ -6,6 +6,15 @@
  * information on distribution conditions.
  */
 
+/*
+ * This command is deprecated.  The utility is in maintenance mode,
+ * meaning we keep them in source tree for backward compatibility
+ * only.  Do not waste time making this command better, unless the
+ * fix is about security or other very critical issue.
+ *
+ * See Documentation/deprecated.txt for more information.
+ */
+
 /*
  * $Log: tunelp.c,v $
  * Revision 1.9  1998/06/08 19:37:11  janl
@@ -42,7 +51,7 @@
  * Revision 1.2  1995/01/03  07:33:44  johnsonm
  * revisions for lp driver updates in Linux 1.1.76
  *
- * 1999-02-22 Arkadiusz Mikiewicz <misiek@pld.ORG.PL>
+ * 1999-02-22 Arkadiusz Miśkiewicz <misiek@pld.ORG.PL>
  * - added Native Language Support
  *
  * 1999-05-07 Merged LPTRUSTIRQ patch by Andrea Arcangeli (1998/11/29), aeb
@@ -52,7 +61,6 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <getopt.h>
-#include <malloc.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 #include <unistd.h>
 
-#include "lp.h"
+#include <linux/lp.h>
+
 #include "nls.h"
-#include "xalloc.h"
+#include "closestream.h"
+#include "strutils.h"
 
-#define EXIT_BAD_VALUE 3
-#define EXIT_LP_IO_ERR 4
+#define EXIT_LP_MALLOC         2
+#define EXIT_LP_BADVAL         3
+#define EXIT_LP_IO_ERR         4
+
+#define XALLOC_EXIT_CODE EXIT_LP_MALLOC
+#include "xalloc.h"
 
 struct command {
        long op;
@@ -74,11 +88,15 @@ struct command {
        struct command *next;
 };
 
-static void __attribute__((__noreturn__)) print_usage(FILE *out)
+static void __attribute__((__noreturn__)) usage(void)
 {
+       FILE *out = stdout;
        fputs(USAGE_HEADER, out);
        fprintf(out, _(" %s [options] <device>\n"), program_invocation_short_name);
 
+       fputs(USAGE_SEPARATOR, out);
+       fputs(_("Set various parameters for the line printer.\n"), out);
+
        fputs(USAGE_OPTIONS, out);
        fputs(_(" -i, --irq <num>              specify parallel port irq\n"), out);
        fputs(_(" -t, --time <ms>              driver wait time in milliseconds\n"), out);
@@ -91,30 +109,13 @@ static void __attribute__((__noreturn__)) print_usage(FILE *out)
        fputs(_(" -o, --check-status <on|off>  check printer status before printing\n"), out);
        fputs(_(" -C, --careful <on|off>       extra checking to status check\n"), out);
        fputs(_(" -s, --status                 query printer status\n"), out);
-       fputs(_(" -T, --trust-irq <on|off>     make driver to trust irq\n"), out);
        fputs(_(" -r, --reset                  reset the port\n"), out);
        fputs(_(" -q, --print-irq <on|off>     display current irq setting\n"), out);
        fputs(USAGE_SEPARATOR, out);
-       fputs(USAGE_HELP, out);
-       fputs(USAGE_VERSION, out);
+       fprintf(out, USAGE_HELP_OPTIONS(30));
        fprintf(out, USAGE_MAN_TAIL("tunelp(8)"));
 
-       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
-}
-
-static long get_val(char *val)
-{
-       long ret;
-       if (!(sscanf(val, "%ld", &ret) == 1))
-               errx(EXIT_BAD_VALUE, _("bad value"));
-       return ret;
-}
-
-static long get_onoff(char *val)
-{
-       if (!strncasecmp("on", val, 2))
-               return 1;
-       return 0;
+       exit(EXIT_SUCCESS);
 }
 
 int main(int argc, char **argv)
@@ -143,74 +144,72 @@ int main(int argc, char **argv)
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
+       close_stdout_atexit();
+
+       strutils_set_exitcode(EXIT_LP_BADVAL);
 
-       if (argc < 2)
-               print_usage(stderr);
+       if (argc < 2) {
+               warnx(_("not enough arguments"));
+               errtryhelp(EXIT_FAILURE);
+       }
 
        cmdst = cmds = xmalloc(sizeof(struct command));
-       cmds->next = 0;
+       cmds->next = NULL;
 
        show_irq = 1;
        while ((c = getopt_long(argc, argv, "t:c:w:a:i:ho:C:sq:rT:vV", longopts, NULL)) != -1) {
                switch (c) {
-               case 'h':
-                       print_usage(stdout);
-                       break;
                case 'i':
                        cmds->op = LPSETIRQ;
-                       cmds->val = get_val(optarg);
+                       cmds->val = strtol_or_err(optarg, _("argument error"));
                        cmds->next = xmalloc(sizeof(struct command));
                        cmds = cmds->next;
-                       cmds->next = 0;
+                       cmds->next = NULL;
                        break;
                case 't':
                        cmds->op = LPTIME;
-                       cmds->val = get_val(optarg);
+                       cmds->val = strtol_or_err(optarg, _("argument error"));
                        cmds->next = xmalloc(sizeof(struct command));
                        cmds = cmds->next;
-                       cmds->next = 0;
+                       cmds->next = NULL;
                        break;
                case 'c':
                        cmds->op = LPCHAR;
-                       cmds->val = get_val(optarg);
+                       cmds->val = strtol_or_err(optarg, _("argument error"));
                        cmds->next = xmalloc(sizeof(struct command));
                        cmds = cmds->next;
-                       cmds->next = 0;
+                       cmds->next = NULL;
                        break;
                case 'w':
                        cmds->op = LPWAIT;
-                       cmds->val = get_val(optarg);
+                       cmds->val = strtol_or_err(optarg, _("argument error"));
                        cmds->next = xmalloc(sizeof(struct command));
                        cmds = cmds->next;
-                       cmds->next = 0;
+                       cmds->next = NULL;
                        break;
                case 'a':
                        cmds->op = LPABORT;
-                       cmds->val = get_onoff(optarg);
+                       cmds->val = parse_switch(optarg, _("argument error"), "on", "off", NULL);
                        cmds->next = xmalloc(sizeof(struct command));
                        cmds = cmds->next;
-                       cmds->next = 0;
+                       cmds->next = NULL;
                        break;
                case 'q':
-                       if (get_onoff(optarg)) {
-                               show_irq = 1;
-                       } else {
-                               show_irq = 0;
-                       }
-#ifdef LPGETSTATUS
+                       show_irq = parse_switch(optarg, _("argument error"), "on", "off", NULL);
+                       break;
                case 'o':
                        cmds->op = LPABORTOPEN;
-                       cmds->val = get_onoff(optarg);
+                       cmds->val = parse_switch(optarg, _("argument error"), "on", "off", NULL);
                        cmds->next = xmalloc(sizeof(struct command));
                        cmds = cmds->next;
-                       cmds->next = 0;
+                       cmds->next = NULL;
                        break;
                case 'C':
                        cmds->op = LPCAREFUL;
-                       cmds->val = get_onoff(optarg);
+                       cmds->val = parse_switch(optarg, _("argument error"), "on", "off", NULL);
                        cmds->next = xmalloc(sizeof(struct command));
                        cmds = cmds->next;
-                       cmds->next = 0;
+                       cmds->next = NULL;
                        break;
                case 's':
                        show_irq = 0;
@@ -218,43 +217,32 @@ int main(int argc, char **argv)
                        cmds->val = 0;
                        cmds->next = xmalloc(sizeof(struct command));
                        cmds = cmds->next;
-                       cmds->next = 0;
+                       cmds->next = NULL;
                        break;
-#endif
-#ifdef LPRESET
                case 'r':
                        cmds->op = LPRESET;
                        cmds->val = 0;
                        cmds->next = xmalloc(sizeof(struct command));
                        cmds = cmds->next;
-                       cmds->next = 0;
+                       cmds->next = NULL;
                        break;
-#endif
-#ifdef LPTRUSTIRQ
-               case 'T':
-                       /* Note: this will do the wrong thing on
-                        * 2.0.36 when compiled under 2.2.x
-                        */
-                       cmds->op = LPTRUSTIRQ;
-                       cmds->val = get_onoff(optarg);
-                       cmds->next = xmalloc(sizeof(struct command));
-                       cmds = cmds->next;
-                       cmds->next = 0;
-                       break;
-#endif
+
+               case 'h':
+                       usage();
                case 'v':
                case 'V':
-                       printf(UTIL_LINUX_VERSION);
-                       return EXIT_SUCCESS;
+                       print_version(EXIT_SUCCESS);
                default:
-                       print_usage(stderr);
+                       errtryhelp(EXIT_FAILURE);
                }
        }
 
-       if (optind != argc - 1)
-               print_usage(stderr);
+       if (optind != argc - 1) {
+               warnx(_("no device specified"));
+               errtryhelp(EXIT_FAILURE);
+       }
 
-       filename = strdup(argv[optind]);
+       filename = xstrdup(argv[optind]);
        fd = open(filename, O_WRONLY | O_NONBLOCK, 0);
        /* Need to open O_NONBLOCK in case ABORTOPEN is already set
         * and printer is off or off-line or in an error condition.
@@ -263,11 +251,12 @@ int main(int argc, char **argv)
        if (fd < 0)
                err(EXIT_FAILURE, "%s", filename);
 
-       fstat(fd, &statbuf);
+       if (fstat(fd, &statbuf))
+               err(EXIT_FAILURE, "%s: stat() failed", filename);
 
        if (!S_ISCHR(statbuf.st_mode)) {
                warnx(_("%s not an lp device"), filename);
-               print_usage(stderr);
+               errtryhelp(EXIT_FAILURE);
        }
        /* Allow for binaries compiled under a new kernel to work on
         * the old ones The irq argument to ioctl isn't touched by
@@ -281,7 +270,6 @@ int main(int argc, char **argv)
 
        cmds = cmdst;
        while (cmds->next) {
-#ifdef LPGETSTATUS
                if (cmds->op == LPGETSTATUS) {
                        status = 0xdeadbeef;
                        retval = ioctl(fd, LPGETSTATUS - offset, &status);
@@ -304,9 +292,7 @@ int main(int argc, char **argv)
                                        printf(_(", error"));
                                printf("\n");
                        }
-               } else
-#endif /* LPGETSTATUS */
-               if (ioctl(fd, cmds->op - offset, cmds->val) < 0)
+               } else if (ioctl(fd, cmds->op - offset, cmds->val) < 0)
                        warn(_("ioctl failed"));
                cmdst = cmds;
                cmds = cmds->next;
@@ -326,7 +312,7 @@ int main(int argc, char **argv)
                else
                        printf(_("%s using polling\n"), filename);
        }
-
+       free(filename);
        close(fd);
 
        return EXIT_SUCCESS;