main index

section index




/*-------------------------------------------------------------*/
/***************************************************************
 * Under Windows, '/' and '\' are recognized as directory      *
 * separator. However, under Unix, only '/' is valid.          *
 * So, if you want your code to be portable, use '/'.          *
 ***************************************************************/

/*-------------------------------------------------------------*/
/* Name : netwib_path_canon
   Description :
     Clean a filename ("//"->"/", "/./"->"/", "/aa/../"->"/", etc.).
   Input parameter(s) :
     pathname : filename (ex : "/tmp/dir/..//./file")
   Input/output parameter(s) :
   Output parameter(s) :
     *pcanonizedpathname : cleaned filename (ex : "/tmp/file")
   Normal return values :
     NETWIB_ERR_OK : ok
   Examples of canonical paths :
     ./file        ./dir        ./dir/file        ./dir/dir
     /file         /dir         /dir/file         /dir/dir
     ../file       ../dir       ../dir/file       ../dir/dir
     /             .
     c:./file      c:./dir      c:./dir/file      c:./dir/dir
     c:/file       c:/dir       c:/dir/file       c:/dir/dir
     c:../file     c:../dir     c:../dir/file     c:../dir/dir
     c:/           c:.
     \\s\sh\file   \\s\sh\dir   \\s\sh\dir/file   \\s\sh\dir/dir
     \\s\sh\
*/
netwib_err netwib_path_canon(netwib_constbuf *pathname,
                             netwib_buf *pcanonizedpathname);

/*-------------------------------------------------------------*/
/***************************************************************
 * User should canonize paths before using them. Indeed, netwib*
 * functions do not do it internally, and suppose the given    *
 * path is canonized.                                          *
 * Note : functions might work but it's not sure. For example, *
 * opening file "/dir/../dir2/file" can be rejected by the     *
 * underlying system.                                          *
 ***************************************************************/

/*-------------------------------------------------------------*/
/* Name : netwib_path_check
   Description :
     Check is a path is of given type
   Input parameter(s) :
     *pathname : pathname
     type : type to check against
   Input/output parameter(s) :
   Output parameter(s) :
     *pyes : NETWIB_TRUE is true
   Normal return values :
     NETWIB_ERR_OK : ok
*/
typedef enum {
  NETWIB_PATH_CHECKTYPE_ABSOLUTE = 1, /* see below */
  NETWIB_PATH_CHECKTYPE_RELATIVE,     /* see below */
  NETWIB_PATH_CHECKTYPE_ROOT,         /* see below */
  NETWIB_PATH_CHECKTYPE_START         /* see below */
} netwib_path_checktype;
/*
Examples for ABSOLUTE, RELATIVE, ROOT, START :
 pathname     absolute  relative  root   start
  ./file         0          1       0      0
  /file          1          0       0      0
  ../file        0          1       0      0
  /              1          0       1      1
  .              0          1       0      1
  c:./file       0          0       0      0
  c:/file        1          0       0      0
  c:../file      0          0       0      0
  c:/            1          0       1      1
  c:.            0          0       0      1
  \\s\sh\file    1          0       0      0
  \\s\sh\        1          0       1      1
*/
netwib_err netwib_path_check(netwib_constbuf *pathname,
                             netwib_path_checktype type,
                             netwib_bool *pyes);
#define netwib_path_check_absolute(pathname,pyes) netwib_path_check(pathname,NETWIB_PATH_CHECKTYPE_ABSOLUTE,pyes)
#define netwib_path_check_relative(pathname,pyes) netwib_path_check(pathname,NETWIB_PATH_CHECKTYPE_RELATIVE,pyes)
#define netwib_path_check_root(pathname,pyes) netwib_path_check(pathname,NETWIB_PATH_CHECKTYPE_ROOT,pyes)
#define netwib_path_check_start(pathname,pyes) netwib_path_check(pathname,NETWIB_PATH_CHECKTYPE_START,pyes)

