• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <errno.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include "filepath_util.h"
20 
21 const char *str = "Hello";
22 
23 /**
24  * @tc.name      : setlinebuf_0100
25  * @tc.desc      : Set file stream to linear buffer
26  * @tc.level     : Level 0
27  */
setlinebuf_0100(void)28 void setlinebuf_0100(void)
29 {
30     char path[PATH_MAX] = {0};
31     FILE_ABSOLUTE_PATH(STR_FILE_TXT, path);
32     FILE *f = fopen(path, "w+");
33     char buffer[BUFSIZ];
34     errno = 0;
35     setlinebuf(f);
36     if (errno != 0) {
37         t_error("%s failed: errno = %d\n", __func__, errno);
38     }
39 
40     fputs(str, f);
41     fclose(f);
42     f = fopen(path, "r");
43 
44     memset(buffer, 0, sizeof(buffer));
45     fread(buffer, sizeof(buffer), 1, f);
46 
47     if (strcmp(buffer, str)) {
48         t_error("%s failed: setlinebuf. buf = %s\n", __func__, buffer);
49     }
50     fclose(f);
51     remove(path);
52 }
53 
54 /**
55  * @tc.name      : setlinebuf_0200
56  * @tc.desc      : Output to console
57  * @tc.level     : Level 2
58  */
setlinebuf_0200(void)59 void setlinebuf_0200(void)
60 {
61     errno = 0;
62     setlinebuf(stdout);
63     if (errno != 0) {
64         t_error("%s failed: errno = %d\n", __func__, errno);
65     }
66 
67     printf("test");
68     sleep(1);
69 }
70 
main(int argc,char * argv[])71 int main(int argc, char *argv[])
72 {
73     setlinebuf_0100();
74     setlinebuf_0200();
75     return t_status;
76 }