1

I have a C function that accepts directory path as a string. The function creates a directory at the given path.

int create_directory(const char *path) {
    // given path may be absolute or relative
    // step 1:-need to validate the given path(cross platform for both Linux and windows)
    // what are the criteria for path validation?

    // step 2:- check permission about user accessibility ,means can not create directory, if path is   like /usr/bin  or /root).but in root login then we create.
    So what are the criteria for validation?

    // step 3:-if directory, subdirectory already exist at the path, then return?

    // step 4:-if not exists then create directory ;  
}

basically I have problem in step 1 and step 2. I am not able to identify what the exact criteria for path validation are.

3 Answers 3

3

The best way to check the validity of a path given to you is to try the operation with it. The OS will give you an error code (errno or GetLastError() or something) and you can use that to determine why it wasn't possible.

0

I would let the underlying OS perform the validation.

Create the directory and then check the error code.

0

Linux uses the "/" to separate between directories in a given path while windows uses "\" plus almost all the windows paths begin with C:\\ or (D:\\ it doesn't matter the most important is the :) so you can just use a strchr() to look for those signs or you can just use perror() which is much easier (you'll have to include <errno.h>)

1
  • thanks for reply, forward slash or backward slash only one criteria. what about other validation criteria like path contain other special character,white space,//,\\, and many other.i want to validate all possible cases. Aug 21, 2013 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.