malloc problem!
Σταμάτης Κεκές
skekes at pylones.gr
Mon Jun 9 13:33:01 EEST 2003
GRafo ena programmataki gia na kano dokimi se kapoies routines pou grafo
gia parsing.
Kai apotygxanei i malloc. Mporei kapoios na mou pei pou kano ti vlakeia ?
Idou o kodikas
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
unsigned int svcnum( const char * );
unsigned int difference( const char * );
int mkportlist( long *, const char * );
int getpairports( const char *, long *, long *);
char *mindex( const char *p, char pp )
{
int i;
char *v = p;
for( i=0; *v != pp && i < strlen( p ) ; v++, i++ );
if( *v != pp )
return NULL;
return v;
}
int mkportlist( long *psvcarray, const char *pPortStr )
{
char *p = malloc( strlen( pPortStr )+1 );
char *v, *commapos, *ss, *pavlapos;
int retval = 0;
long i, first_port, last_port, j, m, n;
if( p == NULL )
return 0;
strcpy( p, pPortStr );
/*InitList( pList );*/
v = p;
/*printf( " aaa \n" ); */
if( index( v, ',' )!=NULL )
{
while( ( commapos = index( v, ',' ) )!=NULL )
{
ss = v;
v = commapos+1;
*commapos = '\0';
/*printf( "-----%s %s %s\n", ss, v, commapos ); */
m=getpairports( ss, &first_port, &last_port );
n = retval;
retval += m;
for( i =first_port, j=n+1; i <= last_port; i++, j++ )
{
psvcarray[j] = i;
}
}
printf( "----- o%s %s %s\n", ss, v, commapos );
m=getpairports( v, &first_port, &last_port );
n = retval;
retval += m;
for( i =first_port, j=n+1; i <= last_port; i++, j++ )
{
psvcarray[j] = i;
}
printf( "----- o%s %s %s\n", ss, v, commapos );
}
else
{
printf( "----- o" );
m=getpairports( v, &first_port, &last_port );
n = retval;
retval += m;
for( i =first_port, j=n+1; i <= last_port; i++, j++ )
{
psvcarray[j] = i;
}
}
printf( "----- o" );
/*free( p ); */
printf( "----- o" );
return retval;
}
int getpairports( const char *psvcstr, long *first_port, long *last_port )
{
char *pavlapos=NULL, *p = malloc( strlen( psvcstr ) + 1 );
int retval =0, i=0, f=0;
memset( p, 0, strlen( psvcstr+1 ) );
strcpy( p, psvcstr );
pavlapos = NULL;
for( i=0; i<strlen(p); i++)
{
if( ( f = p[i] == '-') )
pavlapos = p+i;
}
if( pavlapos !=NULL )
{
*first_port = strtol( psvcstr, NULL, 10);
*last_port = strtol( pavlapos+1,NULL, 10 );
retval = (((unsigned int)strtol( pavlapos+1, NULL, 10
))-((unsigned int)strtol( p, NULL, 10)))+1;
}
else
{
*first_port = *last_port = strtol( p, NULL, 10 );
retval = 1;
}
free( p );
return retval;
}
unsigned int difference( const char *psvcstr )
{
char *pavlapos, *p = malloc( strlen( psvcstr ) + 1 );
int retval =0;
memset( p, 0, strlen( psvcstr+1 ) );
strcpy( p, psvcstr );
if( ( pavlapos = index( psvcstr, '-' ) )!=NULL )
{
retval = (((unsigned int)strtol( pavlapos+1,NULL, 10
))-((unsigned int)strtol( psvcstr, NULL, 10)))+1;
printf( " --- > '%s' '%s' \n", pavlapos, p, psvcstr );
}
else
retval = 1;
free( p );
return retval;
}
unsigned int svcnum( const char *psvcstr )
{
char *p = malloc( strlen( psvcstr )+1 );
char *v, *commapos, *ss, *pavlapos;
unsigned int retval =0;
v = p;
strcpy( p, psvcstr );
if( index( v, ',' )!=NULL )
{
while( ( commapos = index( v, ',' ) )!=NULL )
{
ss = v;
v = commapos+1;
*commapos = '\0';
retval += difference( ss );
}
retval += difference( v );
}
else
retval = difference( v );
free( p );
return retval;
}
int main( int argc, char **argv )
{
const char *pstring = "20,21, 6667-7000, 8080, 8081-9000, 20000-61000";
unsigned int num_services = svcnum( pstring ), i;
/*sList sPortList;*/
FILE *pexitfile = fopen( "results.txt", "w" );
long *svcarray = NULL;
if( pexitfile == NULL )
{
printf( "Astoxisa!!!\n" );
free( svcarray );
return(0);
}
svcarray = calloc( num_services, sizeof( long ) );
if( svcarray == NULL );
{
printf( "Error allocating memory %d %d\n", sizeof( long
)*num_services, sizeof(long) );
return(0);
}
/*memset( &sPortList,0,sizeof sPortList );*/
printf( "'%s' makes %d services\n", pstring, num_services );
num_services = mkportlist( svcarray, pstring );
printf( "'%s' makes %d services\n", pstring, num_services );
for( i=0; i <= num_services; i++ )
{
printf( " %d\n", i );
fprintf( pexitfile, "Service %d has port %ld\n", i, svcarray[i] );
fflush( pexitfile );
}
printf( " %d\n", i );
/*free( svcarray );*/
printf( "'%s' makes %d services\n", pstring, num_services );
/*fclose( pexitfile );*/
return 0;
}
More information about the Linux-greek-users
mailing list