Skip to content

Commit

Permalink
2008-03-05 Norm Jacobs <[email protected]>
Browse files Browse the repository at this point in the history
        * cupsppd.c (getline): emulate getline() for systems without it.
          (PPD_writeFd): replace strndup with calloc/strncpy for portability.
        * cupsconnection.c (Connection_getDocument): generate tempfile template
        with snprintf instead of alloc, copy, copy.
        (Connection_addPrinter): generate tempfile template with snprintf
        instead of strpcpy, strcpy.
        (PyObject_to_String): avoid using alloca() by declaring a buffer at
        the top of the function.
        (Connection_getServerPPD): test for CUPS version using cups.h supplied
        values.
        (Connection_getDocument): test for CUPS version using cups.h supplied
        values.
        * cupsmodule.c: test for CUPS version using cups.h supplied values.

git-svn-id: svn+ssh://svn.fedorahosted.org/svn/pycups/trunk@287 e7545f15-59ab-4934-8816-61942c173d0f
  • Loading branch information
twaugh committed Mar 6, 2008
1 parent 2450bef commit 9f399f2
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 26 deletions.
16 changes: 16 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
2008-03-05 Norm Jacobs <[email protected]>

* cupsppd.c (getline): emulate getline() for systems without it.
(PPD_writeFd): replace strndup with calloc/strncpy for portability.
* cupsconnection.c (Connection_getDocument): generate tempfile template
with snprintf instead of alloc, copy, copy.
(Connection_addPrinter): generate tempfile template with snprintf
instead of strpcpy, strcpy.
(PyObject_to_String): avoid using alloca() by declaring a buffer at
the top of the function.
(Connection_getServerPPD): test for CUPS version using cups.h supplied
values.
(Connection_getDocument): test for CUPS version using cups.h supplied
values.
* cupsmodule.c: test for CUPS version using cups.h supplied values.

2008-03-05 Tim Waugh <[email protected]>

* Makefile: Version 1.9.36.
Expand Down
52 changes: 28 additions & 24 deletions cupsconnection.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@
#include "cupsppd.h"
#include "cupsmodule.h"

#ifndef __SVR4
#include <paths.h>
#endif
#include <stdlib.h>
#include <string.h>

#ifndef _PATH_TMP
#define _PATH_TMP P_tmpdir
#endif

PyObject *HTTPError;
PyObject *IPPError;

Expand Down Expand Up @@ -634,11 +640,7 @@ Connection_getPPDs (Connection *self)
static PyObject *
Connection_getServerPPD (Connection *self, PyObject *args)
{
#ifndef HAVE_CUPS_1_3
PyErr_SetString (PyExc_RuntimeError,
"Operation not supported - recompile against CUPS 1.3");
return NULL;
#else /* CUPS 1.3 */
#if CUPS_VERSION_MAJOR >= 1 && CUPS_VERSION_MINOR >= 3
const char *ppd_name, *filename;
if (!PyArg_ParseTuple (args, "s", &ppd_name))
return NULL;
Expand All @@ -654,17 +656,17 @@ Connection_getServerPPD (Connection *self, PyObject *args)
debugprintf ("<- Connection_getServerPPD(\"%s\") = \"%s\"\n",
ppd_name, filename);
return PyString_FromString (filename);
#else /* earlier than CUPS 1.3 */
PyErr_SetString (PyExc_RuntimeError,
"Operation not supported - recompile against CUPS 1.3 or later");
return NULL;
#endif /* CUPS 1.3 */
}

static PyObject *
Connection_getDocument (Connection *self, PyObject *args)
{
#ifndef HAVE_CUPS_1_4
PyErr_SetString (PyExc_RuntimeError,
"Operation not supported - recompile against CUPS 1.4");
return NULL;
#else /* CUPS 1.3 */
#if CUPS_VERSION_MAJOR >= 1 && CUPS_VERSION_MINOR >= 4
PyObject *dict;
PyObject *obj;
PyObject *uriobj;
Expand All @@ -675,7 +677,6 @@ Connection_getDocument (Connection *self, PyObject *args)
const char *format = NULL;
const char *name = NULL;
char docfilename[PATH_MAX];
const size_t tmplen = strlen (_PATH_TMP);
int fd;

if (!PyArg_ParseTuple (args, "Oii", &uriobj, &jobid, &docnum))
Expand All @@ -694,8 +695,7 @@ Connection_getDocument (Connection *self, PyObject *args)
ippAddInteger (request, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
"document-number", docnum);

strcpy (docfilename, _PATH_TMP);
strcpy (docfilename + tmplen, "jobdoc-XXXXXX");
snprintf(docfilename, "%s/jobdoc-XXXXX", _PATH_TMP);
fd = mkstemp (docfilename);
if (fd < 0) {
PyErr_SetFromErrno (PyExc_RuntimeError);
Expand Down Expand Up @@ -752,7 +752,11 @@ Connection_getDocument (Connection *self, PyObject *args)
name ? name : "(nul)");
ippDelete (answer);
return dict;
#endif /* CUPS 1.3 */
#else /* earlier than CUPS 1.4 */
PyErr_SetString (PyExc_RuntimeError,
"Operation not supported - recompile against CUPS 1.4 or later");
return NULL;
#endif /* CUPS 1.4 */
}

