Why vsscanf() doesn't change errno for range error?

R

RoSsIaCrIiLoIA

Is it good the use of va_arg?
____________________
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>

unsigned
gcd_my(unsigned a, unsigned b)
{unsigned t;

if(a==0 || b==0) return 0;
while(a!=0)
{if( a<b )
{t = a; a = b; b = t;}
a = a%b;
}
return b;
}

int domanda(char* a, char* fmt, ... );

int main(void)
{
int first, second, nread;
char input[9];

printf
("This program will figure out the greatest common\n"
"divisor in two numbers you give.\n");
nread= domanda("Please enter the numbers> ",
"%d%d", &first, &second) ;
if(!nread)
{
printf("You don't want to play? Ok.\n");
exit(0);
}

printf( "first=%d, second=%d\n", first, second );

first = first >=0 ? first : -first ;
second = second>=0 ? second: -second;

printf("The GCD is %d\n", (int) gcd_my(first, second));
exit(0);
}


size_t fgetm(char* s, size_t n, FILE* iop)
{
int c;
char *cs;

if(!s) return 0;
else if( n==0 || iop==0 )
{*s = 0; return 0;}

cs=s;
while(--n>0 && (c = getc(iop))!=EOF)
if((*cs++ = c)=='\n') break;
*cs = '\0';

return (cs - s);
}

unsigned conta(char* a)
{
unsigned num=0;

if(!a) return 0;
while(*a)
{if(*a=='%')
if(a[1]=='*' || a[1]=='%') ;
else ++num;
++a;
}
return num;
}


int domanda(char* a, char* fmt, ... )
{va_list ap;
int h;
size_t j;
char buffer[258];


ricomincia:
printf("%s", a); fflush(stdout);
if((j = fgetm(buffer, 256, stdin))==0)
return 0; /* entra solo EOF*/
if(buffer[j-1]!='\n')
{fprintf(stderr, "Hai sforato la linea di 256 caratteri\n"
"PS: Si esce se tutte le conversioni sono riuscite\n"
"Oppure se EOF (^z o ^d) e' premuto\n");
while( j = fgetm(buffer, 256, stdin), j!=0 && buffer[j-1]!='\n');
if(feof(stdin)) return 0;
goto ricomincia;
}
j=conta(fmt);
va_start(ap, fmt);
errno = 0;
h=(vsscanf( buffer, fmt, ap)==j);
va_end(ap);
if(h && errno==0) return 1;
if(!errno)
fprintf(stderr, "Errore: numero di argomenti insufficienti\n");
else perror("Errore");
goto ricomincia;
}
 
R

RoSsIaCrIiLoIA

/* escuse me */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>

unsigned
gcd_my(unsigned a, unsigned b)
{unsigned t;

if(a==0 || b==0) return 0;
while(a!=0)
{if(a < b)
{t = a; a = b; b = t;}
a = a%b;
}
return b;
}

int domanda(char* a, char* fmt, ... );

int main(void)
{
int first, second, nread;
char input[9];

printf
("This program will figure out the greatest common\n"
"divisor in two numbers you give.\n");
nread= domanda("Please enter the numbers> ",
"%d%d", &first, &second) ;
if(!nread)
{
printf("You don't want to play? Ok.\n");
exit(0);
}

printf( "first=%d, second=%d\n", first, second );

first = first>=0 ? first: -first;
second = second>=0 ? second: -second;

printf("The GCD is %d\n", (int) gcd_my(first, second));
exit(0);
}


size_t fgetm(char* s, size_t n, FILE* iop)
{
int c;
char *cs;

if(!s) return 0;
else if( n==0 || iop==0 )
{*s = 0; return 0;}

cs = s;
while(--n>0 && (c = getc(iop))!=EOF)
if((*cs++ = c)=='\n') break;
*cs = '\0';

return (cs - s);
}

