S60 Open C
Open C Tips and Tricks

Open C Tips and Tricks

Table of Contents

Mapping standard C library error codes to Symbian error codes
errno example
perror example
Mapping between libc error codes and Symbian error codes

 


Mapping standard C library error codes to Symbian error codes

Whenever an API of the standard C library (libc) fails, the failure will be indicated by a return code, which will typically be a non-zero value or -1 (some APIs will return NULL (0) in case of an error ). In such cases, the developer can refer to errno for getting the last error code, which is defined in the header <errno.h>

libc uses errno for recording the last error that happened when any of the libc's API is invoked in that threads context. If the previous libc API returns successfully, it does not reset the value. So, if the user wants to know the error state of some particular API of libc, the user can set errno to 0, call the API, and then check the errno. Libc's errno is thread-specific; that is, any failure in libc's API in one thread will not change the errno of another thread. Each thread will have its own errno variable. Typically this variable will be part of TLS (Thread Local Storage).

 


errno example

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>

int main() {
   int fd = 0;
   /* Assuming file name specified does not exist! */
   FILE* fp = fopen("C:\\does_not_exist.txt", "r");
   if(fp) {
      printf("File Opened in Read Mode, error code is %d\n", errno);
      fclose(fp);
   }
   else {
      printf("File Open Failed with %d\n", errno);
      errno = 0;
   }
   /* Try to create the same file in write mode now */
   fd = open("C:\\does_not_exist.txt", O_WRONLY | O_CREAT);
   
   if(fd == -1) {
      printf("File create in write mode Failed with %d\n", errno);
   }
   else {
      printf("File Created for write, error code : %d\n", errno);
      errno = 0;
   }
   close(fd);
   return 0;
}

The output will be:

File Open Failed with 2
File Created for write, error code : 0

If the code errno = 0 is commented in the example, the second output message will display "error code : 2"

 


perror example

In addition to errno, the user can also use perror for getting the last error displayed as a string. The signature of perror is as follows:

void perror(const char *s);

perror() will write a message to the standard error output, which describes the last error encountered during a call to the libc function. If the passed argument is not NULL, it will display that message first, followed by a colon and the error message corresponding to the error code.

In the previous sample code, if the following codes were inserted after fopen and open respectively:

perror("fopen Status");
perror("open Status");

the output would look like the following (assuming that the file does not exist):

fopen Status : No such file or directory
File Open Failed with 2
open Status : Unknown error : 0
File Created for write, error code : 0

 


Mapping between libc error codes and Symbian error codes

The table below lists the standard C library error codes, which are defined in epoc32\include\stdapis\errno.h and mapping error codes in Symbian OS. Whenever the user gets any such error codes, the reason can be any of the mapping Symbian error codes.

Standard C library Symbian OS
ENOENT KErrNotFound or KErrPathNotFound
EINTR KErrCancel
ENOMEM KErrNoMemory
ENOSYS KErrNotSupported
EINVAL KErrArgument or KErrBadDescriptor or KErrBadName
ERANGE KErrTotalLossOfPrecision
EBADF KErrBadHandle
ERANGE KErrOverflow or KErrUnderflow
EEXIST KErrAlreadyExists
EPIPE KErrDied or KErrDisconnected or KErrSessionClosed or KErrEof or KErrServerTerminated
EACCES KErrInUse or KErrPermissionDenied or KErrAccessDenied or KErrLocked
EBUSY KErrServerBusy
ENODEV KErrUnknown or KErrHardwareNotAvailable or KErrDisMounted
EIO KErrWrite
ENOSPC KErrDiskFull or KErrDirFull
ECOMM KErrCommsLineFail or KErrCommsFrame or KErrCommsOverrun or KErrCommsParity or KErrCommsBreak
ETIMEDOUT KErrTimedOut
ECONNREFUSED KErrCouldNotConnect
EFAUL KErrTooBig
EDOM KErrDivideByZero or KErrBadPower

Give feedback of this section


©Nokia 2007

Back to top


This material, including documentation and any related computer programs, is protected by copyright controlled by Nokia. All rights are reserved. Copying, including reproducing, storing, adapting or translating, any or all of this material requires the prior written consent of Nokia. This material also contains confidential information, which may not be disclosed to others without the prior written consent of Nokia.

Nokia is a registered trademark of Nokia Corporation. S60 and logo is a trademark of Nokia Corporation. Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. Other company and product names mentioned herein may be trademarks or tradenames of their respective owners.