/*
- * "$Id: testlpd.c 6649 2007-07-11 21:46:42Z mike $"
+ * cups-lpd test program for CUPS.
*
- * cups-lpd test program for the Common UNIX Printing System (CUPS).
+ * Copyright 2007-2015 by Apple Inc.
+ * Copyright 2006 by Easy Software Products, all rights reserved.
*
- * Copyright 2007 by Apple Inc.
- * Copyright 2006 by Easy Software Products, all rights reserved.
- *
- * These coded instructions, statements, and computer programs are the
- * property of Apple Inc. and are protected by Federal copyright
- * law. Distribution and use rights are outlined in the file "LICENSE.txt"
- * which should have been included with this file. If this file is
- * file is missing or damaged, see the license at "http://www.cups.org/".
- *
- * Contents:
- *
- * main() - Simulate an LPD client.
- * do_command() - Send the LPD command and wait for a response.
- * print_job() - Submit a file for printing.
- * print_waiting() - Print waiting jobs.
- * remove_job() - Cancel a print job.
- * status_long() - Show the long printer status.
- * status_short() - Show the short printer status.
- * usage() - Show program usage...
+ * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
*/
/*
*/
#include <cups/cups.h>
-#include <cups/string.h>
-#include <stdlib.h>
+#include <cups/string-private.h>
#include <sys/stat.h>
-#include <errno.h>
+#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
*/
static int do_command(int outfd, int infd, const char *command);
-static int print_job(int outfd, int infd, char *dest, char **args);
+static int print_job(int outfd, int infd, char *dest, char **args) __attribute__((nonnull(4)));
static int print_waiting(int outfd, int infd, char *dest);
-static int remove_job(int outfd, int infd, char *dest, char **args);
-static int status_long(int outfd, int infd, char *dest, char **args);
-static int status_short(int outfd, int infd, char *dest, char **args);
-static void usage(void);
+static int remove_job(int outfd, int infd, char *dest, char **args) __attribute__((nonnull(4)));
+static int status_long(int outfd, int infd, char *dest, char **args) __attribute__((nonnull(4)));
+static int status_short(int outfd, int infd, char *dest, char **args) __attribute__((nonnull(4)));
+static void usage(void) __attribute__((noreturn));
/*
char *cupslpd_argv[1000]; /* Arguments for cups-lpd */
int cupslpd_stdin[2], /* Standard input for cups-lpd */
cupslpd_stdout[2], /* Standard output for cups-lpd */
- cupslpd_pid; /* Process ID for cups-lpd */
+ cupslpd_pid, /* Process ID for cups-lpd */
+ cupslpd_status; /* Status of cups-lpd process */
/*
*/
op = NULL;
- opargs = NULL;
+ opargs = argv + argc;
dest = NULL;
cupslpd_argc = 1;
cupslpd_argv[0] = (char *)"cups-lpd";
{
cupslpd_argv[cupslpd_argc++] = argv[i];
- if (argv[i][2])
+ if (!argv[i][2])
{
i ++;
(strcmp(op, "print-job") && strcmp(op, "print-waiting") &&
strcmp(op, "remove-job") && strcmp(op, "status-long") &&
strcmp(op, "status-short")))
+ {
+ printf("op=\"%s\", dest=\"%s\", opargs=%p\n", op, dest, opargs);
usage();
+ }
/*
* Run the cups-lpd program using pipes...
* Child goes here...
*/
- close(0);
- dup(cupslpd_stdin[0]);
+ dup2(cupslpd_stdin[0], 0);
close(cupslpd_stdin[0]);
close(cupslpd_stdin[1]);
- close(1);
- dup(cupslpd_stdout[1]);
+ dup2(cupslpd_stdout[1], 1);
close(cupslpd_stdout[0]);
close(cupslpd_stdout[1]);
close(cupslpd_stdin[1]);
close(cupslpd_stdout[0]);
- kill(cupslpd_pid, SIGTERM);
+
+ while (wait(&cupslpd_status) != cupslpd_pid);
+
+ printf("cups-lpd exit status was %d...\n", cupslpd_status);
/*
* Return the test status...
int infd, /* I - Response file descriptor */
const char *command) /* I - Command line to send */
{
- int len; /* Length of command line */
- char status; /* Status byte */
+ size_t len; /* Length of command line */
+ char status; /* Status byte */
printf("COMMAND: %02X %s", command[0], command + 1);
len = strlen(command);
- if (write(outfd, command, len) < len)
+ if ((size_t)write(outfd, command, len) < len)
{
puts(" Write failed!");
return (-1);
}
if (read(infd, &status, 1) < 1)
- puts("IN: ERROR");
+ puts("STATUS: ERROR");
else
- printf("IN: %d\n", status);
+ printf("STATUS: %d\n", status);
return (status);
}
struct stat fileinfo; /* File information */
char *jobname; /* Job name */
int sequence; /* Sequence number */
- int bytes; /* Bytes read/written */
+ ssize_t bytes; /* Bytes read/written */
/*
* Send the control file...
*/
- bytes = strlen(control);
+ bytes = (ssize_t)strlen(control);
snprintf(command, sizeof(command), "\002%d cfA%03dlocalhost\n",
- bytes, sequence);
+ (int)bytes, sequence);
if ((status = do_command(outfd, infd, command)) != 0)
{
bytes ++;
- if (write(outfd, control, bytes) < bytes)
+ if (write(outfd, control, (size_t)bytes) < bytes)
{
- printf("CONTROL: Unable to write %d bytes!\n", bytes);
+ printf("CONTROL: Unable to write %d bytes!\n", (int)bytes);
close(fd);
return (-1);
}
- printf("CONTROL: Wrote %d bytes.\n", bytes);
+ printf("CONTROL: Wrote %d bytes.\n", (int)bytes);
if (read(infd, command, 1) < 1)
{
- puts("IN: ERROR");
+ puts("STATUS: ERROR");
close(fd);
return (-1);
}
{
status = command[0];
- printf("IN: %d\n", status);
+ printf("STATUS: %d\n", status);
}
/*
while ((bytes = read(fd, buffer, sizeof(buffer))) > 0)
{
- if (write(outfd, buffer, bytes) < bytes)
+ if (write(outfd, buffer, (size_t)bytes) < bytes)
{
- printf("DATA: Unable to write %d bytes!\n", bytes);
+ printf("DATA: Unable to write %d bytes!\n", (int)bytes);
close(fd);
return (-1);
}
if (read(infd, command, 1) < 1)
{
- puts("IN: ERROR");
+ puts("STATUS: ERROR");
close(fd);
return (-1);
}
{
status = command[0];
- printf("IN: %d\n", status);
+ printf("STATUS: %d\n", status);
}
return (status);
{
char command[1024], /* Command buffer */
buffer[8192]; /* Status buffer */
- int bytes; /* Bytes read/written */
+ ssize_t bytes; /* Bytes read/written */
/*
* Send the "send short status" command...
*/
- if (args)
+ if (args[0])
snprintf(command, sizeof(command), "\004%s %s\n", dest, args[0]);
else
snprintf(command, sizeof(command), "\004%s\n", dest);
- bytes = strlen(command);
+ bytes = (ssize_t)strlen(command);
- if (write(outfd, command, bytes) < bytes)
+ if (write(outfd, command, (size_t)bytes) < bytes)
return (-1);
/*
while ((bytes = read(infd, buffer, sizeof(buffer))) > 0)
{
- fwrite(buffer, 1, bytes, stdout);
+ fwrite(buffer, 1, (size_t)bytes, stdout);
fflush(stdout);
}
{
char command[1024], /* Command buffer */
buffer[8192]; /* Status buffer */
- int bytes; /* Bytes read/written */
+ ssize_t bytes; /* Bytes read/written */
/*
* Send the "send short status" command...
*/
- if (args)
+ if (args[0])
snprintf(command, sizeof(command), "\003%s %s\n", dest, args[0]);
else
snprintf(command, sizeof(command), "\003%s\n", dest);
- bytes = strlen(command);
+ bytes = (ssize_t)strlen(command);
- if (write(outfd, command, bytes) < bytes)
+ if (write(outfd, command, (size_t)bytes) < bytes)
return (-1);
/*
while ((bytes = read(infd, buffer, sizeof(buffer))) > 0)
{
- fwrite(buffer, 1, bytes, stdout);
+ fwrite(buffer, 1, (size_t)bytes, stdout);
fflush(stdout);
}
static void
usage(void)
{
- puts("Usage: testlpd [options] print-job printer user filename [... filename]");
+ puts("Usage: testlpd [options] print-job printer filename [... filename]");
puts(" testlpd [options] print-waiting [printer or user]");
puts(" testlpd [options] remove-job printer [user [job-id]]");
puts(" testlpd [options] status-long [printer or user]");
exit(0);
}
-
-
-/*
- * End of "$Id: testlpd.c 6649 2007-07-11 21:46:42Z mike $".
- */