How about
```c
len = (unsigned int)(ret) > buflen ? buflen : (unsigned int)ret;
```
to keep differences minimal?