/*-------------------------------------------------------------*/
/* Name : netwib_path_decode_xyz
   Description :
     Separate a path.
   Input parameter(s) :
     pathname : filename (ex : "/tmp/file")
   Input/output parameter(s) :
   Output parameter(s) :
     *proot : root (as defined in NETWIB_PATH_CHECK_ROOT)
     *pstart : start (as defined in NETWIB_PATH_CHECK_START)
     *pend : pathname=start+end
     *pparentdir : parent directory of pathname
     *plastitem : last part of a path
   Normal return values :
     NETWIB_ERR_OK : ok
   Examples :
     pathname   root    start   end       parentdir  lastitem
     ./file     Error1  .       ./file    .          file
     ./d/f      Error1  .       ./d/f     ./d        f
     /file      /       /       /file     /          file
     /d/f       /       /       /d/f      /d         f
     ../file    Error1  .       ../file   ..         file
     ../d/f     Error1  .       ../d/f    ../d       f
     /          /       /       /         Error2     /
     .          Error1  .       .         ..         .
     c:./file   Error1  c:.     ./file    c:.        file
     c:./d/f    Error1  c:.     ./d/f     c:./d      f
     c:/file    c:/     c:/     /file     c:/        file
     c:/d/f     c:/     c:/     /d/f      c:/d       f
     c:../file  Error1  c:.     ../file   c:..       file
     c:../d/f   Error1  c:.     ../d/f    c:../d     f
     c:/        c:/     c:/     /         Error2     /
     c:.        Error1  c:.     .         c:..       .
     \\s\t\file \\s\t\  \\s\t\  /file     \\s\t\     file
     \\s\t\d/f  \\s\t\  \\s\t\  /d/f      \\s\t\d    f
     \\s\t\     \\s\t\  \\s\t\  /         Error2     /
   Errors are :
     Error1 : NETWIB_ERR_SPPATHRELATIVE
     Error2 : NETWIB_ERR_SPPATHROOTDOTDOT
*/
typedef enum {
  NETWIB_PATH_DECODETYPE_ROOT = 1,
  NETWIB_PATH_DECODETYPE_START,
  NETWIB_PATH_DECODETYPE_END,
  NETWIB_PATH_DECODETYPE_PARENTDIR,
  NETWIB_PATH_DECODETYPE_LASTITEM
} netwib_path_decodetype;
netwib_err netwib_path_decode(netwib_constbuf *pathname,
                              netwib_path_decodetype type,
                              netwib_buf *pout);
#define netwib_path_decode_root(pathname,pout) netwib_path_decode(pathname,NETWIB_PATH_DECODETYPE_ROOT,pout)
#define netwib_path_decode_start(pathname,pout) netwib_path_decode(pathname,NETWIB_PATH_DECODETYPE_START,pout)
#define netwib_path_decode_end(pathname,pout) netwib_path_decode(pathname,NETWIB_PATH_DECODETYPE_END,pout)
#define netwib_path_decode_parentdir(pathname,pout) netwib_path_decode(pathname,NETWIB_PATH_DECODETYPE_PARENTDIR,pout)
#define netwib_path_decode_lastitem(pathname,pout) netwib_path_decode(pathname,NETWIB_PATH_DECODETYPE_LASTITEM,pout)

