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 <stdio.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <fcntl.h>
20 #include <string.h>
21 #include <errno.h>
22 #include "test.h"
23
24 /**
25 * @tc.name : ttyname_r_0100
26 * @tc.desc : Call ttyname_r to get the file name through the file handle
27 * @tc.level : Level 0
28 */
ttyname_r_0100(void)29 void ttyname_r_0100(void)
30 {
31 int fd = open("/data/tempfile_forttyname.txt", O_CREAT, TEST_MODE);
32 char buf[128];
33 int want = 25;
34 int result = ttyname_r(fd, buf, sizeof(buf));
35 if (result != want) {
36 t_error("%s ttyname_r error get result is %d are not want 25\n", __func__, result);
37 }
38 close(fd);
39 unlink("/data/tempfile_forttyname.txt");
40 }
41
42 /**
43 * @tc.name : ttyname_r_0200
44 * @tc.desc : Test the ttyname_r result when a non-existing file is passed in
45 * @tc.level : Level 1
46 */
ttyname_r_0200(void)47 void ttyname_r_0200(void)
48 {
49 int fd = open("/dev/null", O_WRONLY);
50 char buf[1];
51 int result = ttyname_r(fd, buf, sizeof(buf));
52 if (result != errno) {
53 t_error("%s ttyname_r error get result is %d are not want errno\n", __func__, result);
54 }
55 close(fd);
56 }
57
main(int argc,char * argv[])58 int main(int argc, char *argv[])
59 {
60 ttyname_r_0100();
61 ttyname_r_0200();
62 return t_status;
63 }