Thread: errno

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    11

    Question errno

    How can i get the number of errno to pass to a function?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,339
    errno is a global variable that is always available to your program. Just use it like you would an int that you defined yourself.

    And, since it's global, your function already has access to it.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    #include <errno.h>
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    11

    Question

    I know they are global variable, but i need to control an array with the value passed by errno. But, they not works. Here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <errno.h>
    
    const char *descricao[] = {"Operação não permitida", "Arquivo ou diretório inexistente"};
    
    const char *abrir_arquivo(char *arquivo)
    {
    	FILE *fp;
    	if ((fp = fopen(arquivo, "r")) == NULL)
    		return descricao[errno];
    	else
                    return "Ok";
    }
    
    int main (void)
    {
    	printf("%s\n", abrir_arquivo("asdasdasd"));
    	return 1;
    }
    This code returns this following error message:
    Segmentation fault (core dumped)

    Its probably the errno is greater than 1, then the error is displayed.

    This follows worked perfectly, but has much work (I need to do this in 92 messages) and the code is not professional:

    Code:
    char *erro(int erro)
    {
    
    	switch(erro)
    	{
    		case 1:
    			return "Operação não permitida";
                            break;
    		case 2:
    			return "Arquivo ou diretório inexistente";
                            break;
    	}
    	
    }
    
    const char *abrir_arquivo(char *arquivo)
    {
    	FILE *fp;
    	if ((fp = fopen(arquivo, "r")) == NULL)
    		return erro(errno);
    	else
                    return "Ok";
    }
    
    int main (void)
    {
    	printf("%s\n", abrir_arquivo("asdasdasd"));
    	return 1;
    }
    My question is:
    How can I pass the correct number of errno to my function, so that this error is not more show?

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Eh? Why not just call strerror()? The whole purpose of strerror() is to convert errno values to strings. It uses the locale, so it should display in the correct language. As a bonus, your error messages will work in other languages as well.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Or just perror?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Dave_Sinkula View Post
    Or just perror?
    Sure, if all you're going to do is display the message anyway, just call perror().

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    11

    Exclamation

    I do not want to call strerror nor perror because I must translate the error messages to Portuguese.

    Do you know any way to translate them?

    Thanks for the help.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    11

    Wink

    How can I access the locale to translate then?

    Thanks and sorry for my bad reply...

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,339
    Sounds like a couple hours of work for someone who knows English and Portuguese.
    Mainframe assembler programmer by trade. C coder when I can.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    11

    Talking

    I know english and portuguese. Im from Brazil.

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by galindro View Post
    How can I access the locale to translate then?

    Thanks and sorry for my bad reply...
    The locale is supposed to be set automatically. If you are running a Portuguese version of Windows, then perror() should automatically show error messages in Portuguese. If you're running some form of UNIX, then you just need to make sure your locale is configured properly. How you do it is OS-dependent.

    Point begin, you don't have to do anything in the code to make it happen.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by galindro View Post
    My question is:
    How can I pass the correct number of errno to my function, so that this error is not more show?
    Your code is correct, except that judging from your working code:
    Code:
    char *erro(int erro)
    {
    
    	switch(erro)
    	{
    		case 1:
    			return "Opera&#231;&#227;o n&#227;o permitida";
                            break;
    		case 2:
    			return "Arquivo ou diret&#243;rio inexistente";
                            break;
    	}
    	
    }
    Your array probably should be:
    Code:
    const char *descricao[] = {"OK", "Opera&#231;&#227;o n&#227;o permitida", "Arquivo ou diret&#243;rio inexistente"};
    Your previous code would attempt to use the third element of an array with 2 elements - which damn well should segfault.

    Also, in Linux/Unix, most likely setting the environment variable C_LOCALE to something sensible. More info by doing "man locale" and listing the locale names by "locale -a".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    11

    Thumbs up

    matsp, thank you for the help.

    The errno messages of FreeBSD, returns number between 1 and 92. The index of arrays in C, begins with 0. Here is the error. The errno returns me the number 2, but the max value of my array is 1!!!

    Here is the correct code:

    Code:
    const char *descricao[] = {"", "Operação não permitida", "Arquivo ou diretório inexistente"};
    
    const char *abrir_arquivo(char *arquivo)
    {
    	FILE *fp;
    	int *a;
    	
    	if ((fp = fopen(arquivo, "r")) == NULL)
    	{
    		if ((errno >= 0) && (errno <= 2))
    			return descricao[errno];
    		else
    			return "Erro desconhecido";
    	}		
    	else
    		return "Aberto";
    }
    
    int main (void)
    {
    	printf("%s\n", abrir_arquivo("asdasdasd"));
    	return 1;
    }
    Thanks for all

    Best Regards!


  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    11

    Unhappy

    Now, I've got the same error, when I try to concat strings:

    Segmentation fault (core dumped)

    Here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <errno.h>
    
    #define INTSIZE 2
    
    char *itoa(int value)
    {
    	int count, i, sign;
    	char *ptr, *string, *temp;
    
    	count = 0;
    	if ((sign = value) < 0) 
    	{ 						
    		value = -value;		
    		count++; 			
    	}
    
    
    	temp = (char *) malloc(INTSIZE + 2);
    	if (temp == NULL)
    	{
    		return(NULL);
    	}
    	memset(temp,'\0', INTSIZE + 2);
    
    	string = (char *) malloc(INTSIZE + 2);
    	if (string == NULL)
    	{
    		return(NULL);
    	}
    	memset(string,'\0', INTSIZE + 2);
    	ptr = string; 
    
    
    	do
    	{
    		*temp++ = value % 10 + '0'; 
    		count++;
    	} while (( value /= 10) >0);
    
    	if (sign < 0) 
    		*temp++ = '-';
    
    	*temp-- = '\0'; 
    
    	for (i = 0; i < count; i++, temp--, ptr++)
    	{
    		memcpy(ptr,temp,sizeof(char));
    	}
    
    	return(string);
    }
    
    char *descricao[] = {"", "Operação não permitida", "Arquivo ou diretório inexistente"};
    
    char *abrir_arquivo(char *arquivo)
    {
    	FILE *fp;
    	char *erro;
    
      
    	if ((fp = fopen(arquivo, "r")) == NULL)
    	{
    		if ((errno >= 0) && (errno <= 2))
    		{
    			strcat(descricao[errno]," - Cód.: ");
    			strcat(descricao[errno],itoa(errno));
    			return descricao[errno];
    		}	
    		else
    			return "Erro desconhecido";
    	}		
    	else
    		return "Aberto";
    }
    
    int main (void)
    {
    	printf("%s\n", abrir_arquivo("asdasdasd"));
    	return 1;
    }

    Does anyone know what might be happening?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to clean errno?
    By kotoko in forum C Programming
    Replies: 20
    Last Post: 05-14-2009, 12:21 PM
  2. errno function, error TLS/non TLS
    By fran.b in forum C Programming
    Replies: 13
    Last Post: 03-25-2009, 08:06 AM
  3. /usr/bin/ld: errno: TLS definition in /lib/libc.so.6
    By nasim751 in forum C Programming
    Replies: 4
    Last Post: 02-25-2009, 04:15 AM
  4. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM