home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / contrib / samba / samba-1.8 / samba-1 / samba-1.8.05 / pcap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-27  |  5.8 KB  |  209 lines

  1. /* 
  2.    Unix SMB/Netbios implementation.
  3.    Version 1.8.
  4.    Copyright (C) Karl Auer 1993,1994
  5.  
  6.    Re-working by Martin Kiff, 1994
  7.    
  8.    Re-written again by Andrew Tridgell
  9.    
  10.    This program is free software; you can redistribute it and/or modify
  11.    it under the terms of the GNU General Public License as published by
  12.    the Free Software Foundation; either version 2 of the License, or
  13.    (at your option) any later version.
  14.    
  15.    This program is distributed in the hope that it will be useful,
  16.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.    GNU General Public License for more details.
  19.    
  20.    You should have received a copy of the GNU General Public License
  21.    along with this program; if not, write to the Free Software
  22.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24.  
  25. /*
  26.  *  Parse printcap file.
  27.  *
  28.  *  This module does exactly one thing - it looks into the printcap file
  29.  *  and tells callers if a specified string appears as a printer name.
  30.  *
  31.  *  The way this module looks at the printcap file is very simplistic.
  32.  *  Only the local printcap file is inspected (no searching of NIS
  33.  *  databases etc).
  34.  *
  35.  *  There are assumed to be one or more printer names per record, held
  36.  *  as a set of sub-fields separated by vertical bar symbols ('|') in the
  37.  *  first field of the record. The field separator is assumed to be a colon
  38.  *  ':' and the record separator a newline.
  39.  * 
  40.  *  Lines ending with a backspace '\' are assumed to flag that the following
  41.  *  line is a continuation line so that a set of lines can be read as one
  42.  *  printcap entry.
  43.  *
  44.  *  A line stating with a hash '#' is assumed to be a comment and is ignored
  45.  *  Comments are discarded before the record is strung together from the
  46.  *  set of continuation lines.
  47.  *
  48.  *  Opening a pipe for "lpc status" and reading that would probably 
  49.  *  be pretty effective. Code to do this already exists in the freely
  50.  *  distributable PCNFS server code.
  51.  */
  52.  
  53. #include "includes.h"
  54.  
  55. #include "smb.h"
  56. #include "loadparm.h"
  57. #include "pcap.h"
  58.  
  59. extern int DEBUGLEVEL;
  60.  
  61.  
  62. /***************************************************************************
  63. Scan printcap file pszPrintcapname for a printer called pszPrintername. 
  64. Return True if found, else False. Returns False on error, too, after logging 
  65. the error at level 0. For generality, the printcap name may be passed - if
  66. passed as NULL, the configuration will be queried for the name.
  67. ***************************************************************************/
  68. BOOL pcap_printername_ok(char *pszPrintername, char *pszPrintcapname)
  69. {
  70.   char *line=NULL;
  71.   char *psz;
  72.   char *p,*q;
  73.   FILE *pfile;
  74.  
  75.   if (pszPrintername == NULL || pszPrintername[0] == '\0')
  76.     {
  77.       DEBUG(0,( "Attempt to locate null printername! Internal error?\n"));
  78.       return(False);
  79.     }
  80.  
  81.   /* only go looking if no printcap name supplied */
  82.   if ((psz = pszPrintcapname) == NULL || psz[0] == '\0')
  83.     if (((psz = lp_printcapname()) == NULL) || (psz[0] == '\0'))
  84.       {
  85.     DEBUG(0,( "No printcap file name configured!\n"));
  86.     return(False);
  87.       }
  88.  
  89.   if ((pfile = fopen(psz, "r")) == NULL)
  90.     {
  91.       DEBUG(0,( "Unable to open printcap file %s for read!\n", psz));
  92.       return(False);
  93.     }
  94.  
  95.   for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); free(line))
  96.     {
  97.       if (*line == '#' || *line == 0)
  98.     continue;
  99.  
  100.       /* now we have a real printer line - cut it off at the first : */      
  101.       p = strchr(line,':');
  102.       if (p) *p = 0;
  103.       
  104.       /* now just check if the name is in the list */
  105.       /* NOTE: I avoid strtok as the fn calling this one may be using it */
  106.       for (p=line; p; p=q)
  107.     {
  108.       if ((q = strchr(p,'|'))) *q++ = 0;
  109.  
  110.       if (strequal(p,pszPrintername))
  111.         {
  112.           /* normalise the case */
  113.           strcpy(pszPrintername,p);
  114.           free(line);
  115.           fclose(pfile);
  116.           return(True);          
  117.         }
  118.       p = q;
  119.     }
  120.     }
  121.  
  122.  
  123.   fclose(pfile);
  124.   return(False);
  125. }
  126.  
  127.  
  128. /***************************************************************************
  129. run a function on each printer name in the printcap file. The function is 
  130. passed the primary name and the comment (if possible)
  131. ***************************************************************************/
  132. void pcap_printer_fn(void (*fn)())
  133. {
  134.   pstring name,comment;
  135.   char *line;
  136.   char *psz;
  137.   char *p,*q;
  138.   FILE *pfile;
  139.  
  140.   /* only go looking if no printcap name supplied */
  141.   if (((psz = lp_printcapname()) == NULL) || (psz[0] == '\0'))
  142.     {
  143.       DEBUG(0,( "No printcap file name configured!\n"));
  144.       return;
  145.     }
  146.  
  147.   if ((pfile = fopen(psz, "r")) == NULL)
  148.     {
  149.       DEBUG(0,( "Unable to open printcap file %s for read!\n", psz));
  150.       return;
  151.     }
  152.  
  153.   for (;(line = fgets_slash(NULL,sizeof(pstring),pfile)); free(line))
  154.     {
  155.       if (*line == '#' || *line == 0)
  156.     continue;
  157.  
  158.       /* now we have a real printer line - cut it off at the first : */      
  159.       p = strchr(line,':');
  160.       if (p) *p = 0;
  161.       
  162.       /* now find the most likely printer name and comment 
  163.        this is pure guesswork, but it's better than nothing */
  164.       *name = 0;
  165.       *comment = 0;
  166.       for (p=line; p; p=q)
  167.     {
  168.       if ((q = strchr(p,'|'))) *q++ = 0;
  169.  
  170.       if (strchr(p,' ') || strchr(p,'('))
  171.         {
  172.           StrnCpy(comment,p,sizeof(comment)-1);
  173.           continue;
  174.         }
  175.       if (!*name && strlen(p) < 14)
  176.         {
  177.           strcpy(name,p);
  178.           continue;
  179.         }
  180.  
  181.       if (strchr(p,'_') && !strchr(comment,' ') && 
  182.           strlen(p) > strlen(comment) &&
  183.           strlen(name) > 1)
  184.         {
  185.           StrnCpy(comment,p,sizeof(comment)-1);
  186.           continue;
  187.         }
  188.         
  189.       if (strlen(name)<3 && strlen(p) > strlen(name) && strlen(p) < 14)
  190.         {
  191.           strcpy(name,p);
  192.           continue;
  193.         }        
  194.       if (!strchr(comment,' ') && strlen(p) > strlen(comment))
  195.         {
  196.           StrnCpy(comment,p,sizeof(comment)-1);
  197.           continue;
  198.         }
  199.     }
  200.  
  201.       comment[60] = 0;
  202.       name[14] = 0;
  203.  
  204.       if (*name)
  205.     fn(name,comment);
  206.     }
  207.   fclose(pfile);
  208. }
  209.