- if (!(*q++ = *p++))
- return;
- while (*p) {
- if ((*p>='A' && *p<='Z') ||
- (*p>='a' && *p<='z') ||
- (*p>='0' && *p<='9') ||
- *p=='-' || *p=='_' || *p==':' || *p=='.')
- *q++ = *p;
- p++;
+ if ((*q++ = *p++) != '\0') {
+ while (*p) {
+ if ((*p>='A' && *p<='Z') ||
+ (*p>='a' && *p<='z') ||
+ (*p>='0' && *p<='9') ||
+ *p=='-' || *p=='_' || *p==':' || *p=='.')
+ *q++ = *p;
+ p++;
+ }
+
+ *q = '\0';
+ }
+
+ /*
+ * Now we check for clashes with other fragment names, and
+ * adjust this one if necessary by appending a hyphen followed
+ * by a number.
+ */
+ {
+ htmlfragment *frag = snew(htmlfragment);
+ int len = 0; /* >0 indicates we have resized */
+ int suffix = 1;
+
+ frag->file = file;
+ frag->fragment = text;
+
+ while (add234(files->frags, frag) != frag) {
+ if (!len) {
+ len = strlen(text);
+ frag->fragment = text = sresize(text, len+20, char);
+ }
+
+ sprintf(text + len, "-%d", ++suffix);
+ }