+
+ /* Did we actually insert something? */
+ if (bp != pp) {
+ /* Insert single quote if not inside quotes already */
+ if (!quoted && buflen) {
+ len = strlen (pp);
+ memmove (pp + 1, pp, len);
+ *pp++ = '\'';
+ buflen--;
+ bp++;
+ }
+ /* Escape existing quotes */
+ while ((pp = strchr (pp, '\'')) && buflen > 3) {
+ len = strlen (pp++);
+ memmove (pp + 3, pp, len);
+ *pp++ = '\\';
+ *pp++ = '\'';
+ *pp++ = '\'';
+ buflen -= 3;
+ bp += 3;
+ }
+ /* If pp is still set, that means we ran out of space. */
+ if (pp)
+ buflen = 0;
+ if (!quoted && buflen) {
+ *bp++ = '\'';
+ *bp = '\0';
+ buflen--;
+ }
+ }