# unable to open '/dev/cuaU0'



## balanga (Oct 17, 2018)

I trying to debug a program which accesses a serial port - /dev/cuaU0 which does exist and I can access it using `cu -l /dev/cuaU0 -s 115200` but when I run the program through a debugger I get the following error:-



> /root/projects/kwuartboot/kwuartboot: unable to open '/dev/cuaU0': m



This is from

```
int
main(int argc, char *argv[])
{
    struct termios tio;
    char *dev;
    char *fname;
    int tty_fd, file_fd;
    int res;

    argv0 = argv[0];

    if (argc != 3)
    usage();

    dev   = argv[1];
    fname = argv[2];

    memset(&tio, 0, sizeof(tio));
    tio.c_iflag = 0;
    tio.c_oflag = 0;
    tio.c_cflag = CS8|CREAD|CLOCAL;
    tio.c_lflag = 0;
    tio.c_cc[VMIN]  = 1;
    tio.c_cc[VTIME] = 5;

    tty_fd = open(dev, O_RDWR | O_NONBLOCK);
    if (tty_fd < 0) {
    fprintf(stderr, "%s: unable to open '%s': %m\n", argv0, dev);
    exit(1);
    }
```

According to `gdb`:-


> (gdb) p tty_fd
> $2 = 3



Can anyone explain what is going  on?


----------



## SirDice (Oct 17, 2018)

Check `errno` to see the reason:

```
If successful, open() and openat() return a non-negative integer, termed
     a file descriptor.  They return -1 on failure, and set errno to indicate
     the error.
```


----------



## yuripv (Oct 17, 2018)

Apparently, we don't support `%m' conversion (that's string for current errno, right?).  Try using 
	
	



```
err(1, "%s: unable to open '%s'", argv0, dev);
```
 instead of fprintf() and exit() calls.


----------



## SirDice (Oct 17, 2018)

yuripv said:


> Apparently, we don't support `%m' conversion (that's string for current errno, right?).


It's specific to glibc. 

```
The ‘%m’ conversion prints the string corresponding to the error code in errno. See Error Messages. Thus:

fprintf (stderr, "can't open `%s': %m\n", filename);

is equivalent to:

fprintf (stderr, "can't open `%s': %s\n", filename, strerror (errno));

The ‘%m’ conversion is a GNU C Library extension.
```
https://www.gnu.org/software/libc/manual/html_node/Other-Output-Conversions.html


----------

