home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NET-TOOL.1 / NET-TOOL / net-tools / lib / getargs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-19  |  2.3 KB  |  94 lines

  1. /*
  2.  * dip        A program for handling dialup network connecions.
  3.  *        This program handles the connections needed for dialup
  4.  *        network links, like SLIP, PPP or KISS.  It can handle
  5.  *        both incoming and outgoing connections, using password
  6.  *        security for incoming connections.
  7.  *
  8.  *        General argument parser.
  9.  *
  10.  * Version:    @(#)getargs.c    4.0.1    04/05/94
  11.  *
  12.  * Author:      Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  13.  *        Copyright 1993,1994 MicroWalt Corporation
  14.  *
  15.  *        This program is free software; you can redistribute it
  16.  *        and/or  modify it under  the terms of  the GNU General
  17.  *        Public  License as  published  by  the  Free  Software
  18.  *        Foundation;  either  version 2 of the License, or  (at
  19.  *        your option) any later version.
  20.  */
  21. #include "config.h"
  22. #include <sys/types.h>
  23. #include <sys/socket.h>
  24. #include <net/if.h>
  25. #include <errno.h>
  26. #include <signal.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include "support.h"
  32. #include "pathnames.h"
  33.  
  34.  
  35. /* Split the input string into multiple fields. */
  36. int
  37. getargs(char *string, char *arguments[])
  38. {
  39.   char temp[1024];
  40.   char *sp, *ptr;
  41.   int i, argc;
  42.   char want;
  43.  
  44.   /*
  45.    * Copy the string into a buffer.  We may have to modify
  46.    * the original string because of all the quoting...
  47.    */
  48.   sp = string; i = 0;
  49.   strcpy(temp, string);    
  50.   ptr = temp;
  51.  
  52.   /*
  53.    * Look for delimiters ("); if present whatever
  54.    * they enclose will be considered one argument.
  55.    */
  56.   while (*ptr != '\0' && i < 32) {
  57.     /* Ignore leading whitespace on input string. */
  58.     while (*ptr == ' ' || *ptr == '\t') ptr++;
  59.  
  60.     /* Set string pointer. */
  61.     arguments[i++] = sp;
  62.  
  63.     /* Check for any delimiters. */
  64.     if (*ptr == '"' || *ptr == '\'') {
  65.         /*
  66.          * Copy the string up to any whitespace OR the next
  67.          * delimiter. If the delimiter was escaped, skip it
  68.          * as it if was not there.
  69.          */
  70.         want = *ptr++;
  71.         while(*ptr != '\0') {
  72.             if (*ptr == want && *(ptr - 1) != '\\') {
  73.                 ptr++;
  74.                 break;
  75.             }
  76.             *sp++ = *ptr++;
  77.         }
  78.     } else {
  79.         /* Just copy the string up to any whitespace. */
  80.         while(*ptr != '\0' && *ptr != ' ' && *ptr != '\t')
  81.                             *sp++ = *ptr++;
  82.     }
  83.     *sp++ = '\0';
  84.  
  85.     /* Skip trailing whitespace. */
  86.     if (*ptr != '\0') {
  87.         while(*ptr == ' ' || *ptr == '\t') ptr++;
  88.     }
  89.   }  
  90.   argc = i;
  91.   while (i < 32)  arguments[i++] = (char *)NULL;
  92.   return(argc);
  93. }
  94.