Listing D.1b: SMB_URL.c
A Simple SMB URL Parser


/* ------------------------------------------------------ **
 * smb_url.c
 * ------------------------------------------------------ **
 */

#include <stdio.h>
#include <string.h>

#include "smb_url.h"

smb_url *smb_urlParse( char *src, smb_url *url )
  /* ---------------------------------------------------- **
   * Parse an SMB URL string into an smb_url structure.
   *
   * This is a very, very simplistic URL parser...just
   * enough for demonstration purposes.
   * It does not handle the full syntax of SMB URLs.
   * It only handles absolute URLs, and does not do
   * enough error checking.  You can certainly do better,
   * and superior examples can be found on on the web.
   *
   * The function returns NULL on error.
   * ---------------------------------------------------- **
   */
  {
  int    pos;
  uchar *p;

  /* Clear the smb_url structure first. */
  (void)memset( url, 0, sizeof( smb_url ) );

  /* Check for a correct prefix. */
  pos = 0;
  if( 0 == strncasecmp( "smb://", src, 6 ) )
    pos = 6;
  else
    if( 0 == strncasecmp( "cifs://", src, 7 ) )
      pos = 7;
    else
      return( NULL );

  /* Check for an empty URL ("smb://"). */
  if( '\0' == src[pos] )
    return( url );

  /* Copy the original string so that we can carve it up. */
  src = strdup( &src[pos] );

  /* Separate the server, share, path & context
   * components.
   */
  url->server = src;

  /* Look for context. */
  p = strrchr( src, '?' );
  if( NULL != p )
    {
    *p = '\0';
    url->context = ++p;
    }

  /* Share part next. */
  p = strchr( src, '/' );
  if( NULL != p )
    {
    *p = '\0';
    url->share = ++p;
    /* path part. */
    p = strchr( p, '/' );
    if( NULL != p )
      {
      *p = '\0';
      url->path = ++p;
      }
    }

  /* Look for the ntdomain & username subfields
   * in the server string (the Authority field).
   */
  p = strchr( url->server, '@' );
  if( NULL != p )
    {
    *p = '\0';
    url->user = url->server;
    url->server = ++p;
    /* Split the user field into ntdomain;user */
    p = strchr( url->user, ';' );
    if( NULL != p )
      {
      *p = '\0';
      url->ntdomain = url->user;
      url->user     = ++p;
      }
    }

  /* Look for a port number in the server string. */
  p = strchr( url->server, ':' );
  if( NULL != p )
    {
    *p = '\0';
    url->port = ++p;
    }

  return( url );
  } /* smb_urlParse */


void smb_urlContent( smb_url *url )
  /* ---------------------------------------------------- **
   * Dump the contents of an smb_url structure,
   * representing a parsed SMB URL string.
   * ---------------------------------------------------- **
   */
  {
  if( url->ntdomain )
    (void)printf( "ntdomain: %s\n", url->ntdomain );
  if( url->user )
    (void)printf( "    user: %s\n", url->user );
  if( url->server )
    (void)printf( "  server: %s\n", url->server );
  if( url->port )
    (void)printf( "    port: %s\n", url->port );
  if( url->share )
    (void)printf( "   share: %s\n", url->share );
  if( url->path )
    (void)printf( "    path: %s\n", url->path );
  if( url->context )
    (void)printf( " context: %s\n", url->context );
  } /* smb_urlContent */


/* ------------------------------------------------------ */


$Revision: 1.4 $
$Date: 2003/05/22 20:40:46 $
[W3C Validated] Copyright © 2002 Christopher R. Hertel 
Released under the terms of the LGPL