Thread: Anyone know a list of environment variable path standards?

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733

    Anyone know a list of environment variable path standards?

    What I mean is a list of the standards used in paths, like for example:

    Code:
    %VAR%
    $V
    $(VAR)
    Designing a pseudo file system for my cross-platform ABI so that reliant software can reduce the amount of work involved in path usage, for example pawfs://~ will always mean $(HOME) or %USERPROFILE% etc but it should also be valid to do something like this call:

    Code:
    #define PATH u8"$(HOME)/../.."
    pawfs__envpathn( dst, max, PATH, pawmbslen(PATH), pawfs_env_shell_ptr, 1 );
    Which will either fail (because HOME didn't exist) or produce "/home/user/../.." and I want to be able to produce a function that can validate a path based on what is allowed every supported OS and fail if it's not.

  2. #2
    Registered User
    Join Date
    Apr 2021
    Posts
    140
    Depending on your system, try either man 3 wordexp or try asking your favorite search engine for man wordexp The function wordexp is a POSIX function that does variable (and other!) substitution on a given string. Thus, it implements the "official" syntax for all POSIX programs, including /bin/sh, etc. If you look around, there is probably something similar available for Windows, but I don't know the name of it off the top of my head.

  3. #3
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by aghast View Post
    Depending on your system, try either man 3 wordexp or try asking your favorite search engine for man wordexp The function wordexp is a POSIX function that does variable (and other!) substitution on a given string. Thus, it implements the "official" syntax for all POSIX programs, including /bin/sh, etc. If you look around, there is probably something similar available for Windows, but I don't know the name of it off the top of my head.
    Did not know of that, thanks One thing I've noted so far is that is seems less friendly than the version I've made for my own:

    Code:
    typedef struct
    {
    	pawd leng, full;
    	pawmbc fill;
    	pawmbs name;
    } pawfs_env;
    
    /** @brief Identify name portion of string from current position (if it is
     * supposed to be)
     * @note refer to pawfs_env_cmdsh & pawfs_env_shell for example usage */
    typedef pawfs_env (*pawfs_env_cb)( pawmbs str, pawd len );
    
    /** @brief %VAR% */
    PAW_QCK_CB pawfs_env pawfs_env_cmdsh( pawmbs txt, pawd len )
    {
    	pawd i = 0, end = len - 1;
    	pawfs_env env = {0};
    	if ( *txt != '%' )
    		return env;
    	if ( txt[1] == '%' )
    	{
    		env.fill = '%';
    		env.full = 2;
    		return env;
    	}
    	env.name = txt + 1;
    	while ( ++i < end && txt[i] != '%' );
    	env.leng = i - 1;
    	env.full = i + (pawd)(env.name[i] == '%');
    	return env;
    }
    /** @brief $V $(VAR) */
    PAW_QCK_CB pawfs_env pawfs_env_shell( pawmbs txt, pawd len )
    {
    	pawd i = 0, end = len - 2;
    	pawfs_env env = {0};
    	if ( *txt != '$' )
    		return env;
    	if ( txt[1] != '(' )
    	{
    		env.full = 2;
    		if ( txt[1] == '$' )
    		{
    			env.fill = '$';
    			return env;
    		}
    		env.name = txt + 1;
    		env.leng = 1;
    		return env;
    	}
    	if ( txt[2] == ')' )
    	{
    		env.fill = ' ';
    		env.full = 3;
    		return env;
    	}
    	env.name = txt + 2;
    	while ( ++i < end && env.name[i] != ')' );
    	env.leng = i;
    	env.full = 2 + i + (pawd)(env.name[i] == ')');
    	return env;
    }
    ...
    PAW_ANT_CB pawd pawfs__envpathn
    	( pawmbc *dst, pawd max, pawmbs src, pawd len, pawfs_env_cb cb )
    {
    	pawd i = 0, j = 0, cpy = 0;
    	pawmbs text = src + len;
    	pawfs_env find = {0};
    	for ( i = 0; i < len; i += find.full )
    	{
    		find = cb( src + i, len - i );
    		if ( find.fill )
    			++j;
    		else if ( find.name )
    		{
    			text = pawenv_getnamen( find.name, find.leng );
    			j += pawmbslen( text );
    		}
    		else
    		{
    			find.full = 1;
    			++j;
    		}
    		find.full += (pawd)(!(find.full));
    	}
    	if ( max <= j )
    		return j;
    	for ( i = 0; i < len; i += find.full )
    	{
    		find = cb( src + i, len - i );
    		if ( find.fill )
    			dst[j++] = find.fill;
    		else if ( find.name )
    		{
    			text = pawenv_getnamen( find.name, find.leng );
    			if ( !text )]
    				return -1;
    			cpy = pawmbslen( text );
    			pawrawcpy( dst + j, text, cpy );
    			j += cpy;
    		}
    		else
    		{
    			find.full = 1;
    			dst[j++] = src[i];
    		}
    		find.full += (pawd)(!(find.full));
    	}
    	return 0;
    }
    So with this in mind I might forgo wordexp anyways

    Edit: Removed an out of date @return comment doc, was left over from my trial and error in the last hour or so

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-05-2021, 01:50 PM
  2. find home path of C Environment
    By tubby123 in forum C Programming
    Replies: 3
    Last Post: 07-03-2011, 02:10 PM
  3. Retrieving PATH Environment Variable
    By mercury529 in forum Windows Programming
    Replies: 9
    Last Post: 01-09-2007, 08:02 PM
  4. Setting path environment variable
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 09-01-2006, 03:03 PM
  5. Path / Environment in unix
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-10-2001, 04:52 AM

Tags for this Thread