f5608b9
diff -up cups-1.3.9/berkeley/lpr.c.getnameddest cups-1.3.9/berkeley/lpr.c
f5608b9
--- cups-1.3.9/berkeley/lpr.c.getnameddest	2008-10-10 09:35:05.000000000 +0100
f5608b9
+++ cups-1.3.9/berkeley/lpr.c	2008-10-10 09:35:05.000000000 +0100
34fc0f7
@@ -92,9 +92,7 @@ main(int  argc,				/* I - Number of comm
34fc0f7
   int		num_copies;		/* Number of copies per file */
34fc0f7
   int		num_files;		/* Number of files to print */
34fc0f7
   const char	*files[1000];		/* Files to print */
34fc0f7
-  int		num_dests;		/* Number of destinations */
34fc0f7
-  cups_dest_t	*dests,			/* Destinations */
34fc0f7
-		*dest;			/* Selected destination */
34fc0f7
+  cups_dest_t	*dest;			/* Selected destination */
34fc0f7
   int		num_options;		/* Number of options */
34fc0f7
   cups_option_t	*options;		/* Options */
34fc0f7
   int		deletefile;		/* Delete file after print? */
34fc0f7
@@ -112,8 +110,7 @@ main(int  argc,				/* I - Number of comm
34fc0f7
 
34fc0f7
   deletefile  = 0;
34fc0f7
   printer     = NULL;
34fc0f7
-  num_dests   = 0;
34fc0f7
-  dests       = NULL;
34fc0f7
+  dest        = NULL;
34fc0f7
   num_options = 0;
34fc0f7
   options     = NULL;
34fc0f7
   num_files   = 0;
34fc0f7
@@ -282,10 +279,7 @@ main(int  argc,				/* I - Number of comm
34fc0f7
             if ((instance = strrchr(printer, '/')) != NULL)
34fc0f7
 	      *instance++ = '\0';
34fc0f7
 
34fc0f7
-	    if (num_dests == 0)
34fc0f7
-	      num_dests = cupsGetDests(&dests);
34fc0f7
-
34fc0f7
-            if ((dest = cupsGetDest(printer, instance, num_dests, dests)) != NULL)
34fc0f7
+            if ((dest = cupsGetNamedDest(NULL, printer, instance)) != NULL)
34fc0f7
 	    {
34fc0f7
 	      for (j = 0; j < dest->num_options; j ++)
34fc0f7
 	        if (cupsGetOption(dest->options[j].name, num_options,
34fc0f7
@@ -385,10 +379,7 @@ main(int  argc,				/* I - Number of comm
34fc0f7
 
34fc0f7
   if (printer == NULL)
34fc0f7
   {
34fc0f7
-    if (num_dests == 0)
34fc0f7
-      num_dests = cupsGetDests(&dests);
34fc0f7
-
34fc0f7
-    if ((dest = cupsGetDest(NULL, NULL, num_dests, dests)) != NULL)
34fc0f7
+    if ((dest = cupsGetNamedDest(NULL, NULL, NULL)) != NULL)
34fc0f7
     {
34fc0f7
       printer = dest->name;
34fc0f7
 
34fc0f7
@@ -417,7 +408,7 @@ main(int  argc,				/* I - Number of comm
34fc0f7
     else
34fc0f7
       val = "LPDEST";
34fc0f7
 
34fc0f7
-    if (printer && !cupsGetDest(printer, NULL, num_dests, dests))
34fc0f7
+    if (printer && !cupsGetNamedDest(NULL, printer, NULL))
34fc0f7
       _cupsLangPrintf(stderr,
34fc0f7
                       _("%s: Error - %s environment variable names "
34fc0f7
 		        "non-existent destination \"%s\"!\n"),
f5608b9
diff -up cups-1.3.9/cups/cups.h.getnameddest cups-1.3.9/cups/cups.h
f5608b9
--- cups-1.3.9/cups/cups.h.getnameddest	2008-07-23 01:06:46.000000000 +0100
f5608b9
+++ cups-1.3.9/cups/cups.h	2008-10-10 09:35:05.000000000 +0100
34fc0f7
@@ -248,6 +248,9 @@ extern void		cupsSetDefaultDest(const ch
34fc0f7
 					   int num_dests,
34fc0f7
 					   cups_dest_t *dests);
34fc0f7
 
34fc0f7
+/**** New in CUPS 1.4 ****/
34fc0f7
+extern cups_dest_t	*cupsGetNamedDest(http_t *http, const char *name,
34fc0f7
+			                  const char *instance);
34fc0f7
 
34fc0f7
 #  ifdef __cplusplus
34fc0f7
 }
f5608b9
diff -up cups-1.3.9/cups/dest.c.getnameddest cups-1.3.9/cups/dest.c
f5608b9
--- cups-1.3.9/cups/dest.c.getnameddest	2008-09-17 00:37:56.000000000 +0100
f5608b9
+++ cups-1.3.9/cups/dest.c	2008-10-10 09:36:02.000000000 +0100
fec5518
@@ -25,6 +25,7 @@
fec5518
  *                              server.
fec5518
  *   cupsGetDests2()          - Get the list of destinations from the
fec5518
  *                              specified server.
fec5518
+ *   cupsGetNamedDest()       - Get options for the named destination.
fec5518
  *   cupsRemoveDest()         - Remove a destination from the destination list.
fec5518
  *   cupsDestSetDefaultDest() - Set the default destination.
fec5518
  *   cupsSetDests()           - Set the list of destinations for the default
fec5518
@@ -39,6 +40,7 @@
fec5518
  * Include necessary headers...
fec5518
  */
fec5518
 
fec5518
+#include "debug.h"
fec5518
 #include "globals.h"
fec5518
 #include <stdlib.h>
fec5518
 #include <ctype.h>
fec5518
@@ -53,10 +55,13 @@
fec5518
  * Local functions...
fec5518
  */
fec5518
 
fec5518
-static int	cups_get_dests(const char *filename, int num_dests,
fec5518
+static const char *cups_get_default(const char *filename, char *namebuf,
fec5518
+				    size_t namesize, const char **instance);
fec5518
+static int	cups_get_dests(const char *filename, const char *match_name,
fec5518
+		               const char *match_inst, int num_dests,
fec5518
 		               cups_dest_t **dests);
fec5518
-static int	cups_get_sdests(http_t *http, ipp_op_t op, int num_dests,
fec5518
-		                cups_dest_t **dests);
fec5518
+static int	cups_get_sdests(http_t *http, ipp_op_t op, const char *name,
fec5518
+		                int num_dests, cups_dest_t **dests);
fec5518
 
fec5518
 
fec5518
 /*
fec5518
@@ -263,19 +268,17 @@ int					/* O - Number of destinations */
fec5518
 cupsGetDests(cups_dest_t **dests)	/* O - Destinations */
fec5518
 {
fec5518
   int		num_dests;		/* Number of destinations */
fec5518
-  http_t	*http;			/* HTTP connection */
fec5518
+  _cups_globals_t *cg = _cupsGlobals();	/* Pointer to library globals */
fec5518
 
fec5518
 
fec5518
  /*
fec5518
   * Connect to the CUPS server and get the destination list and options...
fec5518
   */
fec5518
 
fec5518
-  http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
fec5518
+  if (!cg->http)
fec5518
+    cg->http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
fec5518
 
fec5518
-  num_dests = cupsGetDests2(http, dests);
fec5518
-
fec5518
-  if (http)
fec5518
-    httpClose(http);
fec5518
+  num_dests = cupsGetDests2(cg->http, dests);
fec5518
 
fec5518
   return (num_dests);
fec5518
 }
f5608b9
@@ -330,9 +333,9 @@ cupsGetDests2(http_t      *http,	/* I - 
fec5518
   * Grab the printers and classes...
fec5518
   */
fec5518
 
fec5518
-  num_dests = cups_get_sdests(http, CUPS_GET_PRINTERS, num_dests, dests);
fec5518
+  num_dests = cups_get_sdests(http, CUPS_GET_PRINTERS, NULL, num_dests, dests);
f5608b9
   if (cupsLastError() < IPP_REDIRECTION_OTHER_SITE)
f5608b9
-    num_dests = cups_get_sdests(http, CUPS_GET_CLASSES, num_dests, dests);
f5608b9
+    num_dests = cups_get_sdests(http, CUPS_GET_CLASSES, NULL, num_dests, dests);
fec5518
 
f5608b9
   if (cupsLastError() >= IPP_REDIRECTION_OTHER_SITE)
f5608b9
   {
f5608b9
@@ -398,7 +401,7 @@ cupsGetDests2(http_t      *http,	/* I - 
fec5518
   */
fec5518
 
fec5518
   snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot);
fec5518
-  num_dests = cups_get_dests(filename, num_dests, dests);
fec5518
+  num_dests = cups_get_dests(filename, NULL, NULL, num_dests, dests);
fec5518
 
fec5518
   if ((home = getenv("HOME")) != NULL)
fec5518
   {
f5608b9
@@ -406,7 +409,7 @@ cupsGetDests2(http_t      *http,	/* I - 
fec5518
     if (access(filename, 0))
fec5518
       snprintf(filename, sizeof(filename), "%s/.lpoptions", home);
fec5518
 
fec5518
-    num_dests = cups_get_dests(filename, num_dests, dests);
fec5518
+    num_dests = cups_get_dests(filename, NULL, NULL, num_dests, dests);
fec5518
   }
fec5518
 
fec5518
  /*
f5608b9
@@ -460,6 +463,136 @@ cupsGetDests2(http_t      *http,	/* I - 
fec5518
 
fec5518
 
fec5518
 /*
fec5518
+ * 'cupsGetNamedDest()' - Get options for the named destination.
fec5518
+ *
fec5518
+ * This function is optimized for retrieving a single destination and should
fec5518
+ * be used instead of cupsGetDests() and cupsGetDest() when you either know
fec5518
+ * the name of the destination or want to print to the default destination.
fec5518
+ * If NULL is returned, the destination does not exist or there is no default
fec5518
+ * destination.
fec5518
+ *
fec5518
+ * If "http" is NULL, the connection to the default print server will be used.
fec5518
+ *
fec5518
+ * If "name" is NULL, the default printer for the current user will be returned.
fec5518
+ *
fec5518
+ * The returned destination must be freed using cupsFreeDests() with a
fec5518
+ * "num_dests" of 1.
fec5518
+ *
fec5518
+ * @since CUPS 1.4@
fec5518
+ */
fec5518
+
fec5518
+cups_dest_t *				/* O - Destination or NULL */
fec5518
+cupsGetNamedDest(http_t     *http,	/* I - HTTP connection or NULL */
fec5518
+                 const char *name,	/* I - Destination name or NULL */
fec5518
+                 const char *instance)	/* I - Instance name or NULL */
fec5518
+{
fec5518
+  cups_dest_t	*dest;			/* Destination */
fec5518
+  char		filename[1024],		/* Path to lpoptions */
fec5518
+		defname[256];		/* Default printer name */
fec5518
+  const char	*home = getenv("HOME");	/* Home directory */
fec5518
+  ipp_op_t	op = IPP_GET_PRINTER_ATTRIBUTES;
fec5518
+					/* IPP operation to get server ops */
fec5518
+  _cups_globals_t *cg = _cupsGlobals();	/* Pointer to library globals */
fec5518
+
fec5518
+
fec5518
+ /*
fec5518
+  * Connect to the server as needed...
fec5518
+  */
fec5518
+
fec5518
+  if (!http)
fec5518
+  {
fec5518
+    if (!cg->http &&
fec5518
+        (cg->http = httpConnectEncrypt(cupsServer(), ippPort(),
fec5518
+                                       cupsEncryption())) == NULL)
fec5518
+      return (NULL);
fec5518
+
fec5518
+    http = cg->http;
fec5518
+  }
fec5518
+
fec5518
+ /*
fec5518
+  * If "name" is NULL, find the default destination...
fec5518
+  */
fec5518
+
fec5518
+  if (!name)
fec5518
+  {
fec5518
+    if ((name = getenv("LPDEST")) == NULL)
fec5518
+      if ((name = getenv("PRINTER")) != NULL && !strcmp(name, "lp"))
fec5518
+        name = NULL;
fec5518
+
fec5518
+    if (!name && home)
fec5518
+    {
fec5518
+     /*
fec5518
+      * No default in the environment, try the user's lpoptions files...
fec5518
+      */
fec5518
+
fec5518
+      snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home);
fec5518
+
fec5518
+      if ((name = cups_get_default(filename, defname, sizeof(defname),
fec5518
+				   &instance)) == NULL)
fec5518
+      {
fec5518
+	snprintf(filename, sizeof(filename), "%s/.lpoptions", home);
fec5518
+	name = cups_get_default(filename, defname, sizeof(defname),
fec5518
+				&instance);
fec5518
+      }
fec5518
+    }
fec5518
+
fec5518
+    if (!name)
fec5518
+    {
fec5518
+     /*
fec5518
+      * Still not there?  Try the system lpoptions file...
fec5518
+      */
fec5518
+
fec5518
+      snprintf(filename, sizeof(filename), "%s/lpoptions",
fec5518
+	       cg->cups_serverroot);
fec5518
+      name = cups_get_default(filename, defname, sizeof(defname), &instance);
fec5518
+    }
fec5518
+
fec5518
+    if (!name)
fec5518
+    {
fec5518
+     /*
fec5518
+      * No locally-set default destination, ask the server...
fec5518
+      */
fec5518
+
fec5518
+      op = CUPS_GET_DEFAULT;
fec5518
+    }
fec5518
+  }
fec5518
+
fec5518
+ /*
fec5518
+  * Get the printer's attributes...
fec5518
+  */
fec5518
+
fec5518
+  if (!cups_get_sdests(http, op, name, 0, &dest))
fec5518
+    return (NULL);
fec5518
+
fec5518
+  if (instance)
fec5518
+    dest->instance = _cupsStrAlloc(instance);
fec5518
+
fec5518
+ /*
fec5518
+  * Then add local options...
fec5518
+  */
fec5518
+
fec5518
+  snprintf(filename, sizeof(filename), "%s/lpoptions", cg->cups_serverroot);
fec5518
+  cups_get_dests(filename, name, instance, 1, &dest);
fec5518
+
fec5518
+  if (home)
fec5518
+  {
fec5518
+    snprintf(filename, sizeof(filename), "%s/.cups/lpoptions", home);
fec5518
+
fec5518
+    if (access(filename, 0))
fec5518
+      snprintf(filename, sizeof(filename), "%s/.lpoptions", home);
fec5518
+
fec5518
+    cups_get_dests(filename, name, instance, 1, &dest);
fec5518
+  }
fec5518
+
fec5518
+ /*
fec5518
+  * Return the result...
fec5518
+  */
fec5518
+
fec5518
+  return (dest);
fec5518
+}
fec5518
+
fec5518
+
fec5518
+/*
fec5518
  * 'cupsRemoveDest()' - Remove a destination from the destination list.
fec5518
  *
fec5518
  * Removing a destination/instance does not delete the class or printer
f5608b9
@@ -556,19 +689,17 @@ void
fec5518
 cupsSetDests(int         num_dests,	/* I - Number of destinations */
fec5518
              cups_dest_t *dests)	/* I - Destinations */
fec5518
 {
fec5518
-  http_t	*http;			/* HTTP connection */
fec5518
+  _cups_globals_t *cg = _cupsGlobals();	/* Pointer to library globals */
fec5518
 
fec5518
 
fec5518
  /*
fec5518
   * Connect to the CUPS server and save the destination list and options...
fec5518
   */
fec5518
 
fec5518
-  http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
fec5518
-
fec5518
-  cupsSetDests2(http, num_dests, dests);
fec5518
+  if (!cg->http)
fec5518
+    cg->http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
fec5518
 
fec5518
-  if (http)
fec5518
-    httpClose(http);
fec5518
+  cupsSetDests2(cg->http, num_dests, dests);
fec5518
 }
fec5518
 
fec5518
 
f5608b9
@@ -614,8 +745,8 @@ cupsSetDests2(http_t      *http,	/* I - 
fec5518
   * Get the server destinations...
fec5518
   */
fec5518
 
fec5518
-  num_temps = cups_get_sdests(http, CUPS_GET_PRINTERS, 0, &temps);
fec5518
-  num_temps = cups_get_sdests(http, CUPS_GET_CLASSES, num_temps, &temps);
fec5518
+  num_temps = cups_get_sdests(http, CUPS_GET_PRINTERS, NULL, 0, &temps);
fec5518
+  num_temps = cups_get_sdests(http, CUPS_GET_CLASSES, NULL, num_temps, &temps);
fec5518
 
fec5518
  /*
fec5518
   * Figure out which file to write to...
f5608b9
@@ -630,7 +761,7 @@ cupsSetDests2(http_t      *http,	/* I - 
fec5518
     * Merge in server defaults...
fec5518
     */
fec5518
 
fec5518
-    num_temps = cups_get_dests(filename, num_temps, &temps);
fec5518
+    num_temps = cups_get_dests(filename, NULL, NULL, num_temps, &temps);
fec5518
 
fec5518
    /*
fec5518
     * Point to user defaults...
f5608b9
@@ -797,24 +928,88 @@ cupsSetDests2(http_t      *http,	/* I - 
fec5518
 
fec5518
 
fec5518
 /*
fec5518
+ * 'cups_get_default()' - Get the default destination from an lpoptions file.
fec5518
+ */
fec5518
+
fec5518
+static const char *			/* O - Default destination or NULL */
fec5518
+cups_get_default(const char *filename,	/* I - File to read */
fec5518
+                 char       *namebuf,	/* I - Name buffer */
fec5518
+		 size_t     namesize,	/* I - Size of name buffer */
fec5518
+		 const char **instance)	/* I - Instance */
fec5518
+{
fec5518
+  cups_file_t	*fp;			/* lpoptions file */
fec5518
+  char		line[8192],		/* Line from file */
fec5518
+		*value,			/* Value for line */
fec5518
+		*nameptr;		/* Pointer into name */
fec5518
+  int		linenum;		/* Current line */  
fec5518
+
fec5518
+
fec5518
+  *namebuf = '\0';
fec5518
+
fec5518
+  if ((fp = cupsFileOpen(filename, "r")) != NULL)
fec5518
+  {
fec5518
+    linenum  = 0;
fec5518
+
fec5518
+    while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
fec5518
+    {
fec5518
+      if (!strcasecmp(line, "default") && value)
fec5518
+      {
fec5518
+        strlcpy(namebuf, value, namesize);
fec5518
+
fec5518
+	if ((nameptr = strchr(namebuf, ' ')) != NULL)
fec5518
+	  *nameptr = '\0';
fec5518
+	if ((nameptr = strchr(namebuf, '\t')) != NULL)
fec5518
+	  *nameptr = '\0';
fec5518
+
fec5518
+	if ((nameptr = strchr(namebuf, '/')) != NULL)
fec5518
+	  *nameptr++ = '\0';
fec5518
+
fec5518
+        *instance = nameptr;
fec5518
+	break;
fec5518
+      }
fec5518
+    }
fec5518
+
fec5518
+    cupsFileClose(fp);
fec5518
+  }
fec5518
+
fec5518
+  return (*namebuf ? namebuf : NULL);
fec5518
+}
fec5518
+
fec5518
+
fec5518
+/*
fec5518
  * 'cups_get_dests()' - Get destinations from a file.
fec5518
  */
fec5518
 
fec5518
 static int				/* O - Number of destinations */
fec5518
 cups_get_dests(const char  *filename,	/* I - File to read from */
fec5518
+               const char  *match_name,	/* I - Destination name we want */
fec5518
+	       const char  *match_inst,	/* I - Instance name we want */
fec5518
                int         num_dests,	/* I - Number of destinations */
fec5518
                cups_dest_t **dests)	/* IO - Destinations */
fec5518
 {
fec5518
   int		i;			/* Looping var */
fec5518
   cups_dest_t	*dest;			/* Current destination */
fec5518
-  FILE		*fp;			/* File pointer */
fec5518
+  cups_file_t	*fp;			/* File pointer */
fec5518
   char		line[8192],		/* Line from file */
fec5518
 		*lineptr,		/* Pointer into line */
fec5518
 		*name,			/* Name of destination/option */
fec5518
 		*instance;		/* Instance of destination */
fec5518
+  int		linenum;		/* Current line number */
fec5518
   const char	*printer;		/* PRINTER or LPDEST */
fec5518
 
fec5518
 
fec5518
+  DEBUG_printf(("cups_get_dests(filename=\"%s\", match_name=\"%s\", "
fec5518
+                "match_inst=\"%s\", num_dests=%d, dests=%p)\n", filename,
fec5518
+		match_name ? match_name : "(null)",
fec5518
+		match_inst ? match_inst : "(null)", num_dests, dests));
fec5518
+
fec5518
+ /*
fec5518
+  * Try to open the file...
fec5518
+  */
fec5518
+
fec5518
+  if ((fp = cupsFileOpen(filename, "r")) == NULL)
fec5518
+    return (num_dests);
fec5518
+
fec5518
  /*
fec5518
   * Check environment variables...
fec5518
   */
f5608b9
@@ -824,12 +1019,8 @@ cups_get_dests(const char  *filename,	/*
fec5518
       if (strcmp(printer, "lp") == 0)
fec5518
         printer = NULL;
fec5518
 
fec5518
- /*
fec5518
-  * Try to open the file...
fec5518
-  */
fec5518
-
fec5518
-  if ((fp = fopen(filename, "r")) == NULL)
fec5518
-    return (num_dests);
fec5518
+  DEBUG_printf(("cups_get_dests: printer=\"%s\"\n",
fec5518
+                printer ? printer : "(null)"));
fec5518
 
fec5518
  /*
fec5518
   * Read each printer; each line looks like:
f5608b9
@@ -838,28 +1029,22 @@ cups_get_dests(const char  *filename,	/*
fec5518
   *    Default name[/instance] options
fec5518
   */
fec5518
 
fec5518
-  while (fgets(line, sizeof(line), fp) != NULL)
fec5518
+  linenum = 0;
fec5518
+
fec5518
+  while (cupsFileGetConf(fp, line, sizeof(line), &lineptr, &linenum))
fec5518
   {
fec5518
    /*
fec5518
     * See what type of line it is...
fec5518
     */
fec5518
 
fec5518
-    if (strncasecmp(line, "dest", 4) == 0 && isspace(line[4] & 255))
fec5518
-      lineptr = line + 4;
fec5518
-    else if (strncasecmp(line, "default", 7) == 0 && isspace(line[7] & 255))
fec5518
-      lineptr = line + 7;
fec5518
-    else
fec5518
-      continue;
fec5518
-
fec5518
-   /*
fec5518
-    * Skip leading whitespace...
fec5518
-    */
fec5518
-
fec5518
-    while (isspace(*lineptr & 255))
fec5518
-      lineptr ++;
fec5518
+    DEBUG_printf(("cups_get_dests: linenum=%d line=\"%s\" lineptr=\"%s\"\n",
fec5518
+                  linenum, line, lineptr ? lineptr : "(null)"));
fec5518
 
fec5518
-    if (!*lineptr)
fec5518
+    if ((strcasecmp(line, "dest") && strcasecmp(line, "default")) || !lineptr)
fec5518
+    {
fec5518
+      DEBUG_puts("cups_get_dests: Not a dest or default line...");
fec5518
       continue;
fec5518
+    }
fec5518
 
fec5518
     name = lineptr;
fec5518
 
f5608b9
@@ -870,9 +1055,6 @@ cups_get_dests(const char  *filename,	/*
fec5518
     while (!isspace(*lineptr & 255) && *lineptr && *lineptr != '/')
fec5518
       lineptr ++;
fec5518
 
fec5518
-    if (!*lineptr)
fec5518
-      continue;
fec5518
-
fec5518
     if (*lineptr == '/')
fec5518
     {
fec5518
      /*
f5608b9
@@ -892,30 +1074,49 @@ cups_get_dests(const char  *filename,	/*
fec5518
     else
fec5518
       instance = NULL;
fec5518
 
fec5518
-    *lineptr++ = '\0';
fec5518
+    if (*lineptr)
fec5518
+      *lineptr++ = '\0';
fec5518
+
fec5518
+    DEBUG_printf(("cups_get_dests: name=\"%s\", instance=\"%s\"\n", name,
fec5518
+                  instance));
fec5518
 
fec5518
    /*
fec5518
     * See if the primary instance of the destination exists; if not,
fec5518
     * ignore this entry and move on...
fec5518
     */
fec5518
 
fec5518
-    if (cupsGetDest(name, NULL, num_dests, *dests) == NULL)
fec5518
-      continue;
fec5518
-
fec5518
-   /*
fec5518
-    * Add the destination...
fec5518
-    */
fec5518
-
fec5518
-    num_dests = cupsAddDest(name, instance, num_dests, dests);
fec5518
+    if (match_name)
fec5518
+    {
fec5518
+      if (strcasecmp(name, match_name) ||
fec5518
+          (!instance && match_inst) ||
fec5518
+	  (instance && !match_inst) ||
fec5518
+	  (instance && strcasecmp(instance, match_inst)))
fec5518
+	continue;
fec5518
 
fec5518
-    if ((dest = cupsGetDest(name, instance, num_dests, *dests)) == NULL)
fec5518
+      dest = *dests;
fec5518
+    }
fec5518
+    else if (cupsGetDest(name, NULL, num_dests, *dests) == NULL)
fec5518
+    {
fec5518
+      DEBUG_puts("cups_get_dests: Not found!");
fec5518
+      continue;
fec5518
+    }
fec5518
+    else
fec5518
     {
fec5518
      /*
fec5518
-      * Out of memory!
fec5518
+      * Add the destination...
fec5518
       */
fec5518
 
fec5518
-      fclose(fp);
fec5518
-      return (num_dests);
fec5518
+      num_dests = cupsAddDest(name, instance, num_dests, dests);
fec5518
+
fec5518
+      if ((dest = cupsGetDest(name, instance, num_dests, *dests)) == NULL)
fec5518
+      {
fec5518
+       /*
fec5518
+	* Out of memory!
fec5518
+	*/
fec5518
+
fec5518
+        DEBUG_puts("cups_get_dests: Out of memory!");
fec5518
+        break;
fec5518
+      }
fec5518
     }
fec5518
 
fec5518
    /*
f5608b9
@@ -926,11 +1127,20 @@ cups_get_dests(const char  *filename,	/*
fec5518
                                          &(dest->options));
fec5518
 
fec5518
    /*
fec5518
+    * If we found what we were looking for, stop now...
fec5518
+    */
fec5518
+
fec5518
+    if (match_name)
fec5518
+      break;
fec5518
+
fec5518
+   /*
fec5518
     * Set this as default if needed...
fec5518
     */
fec5518
 
fec5518
-    if (strncasecmp(line, "default", 7) == 0 && printer == NULL)
fec5518
+    if (!printer && !strcasecmp(line, "default"))
fec5518
     {
fec5518
+      DEBUG_puts("cups_get_dests: Setting as default...");
fec5518
+
fec5518
       for (i = 0; i < num_dests; i ++)
fec5518
         (*dests)[i].is_default = 0;
fec5518
 
f5608b9
@@ -942,7 +1152,7 @@ cups_get_dests(const char  *filename,	/*
fec5518
   * Close the file and return...
fec5518
   */
fec5518
 
fec5518
-  fclose(fp);      
fec5518
+  cupsFileClose(fp);      
fec5518
 
fec5518
   return (num_dests);
fec5518
 }
f5608b9
@@ -954,7 +1164,8 @@ cups_get_dests(const char  *filename,	/*
fec5518
 
fec5518
 static int				/* O - Number of destinations */
fec5518
 cups_get_sdests(http_t      *http,	/* I - HTTP connection */
fec5518
-                ipp_op_t    op,		/* I - get-printers or get-classes */
fec5518
+                ipp_op_t    op,		/* I - IPP operation */
fec5518
+		const char  *name,	/* I - Name of destination */
fec5518
                 int         num_dests,	/* I - Number of destinations */
fec5518
                 cups_dest_t **dests)	/* IO - Destinations */
fec5518
 {
f5608b9
@@ -971,8 +1182,9 @@ cups_get_sdests(http_t      *http,	/* I 
fec5518
   const char	*info,			/* printer-info attribute */
fec5518
 		*location,		/* printer-location attribute */
fec5518
 		*make_model,		/* printer-make-and-model attribute */
fec5518
-		*name;			/* printer-name attribute */
fec5518
-  char		job_sheets[1024],	/* job-sheets-default attribute */
fec5518
+		*printer_name;		/* printer-name attribute */
fec5518
+  char		uri[1024],		/* printer-uri value */
fec5518
+		job_sheets[1024],	/* job-sheets-default attribute */
fec5518
 		auth_info_req[1024],	/* auth-info-required attribute */
fec5518
 		reasons[1024];		/* printer-state-reasons attribute */
fec5518
   int		num_options;		/* Number of options */
f5608b9
@@ -1016,6 +1228,14 @@ cups_get_sdests(http_t      *http,	/* I 
fec5518
   ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
fec5518
                "requesting-user-name", NULL, cupsUser());
fec5518
 
fec5518
+  if (name)
fec5518
+  {
fec5518
+    httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
fec5518
+                     "localhost", ippPort(), "/printers/%s", name);
fec5518
+    ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
fec5518
+                 uri);
fec5518
+  }
fec5518
+
fec5518
  /*
fec5518
   * Do the request and get back a response...
fec5518
   */
f5608b9
@@ -1038,17 +1258,17 @@ cups_get_sdests(http_t      *http,	/* I 
fec5518
       * Pull the needed attributes from this printer...
fec5518
       */
fec5518
 
fec5518
-      accepting   = 0;
fec5518
-      change_time = 0;
fec5518
-      info        = NULL;
fec5518
-      location    = NULL;
fec5518
-      make_model  = NULL;
fec5518
-      name        = NULL;
fec5518
-      num_options = 0;
fec5518
-      options     = NULL;
fec5518
-      shared      = 1;
fec5518
-      state       = IPP_PRINTER_IDLE;
fec5518
-      type        = CUPS_PRINTER_LOCAL;
fec5518
+      accepting    = 0;
fec5518
+      change_time  = 0;
fec5518
+      info         = NULL;
fec5518
+      location     = NULL;
fec5518
+      make_model   = NULL;
fec5518
+      printer_name = NULL;
fec5518
+      num_options  = 0;
fec5518
+      options      = NULL;
fec5518
+      shared       = 1;
fec5518
+      state        = IPP_PRINTER_IDLE;
fec5518
+      type         = CUPS_PRINTER_LOCAL;
fec5518
 
fec5518
       auth_info_req[0] = '\0';
fec5518
       job_sheets[0]    = '\0';
f5608b9
@@ -1099,7 +1319,7 @@ cups_get_sdests(http_t      *http,	/* I 
fec5518
 	  make_model = attr->values[0].string.text;
fec5518
         else if (!strcmp(attr->name, "printer-name") &&
fec5518
 	         attr->value_tag == IPP_TAG_NAME)
fec5518
-	  name = attr->values[0].string.text;
fec5518
+	  printer_name = attr->values[0].string.text;
fec5518
 	else if (!strcmp(attr->name, "printer-state") &&
fec5518
 	         attr->value_tag == IPP_TAG_ENUM)
fec5518
           state = attr->values[0].integer;
f5608b9
@@ -1204,7 +1424,7 @@ cups_get_sdests(http_t      *http,	/* I 
fec5518
       * See if we have everything needed...
fec5518
       */
fec5518
 
fec5518
-      if (!name)
fec5518
+      if (!printer_name)
fec5518
       {
fec5518
         cupsFreeOptions(num_options, options);
fec5518
 
f5608b9
@@ -1214,9 +1434,9 @@ cups_get_sdests(http_t      *http,	/* I 
fec5518
           continue;
fec5518
       }
fec5518
 
fec5518
-      num_dests = cupsAddDest(name, NULL, num_dests, dests);
fec5518
+      num_dests = cupsAddDest(printer_name, NULL, num_dests, dests);
fec5518
 
fec5518
-      if ((dest = cupsGetDest(name, NULL, num_dests, *dests)) != NULL)
fec5518
+      if ((dest = cupsGetDest(printer_name, NULL, num_dests, *dests)) != NULL)
fec5518
       {
fec5518
         dest->num_options = num_options;
fec5518
 	dest->options     = options;
f5608b9
diff -up cups-1.3.9/cups/libcups.exp.getnameddest cups-1.3.9/cups/libcups.exp
f5608b9
--- cups-1.3.9/cups/libcups.exp.getnameddest	2008-04-09 04:39:40.000000000 +0100
f5608b9
+++ cups-1.3.9/cups/libcups.exp	2008-10-10 09:35:05.000000000 +0100
34fc0f7
@@ -114,6 +114,7 @@ _cupsGetFd
34fc0f7
 _cupsGetFile
34fc0f7
 _cupsGetJobs
34fc0f7
 _cupsGetJobs2
34fc0f7
+_cupsGetNamedDest
34fc0f7
 _cupsGetOption
34fc0f7
 _cupsGetPassword
34fc0f7
 _cupsGetPPD
f5608b9
diff -up cups-1.3.9/cups/Makefile.getnameddest cups-1.3.9/cups/Makefile
f5608b9
--- cups-1.3.9/cups/Makefile.getnameddest	2008-09-06 01:30:39.000000000 +0100
f5608b9
+++ cups-1.3.9/cups/Makefile	2008-10-10 09:35:05.000000000 +0100
fec5518
@@ -263,7 +263,7 @@ libcups.so.2 libcups.sl.2:	$(LIBOBJS)
fec5518
 # libcups.2.dylib
fec5518
 #
fec5518
 
fec5518
-libcups.2.dylib:	$(LIBOBJS) $(LIBCUPSORDER)
fec5518
+libcups.2.dylib:	$(LIBOBJS) $(LIBCUPSORDER) libcups.exp
fec5518
 	echo Linking $@...
fec5518
 	$(DSO) $(ARCHFLAGS) $(DSOFLAGS) -o $@ \
fec5518
 		-install_name $(libdir)/$@ \
f5608b9
diff -up cups-1.3.9/cups/testcups.c.getnameddest cups-1.3.9/cups/testcups.c
f5608b9
--- cups-1.3.9/cups/testcups.c.getnameddest	2008-07-11 23:48:49.000000000 +0100
f5608b9
+++ cups-1.3.9/cups/testcups.c	2008-10-10 09:35:05.000000000 +0100
fec5518
@@ -16,7 +16,8 @@
fec5518
  *
fec5518
  * Contents:
fec5518
  *
fec5518
- *   main() - Main entry.
fec5518
+ *   main()        - Main entry.
fec5518
+ *   dests_equal() - Determine whether two destinations are equal.
fec5518
  */
fec5518
 
fec5518
 /*
fec5518
@@ -29,6 +30,14 @@
fec5518
 
fec5518
 
fec5518
 /*
fec5518
+ * Local functions...
fec5518
+ */
fec5518
+
fec5518
+static int	dests_equal(cups_dest_t *a, cups_dest_t *b);
fec5518
+static void	show_diffs(cups_dest_t *a, cups_dest_t *b);
fec5518
+
fec5518
+
fec5518
+/*
fec5518
  * 'main()' - Main entry.
fec5518
  */
fec5518
 
fec5518
@@ -37,9 +46,11 @@ main(int  argc,				/* I - Number of comm
fec5518
      char *argv[])			/* I - Command-line arguments */
fec5518
 {
fec5518
   int		status = 0,		/* Exit status */
fec5518
+		i,			/* Looping var */
fec5518
 		num_dests;		/* Number of destinations */
fec5518
   cups_dest_t	*dests,			/* Destinations */
fec5518
-		*dest;			/* Current destination */
fec5518
+		*dest,			/* Current destination */
fec5518
+		*named_dest;		/* Current named destination */
fec5518
   const char	*ppdfile;		/* PPD file */
fec5518
   ppd_file_t	*ppd;			/* PPD file data */
fec5518
   int		num_jobs;		/* Number of jobs for queue */
fec5518
@@ -61,7 +72,78 @@ main(int  argc,				/* I - Number of comm
fec5518
     return (1);
fec5518
   }
fec5518
   else
fec5518
-    puts("PASS");
fec5518
+  {
fec5518
+    printf("PASS (%d dests)\n", num_dests);
fec5518
+
fec5518
+    for (i = num_dests, dest = dests; i > 0; i --, dest ++)
fec5518
+    {
fec5518
+      printf("    %s", dest->name);
fec5518
+
fec5518
+      if (dest->instance)
fec5518
+        printf("    /%s", dest->instance);
fec5518
+
fec5518
+      if (dest->is_default)
fec5518
+        puts(" ***DEFAULT***");
fec5518
+      else
fec5518
+        putchar('\n');
fec5518
+    }
fec5518
+  }
fec5518
+
fec5518
+ /*
fec5518
+  * cupsGetDest(NULL)
fec5518
+  */
fec5518
+
fec5518
+  fputs("cupsGetDest(NULL): ", stdout);
fec5518
+  fflush(stdout);
fec5518
+
fec5518
+  if ((dest = cupsGetDest(NULL, NULL, num_dests, dests)) == NULL)
fec5518
+  {
fec5518
+    for (i = num_dests, dest = dests; i > 0; i --, dest ++)
fec5518
+      if (dest->is_default)
fec5518
+        break;
fec5518
+
fec5518
+    if (i)
fec5518
+    {
fec5518
+      status = 1;
fec5518
+      puts("FAIL");
fec5518
+    }
fec5518
+    else
fec5518
+      puts("PASS (no default)");
fec5518
+
fec5518
+    dest = NULL;
fec5518
+  }
fec5518
+  else
fec5518
+    printf("PASS (%s)\n", dest->name);
fec5518
+
fec5518
+ /*
fec5518
+  * cupsGetNamedDest(NULL, NULL, NULL)
fec5518
+  */
fec5518
+
fec5518
+  fputs("cupsGetNamedDest(NULL, NULL, NULL): ", stdout);
fec5518
+  fflush(stdout);
fec5518
+
fec5518
+  if ((named_dest = cupsGetNamedDest(NULL, NULL, NULL)) == NULL ||
fec5518
+      !dests_equal(dest, named_dest))
fec5518
+  {
fec5518
+    if (!dest)
fec5518
+      puts("PASS (no default)");
fec5518
+    else if (named_dest)
fec5518
+    {
fec5518
+      puts("FAIL (different values)");
fec5518
+      show_diffs(dest, named_dest);
fec5518
+      status = 1;
fec5518
+    }
fec5518
+    else
fec5518
+    {
fec5518
+      puts("FAIL (no default)");
fec5518
+      status = 1;
fec5518
+    }
fec5518
+  }
fec5518
+  else
fec5518
+    printf("PASS (%s)\n", named_dest->name);
fec5518
+
fec5518
+  if (named_dest)
fec5518
+    cupsFreeDests(1, named_dest);
fec5518
 
fec5518
  /*
fec5518
   * cupsGetDest(printer)
34fc0f7
@@ -79,20 +161,33 @@ main(int  argc,				/* I - Number of comm
fec5518
     puts("PASS");
fec5518
 
fec5518
  /*
fec5518
-  * cupsGetDest(NULL)
fec5518
+  * cupsGetNamedDest(NULL, printer, instance)
fec5518
   */
fec5518
 
fec5518
-  fputs("cupsGetDest(NULL): ", stdout);
fec5518
+  printf("cupsGetNamedDest(NULL, \"%s\", \"%s\"): ", dest->name,
34fc0f7
+	 dest->instance ? dest->instance : "(null)");
fec5518
   fflush(stdout);
fec5518
 
fec5518
-  if ((dest = cupsGetDest(NULL, NULL, num_dests, dests)) == NULL)
fec5518
+  if ((named_dest = cupsGetNamedDest(NULL, dest->name,
34fc0f7
+				     dest->instance)) == NULL ||
fec5518
+      !dests_equal(dest, named_dest))
fec5518
   {
34fc0f7
-    puts("FAIL");
fec5518
+    if (named_dest)
fec5518
+    {
fec5518
+      puts("FAIL (different values)");
fec5518
+      show_diffs(dest, named_dest);
fec5518
+    }
fec5518
+    else
fec5518
+      puts("FAIL (no destination)");
fec5518
+
34fc0f7
     return (1);
fec5518
   }
fec5518
   else
fec5518
     puts("PASS");
fec5518
 
fec5518
+  if (named_dest)
fec5518
+    cupsFreeDests(1, named_dest);
fec5518
+
fec5518
  /*
fec5518
   * cupsPrintFile()
fec5518
   */
34fc0f7
@@ -169,5 +264,83 @@ main(int  argc,				/* I - Number of comm
fec5518
 
fec5518
 
fec5518
 /*
fec5518
+ * 'dests_equal()' - Determine whether two destinations are equal.
fec5518
+ */
fec5518
+
fec5518
+static int				/* O - 1 if equal, 0 if not equal */
fec5518
+dests_equal(cups_dest_t *a,		/* I - First destination */
fec5518
+            cups_dest_t *b)		/* I - Second destination */
fec5518
+{
fec5518
+  int		i;			/* Looping var */
fec5518
+  cups_option_t	*aoption;		/* Current option */
fec5518
+  const char	*bval;			/* Option value */
fec5518
+
fec5518
+
fec5518
+  if (a == b)
fec5518
+    return (1);
fec5518
+
fec5518
+  if ((!a && b) || (a && !b))
fec5518
+    return (0);
fec5518
+
fec5518
+  if (strcasecmp(a->name, b->name) ||
fec5518
+      (a->instance && !b->instance) ||
fec5518
+      (!a->instance && b->instance) ||
fec5518
+      (a->instance && strcasecmp(a->instance, b->instance)) ||
fec5518
+      a->num_options != b->num_options)
fec5518
+    return (0);
fec5518
+
fec5518
+  for (i = a->num_options, aoption = a->options; i > 0; i --, aoption ++)
fec5518
+    if ((bval = cupsGetOption(aoption->name, b->num_options,
fec5518
+                              b->options)) == NULL ||
fec5518
+        strcmp(aoption->value, bval))
fec5518
+      return (0);
fec5518
+
fec5518
+  return (1);
fec5518
+}
fec5518
+
fec5518
+
fec5518
+/*
fec5518
+ * 'show_diffs()' - Show differences between two destinations.
fec5518
+ */
fec5518
+
fec5518
+static void
fec5518
+show_diffs(cups_dest_t *a,		/* I - First destination */
fec5518
+           cups_dest_t *b)		/* I - Second destination */
fec5518
+{
fec5518
+  int		i;			/* Looping var */
fec5518
+  cups_option_t	*aoption;		/* Current option */
fec5518
+  const char	*bval;			/* Option value */
fec5518
+
fec5518
+
fec5518
+  if (!a || !b)
fec5518
+    return;
fec5518
+
fec5518
+  puts("    Item                  cupsGetDest           cupsGetNamedDest");
fec5518
+  puts("    --------------------  --------------------  --------------------");
fec5518
+
fec5518
+  if (strcasecmp(a->name, b->name))
fec5518
+    printf("    name                  %-20.20s  %-20.20s\n", a->name, b->name);
fec5518
+
fec5518
+  if ((a->instance && !b->instance) ||
fec5518
+      (!a->instance && b->instance) ||
fec5518
+      (a->instance && strcasecmp(a->instance, b->instance)))
fec5518
+    printf("    instance              %-20.20s  %-20.20s\n",
fec5518
+           a->instance ? a->instance : "(null)",
fec5518
+	   b->instance ? b->instance : "(null)");
fec5518
+
fec5518
+  if (a->num_options != b->num_options)
fec5518
+    printf("    num_options           %-20d  %-20d\n", a->num_options,
fec5518
+           b->num_options);
fec5518
+
fec5518
+  for (i = a->num_options, aoption = a->options; i > 0; i --, aoption ++)
fec5518
+    if ((bval = cupsGetOption(aoption->name, b->num_options,
fec5518
+                              b->options)) == NULL ||
fec5518
+        strcmp(aoption->value, bval))
fec5518
+      printf("    %-20.20s  %-20.20s  %-20.20s\n", aoption->name,
fec5518
+             aoption->value, bval ? bval : "(null)");
fec5518
+}
fec5518
+
fec5518
+
fec5518
+/*
34fc0f7
  * End of "$Id: testcups.c 7721 2008-07-11 22:48:49Z mike $".
fec5518
  */
f5608b9
diff -up cups-1.3.9/systemv/lp.c.getnameddest cups-1.3.9/systemv/lp.c
f5608b9
--- cups-1.3.9/systemv/lp.c.getnameddest	2008-07-11 23:48:49.000000000 +0100
f5608b9
+++ cups-1.3.9/systemv/lp.c	2008-10-10 09:35:05.000000000 +0100
fec5518
@@ -73,9 +73,7 @@ main(int  argc,				/* I - Number of comm
fec5518
   int		num_copies;		/* Number of copies per file */
fec5518
   int		num_files;		/* Number of files to print */
fec5518
   const char	*files[1000];		/* Files to print */
fec5518
-  int		num_dests;		/* Number of destinations */
fec5518
-  cups_dest_t	*dests,			/* Destinations */
fec5518
-		*dest;			/* Selected destination */
fec5518
+  cups_dest_t	*dest;			/* Selected destination */
fec5518
   int		num_options;		/* Number of options */
fec5518
   cups_option_t	*options;		/* Options */
fec5518
   int		end_options;		/* No more options? */
fec5518
@@ -112,8 +110,7 @@ main(int  argc,				/* I - Number of comm
fec5518
 
fec5518
   silent      = 0;
fec5518
   printer     = NULL;
fec5518
-  num_dests   = 0;
fec5518
-  dests       = NULL;
fec5518
+  dest        = NULL;
fec5518
   num_options = 0;
fec5518
   options     = NULL;
fec5518
   num_files   = 0;
fec5518
@@ -179,10 +176,7 @@ main(int  argc,				/* I - Number of comm
fec5518
             if ((instance = strrchr(printer, '/')) != NULL)
fec5518
 	      *instance++ = '\0';
fec5518
 
fec5518
-	    if (num_dests == 0)
fec5518
-	      num_dests = cupsGetDests(&dests);
fec5518
-
fec5518
-            if ((dest = cupsGetDest(printer, instance, num_dests, dests)) != NULL)
fec5518
+            if ((dest = cupsGetNamedDest(NULL, printer, instance)) != NULL)
fec5518
 	    {
fec5518
 	      for (j = 0; j < dest->num_options; j ++)
fec5518
 	        if (cupsGetOption(dest->options[j].name, num_options, options) == NULL)
fec5518
@@ -593,10 +587,7 @@ main(int  argc,				/* I - Number of comm
fec5518
 
fec5518
   if (printer == NULL)
fec5518
   {
fec5518
-    if (num_dests == 0)
fec5518
-      num_dests = cupsGetDests(&dests);
fec5518
-
fec5518
-    if ((dest = cupsGetDest(NULL, NULL, num_dests, dests)) != NULL)
fec5518
+    if ((dest = cupsGetNamedDest(NULL, NULL, NULL)) != NULL)
fec5518
     {
fec5518
       printer = dest->name;
fec5518
 
fec5518
@@ -625,7 +616,7 @@ main(int  argc,				/* I - Number of comm
fec5518
     else
fec5518
       val = "LPDEST";
fec5518
 
fec5518
-    if (printer && !cupsGetDest(printer, NULL, num_dests, dests))
fec5518
+    if (printer && !cupsGetNamedDest(NULL, printer, NULL))
fec5518
       _cupsLangPrintf(stderr,
fec5518
 		      _("%s: Error - %s environment variable names "
fec5518
 		        "non-existent destination \"%s\"!\n"),