Debian Codegraph @

src/bin/dwarfdump/dd_canonical_append.c

raw
1 /*
2 Copyright (C) 2006 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright 2011-2019 David Anderson. All Rights Reserved.
4 Portions Copyright 2012 SN Systems Ltd. All rights reserved.
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of version 2 of the GNU General
8 Public License as published by the Free Software Foundation.
9
10 This program is distributed in the hope that it would be
11 useful, but WITHOUT ANY WARRANTY; without even the implied
12 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 PURPOSE.
14
15 Further, this software is distributed without any warranty
16 that it is free of the rightful claim of any third person
17 regarding infringement or the like. Any license provided
18 herein, whether implied or otherwise, applies only to this
19 software file. Patent licenses, if any, provided herein
20 do not apply to combinations of this program with other
21 software, or any other product whatsoever.
22
23 You should have received a copy of the GNU General Public
24 License along with this program; if not, write the Free
25 Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
26 Boston MA 02110-1301, USA.
27
28 */
29
30 #include <config.h>
31
32 #include <stddef.h> /* NULL size_t */
33 #include <string.h> /* strlen() */
34
35 #include "dd_canonical_append.h"
36 #include "dd_safe_strcpy.h"
37 /* Given path strings, attempt to make a canonical file name:
38 that is, avoid superfluous '/' so that no
39 '//' (or worse) is created in the output. The path components
40 are to be separated so at least one '/'
41 is to appear between the two 'input strings' when
42 creating the output.
43 */
44 char *
45 _dwarf_canonical_append(char *target, unsigned int target_size,
46 const char *first_string, const char *second_string)
47 {
48 size_t firstlen = strlen(first_string);
49 size_t totallen = firstlen + strlen(second_string) + 1 + 1;
50
51 /* +1 +1: Leave room for added "/" and final NUL, though that is
52 overkill, as we drop a NUL byte too. */
53 if (totallen >= target_size) {
54 /* Not enough space. */
55 return NULL;
56 }
57 for (; *second_string == '/'; ++second_string) {
58 }
59 for (; firstlen > 0 && first_string[firstlen - 1] == '/';
60 --firstlen) {
61 }
62 target[0] = 0;
63 if (firstlen > 0) {
64 dd_safe_strcpy(target,target_size,first_string, firstlen);
65 }
66 target[firstlen] = '/';
67 firstlen++;
68 target[firstlen] = 0;
69 dd_safe_strcpy(target+firstlen, target_size-firstlen,
70 second_string,strlen(second_string));
71 return target;
72 }