static PyObject *
Expand Down Expand Up @@ -1382,14 +1386,12 @@ Connection_addPrinter (Connection *self, PyObject *args, PyObject *kwds)

if (ppd) {
// We've been given a cups.PPD object. Construct a PPD file.
const char *template = "scp-ppd-XXXXXX";
size_t len = strlen (_PATH_TMP) + strlen (template) + 1;
char *p;
char template[PATH_MAX];
int fd;
PyObject *args, *result;
ppdfile = malloc (len);
p = stpcpy (ppdfile, _PATH_TMP);
strcpy (p, template);

snprintf(template, sizeof (template), "%s/scp-ppd-XXXXXX", _PATH_TMP);
ppdfile = strdup(template);
fd = mkstemp (ppdfile);
if (fd < 0) {
PyErr_SetFromErrno (PyExc_RuntimeError);
Expand Down Expand Up @@ -1952,20 +1954,22 @@ Connection_setPrinterUsersDenied (Connection *self, PyObject *args)
static char *
PyObject_to_string (PyObject *pyvalue)
{
char string[BUFSIZ];
char *value = "{unknown type}";

if (PyString_Check (pyvalue) ||
PyUnicode_Check (pyvalue)) {
value = PyString_AsString (pyvalue);
} else if (PyBool_Check (pyvalue)) {
value = (pyvalue == Py_True) ? "true" : "false";
} else if (PyInt_Check (pyvalue)) {
long v = PyInt_AsLong (pyvalue);
value = alloca (20);
snprintf (value, 20, "%ld", v);
snprintf (string, sizeof (string), "%ld", v);
value = string;
} else if (PyFloat_Check (pyvalue)) {
double v = PyFloat_AsDouble (pyvalue);
value = alloca (100);
snprintf (value, 100, "%f", v);
snprintf (string, sizeof (string), "%f", v);
value = string;
}

return strdup (value);
Expand Down
2 changes: 1 addition & 1 deletion cupsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ initcups (void)
STR_CONSTANT (CUPS_SERVER_REMOTE_PRINTERS);
STR_CONSTANT (CUPS_SERVER_SHARE_PRINTERS);
STR_CONSTANT (CUPS_SERVER_USER_CANCEL_ANY);
#ifdef HAVE_CUPS_1_3
#if CUPS_VERSION_MAJOR >= 1 && CUPS_VERSION_MINOR >= 3
STR_CONSTANT (CUPS_SERVER_REMOTE_ANY);
#endif /* CUPS 1.3 */

Expand Down
24 changes: 23 additions & 1 deletion cupsppd.c
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,27 @@ PPD_nondefaultsMarked (PPD *self)
return PyBool_FromLong (nondefaults_marked);
}

#ifdef __SVR4
/*
* A rudimentary emulation of getline() for systems that dont support it
* natively. Since this is used for PPD file reading, it assumes (possibly
* falsely) that BUFSIZ is big enough.
*/
ssize_t
getline(char **line, size_t *linelen, FILE *fp)
{
if (*linelen == 0) {
*linelen = BUFSIZ;
*line = malloc(*linelen);
}

memset(*line, 0, *linelen);
fgets(*line, *linelen, fp);

return (strlen(*line));
}
#endif

PyObject *
PPD_writeFd (PPD *self, PyObject *args)
{
Expand Down Expand Up @@ -489,7 +510,8 @@ PPD_writeFd (PPD *self, PyObject *args)
for (end = start; *end; end++)
if (isspace (*end) || *end == ':')
break;
keyword = strndup (start, end-start);
keyword = calloc(1, (end - start) + 1);
strncpy(keyword, start, end - start);
choice = ppdFindMarkedChoice (self->ppd, keyword);

// Treat PageRegion, PaperDimension and ImageableArea specially:
Expand Down

0 comments on commit 9f399f2

Please sign in to comment.