1 /* ----> DO NOT REMOVE THE FOLLOWING NOTICE <----
2 *
3 * Copyright (c) 2014-2015 Datalight, Inc.
4 * All Rights Reserved Worldwide.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; use version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but "AS-IS," WITHOUT ANY WARRANTY; without even the implied warranty
12 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 /* Businesses and individuals that for commercial or other reasons cannot
21 * comply with the terms of the GPLv2 license may obtain a commercial license
22 * before incorporating Reliance Edge into proprietary software for
23 * distribution in any form. Visit http://www.datalight.com/reliance-edge for
24 * more information.
25 */
26
27 /** @file
28 * @brief Implements common-code utilities for tools and tests.
29 */
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <limits.h>
34
35 #include <redfs.h>
36 #include <redcoreapi.h>
37 #include <redvolume.h>
38 #include <redtoolcmn.h>
39
40
41 /** @brief Convert a string into a volume number.
42 *
43 * In a POSIX-like configuration, @p pszVolume can either be a volume number or
44 * a volume path prefix. In case of ambiguity, the volume number of a matching
45 * path prefix takes precedence.
46 *
47 * In an FSE configuration, @p pszVolume can be a volume number.
48 *
49 * @param pszVolume The volume string.
50 *
51 * @return On success, returns the volume number; on failure, returns
52 #REDCONF_VOLUME_COUNT.
53 */
RedFindVolumeNumber(const char * pszVolume)54 uint8_t RedFindVolumeNumber( const char * pszVolume )
55 {
56 unsigned long ulNumber;
57 const char * pszEndPtr;
58 uint8_t bVolNum = REDCONF_VOLUME_COUNT;
59
60 #if REDCONF_API_POSIX == 1
61 uint8_t bIndex;
62 #endif
63
64 /* Determine if pszVolume can be interpreted as a volume number.
65 */
66 errno = 0;
67 ulNumber = strtoul( pszVolume, ( char ** ) &pszEndPtr, 10 );
68
69 if( ( errno == 0 ) && ( ulNumber != ULONG_MAX ) && ( pszEndPtr[ 0U ] == '\0' ) && ( ulNumber < REDCONF_VOLUME_COUNT ) )
70 {
71 bVolNum = ( uint8_t ) ulNumber;
72 }
73
74 #if REDCONF_API_POSIX == 1
75
76 /* Determine if pszVolume is a valid path prefix.
77 */
78 for( bIndex = 0U; bIndex < REDCONF_VOLUME_COUNT; bIndex++ )
79 {
80 if( strcmp( gaRedVolConf[ bIndex ].pszPathPrefix, pszVolume ) == 0 )
81 {
82 break;
83 }
84 }
85
86 if( bIndex < REDCONF_VOLUME_COUNT )
87 {
88 /* Edge case: It is technically possible for pszVolume to be both a
89 * valid volume number and a valid volume prefix, for different
90 * volumes. For example, if pszVolume is "2", that would be recognized
91 * as volume number 2 above. But if "2" is the (poorly chosen) path
92 * prefix for volume number 4, that would also be matched. Since the
93 * POSIX-like API is primarily name based, and the ability to use
94 * volume numbers with this tool is just a convenience, the volume
95 * prefix takes precedence.
96 */
97 bVolNum = bIndex;
98 }
99 #endif /* if REDCONF_API_POSIX == 1 */
100
101 return bVolNum;
102 }
103