parsefile.c source code
/**********************************************************************
* parseFile removes the token following delimiter if present in str.
* On return *v points to the token. The delimiter and token have been
* removed from s.  It returns 0 if parse was successful or -1
* if there is an error.
**********************************************************************/

#include "ush.h"

int  parseFile(char* str, char delimiter, char** v)
{
    char* p;
    char* q;
    int offset;
    int result = 0;
   
    /* Find position of the delimiting character */
    *v = NULL;
    if ((p = strchr(str, delimiter)) != NULL)  
    {
	/* Split off the token following delimiter */
	if ((q = (char *)malloc(strlen(p + 1) + 1)) == NULL)
	    result = -1;
	else 
	{
	    strcpy(q, p + 1);
	    if ((*v = strtok(q, DELIMITERSET)) == NULL)
		result = -1;
	    offset = strlen(q);
	    strcpy(p, p + offset + 1);
	} 
    }
    return result;
}