• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "c_mock_common.h"
16 
17 #include <dlfcn.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include "securec.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 typedef off_t (*LseekFuncT)(int fd, off_t offset, int whence);
27 typedef ssize_t (*WriteFuncT)(int fd, const void *buf, size_t count);
28 typedef int (*FtruncateFuncT)(int fd, off_t length);
29 typedef errno_t (*MemcpyFuncT)(void *dest, size_t destMax, const void *src, size_t count);
30 
lseek(int fd,off_t offset,int whence)31 off_t lseek(int fd, off_t offset, int whence)
32 {
33     if (IsFuncNeedMock("lseek")) {
34         return -1;
35     }
36 
37     LseekFuncT func = reinterpret_cast<LseekFuncT>(dlsym(RTLD_NEXT, "lseek"));
38     if (func == nullptr) {
39         return -1;
40     }
41     return (*func)(fd, offset, whence);
42 }
43 
write(int fd,const void * buf,size_t count)44 ssize_t write(int fd, const void *buf, size_t count)
45 {
46     if (IsFuncNeedMock("write")) {
47         return -1;
48     }
49 
50     WriteFuncT func = reinterpret_cast<WriteFuncT>(dlsym(RTLD_NEXT, "write"));
51     if (func == nullptr) {
52         return -1;
53     }
54     return (*func)(fd, buf, count);
55 }
56 
ftruncate(int fd,off_t length)57 int ftruncate(int fd, off_t length)
58 {
59     if (IsFuncNeedMock("ftruncate")) {
60         return -1;
61     }
62 
63     FtruncateFuncT func = reinterpret_cast<FtruncateFuncT>(dlsym(RTLD_NEXT, "ftruncate"));
64     if (func == nullptr) {
65         return -1;
66     }
67     return (*func)(fd, length);
68 }
69 
70 
memcpy_s(void * dest,size_t destMax,const void * src,size_t count)71 errno_t memcpy_s(void *dest, size_t destMax, const void *src, size_t count)
72 {
73     if (IsFuncNeedMock("memcpy_s")) {
74         return -1;
75     }
76 
77     MemcpyFuncT func = reinterpret_cast<MemcpyFuncT>(dlsym(RTLD_NEXT, "memcpy_s"));
78     if (func == nullptr) {
79         return -1;
80     }
81     return (*func)(dest, destMax, src, count);
82 }
83 #ifdef __cplusplus
84 }
85 #endif
86