| 1 | /*------------------------------------------------------------------ |
| 2 | * strljustify_s.c |
| 3 | * |
| 4 | * November 2008, Bo Berry |
| 5 | * |
| 6 | * Copyright (c) 2008-2011 by Cisco Systems, Inc |
| 7 | * All rights reseved. |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person |
| 10 | * obtaining a copy of this software and associated documentation |
| 11 | * files (the "Software"), to deal in the Software without |
| 12 | * restriction, including without limitation the rights to use, |
| 13 | * copy, modify, merge, publish, distribute, sublicense, and/or |
| 14 | * sell copies of the Software, and to permit persons to whom the |
| 15 | * Software is furnished to do so, subject to the following |
| 16 | * conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice shall be |
| 19 | * included in all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 28 | * OTHER DEALINGS IN THE SOFTWARE. |
| 29 | *------------------------------------------------------------------ |
| 30 | */ |
| 31 | |
| 32 | #include "safeclib_private.h" |
| 33 | #include "safe_str_constraint.h" |
| 34 | #include "safe_str_lib.h" |
| 35 | |
| 36 | |
| 37 | /** |
| 38 | * NAME |
| 39 | * strljustify_s |
| 40 | * |
| 41 | * SYNOPSIS |
| 42 | * #include "safe_str_lib.h" |
| 43 | * errno_t |
| 44 | * strljustify_s(char *dest, rsize_t dmax) |
| 45 | * |
| 46 | * DESCRIPTION |
| 47 | * Removes beginning whitespace from the string pointed to by |
| 48 | * dest by shifting the text left over writting the beginning |
| 49 | * whitespace, left justifying the text. The left justified |
| 50 | * text is null terminated. |
| 51 | * |
| 52 | * The text is shifted so the original pointer can continue |
| 53 | * to be used. |
| 54 | * |
| 55 | * EXTENSION TO |
| 56 | * ISO/IEC JTC1 SC22 WG14 N1172, Programming languages, environments |
| 57 | * and system software interfaces, Extensions to the C Library, |
| 58 | * Part I: Bounds-checking interfaces |
| 59 | * |
| 60 | * INPUT PARAMETERS |
| 61 | * dest pointer to string to left justify |
| 62 | * |
| 63 | * dmax restricted maximum length of string |
| 64 | * |
| 65 | * OUTPUT PARAMETERS |
| 66 | * dest left justified |
| 67 | * |
| 68 | * RUNTIME CONSTRAINTS |
| 69 | * dest shall not be a null pointer. |
| 70 | * dmax shall not be 0 |
| 71 | * dmax shall not be greater than RSIZE_MAX_STR |
| 72 | * dest shall be null terminated |
| 73 | * |
| 74 | * RETURN VALUE |
| 75 | * EOK |
| 76 | * ESNULLP NULL pointer |
| 77 | * ESZEROL zero length |
| 78 | * ESLEMAX length exceeds max limit |
| 79 | * ESUNTERM dest was not null terminated |
| 80 | * |
| 81 | * ALSO SEE |
| 82 | * strremovews_s(), |
| 83 | * |
| 84 | */ |
| 85 | errno_t |
| 86 | strljustify_s (char *dest, rsize_t dmax) |
| 87 | { |
| 88 | char *orig_dest; |
| 89 | rsize_t orig_dmax; |
| 90 | |
| 91 | if (dest == NULL) { |
| 92 | invoke_safe_str_constraint_handler("strljustify_s_s: " |
| 93 | "dest is null", |
| 94 | NULL, ESNULLP); |
| 95 | return (ESNULLP); |
| 96 | } |
| 97 | |
| 98 | if (dmax == 0 ) { |
| 99 | invoke_safe_str_constraint_handler("strljustify_s_s: " |
| 100 | "dmax is 0", |
| 101 | NULL, ESZEROL); |
| 102 | return (ESZEROL); |
| 103 | } |
| 104 | |
| 105 | if (dmax > RSIZE_MAX_STR) { |
| 106 | invoke_safe_str_constraint_handler("strljustify_s_s: " |
| 107 | "dmax exceeds max", |
| 108 | NULL, ESLEMAX); |
| 109 | return (ESLEMAX); |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * corner case, a dmax of one allows only for a null |
| 114 | */ |
| 115 | if (*dest == '\0' || dmax <= RSIZE_MIN_STR) { |
| 116 | *dest = '\0'; |
| 117 | return (EOK); |
| 118 | } |
| 119 | |
| 120 | orig_dmax = dmax; |
| 121 | orig_dest = dest; |
| 122 | |
| 123 | /* |
| 124 | * scan the string to be sure it is properly terminated |
| 125 | */ |
| 126 | while (*dest) { |
| 127 | if (dmax == 0) { |
| 128 | while (orig_dmax) { *orig_dest++ = '\0'; orig_dmax--; } |
| 129 | |
| 130 | invoke_safe_str_constraint_handler( |
| 131 | "strljustify_s: dest is unterminated", |
| 132 | NULL, ESUNTERM); |
| 133 | return (ESUNTERM); |
| 134 | } |
| 135 | dmax--; |
| 136 | dest++; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * find first non-white space char |
| 141 | */ |
| 142 | dest = orig_dest; |
| 143 | while ((*dest == ' ') || (*dest == '\t')) { |
| 144 | dest++; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * shift text, removing spaces, to left justify |
| 149 | */ |
| 150 | if (orig_dest != dest && *dest) { |
| 151 | while (*dest) { |
| 152 | *orig_dest++ = *dest; |
| 153 | *dest++ = ' '; |
| 154 | } |
| 155 | *orig_dest = '\0'; |
| 156 | } |
| 157 | |
| 158 | return (EOK); |
| 159 | } |
| 160 | EXPORT_SYMBOL(strljustify_s) |