以write为例子.
in Userspace:
int ret;
ret=write(serial_fd,(char *)write_buf,sizeof(write_buf));
if(ret<0)
{
printf("strerror: %s\n", strerror(errno));
}
in Driver
xxx_write()
{
return -EAGAIN;//Try_again
}
A common mistake is to do
if (somecall() == -1) {
printf("somecall() failed\n");
if (errno == ...) { ... }
}
where errno no longer needs to have the value it had upon return from somecall() (i.e., it may have been changed by the printf()). If the value of errno should be preserved across a library call, it must be saved:
if (somecall() == -1) {
int errsv = errno;
printf("somecall() failed\n");
if (errsv == ...) { ... }
}
It was common in traditional C to declare errno manually (i.e., extern int errno) instead of including <errno.h>. Do not do this. It will not work with modern versions of the C library. However, on (very) old Unix systems, there may be no <errno.h> and the declaration is needed.