]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - rpmsg/ti-rpmsg-char.git/blob - src/utils.c
remove ti.ipc4.ping-pong reference
[rpmsg/ti-rpmsg-char.git] / src / utils.c
1 /*
2  * Copyright (c) 2020 Texas Instruments Incorporated - https://www.ti.com
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * *  Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *
11  * *  Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * *  Neither the name of Texas Instruments Incorporated nor the names of
16  *    its contributors may be used to endorse or promote products derived
17  *    from this software without specific prior written permission.
18  *
19  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  */
33 #include <dirent.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <sys/stat.h>
43 int get_child_dir_suffix(char *fpath, const char *child_name_pattern,
44                          unsigned int *suffix)
45 {
46         struct dirent *iter;
47         DIR *parent;
48         int ret = -ENODEV;
50         parent = opendir(fpath);
51         if (!parent)
52                 return -errno;
54         while ((iter = readdir(parent))) {
55                 if (iter->d_type == DT_DIR &&
56                     sscanf(iter->d_name, child_name_pattern, suffix)) {
57                         ret = 0;
58                         break;
59                 }
60         }
62         closedir(parent);
63         return ret;
64 }
66 int get_child_dir_pattern(char *fpath, const char *child_name_pattern,
67                          char *dir_name)
68 {
69         struct dirent *iter;
70         DIR *parent;
71         int ret = -ENODEV;
73         parent = opendir(fpath);
74         if (!parent)
75                 return -errno;
77         while ((iter = readdir(parent))) {
78                 if (iter->d_type == DT_DIR &&
79                         (strncmp(iter->d_name,child_name_pattern,strlen(child_name_pattern))
80                          == 0)) {
81                         strcpy(dir_name,iter->d_name);
82                         ret = 0;
83                         break;
84                 }
85         }
87         closedir(parent);
88         return ret;
89 }
91 int file_read_string(char *fpath, char *buf, int size)
92 {
93         int fd, bytes;
95         fd = open(fpath, O_RDONLY);
96         if (fd < 0) {
97                 fprintf(stderr, "could not open %s: errno = %d\n",
98                         fpath, errno);
99                 return -errno;
100         }
102         bytes = read(fd, buf, size);
103         close(fd);
104         if (bytes <= 0) {
105                 fprintf(stderr, "could not read %s: errno = %d\n",
106                         fpath, errno);
107                 return -EIO;
108         }
109         if (bytes >= size) {
110                 fprintf(stderr, "%d bytes read from %s are larger than size %d\n",
111                         bytes, fpath, size);
112                 return -EIO;
113         }
115         /* suppress the newline */
116         buf[bytes - 1] = '\0';
118         return bytes;
121 int file_read_value(char *fpath)
123         char buf[32];
124         int ret;
126         ret = file_read_string(fpath, buf, sizeof(buf));
127         if (ret < 0)
128                 return ret;
130         return strtol(buf, NULL, 0);
133 int check_dir(char *dirpath)
135         struct stat s;
137         if (stat(dirpath, &s) == 0 && S_ISDIR(s.st_mode))
138                 return 0;
140         return -ENOENT;
143 /* Returns a pointer to allocated memory, needs to be freed once done */
144 char *file_deref_link(char *fpath, char *link_name)
146         char path[512] = { 0 };
147         char rel_path[256] = { 0 };
148         int n, nr;
150         n = snprintf(path, 256, "%s/%s", fpath, link_name);
151         if (n < 0 || n >= 256) {
152                 fprintf(stderr, "%s: could not create full path string\n",
153                         __func__);
154                 return NULL;
155         }
157         nr = readlink(path, rel_path, sizeof(rel_path));
158         if (nr < 0) {
159                 fprintf(stderr, "%s: readlink failed for %s\n", __func__, path);
160                 return NULL;
161         }
163         if (n + nr >= 512) {
164                 fprintf(stderr, "%s: full relative path exceeds buffer size, n = %d nr = %d\n",
165                         __func__, n, nr);
166                 return NULL;
167         }
169         memset(path, 0, sizeof(path));
170         sprintf(path, "%s/%s", fpath, rel_path);
172         return realpath(path, NULL);