/*-------------------------------------------------------------*/
/* Name : netwib_path_init_xyz
   Description :
     Initialize a path.
   Input parameter(s) :
     See below
   Input/output parameter(s) :
     pout : path initialized
   Output parameter(s) :
   Normal return values :
     NETWIB_ERR_OK : ok
*/
typedef enum {
  NETWIB_PATH_INITTYPE_CONCAT = 1,
  NETWIB_PATH_INITTYPE_ABS,
  NETWIB_PATH_INITTYPE_RELA,
  NETWIB_PATH_INITTYPE_RELA2,
  NETWIB_PATH_INITTYPE_RELB,
  NETWIB_PATH_INITTYPE_RELB2
} netwib_path_inittype;
/*
   Definitions :
     concat : path from the beginning of dirname1 to
              pathname2
     abs : absolute path to pathname2 (pathname2 is in a
           file located in dirname1 directory)
     rela : relative path to go to pathname2 from a file
            in dirname1 directory (pathname2 is in a
            file located in dirname1 directory)
     rela2 : idem, except dircommon is pathname were dirname1
             and pathname2 are located
     relb : relative path to go to pathname2 from a file
            in dirname1 directory (pathname1 and pathname2
            are located in the same directory)
     relb2 : idem, except dircommon is pathname were dirname1
             and pathname2 are located
   Example :
     dirname1   pathname2  concat       abs
     ./d1       ./d2/f2    ./d1/d2/f2   Error2
     ./d1       ../d2/f2   ./d2/f2      Error2
     ./d1       /d2/f2     Error1       /d2/f2
     ../d1      ./d2/f2    ../d1/d2/f2  Error2
     ../d1      ../d2/f2   ../d2/f2     Error2
     ../d1      /d2/f2     Error1       /d2/f2
     /d1        ./d2/f2    /d1/d2/f2    /d1/d2/f2
     /d         ../d2/f2   /d2/f2       /d2/f2
     /d1        /d2/f2     Error1       /d2/f2
     isroot     /d2/f2     x/d2/f2      /d2/f2
   Errors are :
     Error1 : pathname2 is absolute and dirname1 is not a root
     Error2 : dirname1 is relative or pathname2 is relative
   Example :
     common dirname1   pathname2  rela          relb
     NULL   ./d1       ./d2/f2    ./d2/f2       ../d2/f2
     NULL   ./d1       ../d2/f2   ../d2/f2      ../../d2/f2
     NULL   ./d1       /d2/f2     Error3        Error4
     /d     ./d1       /d2/f2     ../../d2/f2   ../../d2/f2
     NULL   ../d1      ./d2/f2    ./d2/f2       Error5
     /d     ../d1      ./d2/f2    ./d2/f2       ../d/d2/f2
     NULL   ../d1      ../d2/f2   ../d2/f2      ../d2/f2
     NULL   ../d1      /d2/f2     Error3        Error4
     /d     ../d1      /d2/f2     ../d2/f2      ../d2/f2
     NULL   /d1        ./d2/f2    ./d2/f2       Error4
     /d     /d1        ./d2/f2    ./d2/f2       ../d/d2/f2
     NULL   /d1        ../d2/f2   ../d2/f2      Error4
     /d     /d1        ../d2/f2   ../d2/f2      ../d2/f2
     NULL   /d1        /d2/f2     ../d2/f2      ../d2/f2
   The errors have the same code : NETWIB_ERR_SPPATHCANTINIT
*/
netwib_err netwib_path_init(netwib_constbuf *dircommon,
                            netwib_constbuf *dirname1,
                            netwib_constbuf *pathname2,
                            netwib_path_inittype type,
                            netwib_buf *pout);
#define netwib_path_init_concat(dirname1,pathname2,pout) netwib_path_init(NULL,dirname1,pathname2,NETWIB_PATH_INITTYPE_CONCAT,pout)
#define netwib_path_init_abs(dirname1,pathname2,pout) netwib_path_init(NULL,dirname1,pathname2,NETWIB_PATH_INITTYPE_ABS,pout)
#define netwib_path_init_rela(dirname1,pathname2,pout) netwib_path_init(NULL,dirname1,pathname2,NETWIB_PATH_INITTYPE_RELA,pout)
#define netwib_path_init_rela2(dircommon,dirname1,pathname2,pout) netwib_path_init(dircommon,dirname1,pathname2,NETWIB_PATH_INITTYPE_RELA2,pout)
#define netwib_path_init_relb(dirname1,pathname2,pout) netwib_path_init(NULL,dirname1,pathname2,NETWIB_PATH_INITTYPE_RELB,pout)
#define netwib_path_init_relb2(dircommon,dirname1,pathname2,pout) netwib_path_init(dircommon,dirname1,pathname2,NETWIB_PATH_INITTYPE_RELB2,pout)




main index

section index