Results 1 to 6 of 6

Thread: linux filename and total pathname limits - correct info across linux filesystems - c code

  1. #1

    Default linux filename and total pathname limits - correct info across linux filesystems - c code

    cat /usr/include/linux/limits.h

    ...
    #define NAME_MAX 255 /* # chars in a file name */
    #define PATH_MAX 4096 /* # chars in a path name including nul */
    ...

    I usually add slightly more than the minimum size in coding.


    One mentions some file systems have no pathname limit.

    https://serverfault.com/questions/95...imits-on-linux

    It should be noted, however, that many of the listed limits are not invariant, and at runtime, the value of the limit may differ from those given in this header, for the following reasons:


    • The limit is pathname-dependent.
    • The limit differs between the compile and runtime machines.

    For these reasons, an application may use the fpathconf(), pathconf(), and sysconf() functions to determine the actual value of a limit at runtime.
    https://stackoverflow.com/questions/...length-in-bash



    I need an accurate answer for c coding.

    What is filename limit and pathname size limit across modern linux file systems? Do they both include the null character('\0')? Differences in linux distros?

    Do need to use a function to get correct values for each?


    Thanks. Hopefully, I've explained this clearly enough.

  2. #2
    Join Date
    Jan 2017
    Location
    Nürnberg, Germany
    Posts
    279

    Default Re: linux filename and total pathname limits - correct info across linux filesystems - c code

    The POSIX-ly correct answer is using the fpathconf(3) function, not the predefined constants:
    Code:
    v:~ ▶ man -wK pathconf
    /usr/share/man/man1/getconf.1.gz
    /usr/share/man/man3/fpathconf.3.gz
    /usr/share/man/man3/getcwd.3.gz
    /usr/share/man/man3/confstr.3.gz
    /usr/share/man/man3/fpathconf.3.gz
    /usr/share/man/man3/posix_memalign.3.gz
    /usr/share/man/man3/statvfs.3.gz
    /usr/share/man/man3/readdir.3.gz
    /usr/share/man/man3/realpath.3.gz
    /usr/share/man/man3/sysconf.3.gz
    /usr/share/man/man7/signal-safety.7.gz
    /usr/share/man/man7/pthreads.7.gz
    /usr/share/man/man7/standards.7.gz
    /usr/share/man/man7/posixoptions.7.gz
    v:~ ▶ _
    The function conforms to POSIX.1-2001 and POSIX.1-2008.

    An alternative for shell scripting would be to use the getconf command:

    Code:
    v:~ ▶ getconf -a | grep -i path
    PATH_MAX                           4096
    _POSIX_PATH_MAX                    4096
    PATH                               /bin:/usr/bin
    CS_PATH                            /bin:/usr/bin
    v:~ ▶ _

  3. #3
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    27,132

    Default Re: linux filename and total pathname limits - correct info across linux filesystems - c code

    I am not sure about those limits. Are they from the days of ASCII only? Nowadays filenames are in UTF-8 (I would not recommend to go beyond ASCII for system files though) and thus it is important to know if such a limit is in characters or in bytes.
    Henk van Velden

  4. #4
    Join Date
    Sep 2012
    Posts
    5,907

    Default Re: linux filename and total pathname limits - correct info across linux filesystems - c code

    Quote Originally Posted by hcvv View Post
    it is important to know if such a limit is in characters or in bytes.
    File name in Unix (and Linux) is sequence of bytes.

  5. #5
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    27,132

    Default Re: linux filename and total pathname limits - correct info across linux filesystems - c code

    Quote Originally Posted by arvidjaar View Post
    File name in Unix (and Linux) is sequence of bytes.
    I agree it is. After all this is about programming and the amount of space that should be reserved for storing them.

    But the OP shows
    Code:
    #define NAME_MAX         255    /* # chars in a file name */
    and that thus has to be taken with care.
    Henk van Velden

  6. #6

    Default Re: linux filename and total pathname limits - correct info across linux filesystems - c code

    Thanks for the info.

    https://linux.die.net/man/3/pathconf

    https://www.man7.org/linux/man-pages...athconf.3.html

    https://www.gnu.org/software/libc/ma.../Pathconf.html


    These functions need a filename to get a general filename and path max values. ?? Any random filename??

    Is this correct? I'm not understanding the use of 'filename' in the function call.



    Code:
    #include <unistd.h>
    //long fpathconf(int fd, int name);
    //long pathconf(char *path, int name);
    
    int main(int argc, char *argv[])
    {
         char *tmp_filename = NULL;
         char *tmp_pathname = NULL;
    
         long filename_max_value;
         long pathname_max_value;
    
         filename_max_value= pathconf("filename.ext", _PC_NAME_MAX);
         pathname_max_value= pathconf("filename.ext", _PC_PATH_MAX);
    
         tmp_filename = (char*) malloc(filename_max_value);
         tmp_pathname = (char*) malloc(pathname_max_value);
    
         free(tmp_filename);
         free(tmp_pathname);
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •