Example #1
0
newdirec()
{
	register char	*direc;
	FUNC_EXTERN char	*getfilenm();
	LOCATION	loc;
	char		locbuf[MAX_LOC+1];
	i4		status;
	char		errbuf[ER_MAX_LEN];

	direc = getfilenm();
	/* if the directory is not specified, don't do anything. */
	if (!*direc)
		return;
	STcopy(direc, locbuf);
	if (status = LOfroms(PATH, locbuf, &loc))
	{
		ERreport(status, errbuf);
		putprintf(ERget(F_MO0027_Error_in_path_name), errbuf);
		return;
	}
	if (status = LOchange(&loc))
	{
		ERreport(status, errbuf);
		putprintf(ERget(F_MO0028_Cannot_change_dir), direc, errbuf);
	}
}
Example #2
0
/*
** Name: check_path
**
** Description:
**      Function to test the specified path for:
**          1. Valid characters.
**          2. An existing or valid parent directory.
**          3. Write permissions for 2.
**
** Inputs:
**      chkpath     pointer to the path string for validation.
**      eflags      flags specifying the actions to be taken.
**
** Outputs:
**      None.
**
** Returns:
**      OK          Path validation successful.
**      !OK         Path validation failed.
**
** History:
**      27-Jun-2005 (fanra01)
**          Created.
**      11-Jul-2005 (fanra01)
**          Return a more specific status code.
*/
static STATUS
check_path( char* chkpath, int eflags )
{
    STATUS          status = II_SUCCESSFUL;
    STATUS          rc;
    int             retcode = FAIL;
    LOCATION        tloc;                      /* target location */
    LOCATION        wloc;                      /* working location */
    LOCATION        cloc;                      /* working location */
    LOINFORMATION   linfo;
    i4              info;
    char*           temp = NULL;
    char*           path = NULL;
    char*           work = NULL;
    char*           curr = NULL;
    char*	        d;
    char*           p;
    char*           f;
    char*           e;
    char*           v;
    
    char*           s;

    while(TRUE)
    {
        /*
        ** Allocate working area up front.  Saves declaring arrays on the
        ** stack.
        */
        if ((temp = MEreqmem( 0, (MAX_LOC+1) * 8, TRUE, &status )) == NULL)
        {
            break;
        }
        
        /*
        ** Initialize working pointers with memory
        */
        path = temp;
        work = temp + MAX_LOC + 1;
        curr = work + MAX_LOC + 1;
        d = curr + MAX_LOC + 1;
        p = d + MAX_LOC + 1;
        f = p + MAX_LOC + 1;
        e = f + MAX_LOC + 1;
        v = e + MAX_LOC + 1;

        /*
        ** Initialize a location structure with the specified path
        ** string.
        */
        if ((eflags & (II_CHK_PATHCHAR | II_CHK_PATHDIR | II_CHK_PATHPERM)) &&
            (LOfroms( PATH, chkpath, &tloc ) != OK))
        {
            status = II_BAD_PATH;
            break;
        }

        /*
        ** Perform an illegal characters check, for all path tests.
        */
        if ((eflags & (II_CHK_PATHCHAR | II_CHK_PATHDIR | II_CHK_PATHPERM)) &&
            (check_path_chars( &tloc, &rc )))
        {
            switch(rc)
            {
                case LO_BAD_DEVICE:
                    status = II_BAD_PATH;
                    break;
                case LO_NOT_PATH:
                case LO_NOT_FILE:
                default:
                    status = II_INVAL_CHARS_IN_PATH;
                    break;
            }
            break;
        }

        /*
        ** Duplicate the specified path location into a work location.
        */
        LOcopy( &tloc, work, &wloc );
        
        /*
        ** Create an empty location for the current working device,
        ** split the target path into components and
        ** create a location of the target device.
        */
        if ((eflags & (II_CHK_PATHDIR | II_CHK_PATHPERM)) &&
            ((status = LOfroms( PATH, curr, &cloc )) == OK) &&
            ((status = LOdetail( &wloc, d, p, f, e, v )) == OK) &&
            ((status = LOcompose( d, CURR_DIR, NULL, NULL, NULL,
            &cloc )) == OK))
        {
            /*
            ** Save the current working directory
            */
            LOsave();
            
            /*
            ** Change working path to the target device
            */
            status = LOchange( &cloc );
        
            /*
            ** Starting with the whole path work backwards looking for
            ** a valid directory
            */
            for (s=work, info=0;
                (status == OK) && (retcode != OK) && (s != NULL);
                )
            {
                if ((status = LOfroms( PATH, p, &wloc )) != OK)
                {
                    status = II_BAD_PATH;
                    break;
                }
                /*
                ** Reset requested information flags for each iteration.
                */
                info = (LO_I_TYPE | LO_I_PERMS);

                switch(retcode = LOinfo( &wloc, &info, &linfo ))
                {
                    case OK:
                        /*
                        ** If the path or permission test is requested and
                        ** type info is returned test for directory flag. 
                        */
                        if ((eflags & (II_CHK_PATHDIR | II_CHK_PATHPERM)) &&
                            ((info & LO_I_TYPE) == LO_I_TYPE))
                            status = (linfo.li_type == LO_IS_DIR) ? OK : II_PATH_NOT_DIR;

                        /*
                        ** If the permission test is requested and
                        ** permissions are returned test the flags for read
                        ** and write.
                        */
                        if ((status == OK) && (eflags & II_CHK_PATHPERM))
                        {
                            if (((info & LO_I_PERMS) == LO_I_PERMS) &&
                                (linfo.li_perms & (LO_P_READ|LO_P_WRITE))
                                == (LO_P_READ|LO_P_WRITE))
                            {
                                /*
                                ** Read and write permission
                                */
                                break;
                            }
                            else
                            {
                                /*
                                ** missing a permission
                                */
                                status = II_PATH_CANNOT_WRITE;
                            }
                        }
                        else
                        {
                            break;
                        }
                    case LO_NO_SUCH:
                        /*
                        ** Look backwards for the next path separator
                        */
                        if((s = STrindex( p, PATH_SEPARATOR, 0 )) != NULL)
                        {
                            /*
                            ** If separator found truncate the path
                            ** otherwise the start of the path has
                            ** been reached, update string to test the
                            ** root directory.
                            */
                            if (s != p)
                            {
                                *s = '\0';
                            }
                            else
                            {
                                *(s+1) = '\0';
                            }
                        }
                        else
                        {
                            /*
                            ** A root path character was included in the
                            ** path that has been reached and still no
                            ** installable area found.
                            */                                
                            if (p && *p && *p == SLASH)
                            {
                                status = II_BAD_PATH;
                            }
                            else
                            {
                                /*
                                ** A relative path was specified and no
                                ** installable area has been found.
                                ** Test the current working directory of the
                                ** target device.
                                */
                                if ((status = LOgt( p, &wloc )) == OK)
                                {
                                    /*
                                    ** Reset temporary pointer to a work
                                    ** area to satisfy the loop condition.
                                    */
                                    s = work;
                                }
                            }
                        }
                        break;
                    default:
                        status = II_BAD_PATH;
                        break;
                }
            }
            break;
        }
        else
        {
            if (status != II_SUCCESSFUL)
            {
                status = II_BAD_PATH;
            }
            break;
        }
    }
    /*
    ** Free the working area
    */
    if (temp != NULL)
    {
        MEfree( temp );
    }
    return(status);
}