Bug 14551

Summary: strsep()
Product: Base System Reporter: zh_xf <zh_xf>
Component: miscAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: Unspecified   
Hardware: Any   
OS: Any   

Description zh_xf 1999-10-27 07:50:01 UTC
Hi:
 I am a new user to FreeBSD.
When I use the following program,I expected the out put would be;
argv=aaa
argv=bbbb
argv=dd
argv=kkkk 

but actually the output is
argv=
argv=bbbb
argv=dd
argv=kkkk 

I don't where argv[0] was missing.


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

void main(){
char **ap, *argv[10],*inputstring; //[10]
int i;

 strcpy(inputstring,"aaa\tbbbb\tdd\tkkkk");

		     for (ap = argv; (*ap = strsep(&inputstring, "\t")) != NULL;)
           		           if (**ap != '\0')
	                            if (++ap >= &argv[10])
                                      break;
	for(i=0;i<4;i++) 		printf("argv=%s\n",argv[i]);
                                   
}

Fix: 

Sorry to use following method to fix the problem

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

void main(){
char **ap, *argv[10],*inputstring; //[10]
char s3[32];
 int i;
 strcpy(inputstring,"aaa\tbbbb\tdd\tkkkk");
 
 i=0;
  
		     for (ap = argv; (*ap = strsep(&inputstring, "\t")) != NULL;)
				{
           					if(i==0)
           					    strcpy(s3,*ap);		
              		          i++;
           		           if (**ap != '\0')
                            if (++ap >= &argv[10])
                                      break;
            }
            
	strcpy(argv[0],s3);	
	for(i=0;i<4;i++) 		printf("argv=%s\n",argv[i]);
	
}
Comment 1 Kevin Street 1999-10-31 01:02:07 UTC
bad program.  Add 
	inputstring=(char *) malloc(1024); 
before the 
	strcpy(inputstring,"...");
--
Kevin Street
street@iname.com
Comment 2 Sheldon Hearn freebsd_committer freebsd_triage 1999-11-01 11:10:49 UTC
State Changed
From-To: open->closed

OOps. I replied to this one privately and forgot to close the PR when 
the originator mailed me confirmation.  Pilot error. :-)