Coverage Report

Created: 2026-11-13 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/lib/libbb.c
Line
Count
Source
1
/*
2
 * Utility routines.
3
 *
4
 * Copyright (C) many different people.
5
 * If you wrote this, please acknowledge your work.
6
 *
7
 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8
 */
9
10
#include <common.h>
11
#include <libbb.h>
12
#include <linux/stat.h>
13
#include <fs.h>
14
#include <xfuncs.h>
15
#include <malloc.h>
16
#include <environment.h>
17
18
/* concatenate path and file name to new allocation buffer,
19
 * not adding '/' if path name already has '/'
20
*/
21
char *concat_path_file(const char *path, const char *filename)
22
11.7k
{
23
11.7k
  char *lc, *str;
24
25
11.7k
  if (!path)
26
0
    path = "";
27
11.7k
  lc = last_char_is(path, '/');
28
13.0k
  while (*filename == '/')
29
1.35k
    filename++;
30
31
11.7k
  str = xmalloc(strlen(path) + (lc==NULL ? 1 : 0) + strlen(filename) + 1);
32
11.7k
  sprintf(str, "%s%s%s", path, (lc==NULL ? "/" : ""), filename);
33
34
11.7k
  return str;
35
11.7k
}
36
EXPORT_SYMBOL(concat_path_file);
37
38
/*
39
 * This function make special for recursive actions with usage
40
 * concat_path_file(path, filename)
41
 * and skipping "." and ".." directory entries
42
 */
43
44
char *concat_subpath_file(const char *path, const char *f)
45
17.7k
{
46
17.7k
  if (DOT_OR_DOTDOT(f))
47
7.44k
    return NULL;
48
10.3k
  return concat_path_file(path, f);
49
17.7k
}
50
EXPORT_SYMBOL(concat_subpath_file);
51
52
/**
53
 * find_path - find a file in a colon separated path
54
 * @path: The search path, colon separated
55
 * @filename: The filename to search for
56
 * @filter: filter function
57
 *
58
 * searches for @filename in a colon separated list of directories given in
59
 * @path. @filter should return true when the current file matches the expectations,
60
 * false otherwise. @filter should check for existence of the file, but could also
61
 * check for additional flags.
62
 */
63
char *find_path(const char *path, const char *filename,
64
    bool (*filter)(const char *))
65
0
{
66
0
  char *p, *n, *freep;
67
68
0
  freep = p = strdup(path);
69
0
  while (p) {
70
0
    n = strchr(p, ':');
71
0
    if (n)
72
0
      *n++ = '\0';
73
0
    if (*p != '\0') { /* it's not a PATH="foo::bar" situation */
74
0
      p = concat_path_file(p, filename);
75
0
      if (filter(p)) {
76
0
        free(freep);
77
0
        return p;
78
0
      }
79
0
      free(p);
80
0
    }
81
0
    p = n;
82
0
  }
83
0
  free(freep);
84
0
  return NULL;
85
0
}
86
EXPORT_SYMBOL(find_path);
87
88
/* check if path points to an executable file;
89
 * return 1 if found;
90
 * return 0 otherwise;
91
 */
92
bool execable_file(const char *name)
93
0
{
94
0
  struct stat s;
95
0
  return (!stat(name, &s) && S_ISREG(s.st_mode));
96
0
}
97
EXPORT_SYMBOL(execable_file);
98
99
100
/* search $PATH for an executable file;
101
 * return allocated string containing full path if found;
102
 * return NULL otherwise;
103
 */
104
char *find_execable(const char *filename)
105
0
{
106
0
  return find_path(getenv("PATH"), filename, execable_file);
107
0
}
108
EXPORT_SYMBOL(find_execable);
109
110
/* Find out if the last character of a string matches the one given.
111
 * Don't underrun the buffer if the string length is 0.
112
 */
113
char* last_char_is(const char *s, int c)
114
11.7k
{
115
11.7k
  if (s && *s) {
116
11.7k
    size_t sz = strlen(s) - 1;
117
11.7k
    s += sz;
118
11.7k
    if ( (unsigned char)*s == c)
119
0
      return (char*)s;
120
11.7k
  }
121
11.7k
  return NULL;
122
11.7k
}
123
EXPORT_SYMBOL(last_char_is);
124
125
/* Like strncpy but make sure the resulting string is always 0 terminated. */
126
char * safe_strncpy(char *dst, const char *src, size_t size)
127
0
{
128
0
  if (!size) return dst;
129
0
  dst[--size] = '\0';
130
0
  return strncpy(dst, src, size);
131
0
}
132
EXPORT_SYMBOL(safe_strncpy);