[PATCH] ioctl_tty.2: Add example how to get or set baudrate on the serial port

Alejandro Colomar (man-pages) alx.manpages@gmail.com
Fri Jul 30 11:47:06 GMT 2021


Hi Pali,

On 7/30/21 11:53 AM, Pali Rohár wrote:
> Signed-off-by: Pali Rohár <pali@kernel.org>

Thanks for the patch!

Please see some comments below.

Cheers,

Alex


> ---
>   man2/ioctl_tty.2 | 60 ++++++++++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 60 insertions(+)
> 
> diff --git a/man2/ioctl_tty.2 b/man2/ioctl_tty.2
> index 0b0083c671a7..9d394572ae93 100644
> --- a/man2/ioctl_tty.2
> +++ b/man2/ioctl_tty.2
> @@ -750,6 +750,66 @@ main(void)
>       close(fd);
>   }
>   .EE
> +.PP
> +Get or set arbitrary baudrate on the serial port.
> +.PP
> +.EX
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <sys/types.h>
> +#include <fcntl.h>
> +#include <unistd.h>
> +#include <sys/ioctl.h>
> +#include <asm/termbits.h>

Unless there's a reason to use a specific include order (and if so, add 
a comment), please use alphabetical order.

> +
> +int
> +main(int argc, char *argv[])
> +{
> +#if !defined(TCGETS2) || !defined(TCSETS2) || !defined(BOTHER)
> +    fprintf(stderr, "TCGETS2, TCSETS2 or BOTHER is unsupported\\n");
> +    return 1;
> +#else

Do we want the program to compile if those are unsupported?

Maybe you can #error there and simplify the reader having to parse the 
preprocessor directive mentally:

#if !defined...
# error ...
#endif

I know it's non-standard, but I think it's common enough so that we can 
use it here.

> +    struct termios2 tio2;
> +    int fd, rc;
> +
> +    if (argc != 2 && argc != 3) {
> +        fprintf(stderr, "Usage: %s device [new_baudrate]\\n", argv[0]);

We use \e for printing the escape character.  Not \\
CC: Branden

See groff_man(7):
    Portability
        [...]

        Similar  caveats  apply  to escapes.  Some escape sequences
        are however required for correct typesetting  even  in  man
        pages and usually do not cause portability problems:

        [...]

        \e     Widely used in man pages to  represent  a  backslash
               output  glyph.  It works reliably as long as the .ec
               request is not used, which should  never  happen  in
               man pages, and it is slightly more portable than the
               more exact ‘\(rs’  (“reverse  solidus”)  escape  se‐
               quence.


> +        return 1;
> +    }
> +
> +    fd = open(argv[1], O_RDWR | O_NONBLOCK | O_NOCTTY);
> +    if (fd < 0) {
> +        perror("open");
> +        return 1;

exit(EXIT_FAILURE);

> +    }
> +
> +    rc = ioctl(fd, TCGETS2, &tio2);
> +    if (rc) {
> +        perror("TCGETS2");
> +        close(fd);
> +        return 1;

exit(3)

> +    }
> +
> +    printf("%u\\n", tio2.c_ospeed);

\e

> +
> +    if (argc == 3) {
> +        tio2.c_cflag &= ~CBAUD;
> +        tio2.c_cflag |= BOTHER;
> +        tio2.c_ospeed = tio2.c_ispeed = atoi(argv[2]);
> +
> +        rc = ioctl(fd, TCSETS2, &tio2);
> +        if (rc) {
> +            perror("TCSETS2");
> +            close(fd);
> +            return 1;

exit(3)

> +        }
> +    }
> +
> +    close(fd);
> +    return 0;

exit(3)

> +#endif
> +}
> +.EE
>   .SH SEE ALSO
>   .BR ldattach (1),
>   .BR ioctl (2),
> 


-- 
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/


More information about the Libc-alpha mailing list