source: rtems/cpukit/libblock/src/media-path.c @ f381e9b

Last change on this file since f381e9b was f381e9b, checked in by Joel Sherrill <joel@…>, on 02/17/22 at 15:47:33

cpukit/libblock: Change license to BSD-2

Updates #3053.

  • Property mode set to 100644
File size: 2.9 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/**
4 * @file
5 *
6 * @ingroup RTEMSMedia
7 *
8 * @brief Media implementation.
9 */
10
11/*
12 * Copyright (c) 2009, 2010 embedded brains GmbH.  All rights reserved.
13 *
14 *  embedded brains GmbH
15 *  Obere Lagerstr. 30
16 *  82178 Puchheim
17 *  Germany
18 *  <rtems@embedded-brains.de>
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 * 1. Redistributions of source code must retain the above copyright
24 *    notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 *    notice, this list of conditions and the following disclaimer in the
27 *    documentation and/or other materials provided with the distribution.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
30 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
33 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
40 */
41
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <inttypes.h>
46#include <assert.h>
47
48#include <rtems/media.h>
49
50char *rtems_media_create_path(
51  const char *prefix,
52  const char *name,
53  rtems_device_major_number major
54)
55{
56  size_t const size = strlen(prefix) + 1 + strlen(name) + 1 + 10 + 1;
57  char *const s = malloc(size);
58
59  if (s != NULL) {
60#ifndef NDEBUG
61    int rv =
62#endif
63    snprintf(s, size, "%s/%s-%" PRIu32, prefix, name, major);
64    assert(rv < (int) size);
65  }
66
67  return s;
68}
69
70char *rtems_media_replace_prefix(const char *new_prefix, const char *path)
71{
72  const char *const name_try = strrchr(path, '/');
73  const char *const name = (name_try == NULL) ? path : name_try + 1;
74  size_t const new_prefix_len = strlen(new_prefix);
75  size_t const name_size = strlen(name) + 1;
76  size_t const size = new_prefix_len + 1 + name_size;
77  char *const s = malloc(size);
78
79  if (s != NULL) {
80    memcpy(s, new_prefix, new_prefix_len);
81    s [new_prefix_len] = '/';
82    memcpy(s + new_prefix_len + 1, name, name_size);
83  }
84
85  return s;
86}
87
88char *rtems_media_append_minor(
89  const char *path,
90  rtems_device_minor_number minor
91)
92{
93  size_t const size = strlen(path) + 1 + 10 + 1;
94  char *const s = malloc(size);
95
96  if (s != NULL) {
97#ifndef NDEBUG
98    int rv =
99#endif
100    snprintf(s, size, "%s-%" PRIu32, path, minor);
101    assert(rv < (int) size);
102  }
103
104  return s;
105}
Note: See TracBrowser for help on using the repository browser.