Thread: Help with confstr() and pathconf()

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    28

    Question Help with confstr() and pathconf()

    Hey, I'm writing a program that checks various attributes of the system it's ran on for my operating systems class. I did every one except two...one using the function confstr(), the other using pathconf(). I looked at the man pages for both, and have tried to find examples on their use, but I just don't understand the syntax, and couldn't find any examples. Could you guys give me examples on how to use these two functions? Thanks!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm afraid I don't understand your confusion. Reading the man page for pathconf and looking
    at the _PC_ descriptions, it seems quite clear what it's used for.
    _PC_PATH_MAX

    returns the maximum length of a relative pathname
    when path or filedes is the current working direc-
    tory. The corresponding macro is _POSIX_PATH_MAX.
    Seems to me, that passed a file name, and this flag, you'll be told how long the path for the is
    allowed to be. The other flags/examples also seemed rather clear in their descriptions.

    The confstr man page even gives you an example of its use! Oh, sure, it's not 100% complete,
    they don't give you a full example, but you can easily do that yourself, as I did to write this post:
    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main( void )
    {
            char *pathbuf; size_t n;
    
            n = confstr(_CS_PATH,NULL,(size_t)0);
            if ((pathbuf = malloc(n)) == NULL) abort();
            confstr(_CS_PATH, pathbuf, n);
    
            printf("confstr gives us \'%s\'\n", pathbuf );
            free( pathbuf );
    
            return 0;
    }
    
    
    /*
     * my output:
     *
    confstr gives us '/bin:/usr/bin'
     *
     */
    I mean it wasn't that hard. Jumbling three keywords around should give you some examples
    and links on what confstr used for. Or what used for confstr. I'll leave it up to you to do more,
    because it isn't my project, and I don't much care to search further. In case you miss it, there
    seem to be quite a number of flags you can use with confstr.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    28

    Lightbulb

    Oh, I looked at the man pages and I was confused as to how to implement it. I was trying to use confstr the same way as you were, but I didn't know you could simply put NULL and (size_t)0 as the arguments. God, this class is going to eat me alive! You should see my next project, it's a doozie, and I only have two weeks to do it! Thank you very much.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by RazielX
    Oh, I looked at the man pages and I was confused as to how to implement it. I was trying to use confstr the same way as you were, but I didn't know you could simply put NULL and (size_t)0 as the arguments.
    Are you sure you looked at the man page?

    SYNOPSIS

    #define __USE_POSIX_2
    #include <unistd.h>

    size_t confstr(int
    name, char *buf, size_t len);
    DESCRIPTION

    confstr()[/b] gets the value of configuration - dependent
    string variables.

    The name argument is the system variable to be queried.
    The following variables are supported:

    _CS_PATH

    A value for the PATH variable which indicates where
    all the POSIX.2 standard utilities can be found.


    If buf is not NULL, and len is not zero, confstr() copies
    the value of the string to buf truncated to len - 1 char-
    acters if necessary, with a null character as termination.
    This can be detected by comparing the return value of con-
    fstr() against len.

    If len is zero and buf is NULL, confstr() just returns the
    value as defined below.

    RETURN VALUE

    If name does not correspond to a valid configuration vari-
    able, confstr() returns 0.
    EXAMPLES

    The following code fragment determines the path where to
    find the POSIX.2 system utilities:

    char *pathbuf; size_t n;

    n = confstr(_CS_PATH,NULL,(size_t)0);
    if ((pathbuf = malloc(n)) == NULL) abort();
    confstr(_CS_PATH, pathbuf, n);
    I remind you, you're under oath.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    I looked at the man pages on the SunOS 5 system at school. Even printed them out...and they were kinda confusing...It didn't look as simple as the one you just showed me. It probably was and I was just too stressed to pay attention. I was rushing to do the project while on my job yesterday evening, and it was due that night by midnight. What a mess. I'm starting on the next one today! (Not that I didn't start on the other one early, I just looked at the man pages, made a simple version of the program to get used to RCS and whatnot, and put it off for a week.)

Popular pages Recent additions subscribe to a feed