Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:14

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSMedia
0007  *
0008  * @brief Media implementation.
0009  */
0010 
0011 /*
0012  * Copyright (C) 2009, 2010 embedded brains GmbH & Co. KG
0013  *
0014  * Redistribution and use in source and binary forms, with or without
0015  * modification, are permitted provided that the following conditions
0016  * are met:
0017  * 1. Redistributions of source code must retain the above copyright
0018  *    notice, this list of conditions and the following disclaimer.
0019  * 2. Redistributions in binary form must reproduce the above copyright
0020  *    notice, this list of conditions and the following disclaimer in the
0021  *    documentation and/or other materials provided with the distribution.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0033  * POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 
0036 #include <stdio.h>
0037 #include <stdlib.h>
0038 #include <string.h>
0039 #include <inttypes.h>
0040 #include <assert.h>
0041 
0042 #include <rtems/media.h>
0043 
0044 char *rtems_media_create_path(
0045   const char *prefix,
0046   const char *name,
0047   rtems_device_major_number major
0048 )
0049 {
0050   size_t const size = strlen(prefix) + 1 + strlen(name) + 1 + 10 + 1;
0051   char *const s = malloc(size);
0052 
0053   if (s != NULL) {
0054 #ifndef NDEBUG
0055     int rv = 
0056 #endif
0057     snprintf(s, size, "%s/%s-%" PRIu32, prefix, name, major);
0058     assert(rv < (int) size);
0059   }
0060 
0061   return s;
0062 }
0063 
0064 char *rtems_media_replace_prefix(const char *new_prefix, const char *path)
0065 {
0066   const char *const name_try = strrchr(path, '/');
0067   const char *const name = (name_try == NULL) ? path : name_try + 1;
0068   size_t const new_prefix_len = strlen(new_prefix);
0069   size_t const name_size = strlen(name) + 1;
0070   size_t const size = new_prefix_len + 1 + name_size;
0071   char *const s = malloc(size);
0072 
0073   if (s != NULL) {
0074     memcpy(s, new_prefix, new_prefix_len);
0075     s [new_prefix_len] = '/';
0076     memcpy(s + new_prefix_len + 1, name, name_size);
0077   }
0078 
0079   return s;
0080 }
0081 
0082 char *rtems_media_append_minor(
0083   const char *path,
0084   rtems_device_minor_number minor
0085 )
0086 {
0087   size_t const size = strlen(path) + 1 + 10 + 1;
0088   char *const s = malloc(size);
0089 
0090   if (s != NULL) {
0091 #ifndef NDEBUG
0092     int rv = 
0093 #endif
0094     snprintf(s, size, "%s-%" PRIu32, path, minor);
0095     assert(rv < (int) size);
0096   }
0097 
0098   return s;
0099 }