Windows: Use snprintf() instead of StringCch*()
authorBart Van Assche <bvanassche@acm.org>
Mon, 6 Jan 2020 01:51:19 +0000 (17:51 -0800)
committerBart Van Assche <bvanassche@acm.org>
Mon, 6 Jan 2020 19:10:56 +0000 (11:10 -0800)
Use ANSI C functions instead of Windows-specific functions.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
os/windows/posix.c

index fd1d5582f1b33c73b84c21f4fffec4c8aafd79b9..e36453e9e8c77d129e760fe31424cb8af88c57e6 100644 (file)
 extern unsigned long mtime_since_now(struct timespec *);
 extern void fio_gettime(struct timespec *, void *);
 
-/* These aren't defined in the MinGW headers */
-HRESULT WINAPI StringCchCopyA(char *pszDest, size_t cchDest, const char *pszSrc);
-HRESULT WINAPI StringCchPrintfA(char *pszDest, size_t cchDest, const char *pszFormat, ...);
-
 int win_to_posix_error(DWORD winerr)
 {
        switch (winerr) {
@@ -312,11 +308,11 @@ char *ctime_r(const time_t *t, char *buf)
         * We don't know how long `buf` is, but assume it's rounded up from
         * the minimum of 25 to 32
         */
-       StringCchPrintfA(buf, 31, "%s %s %d %02d:%02d:%02d %04d\n",
-                               dayOfWeek[systime.wDayOfWeek % 7],
-                               monthOfYear[(systime.wMonth - 1) % 12],
-                               systime.wDay, systime.wHour, systime.wMinute,
-                               systime.wSecond, systime.wYear);
+       snprintf(buf, 32, "%s %s %d %02d:%02d:%02d %04d\n",
+                dayOfWeek[systime.wDayOfWeek % 7],
+                monthOfYear[(systime.wMonth - 1) % 12],
+                systime.wDay, systime.wHour, systime.wMinute,
+                systime.wSecond, systime.wYear);
        return buf;
 }
 
@@ -958,8 +954,8 @@ DIR *opendir(const char *dirname)
                                OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
        if (file != INVALID_HANDLE_VALUE) {
                CloseHandle(file);
-               dc = (struct dirent_ctx*)malloc(sizeof(struct dirent_ctx));
-               StringCchCopyA(dc->dirname, MAX_PATH, dirname);
+               dc = malloc(sizeof(struct dirent_ctx));
+               snprintf(dc->dirname, sizeof(dc->dirname), "%s", dirname);
                dc->find_handle = INVALID_HANDLE_VALUE;
        } else {
                DWORD error = GetLastError();
@@ -999,7 +995,8 @@ struct dirent *readdir(DIR *dirp)
        if (dirp->find_handle == INVALID_HANDLE_VALUE) {
                char search_pattern[MAX_PATH];
 
-               StringCchPrintfA(search_pattern, MAX_PATH-1, "%s\\*", dirp->dirname);
+               snprintf(search_pattern, sizeof(search_pattern), "%s\\*",
+                        dirp->dirname);
                dirp->find_handle = FindFirstFileA(search_pattern, &find_data);
                if (dirp->find_handle == INVALID_HANDLE_VALUE)
                        return NULL;
@@ -1008,7 +1005,7 @@ struct dirent *readdir(DIR *dirp)
                        return NULL;
        }
 
-       StringCchCopyA(de.d_name, MAX_PATH, find_data.cFileName);
+       snprintf(de.d_name, sizeof(de.d_name), find_data.cFileName);
        de.d_ino = 0;
 
        return &de;