If signing starts successfully, don't just turn it off automatically if
[jra/samba/.git] / source3 / printing / pcap.c
index 62010706bb3abe30139050f00df44eb435bb6296..a5fb53a320dca01f7f180d3a93b95f9f4d873f85 100644 (file)
@@ -1,6 +1,5 @@
 /* 
-   Unix SMB/Netbios implementation.
-   Version 1.9.
+   Unix SMB/CIFS implementation.
    printcap parsing
    Copyright (C) Karl Auer 1993-1998
 
 
 #include "includes.h"
 
-#include "smb.h"
-
-extern int DEBUGLEVEL;
-
 #ifdef AIX
 /*  ******************************************
      Extend for AIX system and qconfig file
@@ -99,14 +94,14 @@ static int strlocate(char *xpLine,char *xpS)
 static void ScanQconfig_fn(char *psz,void (*fn)(char *, char *))
 {
        int iEtat;
-       FILE *pfile;
+       XFILE *pfile;
        char *line,*p;
        pstring name,comment;
        line  = NULL;
        *name = 0;
        *comment = 0;
 
-       if ((pfile = sys_fopen(psz, "r")) == NULL)
+       if ((pfile = x_fopen(psz, O_RDONLY, 0)) == NULL)
        {
              DEBUG(0,( "Unable to open qconfig file %s for read!\n", psz));
              return;
@@ -114,7 +109,7 @@ static void ScanQconfig_fn(char *psz,void (*fn)(char *, char *))
 
        iEtat = 0;
        /* scan qconfig file for searching <printername>:       */
-       for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); free(line))
+       for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); safe_free(line))
        {
                if (*line == '*' || *line == 0)
                continue;
@@ -122,7 +117,7 @@ static void ScanQconfig_fn(char *psz,void (*fn)(char *, char *))
                {
                        case 0: /* locate an entry */
                         if (*line == '\t' || *line == ' ') continue;
-                        if ((p=strchr(line,':')))
+                        if ((p=strchr_m(line,':')))
                         {
                                *p = '\0';
                                p = strtok(line,":");
@@ -160,7 +155,7 @@ static void ScanQconfig_fn(char *psz,void (*fn)(char *, char *))
                          break;
                }
        }
-       fclose(pfile);
+       x_fclose(pfile);
 }
 
 /* Scan qconfig file and locate de printername */
@@ -168,7 +163,7 @@ static void ScanQconfig_fn(char *psz,void (*fn)(char *, char *))
 static BOOL ScanQconfig(char *psz,char *pszPrintername)
 {
        int iLg,iEtat;
-       FILE *pfile;
+       XFILE *pfile;
        char *pName;
        char *line;
 
@@ -181,10 +176,10 @@ static BOOL ScanQconfig(char *psz,char *pszPrintername)
                DEBUG(0,(" Unable to allocate memory for printer %s\n",pszPrintername));
                return(False);
        }
-       if ((pfile = sys_fopen(psz, "r")) == NULL)
+       if ((pfile = x_fopen(psz, O_RDONLY, 0)) == NULL)
        {
              DEBUG(0,( "Unable to open qconfig file %s for read!\n", psz));
-             free(pName);
+             SAFE_FREE(pName);
              return(False);
        }
        slprintf(pName, iLg + 9, "%s:",pszPrintername);
@@ -192,7 +187,7 @@ static BOOL ScanQconfig(char *psz,char *pszPrintername)
        /*DEBUG(3,( " Looking for entry %s\n",pName));*/
        iEtat = 0;
        /* scan qconfig file for searching <printername>:       */
-       for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); free(line))
+       for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); safe_free(line))
        {
                if (*line == '*' || *line == 0)
                continue;
@@ -212,8 +207,8 @@ static BOOL ScanQconfig(char *psz,char *pszPrintername)
                           /* name is found without stanza device  */
                           /* probably a good printer ???               */
                           free (line);
-                          free(pName);
-                          fclose(pfile);
+                          SAFE_FREE(pName);
+                          x_fclose(pfile);
                           return(True);
                          }
                        
@@ -226,15 +221,15 @@ static BOOL ScanQconfig(char *psz,char *pszPrintername)
                          {
                                /* it's a good virtual printer */
                                free (line);
-                               free(pName);
-                               fclose(pfile);
+                               SAFE_FREE(pName);
+                               x_fclose(pfile);
                                return(True);
                          }
                          break;
                }
        }
        free (pName);
-       fclose(pfile);
+       x_fclose(pfile);
        return(False);
 }
 #endif /* AIX */
@@ -244,14 +239,14 @@ static BOOL ScanQconfig(char *psz,char *pszPrintername)
 Scan printcap file pszPrintcapname for a printer called pszPrintername. 
 Return True if found, else False. Returns False on error, too, after logging 
 the error at level 0. For generality, the printcap name may be passed - if
-passed as NULL, the configuration will be queried for the name.
+passed as NULL, the configuration will be queried for the name. 
 ***************************************************************************/
