Re: [PATCH v8] man/man7/pathname.7: Add file documenting format of pathnames

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Jason,

On Mon, Jan 20, 2025 at 11:24:14AM -0500, Jason Yundt wrote:
> +.SS Program source
> +.\" SRC BEGIN (pathname_encoding_example.c)
> +.EX
> +#include <err.h>
> +#include <iconv.h>
> +#include <langinfo.h>
> +#include <locale.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <uchar.h>
> +\&
> +#define NELEMS(a)  (sizeof(a) / sizeof(a[0]))
> +\&
> +int
> +main(void)
> +{
> +    size_t size;
> +    char32_t utf32_pathname[] = U"example";

This should be const-qualified, since we won't be modifying it.  Please
add const where needed.

> +    char *locale_pathname;
> +    iconv_t cd;
> +    char *inbuf;
> +    size_t inbytesleft;
> +    char *outbuf;
> +    size_t outbytesleft;
> +    size_t iconv_result;
> +    FILE *fp;

Please group declarations of the same type in consecutive lines.
Shorter type names up and longer type names below.  For same length,
please use alphabetic order.

> +
> +    if (setlocale(LC_ALL, "") == NULL) {
> +        err(EXIT_FAILURE, "setlocale");
> +    }

Please don't use braces for a single statement.
Add a blank line where closing brace is now, for readability.

> +    size = NELEMS(utf32_pathname) * MB_CUR_MAX;
> +    locale_pathname = malloc(size);
> +    if (locale_pathname == NULL) {
> +      err(EXIT_FAILURE, "malloc");
> +    }
> +\&
> +    cd = iconv_open(nl_langinfo(CODESET), "UTF\-32");
> +    if (cd == (iconv_t) \- 1) {
> +        err(EXIT_FAILURE, "iconv_open");
> +    }
> +    inbuf = (char *) utf32_pathname;
> +    inbytesleft = sizeof utf32_pathname;
> +    outbuf = locale_pathname;
> +    outbytesleft = size;
> +    iconv_result =
> +        iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);

I would rename the variables {in,out}bytesleft to just {in,out}bytes.

> +    if (iconv_result == \-1) {
> +        err(EXIT_FAILURE, "iconv");
> +    }
> +    // This ensures that the conversion is 100% complete.
> +    // See iconv(3) for details.
> +    iconv_result =
> +        iconv(cd, NULL, &inbytesleft, &outbuf, &outbytesleft);
> +    if (iconv_result == \-1) {
> +        err(EXIT_FAILURE, "iconv");
> +    }
> +    if (iconv_close(cd) == \-1) {
> +        err(EXIT_FAILURE, "iconv_close");
> +    }
> +\&
> +    fp = fopen(locale_pathname, "w");
> +    fputs("Hello, world!\\n", fp);

For writing a '\', you should use \[rs] (that means "reverse solidus").


Have a lovely day!
Alex

> +    if (fclose(fp) == EOF) {
> +        err(EXIT_FAILURE, "fclose");
> +    }
> +\&
> +    free(locale_pathname);
> +    exit(EXIT_SUCCESS);
> +}
> +.EE
> +.\" SRC END
> +.SH SEE ALSO
> +.BR limits.h (0p),
> +.BR open (2),
> +.BR fpathconf (3),
> +.BR iconv (3),
> +.BR nl_langinfo (3),
> +.BR path_resolution (7),
> +.BR mount (8)
> -- 
> 2.47.1
> 

-- 
<https://www.alejandro-colomar.es/>

Attachment: signature.asc
Description: PGP signature


[Index of Archives]     [Kernel Documentation]     [Netdev]     [Linux Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux