18bf80f4bSopenharmony_ci/*
28bf80f4bSopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
38bf80f4bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48bf80f4bSopenharmony_ci * you may not use this file except in compliance with the License.
58bf80f4bSopenharmony_ci * You may obtain a copy of the License at
68bf80f4bSopenharmony_ci *
78bf80f4bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88bf80f4bSopenharmony_ci *
98bf80f4bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108bf80f4bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118bf80f4bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128bf80f4bSopenharmony_ci * See the License for the specific language governing permissions and
138bf80f4bSopenharmony_ci * limitations under the License.
148bf80f4bSopenharmony_ci */
158bf80f4bSopenharmony_ci#include "dir.h"
168bf80f4bSopenharmony_ci
178bf80f4bSopenharmony_ci#if _WIN32
188bf80f4bSopenharmony_ci#include <windows.h>
198bf80f4bSopenharmony_ci#include <string>
208bf80f4bSopenharmony_cistruct DIR {
218bf80f4bSopenharmony_ci    HANDLE handle;
228bf80f4bSopenharmony_ci    WIN32_FIND_DATAA data;
238bf80f4bSopenharmony_ci    dirent cur;
248bf80f4bSopenharmony_ci};
258bf80f4bSopenharmony_ciDIR* opendir(const char* path)
268bf80f4bSopenharmony_ci{
278bf80f4bSopenharmony_ci    DIR* d = new DIR();
288bf80f4bSopenharmony_ci    std::string tmp = path;
298bf80f4bSopenharmony_ci    tmp.append("\\");
308bf80f4bSopenharmony_ci    tmp.append("*.*");
318bf80f4bSopenharmony_ci    d->handle = FindFirstFileA(tmp.c_str(), &d->data);
328bf80f4bSopenharmony_ci    if (d->handle == INVALID_HANDLE_VALUE) {
338bf80f4bSopenharmony_ci        delete d;
348bf80f4bSopenharmony_ci        return nullptr;
358bf80f4bSopenharmony_ci    }
368bf80f4bSopenharmony_ci    return d;
378bf80f4bSopenharmony_ci}
388bf80f4bSopenharmony_cistruct dirent* readdir(DIR* d)
398bf80f4bSopenharmony_ci{
408bf80f4bSopenharmony_ci    if (d->data.cFileName[0] == 0) {
418bf80f4bSopenharmony_ci        // end.
428bf80f4bSopenharmony_ci        return nullptr;
438bf80f4bSopenharmony_ci    }
448bf80f4bSopenharmony_ci    strcpy(d->cur.d_name, d->data.cFileName);
458bf80f4bSopenharmony_ci    if (d->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
468bf80f4bSopenharmony_ci        d->cur.d_type = DT_DIR;
478bf80f4bSopenharmony_ci    } else {
488bf80f4bSopenharmony_ci        d->cur.d_type = DT_REG;
498bf80f4bSopenharmony_ci    }
508bf80f4bSopenharmony_ci    if (false == FindNextFileA(d->handle, &d->data)) {
518bf80f4bSopenharmony_ci        // clear;
528bf80f4bSopenharmony_ci        d->data = {};
538bf80f4bSopenharmony_ci    }
548bf80f4bSopenharmony_ci    return &d->cur;
558bf80f4bSopenharmony_ci}
568bf80f4bSopenharmony_civoid closedir(DIR* d)
578bf80f4bSopenharmony_ci{
588bf80f4bSopenharmony_ci    FindClose(d->handle);
598bf80f4bSopenharmony_ci}
608bf80f4bSopenharmony_ci#else
618bf80f4bSopenharmony_ci#include <sys/stat.h>
628bf80f4bSopenharmony_ci#include <sys/types.h>
638bf80f4bSopenharmony_ci#include <dirent.h>
648bf80f4bSopenharmony_ci#include <string>
658bf80f4bSopenharmony_ci#endif
66