rem: DBG("DEBUG:put_command:CDS=[%.*s]\n",ptr-pos,pos);
add: DBG("DEBUG:put_command:CDS=[%.*s]\n",(int)(ptr-pos),pos);
context: int put_command( struct modem *mdm, char* cmd, int cmd_len, char* answer,											int max, int timeout,char* exp_end){	static char buf[MAX_BUF];	static int  buf_len = 0;	char* pos;	char* foo;	char* ptr;	char* to_move;	char* answer_s;	char* answer_e;	int   timeoutcounter;	int   available;	int   status;	int   exp_end_len;	int   n;	/* check if fd is "clean" for reading */	timeoutcounter = 0;	ioctl(mdm->fd,TIOCMGET,&status);	while (!(status & TIOCM_CTS))	{		usleep( READ_SLEEP );		timeoutcounter++;		ioctl(mdm->fd,TIOCMGET,&status);		if (timeoutcounter>=timeout) {			LOG(L_INFO,"INFO:put_command: Modem is not clear to send\n");			return 0;		}	}#ifdef SHOW_SMS_MODEM_COMMAND	DBG("DEBUG: put_command: -<%d>-->[%.*s] \n",cmd_len,cmd_len,cmd);#endif	/* send the command to the modem */	write(mdm->fd,cmd,cmd_len);	tcdrain(mdm->fd);	/* read from the modem */	exp_end_len = exp_end?strlen(exp_end):0;	answer_s = buf;	answer_e = 0;	to_move = 0;	do	{		/* try to read some bytes */		ioctl(mdm->fd,FIONREAD,&available);		/* how many bytes are available to read? */		if (available<1)  /* if 0 then wait a little bit and retry */		{			usleep( READ_SLEEP );			timeoutcounter++;			ioctl(mdm->fd,FIONREAD,&available);		}		if (available>0)		{			/* How many bytes do I want to read maximum?			Not more than buffer size. And how many bytes are available? */			n = (available>MAX_BUF-buf_len-1)?MAX_BUF-buf_len-1:available;			/* read data */			n = read( mdm->fd, buf+buf_len, n);			if (n<0) {				LOG(L_ERR,"ERROR:put_command: error reading from modem: %s\n",					strerror(errno));				goto error;			}			if (n) {				buf_len += n;				buf[buf_len] = 0;				//DBG("DEBUG:put_commnad: read = [%s]\n",buf+buf_len-n);				foo = pos = 0;				if ( (!exp_end && ((pos=strstr(optz(n,4),"OK\r\n"))				|| (foo=strstr(optz(n,5),"ERROR"))))				|| (exp_end && (pos=strstr(optz(n,exp_end_len),exp_end)) )) {					/* we found the end */					//DBG("DEBUG:put_commnad: end found = %s\n",					//	(foo?"ERROR":(exp_end?exp_end:"OK")));					/* for ERROR we still have to read EOL */					if (!foo || (foo=strstr(foo+5,"\r\n"))) {						answer_e = foo?foo+2:(pos+(exp_end?exp_end_len:4));						timeoutcounter = timeout;					}				}			}		}	/* repeat until timout */	}while (timeoutcounter<timeout);	if (!answer_e)		answer_e = buf+buf_len;	/* CDS report is activ? */	if (sms_report_type==CDS_REPORT) {		to_move = 0;		ptr = buf;		/* do we have a CDS reply inside? */		while ( (pos=strstr(ptr,CDS_HDR)) ) {			if (pos!=ptr) {  /* here we have the command response */				answer_s = ptr;			}			/* look for the end of CDS response */			ptr = pos+CDS_HDR_LEN;			for( n=0 ; n<2&&(foo=strstr(ptr,"\r\n")) ; ptr=foo+2,n++ );			if (n<2) { /* we haven't read the entire CDS response */				DBG("DEBUG:put_command: CDS end not found!\n");				to_move = pos;				ptr = buf + buf_len;			}else{				/* process the CDS */				DBG("DEBUG:put_command:CDS=[%.*s]\n",ptr-pos,pos);				cds_report_func(mdm,pos,ptr-pos);			}		}		if ((*ptr)) {			answer_s = ptr;			ptr = answer_e;		}		if (ptr!=buf+buf_len)			to_move = ptr;	}	/* copy the response in answer buffer - as much as fits */	if (answer && max) {		n = max-1<answer_e-answer_s?max-1:answer_e-answer_s;		memcpy(answer,answer_s,n);		answer[n] = 0;	}	/* shift left the remaining data into the buffer - if needs */	if (sms_report_type==CDS_REPORT && to_move) {		buf_len = buf_len - (to_move-buf);		memcpy(buf,to_move,buf_len);		buf[buf_len] = 0;		DBG("DEBUG:put_commnad: buffer shifted left=[%d][%s]\n",buf_len,buf);	} else {		buf_len = 0;	}#ifdef SHOW_SMS_MODEM_COMMAND	DBG("DEBUG:put_command: <-[%s] \n",answer);#endif	return answer_e-answer_s;error:	return 0;}