-BOOL pcap_printername_ok(char *pszPrintername, char *pszPrintcapname)
+BOOL pcap_printername_ok(const char *pszPrintername, const char *pszPrintcapname)
 {
   char *line=NULL;
-  char *psz;
+  const char *psz;
   char *p,*q;
-  FILE *pfile;
+  XFILE *pfile;
 
   if (pszPrintername == NULL || pszPrintername[0] == '\0')
     {
@@ -267,10 +262,10 @@ BOOL pcap_printername_ok(char *pszPrintername, char *pszPrintcapname)
        return(False);
       }
 
-#ifdef HAVE_LIBCUPS
+#ifdef HAVE_CUPS
     if (strequal(psz, "cups"))
        return (cups_printername_ok(pszPrintername));
-#endif /* HAVE_LIBCUPS */
+#endif /* HAVE_CUPS */
 
 #ifdef SYSV
     if (strequal(psz, "lpstat"))
@@ -282,47 +277,47 @@ BOOL pcap_printername_ok(char *pszPrintername, char *pszPrintcapname)
      return(ScanQconfig(psz,pszPrintername));
 #endif
 
-  if ((pfile = sys_fopen(psz, "r")) == NULL)
+  if ((pfile = x_fopen(psz, O_RDONLY, 0)) == NULL)
     {
       DEBUG(0,( "Unable to open printcap file %s for read!\n", psz));
       return(False);
     }
 
-  for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); free(line))
+  for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); safe_free(line))
     {
       if (*line == '#' || *line == 0)
        continue;
 
       /* now we have a real printer line - cut it off at the first : */      
-      p = strchr(line,':');
+      p = strchr_m(line,':');
       if (p) *p = 0;
       
       /* now just check if the name is in the list */
       /* NOTE: I avoid strtok as the fn calling this one may be using it */
       for (p=line; p; p=q)
        {
-         if ((q = strchr(p,'|'))) *q++ = 0;
+         if ((q = strchr_m(p,'|'))) *q++ = 0;
 
          if (strequal(p,pszPrintername))
            {
-             /* normalise the case */
-             pstrcpy(pszPrintername,p);
-             free(line);
-             fclose(pfile);
+             SAFE_FREE(line);
+             x_fclose(pfile);
              return(True);           
            }
          p = q;
        }
     }
 
-  fclose(pfile);
+  x_fclose(pfile);
   return(False);
 }
 
 
 /***************************************************************************
 run a function on each printer name in the printcap file. The function is 
-passed the primary name and the comment (if possible)
+passed the primary name and the comment (if possible). Note the fn() takes
+strings in DOS codepage. This means the xxx_printer_fn() calls must be fixed
+to return DOS codepage. FIXME !! JRA.
 ***************************************************************************/
 void pcap_printer_fn(void (*fn)(char *, char *))
 {
@@ -330,7 +325,7 @@ void pcap_printer_fn(void (*fn)(char *, char *))
   char *line;
   char *psz;
   char *p,*q;
-  FILE *pfile;
+  XFILE *pfile;
 
   /* only go looking if no printcap name supplied */
   if (((psz = lp_printcapname()) == NULL) || (psz[0] == '\0'))
@@ -339,12 +334,12 @@ void pcap_printer_fn(void (*fn)(char *, char *))
       return;
     }
 
-#ifdef HAVE_LIBCUPS
+#ifdef HAVE_CUPS
     if (strequal(psz, "cups")) {
       cups_printer_fn(fn);
       return;
     }
-#endif /* HAVE_LIBCUPS */
+#endif /* HAVE_CUPS */
 
 #ifdef SYSV
     if (strequal(psz, "lpstat")) {
@@ -361,19 +356,19 @@ void pcap_printer_fn(void (*fn)(char *, char *))
   }
 #endif
 
-  if ((pfile = sys_fopen(psz, "r")) == NULL)
+  if ((pfile = x_fopen(psz, O_RDONLY, 0)) == NULL)
     {
       DEBUG(0,( "Unable to open printcap file %s for read!\n", psz));
       return;
     }
 
-  for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); free(line))
+  for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); safe_free(line))
     {
       if (*line == '#' || *line == 0)
        continue;
 
       /* now we have a real printer line - cut it off at the first : */      
-      p = strchr(line,':');
+      p = strchr_m(line,':');
       if (p) *p = 0;
       
       /* now find the most likely printer name and comment 
@@ -383,13 +378,13 @@ void pcap_printer_fn(void (*fn)(char *, char *))
       for (p=line; p; p=q)
        {
          BOOL has_punctuation;
-         if ((q = strchr(p,'|'))) *q++ = 0;
+         if ((q = strchr_m(p,'|'))) *q++ = 0;
 
-         has_punctuation = (strchr(p,' ') || strchr(p,'(') || strchr(p,')'));
+         has_punctuation = (strchr_m(p,' ') || strchr_m(p,'\t') || strchr_m(p,'(') || strchr_m(p,')'));
 
          if (strlen(p)>strlen(comment) && has_punctuation)
            {
-             StrnCpy(comment,p,sizeof(comment)-1);
+             pstrcpy(comment,p);
              continue;
            }
 
@@ -400,19 +395,19 @@ void pcap_printer_fn(void (*fn)(char *, char *))
              continue;
            }
 
-         if (!strchr(comment,' ') && 
+         if (!strchr_m(comment,' ') && 
              strlen(p) > strlen(comment))
            {
-             StrnCpy(comment,p,sizeof(comment)-1);
-             continue;
+                   pstrcpy(comment,p);
+                   continue;
            }
        }
 
       comment[60] = 0;
       name[MAXPRINTERLEN] = 0;
 
-      if (*name)
+      if (*name) 
        fn(name,comment);
     }
-  fclose(pfile);
+  x_fclose(pfile);
 }