fullpath.c 3.4 KB
Newer Older
1
/*****************************************************************************\
S
Shengliang Guan 已提交
2 3 4 5 6 7 8 9 10 11
*                                                                             *
*   Filename	    fullpath.c						      *
*									      *
*   Description     Get the absolute pathname for a relative UTF-8 path       *
*                                                                             *
*   Notes	                                                              *
*                                                                             *
*   History								      *
*    2016-09-13 JFL Created this module with routine from truename.c.         *
*                                                                             *
12
*         Copyright 2016 Hewlett Packard Enterprise Development LP          *
S
Shengliang Guan 已提交
13 14 15 16 17 18 19 20 21
* Licensed under the Apache 2.0 license - www.apache.org/licenses/LICENSE-2.0 *
\*****************************************************************************/

#define _CRT_SECURE_NO_WARNINGS 1 /* Avoid Visual C++ security warnings */

#define _UTF8_SOURCE /* Generate the UTF-8 version of routines */

#include <stdlib.h>
#include <malloc.h>
S
TD-1057  
Shengliang Guan 已提交
22
#include "msvcLimits.h"
S
Shengliang Guan 已提交
23 24 25

#ifdef _WIN32	/* Automatically defined when targeting a Win32 application */

S
TD-1057  
Shengliang Guan 已提交
26
#include "msvcWindows.h"		/* Also includes MsvcLibX' WIN32 UTF-8 extensions */
S
Shengliang Guan 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

/*---------------------------------------------------------------------------*\
*                                                                             *
|   Function:	    _fullpathU						      |
|                                                                             |
|   Description:    Get the absolute pathname for a relative UTF-8 path       |
|                                                                             |
|   Parameters:     See MSVC's _fullpath() in stdlib.h			      |
|                                                                             |
|   Return value:   Pointer to the full pathname, or NULL if error	      |
|                                                                             |
|   Notes:	    Warning: Windows' GetFullPathname and MSVC's _fullpath    |
|		    trim trailing dots and spaces from the path.	      |
|		    This derived function reproduces the bug.		      |
|                   The caller MUST add trailing dots & spaces back if needed.|
|                                                                             |
|   History:								      |
|    2014-03-25 JFL Created this routine.				      |
|    2017-01-30 JFL Fixed bug when the output buffer is allocated here.	      |
*                                                                             *
\*---------------------------------------------------------------------------*/

char *_fullpathU(char *absPath, const char *relPath, size_t maxLength) {
  char *absPath0 = absPath;
  DWORD n;
  if (!absPath) { /* Then allocate a buffer for the output string */
    maxLength = UTF8_PATH_MAX;
    absPath = malloc(maxLength); /* Worst case for UTF-8 is 4 bytes/Unicode character */
    if (!absPath) return NULL;
  }
  n = GetFullPathNameU(relPath, (DWORD)maxLength, absPath, NULL);
  if (!n) {
    errno = Win32ErrorToErrno();
    if (!absPath0) free(absPath);
    return NULL;
  }
  if (!absPath0) absPath = realloc(absPath, strlen(absPath) + 1);
  return absPath;
}

#endif /* defined(_WIN32) */