Append " or "+new_str to the end of buf if new_str doesn't already exist as a substring of buf. Return 1 if the string was appended, 0 otherwise. It is expected that buf is of size BUFSZ. */
| 79 | as a substring of buf. Return 1 if the string was appended, 0 otherwise. |
| 80 | It is expected that buf is of size BUFSZ. */ |
| 81 | staticfn int |
| 82 | append_str(char *buf, const char *new_str) |
| 83 | { |
| 84 | static const char sep[] = " or "; |
| 85 | size_t oldlen, space_left; |
| 86 | |
| 87 | if (strstri(buf, new_str)) |
| 88 | return 0; /* already present */ |
| 89 | |
| 90 | oldlen = strlen(buf); |
| 91 | if (oldlen >= BUFSZ - 1) { |
| 92 | if (oldlen > BUFSZ - 1) |
| 93 | impossible("append_str: 'buf' contains %lu characters.", |
| 94 | (unsigned long) oldlen); |
| 95 | return 0; /* no space available */ |
| 96 | } |
| 97 | |
| 98 | /* some space available, but not necessarily enough for full append */ |
| 99 | space_left = BUFSZ - 1 - oldlen; /* space remaining in buf */ |
| 100 | (void) strncat(buf, sep, space_left); |
| 101 | if (space_left > sizeof sep - 1) |
| 102 | (void) strncat(buf, new_str, space_left - (sizeof sep - 1)); |
| 103 | return 1; /* something was appended, possibly just part of " or " */ |
| 104 | } |
| 105 | |
| 106 | /* shared by monster probing (via query_objlist!) as well as lookat() */ |
| 107 | char * |
no test coverage detected