This is the mail archive of the cygwin-apps mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [maybe-ITP] gamin


Lapo Luchini wrote:
> if (cchBufferLength < 4)
> return 0;
>   
Before being included in the mail this code was better indented, of
course.. let's try with an attach.
#include <windows.h>
#include <string.h>

BOOL EmGetVolumePathName(
  LPCTSTR lpszFileName,
  LPTSTR lpszVolumePathName,
  DWORD cchBufferLength
)
{
  if (cchBufferLength < 4)
    return 0;
  if (isalpha(lpszFileName[0])) {
    lpszVolumePathName[0] = lpszFileName[1] == ':' ? lpszFileName[0] : 'C';
    lpszVolumePathName[1] = ':';
    lpszVolumePathName[2] = '\\';
    lpszVolumePathName[3] = 0;
    return 1;
  }
  if (lpszFileName[0] == '\\' && lpszFileName[1] == '\\' && isalpha(lpszFileName[2])) {
    int slash = 0;
    while (*lpszFileName != 0 && slash < 4) {
      if (*lpszFileName == '\\')
        ++slash;
      if (cchBufferLength-- > 1)
        *lpszVolumePathName++ = *lpszFileName++;
      else
        return 0;
    }
    if (slash == 2)
      return 0;
    else if (slash == 3) {
      if (cchBufferLength-- > 1)
        *lpszVolumePathName++ = '\\';
      else
        return 0;
    }
    *lpszVolumePathName = 0;
    return 1;
  }
  return 0;
}

BOOL MyGetVolumePathName(
  LPCTSTR lpszFileName,
  LPTSTR lpszVolumePathName,
  DWORD cchBufferLength
)
{
  HINSTANCE hinstLib = LoadLibrary("Kernel32");
  if (hinstLib != NULL) {
    BOOL (*fun)(LPCTSTR, LPTSTR, DWORD) = (BOOL (*)(LPCTSTR, LPTSTR, DWORD))
      GetProcAddress(hinstLib, "GetVolumePathNameA");
    // If the function address is valid, call the function.
    if (fun != NULL) {
      BOOL ret = fun(lpszFileName, lpszVolumePathName, cchBufferLength);
      FreeLibrary(hinstLib);
      return ret;
    }
    FreeLibrary(hinstLib);
  }
  return EmGetVolumePathName(lpszFileName, lpszVolumePathName, cchBufferLength);
}

main() {
  char try[][80] = {
    "Z",
    "Z:",
    "Z:\\ciao",
    "\\\\cyberone",
    "\\\\cyberone\\Musica",
    "\\\\cyberone\\Musica\\ciao",
    "\\\\cyberone\\Musica-ciao-too-much--way-too-much",
    "\\\\\\cyberone"
  };
  char root[40];
  int i;
  BOOL b;
  for (i = 0; i < sizeof(try) / sizeof(try[0]); ++i) {
    printf("Orig = %s\n", try[i]);
    strcpy(root, "--------------------");
    b = MyGetVolumePathName(try[i], root, 40);
    printf("Real = %d %s\n", b, root);
    strcpy(root, "--------------------");
    b = EmGetVolumePathName(try[i], root, 40);
    printf("Emul = %d %s\n", b, root);
    putchar('\n');
  }
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]