1/*
2 * Copyright (c) 2022 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
16#include <fcntl.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <sys/uio.h>
21#include "test.h"
22
23void vmsplice_0100(void)
24{
25    int pipe_size = 2;
26    int iov_length = 6;
27    int pipe_fds[pipe_size];
28    if (pipe(pipe_fds) != 0) {
29        t_error("%s create pipe error\n", __func__);
30    }
31    struct iovec v[pipe_size];
32    char *str_hello = "hello ";
33    char *str_world = "world\n";
34    v[0].iov_base = str_hello;
35    v[0].iov_len = iov_length;
36    v[1].iov_base = str_world;
37    v[1].iov_len = iov_length;
38    size_t result = vmsplice(pipe_fds[1], v, sizeof(v) / sizeof(struct iovec), 0);
39    if (result != v[0].iov_len + v[1].iov_len) {
40        t_error("%s vmsplice error get result is %d are not want %d\n", __func__, result, v[0].iov_len + v[1].iov_len);
41    }
42    close(pipe_fds[1]);
43    char buf[BUFSIZ];
44    FILE *fp = fdopen(pipe_fds[0], "r");
45    if (!fp) {
46        t_error("%s fdopen get ptr is NULL\n", __func__);
47    }
48    if (!fgets(buf, sizeof(buf), fp)) {
49        t_error("%s fgets get ptr is NULL\n", __func__);
50    }
51    fclose(fp);
52    if (strcmp(buf, "hello world\n") != 0) {
53        t_error("%s fgets get str is '%s' are not want 'hello world'\n", __func__, buf);
54    }
55}
56
57int main(int argc, char *argv[])
58{
59    vmsplice_0100();
60    return t_status;
61}