unsigned conta(char* a)
{
unsigned num = 0;

if(!a) return 0;
while(*a)
{if(*a=='%')
if(a[1]=='*' || a[1]=='%') ;
else ++num;
++a;
}
return num;
}


int domanda(char* a, char* fmt, ... )
{va_list ap;
int h;
size_t j;
char buffer[258];


ricomincia:
printf("%s", a); fflush(stdout);
if((j = fgetm(buffer, 256, stdin))==0)
return 0; /* entra solo EOF*/
if(buffer[j-1]!='\n' && !feof(stdin) )
{fprintf(stderr, "Hai sforato la linea di 256 caratteri\n"
"PS: Si esce se tutte le conversioni sono riuscite\n"
"Oppure se EOF (^z o ^d) e' premuto\n");
while( j = fgetm(buffer, 256, stdin), j!=0 && buffer[j-1]!='\n');
if(feof(stdin)) return 0;
goto ricomincia;
}
j=conta(fmt);
va_start(ap, fmt);
errno = 0;
h=(vsscanf( buffer, fmt, ap)==j);
va_end(ap);
if(h && errno==0) return 1;
if(!errno)
fprintf(stderr, "Errore: numero di argomenti insufficienti\n");
else perror("Errore");
if(feof(stdin)) return 0;
goto ricomincia;
}
 
R

RoSsIaCrIiLoIA

/* Is it legible? */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>

unsigned
gcd_my(unsigned a, unsigned b)
{
unsigned t;

if(a==0 || b==0)
return 0;
while(a!=0)
{if(a < b)
{t = a; a = b; b = t;}
a = a%b;
}
return b;
}

int domanda(char* a, char* fmt, ... );

int main(void)
{
int first, second, nread;
char input[9];

printf
("This program will figure out the greatest common\n"
"divisor in two numbers you give.\n");

nread = domanda("Please enter the numbers> ",
"%d%d", &first, &second) ;
if(!nread)
{
printf("You don't want to play? Ok.\n");
exit(0);
}

printf( "first=%d, second=%d\n", first, second );

first = first>=0 ? first: -first;
second = second>=0 ? second: -second;

printf("The GCD is %d\n", (int) gcd_my(first, second));
exit(0);
}


size_t fgetm(char* s, size_t n, FILE* iop)
{
int c;
char *cs;

if(!s) return 0;
else if( n==0 || iop==0 || feof(iop) )
{*s = 0; return 0;}

cs = s;
while(--n>0 && (c = getc(iop))!=EOF)
if((*cs++ = c)=='\n') break;
*cs = '\0';

return (cs - s);
}

unsigned conta(char* a)
{
unsigned num = 0;

if(!a) return 0;
while(*a)
{
if(*a=='%')
{
if(a[1]=='*' || a[1]=='%') ;
else ++num;
}
++a;
}
return num;
}


int domanda(char* a, char* fmt, ... )
{
va_list ap;
int h;
size_t j;
char buffer[258];


ricomincia:
if(feof(stdin))
return 0;

printf("%s", a); fflush(stdout);

if((j = fgetm(buffer, 256, stdin))==0)
return 0; /* entra solo EOF*/

if(buffer[j-1]!='\n' && !feof(stdin) )
{
fprintf(stderr, "Hai sforato la linea di 256 caratteri\n"
"PS: Si esce se tutte le conversioni sono riuscite\n"
"Oppure se EOF (^z o ^d) e' premuto\n");
while( j = fgetm(buffer, 256, stdin), j!=0 && buffer[j-1]!='\n');
if(feof(stdin)) return 0;
goto ricomincia;
}

j=conta(fmt);
va_start(ap, fmt);
errno = 0;
h=(vsscanf( buffer, fmt, ap)==j);
va_end(ap);

if(h && errno==0) return 1;
if(!errno)
fprintf(stderr, "Errore: numero di argomenti insufficienti\n");
else perror("Errore");
if(feof(stdin)) return 0;
goto ricomincia;

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top