first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[kai/samba.git] / source3 / printing / pcap.c
index 549ebcd70df6b63d63efe426232325f4f82357af..62010706bb3abe30139050f00df44eb435bb6296 100644 (file)
@@ -2,11 +2,15 @@
    Unix SMB/Netbios implementation.
    Version 1.9.
    printcap parsing
-   Copyright (C) Karl Auer 1993-1997
+   Copyright (C) Karl Auer 1993-1998
 
    Re-working by Martin Kiff, 1994
    
    Re-written again by Andrew Tridgell
+
+   Modified for SVID support by Norm Jacobs, 1997
+
+   Modified for CUPS support by Michael Sweet, 1999
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
  *  Opening a pipe for "lpc status" and reading that would probably 
  *  be pretty effective. Code to do this already exists in the freely
  *  distributable PCNFS server code.
+ *
+ *  Modified to call SVID/XPG4 support if printcap name is set to "lpstat"
+ *  in smb.conf under Solaris.
+ *
+ *  Modified to call CUPS support if printcap name is set to "cups"
+ *  in smb.conf.
  */
 
 #include "includes.h"
@@ -64,7 +74,7 @@ extern int DEBUGLEVEL;
     ****************************************** */
 static int strlocate(char *xpLine,char *xpS)
 {
-       int iS,iL,i,iRet;
+       int iS,iL,iRet;
        char *p;
        iS = strlen(xpS);
        iL = strlen(xpLine);
@@ -86,9 +96,9 @@ static int strlocate(char *xpLine,char *xpS)
 /* ******************************************************************* */
 /* *    Scan qconfig and search all virtual printer (device printer) * */
 /* ******************************************************************* */
-static void ScanQconfig_fn(char *psz,void (*fn)())
+static void ScanQconfig_fn(char *psz,void (*fn)(char *, char *))
 {
-       int iLg,iEtat;
+       int iEtat;
        FILE *pfile;
        char *line,*p;
        pstring name,comment;
@@ -96,7 +106,7 @@ static void ScanQconfig_fn(char *psz,void (*fn)())
        *name = 0;
        *comment = 0;
 
-       if ((pfile = fopen(psz, "r")) == NULL)
+       if ((pfile = sys_fopen(psz, "r")) == NULL)
        {
              DEBUG(0,( "Unable to open qconfig file %s for read!\n", psz));
              return;
@@ -118,7 +128,7 @@ static void ScanQconfig_fn(char *psz,void (*fn)())
                                p = strtok(line,":");
                                if (strcmp(p,"bsh")!=0)
                                  {
-                                   strcpy(name,p);
+                                   pstrcpy(name,p);
                                    iEtat = 1;
                                    continue;
                                  }
@@ -171,13 +181,13 @@ static BOOL ScanQconfig(char *psz,char *pszPrintername)
                DEBUG(0,(" Unable to allocate memory for printer %s\n",pszPrintername));
                return(False);
        }
-       if ((pfile = fopen(psz, "r")) == NULL)
+       if ((pfile = sys_fopen(psz, "r")) == NULL)
        {
              DEBUG(0,( "Unable to open qconfig file %s for read!\n", psz));
              free(pName);
              return(False);
        }
-       sprintf(pName,"%s:",pszPrintername);
+       slprintf(pName, iLg + 9, "%s:",pszPrintername);
        iLg = strlen(pName);
        /*DEBUG(3,( " Looking for entry %s\n",pName));*/
        iEtat = 0;
@@ -227,8 +237,9 @@ static BOOL ScanQconfig(char *psz,char *pszPrintername)
        fclose(pfile);
        return(False);
 }
+#endif /* AIX */
+
 
-#endif
 /***************************************************************************
 Scan printcap file pszPrintcapname for a printer called pszPrintername. 
 Return True if found, else False. Returns False on error, too, after logging 
@@ -255,11 +266,23 @@ BOOL pcap_printername_ok(char *pszPrintername, char *pszPrintcapname)
        DEBUG(0,( "No printcap file name configured!\n"));
        return(False);
       }
+
+#ifdef HAVE_LIBCUPS
+    if (strequal(psz, "cups"))
+       return (cups_printername_ok(pszPrintername));
+#endif /* HAVE_LIBCUPS */
+
+#ifdef SYSV
+    if (strequal(psz, "lpstat"))
+       return (sysv_printername_ok(pszPrintername));
+#endif
+
 #ifdef AIX
-  if (strlocate(psz,"/qconfig") != NULL)
+  if (strlocate(psz,"/qconfig"))
      return(ScanQconfig(psz,pszPrintername));
 #endif
-  if ((pfile = fopen(psz, "r")) == NULL)
+
+  if ((pfile = sys_fopen(psz, "r")) == NULL)
     {
       DEBUG(0,( "Unable to open printcap file %s for read!\n", psz));
       return(False);
@@ -283,7 +306,7 @@ BOOL pcap_printername_ok(char *pszPrintername, char *pszPrintcapname)
          if (strequal(p,pszPrintername))
            {
              /* normalise the case */
-             strcpy(pszPrintername,p);
+             pstrcpy(pszPrintername,p);
              free(line);
              fclose(pfile);
              return(True);           
@@ -292,7 +315,6 @@ BOOL pcap_printername_ok(char *pszPrintername, char *pszPrintcapname)
        }
     }
 
-
   fclose(pfile);
   return(False);
 }
@@ -302,7 +324,7 @@ BOOL pcap_printername_ok(char *pszPrintername, char *pszPrintcapname)
 run a function on each printer name in the printcap file. The function is 
 passed the primary name and the comment (if possible)
 ***************************************************************************/
-void pcap_printer_fn(void (*fn)())
+void pcap_printer_fn(void (*fn)(char *, char *))
 {
   pstring name,comment;
   char *line;
@@ -317,14 +339,29 @@ void pcap_printer_fn(void (*fn)())
       return;
     }
 
+#ifdef HAVE_LIBCUPS
+    if (strequal(psz, "cups")) {
+      cups_printer_fn(fn);
+      return;
+    }
+#endif /* HAVE_LIBCUPS */
+
+#ifdef SYSV
+    if (strequal(psz, "lpstat")) {
+      sysv_printer_fn(fn);
+      return;
+    }
+#endif
+
 #ifdef AIX
-  if (strlocate(psz,"/qconfig") != NULL)
+  if (strlocate(psz,"/qconfig"))
   {
        ScanQconfig_fn(psz,fn);
      return;
   }
 #endif
-  if ((pfile = fopen(psz, "r")) == NULL)
+
+  if ((pfile = sys_fopen(psz, "r")) == NULL)
     {
       DEBUG(0,( "Unable to open printcap file %s for read!\n", psz));
       return;
@@ -356,10 +393,10 @@ void pcap_printer_fn(void (*fn)())
              continue;
            }
 
-         if (strlen(p) <= 8 && strlen(p)>strlen(name) && !has_punctuation)
+         if (strlen(p) <= MAXPRINTERLEN && strlen(p)>strlen(name) && !has_punctuation)
            {
-             if (!*comment) strcpy(comment,name);
-             strcpy(name,p);
+             if (!*comment) pstrcpy(comment,name);
+             pstrcpy(name,p);
              continue;
            }
 
@@ -372,7 +409,7 @@ void pcap_printer_fn(void (*fn)())
        }
 
       comment[60] = 0;
-      name[8] = 0;
+      name[MAXPRINTERLEN] = 0;
 
       if (*name)
        fn(name,comment);