Annotation of Amaya/amaya/AHTURLTools.c, revision 1.170
1.7 cvs 1: /*
2: *
1.160 vatton 3: * (c) COPYRIGHT INRIA and W3C, 1996-2003
1.7 cvs 4: * Please first read the full copyright statement in file COPYRIGHT.
5: *
6: */
1.9 cvs 7:
1.10 cvs 8: /*
9: * AHTURLTools.c: contains all the functions for testing, manipulating,
1.25 cvs 10: * and normalizing URLs. It also contains a local copy of the libWWW
11: * URL parsing functions.
1.10 cvs 12: *
13: * Authors: J. Kahan, I. Vatton
1.106 cvs 14: * R. Guetari: Windows.
1.10 cvs 15: *
16: */
1.15 cvs 17: #define THOT_EXPORT extern
1.3 cvs 18: #include "amaya.h"
19:
1.8 cvs 20: #include "init_f.h"
21: #include "AHTURLTools_f.h"
1.100 kahan 22: #include "query_f.h"
1.8 cvs 23:
1.24 cvs 24: #define MAX_PRINT_URL_LENGTH 50
1.106 cvs 25: typedef struct _HTURI
26: {
27: char *access; /* Now known as "scheme" */
28: char *host;
29: char *absolute;
30: char *relative;
31: char *fragment;
1.29 cvs 32: } HTURI;
1.24 cvs 33:
1.152 kahan 34: #ifdef _WINDOWS
35: #define TMPDIR "TMP"
1.164 kahan 36: #ifndef PATH_MAX
1.163 cvs 37: #define PATH_MAX MAX_PATH
1.164 kahan 38: #endif
1.155 cvs 39: #define stat _stat
40: #define uint64_t unsigned __int64
41: #define getpid _getpid
1.161 kahan 42: #else /* _WINDOWS */
1.152 kahan 43: #define TMPDIR "TMPDIR"
1.161 kahan 44: #if HAVE_STDINT_H
45: #include <stdint.h>
46: #endif /* HAVE_STDINT_H */
1.152 kahan 47: #endif /* _WINDOWS */
1.28 cvs 48:
49: /*----------------------------------------------------------------------
50: ConvertToLowerCase
51: Converts a string to lowercase.
52: ----------------------------------------------------------------------*/
1.124 vatton 53: void ConvertToLowerCase (char *string)
1.28 cvs 54: {
55: int i;
1.93 cvs 56:
1.28 cvs 57: if (!string)
58: return;
59:
1.106 cvs 60: for (i = 0; string[i] != EOS; i++)
1.123 vatton 61: string[i] = tolower (string[i]);
1.28 cvs 62: }
1.22 cvs 63:
1.8 cvs 64: /*----------------------------------------------------------------------
1.75 cvs 65: EscapeChar
66: writes the equivalent escape code of a char in a string
67: ----------------------------------------------------------------------*/
1.109 cvs 68: void EscapeChar (char *string, char c)
1.75 cvs 69: {
1.109 cvs 70: unsigned int i;
71:
72: i = (unsigned char) c & 0xFF;
73: sprintf (string, "%02x", i);
1.75 cvs 74: }
75:
76: /*----------------------------------------------------------------------
1.96 cvs 77: UnEscapeChar
78: writes the equivalent hex code to a %xx coded char
79: ----------------------------------------------------------------------*/
1.109 cvs 80: static char UnEscapeChar (char c)
1.96 cvs 81: {
1.106 cvs 82: return c >= '0' && c <= '9' ? c - '0'
83: : c >= 'A' && c <= 'F' ? c - 'A' + 10
84: : c - 'a' + 10; /* accept small letters just in case */
1.96 cvs 85: }
86:
87: /*----------------------------------------------------------------------
1.75 cvs 88: EscapeURL
89: Takes a URL and escapes all protected chars into
90: %xx sequences. Also, removes any leading white spaces
91: Returns either NULL or a new buffer, which must be freed by the caller
92: ----------------------------------------------------------------------*/
1.106 cvs 93: char *EscapeURL (const char *url)
94: {
95: char *buffer;
96: int buffer_len;
97: int buffer_free_mem;
98: char *ptr;
99: int new_chars;
1.75 cvs 100: void *status;
101:
102: if (url && *url)
103: {
1.106 cvs 104: buffer_free_mem = strlen (url) + 20;
105: buffer = TtaGetMemory (buffer_free_mem + 1);
1.107 kahan 106: ptr = (char *) url;
1.75 cvs 107: buffer_len = 0;
108:
109: while (*ptr)
110: {
111: switch (*ptr)
112: {
113: /* put here below all the chars that need to
114: be escaped into %xx */
1.81 cvs 115: case 0x20: /* space */
1.143 vatton 116: /*case 0x26:*/ /* & */
1.140 kahan 117: case 0x27: /* antislash */
1.75 cvs 118: new_chars = 3;
119: break;
120:
121: default:
1.122 kahan 122: if ((unsigned char )*ptr > 127)
123: new_chars = 3;
124: else
125: new_chars = 1;
1.75 cvs 126: break;
127: }
128:
129: /* see if we need extra room in the buffer */
130: if (new_chars > buffer_free_mem)
131: {
1.76 cvs 132: buffer_free_mem = 20;
1.106 cvs 133: status = TtaRealloc (buffer, sizeof (char)
1.75 cvs 134: * (buffer_len + buffer_free_mem + 1));
135: if (status)
1.114 cvs 136: buffer = (char *) status;
1.106 cvs 137: else
138: {
139: /* @@ maybe we should do some other behavior here, like
140: freeing the buffer and return a void thing */
141: buffer[buffer_len] = EOS;
142: break;
143: }
1.75 cvs 144: }
145: /* escape the char */
1.140 kahan 146: if (new_chars == 3)
147: {
1.106 cvs 148: buffer[buffer_len] = '%';
1.75 cvs 149: EscapeChar (&buffer[buffer_len+1], *ptr);
150: }
1.140 kahan 151: else
152: buffer[buffer_len] = *ptr;
153:
154: /* update the status */
155: buffer_len += new_chars;
156: buffer_free_mem -= new_chars;
157: /* examine the next char */
158: ptr++;
159: }
160: buffer[buffer_len] = EOS;
161: }
162: else
163: buffer = NULL;
164:
165: return (buffer);
166: }
167:
168: /*----------------------------------------------------------------------
169: EscapeXML
170: Takes a string and escapes all protected chars into entity
171: sequences.
172: Returns either NULL or a new buffer, which must be freed by the caller
173: ----------------------------------------------------------------------*/
174: char *EscapeXML (const char *string)
175: {
176: char *buffer;
177: int buffer_len;
178: int buffer_free_mem;
179: char *ptr;
180: char *entity = NULL;
181: int new_chars;
182: void *status;
183:
184: if (string && *string)
185: {
186: buffer_free_mem = strlen (string) + 20;
187: buffer = TtaGetMemory (buffer_free_mem + 1);
188: ptr = (char *) string;
189: buffer_len = 0;
190:
191: while (*ptr)
192: {
193: switch (*ptr)
194: {
195: case 0x26: /* & */
196: entity = "&";
1.141 kahan 197: new_chars = sizeof (entity) - 1;
198: break;
1.140 kahan 199:
1.141 kahan 200: case '<': /* < */
1.140 kahan 201: entity = "<";
1.141 kahan 202: new_chars = sizeof (entity) - 1;
1.140 kahan 203: break;
204:
1.141 kahan 205: case '>': /* > */
1.140 kahan 206: entity = ">";
1.141 kahan 207: new_chars = sizeof (entity) - 1;
208: break;
209:
210: case '"': /* "e; */
211: entity = ""e;";
212: new_chars = sizeof (entity) - 1;
1.140 kahan 213: break;
214:
215: default:
216: new_chars = 1;
217: break;
218: }
219:
220: /* see if we need extra room in the buffer */
221: if (new_chars > buffer_free_mem)
222: {
223: buffer_free_mem = 20;
224: status = TtaRealloc (buffer, sizeof (char)
225: * (buffer_len + buffer_free_mem + 1));
226: if (status)
227: buffer = (char *) status;
228: else
229: {
230: /* @@ maybe we should do some other behavior here, like
231: freeing the buffer and return a void thing */
232: buffer[buffer_len] = EOS;
233: break;
234: }
235: }
236: /* escape the char */
237: if (entity)
238: {
239: sprintf (&buffer[buffer_len], "%s", entity);
240: entity = NULL;
241: }
1.75 cvs 242: else
243: buffer[buffer_len] = *ptr;
244:
245: /* update the status */
246: buffer_len += new_chars;
247: buffer_free_mem -= new_chars;
248: /* examine the next char */
249: ptr++;
250: }
1.106 cvs 251: buffer[buffer_len] = EOS;
1.75 cvs 252: }
1.76 cvs 253: else
254: buffer = NULL;
255:
1.75 cvs 256: return (buffer);
1.122 kahan 257: }
258:
1.75 cvs 259:
260: /*----------------------------------------------------------------------
1.11 cvs 261: ExplodeURL
1.8 cvs 262: ----------------------------------------------------------------------*/
1.106 cvs 263: void ExplodeURL (char *url, char **proto, char **host, char **dir,
264: char **file)
1.8 cvs 265: {
1.33 cvs 266: char *curr, *temp;
267: char used_sep;
1.32 cvs 268:
1.33 cvs 269: if (url && strchr (url, URL_SEP))
270: used_sep = URL_SEP;
271: else
272: used_sep = DIR_SEP;
1.8 cvs 273:
274: if ((url == NULL) || (proto == NULL) || (host == NULL) ||
275: (dir == NULL) || (file == NULL))
276: return;
277:
278: /* initialize every pointer */
279: *proto = *host = *dir = *file = NULL;
280:
281: /* skip any leading space */
282: while ((*url == SPACE) || (*url == TAB))
283: url++;
1.9 cvs 284: curr = url;
285: if (*curr == 0)
1.8 cvs 286: goto finished;
287:
288: /* go to the end of the URL */
1.68 cvs 289: while ((*curr != EOS) && (*curr != SPACE) && (*curr != BSPACE) &&
290: (*curr != __CR__) && (*curr != EOL))
1.9 cvs 291: curr++;
1.8 cvs 292:
293: /* mark the end of the chain */
1.9 cvs 294: *curr = EOS;
295: curr--;
296: if (curr <= url)
1.8 cvs 297: goto finished;
298:
299: /* search the next DIR_SEP indicating the beginning of the file name */
300: do
1.11 cvs 301: curr--;
1.33 cvs 302: while ((curr >= url) && (*curr != used_sep));
1.11 cvs 303:
1.9 cvs 304: if (curr < url)
1.8 cvs 305: goto finished;
1.9 cvs 306: *file = curr + 1;
1.8 cvs 307:
308: /* mark the end of the dir */
1.9 cvs 309: *curr = EOS;
310: curr--;
311: if (curr < url)
1.8 cvs 312: goto finished;
313:
1.29 cvs 314: /* search for the DIR_STR indicating the host name start */
1.33 cvs 315: while ((curr > url) && ((*curr != used_sep) || (*(curr + 1) != used_sep)))
1.9 cvs 316: curr--;
1.8 cvs 317:
318: /* if we found it, separate the host name from the directory */
1.102 kahan 319: if ((*curr == used_sep) && (*(curr + 1) == used_sep))
1.8 cvs 320: {
1.9 cvs 321: *host = temp = curr + 2;
1.33 cvs 322: while ((*temp != 0) && (*temp != used_sep))
1.8 cvs 323: temp++;
1.33 cvs 324: if (*temp == used_sep)
1.8 cvs 325: {
326: *temp = EOS;
327: *dir = temp + 1;
328: }
329: }
330: else
1.11 cvs 331: *dir = curr;
332:
1.9 cvs 333: if (curr <= url)
1.8 cvs 334: goto finished;
335:
336: /* mark the end of the proto */
1.9 cvs 337: *curr = EOS;
338: curr--;
339: if (curr < url)
1.8 cvs 340: goto finished;
341:
1.106 cvs 342: if (*curr == ':')
1.8 cvs 343: {
1.9 cvs 344: *curr = EOS;
345: curr--;
1.8 cvs 346: }
347: else
348: goto finished;
1.11 cvs 349:
1.9 cvs 350: if (curr < url)
1.8 cvs 351: goto finished;
1.9 cvs 352: while ((curr > url) && (isalpha (*curr)))
353: curr--;
354: *proto = curr;
1.8 cvs 355:
356: finished:;
357:
358: #ifdef AMAYA_DEBUG
359: fprintf (stderr, "ExplodeURL(%s)\n\t", url);
360: if (*proto)
361: fprintf (stderr, "proto : %s, ", *proto);
362: if (*host)
363: fprintf (stderr, "host : %s, ", *host);
364: if (*dir)
365: fprintf (stderr, "dir : %s, ", *dir);
366: if (*file)
367: fprintf (stderr, "file : %s ", *file);
368: fprintf (stderr, "\n");
369: #endif
370:
371: }
1.3 cvs 372:
1.116 kahan 373: /*----------------------------------------------------------------------
374: PicTypeToMime
375: Converts a Thot PicType into the equivalent MIME type. If no convertion
376: is possible, it returns NULL.
377: ----------------------------------------------------------------------*/
378: char *PicTypeToMIME (PicType contentType)
379: {
380: char *mime_type;
381:
382: switch (contentType)
383: {
384: case xbm_type:
385: mime_type ="image/x-xbitmap";
386: break;
387: case eps_type:
388: mime_type ="application/postscript";
389: break;
390: case xpm_type:
391: mime_type ="image/x-xpicmap";
392: break;
393: case gif_type:
394: mime_type ="image/gif";
395: break;
396: case jpeg_type:
397: mime_type ="image/jpeg";
398: break;
399: case png_type:
400: mime_type ="image/png";
401: break;
402: case svg_type:
1.165 cvs 403: mime_type = AM_SVG_MIME_TYPE;
404: break;
405: case html_type:
406: mime_type = AM_XHTML_MIME_TYPE;
407: break;
408: case mathml_type:
409: mime_type = AM_MATHML_MIME_TYPE;
1.116 kahan 410: break;
411: case unknown_type:
412: default:
413: mime_type = NULL;
414: }
415:
416: return mime_type;
417: }
1.61 cvs 418:
419: /*----------------------------------------------------------------------
1.117 kahan 420: ImageElement
421: Returns the element (image parameter) and URL (url parameter) of an
422: image in a docImage document. The user must free the memory associated
1.120 kahan 423: with the url parameter if the function is succesful.
424: If the url parameter is NULL, we won't initialize it.
1.117 kahan 425: Returns TRUE if succesful, FALSE otherwise.
426: ----------------------------------------------------------------------*/
427: ThotBool ImageElement (Document doc, char **url, Element *image)
428: {
429: Element el, imgEl;
430: Attribute attr, srcAttr;
431: AttributeType attrType;
432: int length;
433: char *value;
434:
435: if (DocumentTypes[doc] != docImage)
436: return FALSE;
437:
438: /* find the value of the src attribute */
439: attrType.AttrSSchema = TtaGetSSchema ("HTML", doc);
440: attrType.AttrTypeNum = HTML_ATTR_SRC;
441: el = TtaGetRootElement (doc);
442: TtaSearchAttribute (attrType, SearchInTree, el, &imgEl, &srcAttr);
443:
444: if (!imgEl)
445: return FALSE;
446: *image = imgEl;
447:
1.120 kahan 448: if (url)
449: {
450: attr = TtaGetAttribute (imgEl, attrType);
451: length = TtaGetTextAttributeLength (srcAttr) + 1;
452: value = TtaGetMemory (length);
453: TtaGiveTextAttributeValue (srcAttr, value, &length);
454: *url = value;
455: }
1.117 kahan 456: return TRUE;
457: }
458:
459: /*----------------------------------------------------------------------
460: DocImageMimeType
461: Returns the MIME type of a docImage document.
462: ----------------------------------------------------------------------*/
463: char *DocImageMimeType (Document doc)
464: {
465: char *mime_type;
466: LoadedImageDesc *pImage;
467: PicType type;
468: Element image;
469:
470: if (DocumentTypes[doc] != docImage)
471: return NULL;
472:
473: mime_type = NULL;
474: if (!IsHTTPPath (DocumentURLs[doc]))
475: {
476: /* it is a local image */
1.120 kahan 477: if (ImageElement (doc, NULL, &image))
1.117 kahan 478: {
479: type = TtaGetPictureType (image);
480: mime_type = PicTypeToMIME (type);
481: }
482: }
483: else
484: {
485: /* find the value of the src attribute */
486: pImage = ImageURLs;
487: while (pImage != NULL)
488: {
489: if (pImage->document == doc)
490: {
491: if (pImage->content_type)
492: mime_type = pImage->content_type;
493: else if (pImage->elImage && pImage->elImage->currentElement)
494: {
495: type = TtaGetPictureType (pImage->elImage->currentElement);
496: mime_type = PicTypeToMIME (type);
497: }
498: break;
499: }
500: }
501: }
502: return (mime_type);
503: }
504:
1.4 cvs 505: /*----------------------------------------------------------------------
1.9 cvs 506: IsHTMLName
507: returns TRUE if path points to an HTML resource.
1.4 cvs 508: ----------------------------------------------------------------------*/
1.109 cvs 509: ThotBool IsHTMLName (const char *path)
1.106 cvs 510: {
1.136 cvs 511: char temppath[MAX_LENGTH];
512: char suffix[MAX_LENGTH];
513: char nsuffix[MAX_LENGTH];
514: int i;
1.5 cvs 515:
1.101 cvs 516: if (!path)
517: return (FALSE);
1.5 cvs 518:
1.106 cvs 519: strcpy (temppath, path);
1.124 vatton 520: TtaExtractSuffix (temppath, suffix);
1.101 cvs 521: i = 0;
1.106 cvs 522: while (suffix[i] != EOS)
1.101 cvs 523: {
524: /* Normalize the suffix */
525: i = 0;
1.106 cvs 526: while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.101 cvs 527: {
1.123 vatton 528: nsuffix[i] = tolower (suffix[i]);
1.101 cvs 529: i++;
530: }
1.106 cvs 531: nsuffix[i] = EOS;
532: if (!strcmp (nsuffix, "html") ||
533: !strcmp (nsuffix, "htm") ||
534: !strcmp (nsuffix, "shtml") ||
535: !strcmp (nsuffix, "jsp") ||
1.159 vatton 536: !strcmp (nsuffix, "tpl") ||
1.106 cvs 537: !strcmp (nsuffix, "xht") ||
538: !strcmp (nsuffix, "xhtm") ||
1.144 cvs 539: !strcmp (nsuffix, "lhtml") ||
1.106 cvs 540: !strcmp (nsuffix, "xhtml"))
1.101 cvs 541: return (TRUE);
1.106 cvs 542: else if (!strcmp (nsuffix, "gz"))
1.101 cvs 543: {
544: /* take into account compressed files */
1.124 vatton 545: TtaExtractSuffix (temppath, suffix);
1.101 cvs 546: /* Normalize the suffix */
547: i = 0;
1.106 cvs 548: while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.101 cvs 549: {
1.123 vatton 550: nsuffix[i] = tolower (suffix[i]);
1.101 cvs 551: i++;
552: }
1.106 cvs 553: nsuffix[i] = EOS;
554: if (!strcmp (nsuffix, "html") ||
555: !strcmp (nsuffix, "htm") ||
556: !strcmp (nsuffix, "shtml") ||
557: !strcmp (nsuffix, "jsp") ||
1.159 vatton 558: !strcmp (nsuffix, "tpl") ||
1.106 cvs 559: !strcmp (nsuffix, "xht") ||
560: !strcmp (nsuffix, "xhtm") ||
1.144 cvs 561: !strcmp (nsuffix, "lhtml") ||
1.106 cvs 562: !strcmp (nsuffix, "xhtml"))
1.101 cvs 563: return (TRUE);
564: else
565: return (FALSE);
566: }
567: else
568: /* check if there is another suffix */
1.124 vatton 569: TtaExtractSuffix (temppath, suffix);
1.101 cvs 570: }
1.88 cvs 571: return (FALSE);
1.3 cvs 572: }
573:
1.4 cvs 574: /*----------------------------------------------------------------------
1.136 cvs 575: IsMathMLName
576: returns TRUE if path points to an MathML resource.
1.56 cvs 577: ----------------------------------------------------------------------*/
1.136 cvs 578: ThotBool IsMathMLName (const char *path)
1.56 cvs 579: {
1.136 cvs 580: char temppath[MAX_LENGTH];
581: char suffix[MAX_LENGTH];
1.56 cvs 582:
583: if (!path)
584: return (FALSE);
585:
1.106 cvs 586: strcpy (temppath, path);
1.124 vatton 587: TtaExtractSuffix (temppath, suffix);
1.56 cvs 588:
1.136 cvs 589: if (!strcasecmp (suffix, "mml"))
1.56 cvs 590: return (TRUE);
1.106 cvs 591: else if (!strcmp (suffix, "gz"))
1.56 cvs 592: {
593: /* take into account compressed files */
1.124 vatton 594: TtaExtractSuffix (temppath, suffix);
1.136 cvs 595: if (!strcasecmp (suffix, "mml"))
1.60 cvs 596: return (TRUE);
597: else
598: return (FALSE);
599: }
600: else
601: return (FALSE);
602: }
603:
604: /*----------------------------------------------------------------------
1.136 cvs 605: IsSVGName
606: returns TRUE if path points to an SVG resource.
1.133 vatton 607: ----------------------------------------------------------------------*/
1.136 cvs 608: ThotBool IsSVGName (const char *path)
1.133 vatton 609: {
1.136 cvs 610: char temppath[MAX_LENGTH];
611: char suffix[MAX_LENGTH];
1.133 vatton 612:
613: if (!path)
614: return (FALSE);
615:
616: strcpy (temppath, path);
617: TtaExtractSuffix (temppath, suffix);
618:
1.158 vatton 619: if (!strcasecmp (suffix, "svg") || !strcasecmp (suffix, "svgz"))
1.133 vatton 620: return (TRUE);
621: else if (!strcmp (suffix, "gz"))
622: {
623: /* take into account compressed files */
624: TtaExtractSuffix (temppath, suffix);
1.136 cvs 625: if (!strcasecmp (suffix, "svg"))
1.133 vatton 626: return (TRUE);
627: else
628: return (FALSE);
629: }
630: else
631: return (FALSE);
632: }
633:
634: /*----------------------------------------------------------------------
1.136 cvs 635: IsXMLName
636: returns TRUE if path points to an XML resource.
1.103 cvs 637: ----------------------------------------------------------------------*/
1.136 cvs 638: ThotBool IsXMLName (const char *path)
1.103 cvs 639: {
1.136 cvs 640: char temppath[MAX_LENGTH];
641: char suffix[MAX_LENGTH];
1.103 cvs 642:
643: if (!path)
644: return (FALSE);
645:
1.106 cvs 646: strcpy (temppath, path);
1.124 vatton 647: TtaExtractSuffix (temppath, suffix);
1.103 cvs 648:
1.136 cvs 649: if (!strcasecmp (suffix, "xml") ||
650: !strcasecmp (suffix, "xht") ||
651: !strcmp (suffix, "xhtm") ||
1.145 kahan 652: !strcmp (suffix, "xhtml") ||
653: !strcmp (suffix, "smi"))
1.103 cvs 654: return (TRUE);
1.106 cvs 655: else if (!strcmp (suffix, "gz"))
1.103 cvs 656: {
657: /* take into account compressed files */
1.124 vatton 658: TtaExtractSuffix (temppath, suffix);
1.136 cvs 659: if (!strcasecmp (suffix, "xml") ||
660: !strcasecmp (suffix, "xht") ||
661: !strcmp (suffix, "xhtm") ||
1.145 kahan 662: !strcmp (suffix, "xhtml") ||
663: !strcmp (suffix, "smi"))
1.103 cvs 664: return (TRUE);
665: else
666: return (FALSE);
667: }
668: else
669: return (FALSE);
670: }
671:
672: /*----------------------------------------------------------------------
1.136 cvs 673: IsUndisplayedName
674: returns TRUE if path points to an undisplayed resource.
1.103 cvs 675: ----------------------------------------------------------------------*/
1.136 cvs 676: ThotBool IsUndisplayedName (const char *path)
1.103 cvs 677: {
1.106 cvs 678: char temppath[MAX_LENGTH];
679: char suffix[MAX_LENGTH];
1.103 cvs 680:
681: if (!path)
682: return (FALSE);
683:
1.106 cvs 684: strcpy (temppath, path);
1.124 vatton 685: TtaExtractSuffix (temppath, suffix);
1.103 cvs 686:
1.136 cvs 687: if (!strcasecmp (suffix, "exe") ||
688: !strcasecmp (suffix, "zip") ||
689: !strcasecmp (suffix, "ppt") ||
690: !strcasecmp (suffix, "pdf") ||
691: !strcasecmp (suffix, "ps") ||
692: !strcasecmp (suffix, "eps") ||
693: !strcasecmp (suffix, "tar") ||
694: !strcasecmp (suffix, "tgz") ||
695: !strcasecmp (suffix, "ddl") ||
696: !strcasecmp (suffix, "o"))
1.103 cvs 697: return (TRUE);
1.106 cvs 698: else if (!strcmp (suffix, "gz"))
1.103 cvs 699: {
700: /* take into account compressed files */
1.124 vatton 701: TtaExtractSuffix (temppath, suffix);
1.136 cvs 702: if (!strcasecmp (suffix, "exe") ||
703: !strcasecmp (suffix, "zip") ||
704: !strcasecmp (suffix, "ppt") ||
705: !strcasecmp (suffix, "pdf") ||
706: !strcasecmp (suffix, "ps") ||
707: !strcasecmp (suffix, "eps") ||
708: !strcasecmp (suffix, "tar") ||
709: !strcasecmp (suffix, "ddl") ||
710: !strcasecmp (suffix, "o"))
1.103 cvs 711: return (TRUE);
712: else
713: return (FALSE);
714: }
715: else
716: return (FALSE);
717: }
718:
719: /*----------------------------------------------------------------------
1.60 cvs 720: IsCSSName
721: returns TRUE if path points to an XML resource.
722: ----------------------------------------------------------------------*/
1.111 cvs 723: ThotBool IsCSSName (const char *path)
1.60 cvs 724: {
1.106 cvs 725: char temppath[MAX_LENGTH];
726: char suffix[MAX_LENGTH];
1.60 cvs 727:
728: if (!path)
729: return (FALSE);
730:
1.106 cvs 731: strcpy (temppath, path);
1.124 vatton 732: TtaExtractSuffix (temppath, suffix);
1.60 cvs 733:
1.106 cvs 734: if (!strcasecmp (suffix, "css"))
1.60 cvs 735: return (TRUE);
1.106 cvs 736: else if (!strcmp (suffix, "gz"))
1.60 cvs 737: {
738: /* take into account compressed files */
1.124 vatton 739: TtaExtractSuffix (temppath, suffix);
1.106 cvs 740: if (!strcasecmp (suffix, "css"))
1.56 cvs 741: return (TRUE);
742: else
743: return (FALSE);
744: }
745: else
746: return (FALSE);
747: }
748:
749: /*----------------------------------------------------------------------
1.9 cvs 750: IsImageName
751: returns TRUE if path points to an image resource.
1.4 cvs 752: ----------------------------------------------------------------------*/
1.111 cvs 753: ThotBool IsImageName (const char *path)
1.106 cvs 754: {
755: char temppath[MAX_LENGTH];
756: char suffix[MAX_LENGTH];
757: char nsuffix[MAX_LENGTH];
1.5 cvs 758: int i;
759:
760: if (!path)
1.13 cvs 761: return (FALSE);
1.5 cvs 762:
1.106 cvs 763: strcpy (temppath, path);
1.124 vatton 764: TtaExtractSuffix (temppath, suffix);
1.5 cvs 765:
766: /* Normalize the suffix */
767: i = 0;
1.106 cvs 768: while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.13 cvs 769: {
1.123 vatton 770: nsuffix[i] = tolower (suffix[i]);
1.13 cvs 771: i++;
772: }
1.106 cvs 773: nsuffix[i] = EOS;
774: if ((!strcmp (nsuffix, "gif")) || (!strcmp (nsuffix, "xbm")) ||
775: (!strcmp (nsuffix, "xpm")) || (!strcmp (nsuffix, "jpg")) ||
776: (!strcmp (nsuffix, "png")) || (!strcmp (nsuffix, "au")))
1.39 cvs 777: return (TRUE);
778: return (FALSE);
1.3 cvs 779: }
780:
1.4 cvs 781: /*----------------------------------------------------------------------
1.58 cvs 782: IsImageType
783: returns TRUE if type points to an image resource.
784: ----------------------------------------------------------------------*/
1.111 cvs 785: ThotBool IsImageType (const char *type)
1.58 cvs 786: {
1.106 cvs 787: char temptype[MAX_LENGTH];
1.58 cvs 788: int i;
789:
790: if (!type)
791: return (FALSE);
792:
1.106 cvs 793: strcpy (temptype, type);
1.58 cvs 794: /* Normalize the type */
795: i = 0;
1.106 cvs 796: while (temptype[i] != EOS)
1.58 cvs 797: {
798: temptype[i] = tolower (temptype[i]);
799: i++;
800: }
1.166 vatton 801: if (!strncmp (temptype, "image/", sizeof ("image/") - 1))
802: i = sizeof ("image/") - 1;
803: else
804: i = 0;
805: if (!strcmp (&temptype[i], "gif") || !strcmp (&temptype[i], "x-xbitmap") ||
806: !strcmp (&temptype[i], "x-xpixmap") || !strcmp (&temptype[i], "jpeg") ||
807: !strcmp (&temptype[i], "png"))
1.58 cvs 808: return (TRUE);
809: return (FALSE);
810: }
811:
812: /*----------------------------------------------------------------------
1.9 cvs 813: IsTextName
1.4 cvs 814: ----------------------------------------------------------------------*/
1.111 cvs 815: ThotBool IsTextName (const char *path)
1.106 cvs 816: {
817: char temppath[MAX_LENGTH];
818: char suffix[MAX_LENGTH];
819: char nsuffix[MAX_LENGTH];
1.5 cvs 820: int i;
821:
822: if (!path)
1.13 cvs 823: return (FALSE);
1.5 cvs 824:
1.106 cvs 825: strcpy (temppath, path);
1.124 vatton 826: TtaExtractSuffix (temppath, suffix);
1.5 cvs 827:
828: /* Normalize the suffix */
829: i = 0;
1.106 cvs 830: while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.5 cvs 831: {
1.25 cvs 832: nsuffix[i] = tolower (suffix[i]);
1.5 cvs 833: i++;
834: }
1.106 cvs 835: nsuffix[i] = EOS;
1.5 cvs 836:
1.111 cvs 837: if (!strcmp (nsuffix, "txt") || !strcmp (nsuffix, "dtd"))
1.13 cvs 838: return (TRUE);
1.106 cvs 839: else if (!strcmp (nsuffix, "gz"))
1.13 cvs 840: {
1.39 cvs 841: /* take into account compressed files */
1.124 vatton 842: TtaExtractSuffix (temppath, suffix);
1.13 cvs 843: /* Normalize the suffix */
844: i = 0;
1.106 cvs 845: while (suffix[i] != EOS && i < MAX_LENGTH -1)
1.13 cvs 846: {
1.25 cvs 847: nsuffix[i] = tolower (suffix[i]);
1.13 cvs 848: i++;
849: }
1.106 cvs 850: nsuffix[i] = EOS;
1.111 cvs 851: if (!strcmp (nsuffix, "txt") || !strcmp (nsuffix, "dtd"))
1.13 cvs 852: return (TRUE);
853: else
854: return (FALSE);
855: }
856: else
857: return (FALSE);
1.3 cvs 858: }
859:
1.4 cvs 860: /*----------------------------------------------------------------------
1.9 cvs 861: IsHTTPPath
862: returns TRUE if path is in fact an http URL.
1.4 cvs 863: ----------------------------------------------------------------------*/
1.112 cvs 864: ThotBool IsHTTPPath (const char *path)
1.3 cvs 865: {
1.5 cvs 866: if (!path)
867: return FALSE;
1.3 cvs 868:
1.106 cvs 869: if ((!strncmp (path, "http:", 5) != 0)
870: || (AHTFTPURL_flag () && !strncmp (path, "ftp:", 4))
871: || !strncmp (path, "internal:", 9))
1.58 cvs 872: return TRUE;
873: return FALSE;
1.3 cvs 874: }
875:
1.4 cvs 876: /*----------------------------------------------------------------------
1.9 cvs 877: IsWithParameters
878: returns TRUE if url has a concatenated query string.
1.4 cvs 879: ----------------------------------------------------------------------*/
1.133 vatton 880: ThotBool IsWithParameters (const char *url)
1.3 cvs 881: {
1.5 cvs 882: int i;
1.3 cvs 883:
1.9 cvs 884: if ((!url) || (url[0] == EOS))
1.5 cvs 885: return FALSE;
1.3 cvs 886:
1.9 cvs 887: i = strlen (url) - 1;
888: while (i > 0 && url[i--] != '?')
1.5 cvs 889: if (i < 0)
890: return FALSE;
1.3 cvs 891:
1.5 cvs 892: /* There is a parameter */
893: return TRUE;
1.3 cvs 894: }
895:
1.4 cvs 896: /*----------------------------------------------------------------------
1.9 cvs 897: IsW3Path
898: returns TRUE if path is in fact a URL.
1.4 cvs 899: ----------------------------------------------------------------------*/
1.133 vatton 900: ThotBool IsW3Path (const char *path)
1.106 cvs 901: {
1.170 ! quint 902: if (path == NULL)
! 903: return FALSE;
1.106 cvs 904: if (strncmp (path, "http:", 5) &&
905: strncmp (path, "ftp:", 4) &&
906: strncmp (path, "telnet:", 7) &&
907: strncmp (path, "wais:", 5) &&
908: strncmp (path, "news:", 5) &&
909: strncmp (path, "gopher:", 7) &&
910: strncmp (path, "mailto:", 7) &&
1.132 cheyroul 911: strncmp (path, "archie:", 7) &&
912: strncmp (path, "https:", 6))
1.72 cvs 913: return FALSE;
914: return TRUE;
1.3 cvs 915: }
916:
1.4 cvs 917: /*----------------------------------------------------------------------
1.90 cvs 918: IsFilePath
919: returns TRUE if path is in fact a URL.
920: ----------------------------------------------------------------------*/
1.133 vatton 921: ThotBool IsFilePath (const char *path)
1.90 cvs 922: {
1.106 cvs 923: if (strncmp (path, "file:", 5))
1.90 cvs 924: return FALSE;
925: return TRUE;
926: }
927:
928: /*----------------------------------------------------------------------
1.9 cvs 929: IsValidProtocol
930: returns true if the url protocol is supported by Amaya.
1.4 cvs 931: ----------------------------------------------------------------------*/
1.133 vatton 932: ThotBool IsValidProtocol (const char *url)
1.106 cvs 933: {
934: if (!strncmp (url, "http:", 5)
935: || !strncmp (url, "internal:", 9)
936: || (AHTFTPURL_flag () && !strncmp (url, "ftp:", 4)))
1.22 cvs 937: /* experimental */
1.24 cvs 938: /*** || !strncmp (path, "news:", 5)***/
1.8 cvs 939: return (TRUE);
1.5 cvs 940: else
1.8 cvs 941: return (FALSE);
1.3 cvs 942: }
943:
1.31 cvs 944:
945: /*----------------------------------------------------------------------
946: GetBaseURL
947: normalizes orgName according to a base associated with doc, and
948: following the standard URL format rules.
949: The function returns the base used to solve relative URL and SRC:
950: - the base of the document,
951: - or the document path (without document name).
952: ----------------------------------------------------------------------*/
1.106 cvs 953: char *GetBaseURL (Document doc)
1.31 cvs 954: {
955: Element el;
956: ElementType elType;
957: AttributeType attrType;
958: Attribute attr;
1.106 cvs 959: char *ptr, *basename;
1.31 cvs 960: int length;
1.151 kahan 961: ThotBool hasDocBase;
1.31 cvs 962:
1.113 cvs 963: if (doc == 0 || !DocumentURLs[doc])
1.110 cvs 964: return NULL;
1.148 kahan 965: /* the other functions expect basename to have no more than MAX_LENGTH chars */
1.106 cvs 966: basename = TtaGetMemory (MAX_LENGTH);
1.148 kahan 967: basename[0] = EOS;
1.31 cvs 968: length = MAX_LENGTH -1;
1.151 kahan 969: hasDocBase = FALSE;
970:
971: /* If the document has a base URL, it has a priority over the headers. */
972: /* @@ We need to do this too when we support XML:base */
973:
974: /* is it a HTML document ? */
975: elType.ElSSchema = TtaGetDocumentSSchema (doc);
976: if (!strcmp (TtaGetSSchemaName (elType.ElSSchema), "HTML"))
977: /* it's a HTML document */
978: {
979: /* get the document element */
980: el = TtaGetMainRoot (doc);
981: /* search the BASE element */
982: elType.ElTypeNum = HTML_EL_HEAD;
983: el = TtaSearchTypedElement (elType, SearchForward, el);
984: if (el)
985: /* there is a HEAD element */
986: {
987: /* look for a BASE element within the HEAD */
988: elType.ElTypeNum = HTML_EL_BASE;
989: el = TtaSearchTypedElement (elType, SearchInTree, el);
990: }
991: if (el)
992: {
993: /* The document has a BASE element. Get the HREF attribute of the
994: BASE element */
995: hasDocBase = TRUE;
996: attrType.AttrSSchema = elType.ElSSchema;
997: attrType.AttrTypeNum = HTML_ATTR_HREF_;
998: attr = TtaGetAttribute (el, attrType);
999: if (attr)
1000: {
1001: /* Use the base path of the document */
1002: TtaGiveTextAttributeValue (attr, basename, &length);
1003: }
1004: }
1005: }
1006:
1007: /* there was no BASE. Do we have a location header? */
1008: if (!hasDocBase && DocumentMeta[doc] && DocumentMeta[doc]->full_content_location
1.148 kahan 1009: && DocumentMeta[doc]->full_content_location[0] != EOS)
1.65 cvs 1010: {
1.148 kahan 1011: strncpy (basename, DocumentMeta[doc]->full_content_location, MAX_LENGTH-1);
1012: basename[MAX_LENGTH-1] = EOS;
1013: length = strlen (basename);
1014: }
1015:
1016: if (basename[0] != EOS)
1017: {
1018: /* base and orgName have to be separated by a DIR_SEP */
1019: length--;
1020: if (basename[0] != EOS && basename[length] != URL_SEP &&
1021: basename[length] != DIR_SEP)
1022: /* verify if the base has the form "protocol://server:port" */
1.31 cvs 1023: {
1.148 kahan 1024: ptr = AmayaParseUrl (basename, "", AMAYA_PARSE_ACCESS |
1025: AMAYA_PARSE_HOST |
1026: AMAYA_PARSE_PUNCTUATION);
1027: if (ptr && !strcmp (ptr, basename))
1.31 cvs 1028: {
1.148 kahan 1029: /* it has this form, complete it by adding a URL_STR */
1030: if (strchr (basename, DIR_SEP))
1031: strcat (basename, DIR_STR);
1032: else
1033: strcat (basename, URL_STR);
1034: length++;
1.31 cvs 1035: }
1.149 kahan 1036: else if (!ptr || ptr[0] == EOS)
1037: {
1038: /* no host was detected, we may have a relative URL. We test
1039: if it begins with a URL_SEP, DIR_SEP or period. If yes, it's
1040: relative. */
1041: if (! (basename[0] == '.' || basename[0] == URL_SEP
1042: || basename[0] == DIR_SEP))
1043: basename[0] = EOS;
1044: }
1.148 kahan 1045: if (ptr)
1046: TtaFreeMemory (ptr);
1.31 cvs 1047: }
1.113 cvs 1048: }
1049:
1.148 kahan 1050: /* there was no base element and no location header, we use the DocumentURL */
1051: if (basename[0] == EOS)
1052: {
1053: strncpy (basename, DocumentURLs[doc], MAX_LENGTH-1);
1054: basename[MAX_LENGTH-1] = EOS;
1055: }
1056:
1.31 cvs 1057: /* Remove anything after the last DIR_SEP char. If no such char is found,
1058: * then search for the first ":" char, hoping that what's before that is a
1059: * protocol. If found, end the string there. If neither char is found,
1060: * then discard the whole base element.
1061: */
1.106 cvs 1062: length = strlen (basename) - 1;
1.31 cvs 1063: /* search for the last DIR_SEP char */
1.106 cvs 1064: while (length >= 0 && basename[length] != URL_SEP && basename[length] != DIR_SEP)
1.31 cvs 1065: length--;
1066: if (length >= 0)
1067: /* found the last DIR_SEP char, end the string there */
1.106 cvs 1068: basename[length + 1] = EOS;
1.31 cvs 1069: else
1070: /* search for the first PATH_STR char */
1071: {
1.106 cvs 1072: for (length = 0; basename[length] != ':' &&
1073: basename[length] != EOS; length ++);
1074: if (basename[length] == ':')
1.31 cvs 1075: /* found, so end the string there */
1.106 cvs 1076: basename[length + 1] = EOS;
1.31 cvs 1077: else
1078: /* not found, discard the base */
1.106 cvs 1079: basename[0] = EOS;
1.31 cvs 1080: }
1081: return (basename);
1082: }
1083:
1084:
1.4 cvs 1085: /*----------------------------------------------------------------------
1.40 cvs 1086: GetLocalPath
1087: Allocate and return the local document path associated to the url
1088: ----------------------------------------------------------------------*/
1.150 vatton 1089: char *GetLocalPath (Document doc, char *url)
1.106 cvs 1090: {
1091: char *ptr;
1092: char *n;
1093: char *documentname;
1094: char url_sep;
1.83 cvs 1095: int len;
1.67 cvs 1096: ThotBool noFile;
1.40 cvs 1097:
1.153 vatton 1098: if (url)
1.40 cvs 1099: {
1100: /* check whether the file name exists */
1.106 cvs 1101: len = strlen (url) - 1;
1.71 cvs 1102: if (IsW3Path (url))
1.157 kahan 1103: url_sep = URL_SEP;
1.41 cvs 1104: else
1.106 cvs 1105: url_sep = DIR_SEP;
1.41 cvs 1106: noFile = (url[len] == url_sep);
1.40 cvs 1107: if (noFile)
1.106 cvs 1108: url[len] = EOS;
1109: ptr = TtaGetMemory (MAX_LENGTH);
1110: documentname = TtaGetMemory (MAX_LENGTH);
1.78 cvs 1111: TtaExtractName (url, ptr, documentname);
1.106 cvs 1112: sprintf (ptr, "%s%s%d%s", TempFileDirectory, DIR_STR, doc, DIR_STR);
1.40 cvs 1113: if (!TtaCheckDirectory (ptr))
1114: /* directory did not exist */
1.72 cvs 1115: TtaMakeDirectory (ptr);
1.47 cvs 1116:
1.153 vatton 1117: if (doc == 0)
1118: {
1119: n = strrchr (documentname, '.');
1120: if (n)
1121: *n = EOS;
1122: if (documentname[0] == EOS)
1123: strcpy (documentname, "noname");
1124: n = GetTempName (ptr, documentname);
1125: TtaFreeMemory (ptr);
1126: ptr = n;
1127: }
1.69 cvs 1128: else
1.153 vatton 1129: {
1130: /* don't include the query string within document name */
1131: n = strrchr (documentname, '?');
1132: if (n)
1133: *n = EOS;
1134: /* don't include ':' within document name */
1135: n = strchr (documentname, ':');
1136: if (n)
1137: *n = EOS;
1138: /* if after all this operations document name
1139: is empty, let's use noname.html instead */
1140: if (documentname[0] == EOS)
1141: strcat (ptr, "noname.html");
1142: else
1143: strcat (ptr, documentname);
1144: }
1.40 cvs 1145: TtaFreeMemory (documentname);
1.157 kahan 1146: /* substitute invalid chars in file names by a _ */
1147: n = ptr;
1148: while (*n)
1149: {
1150: if (*n == '*' || *n == ',')
1151: *n = '_';
1152: n++;
1153: }
1.40 cvs 1154: /* restore the url */
1155: if (noFile)
1.41 cvs 1156: url[len] = url_sep;
1.40 cvs 1157: return (ptr);
1158: }
1159: else
1160: return (NULL);
1161: }
1162:
1.73 cvs 1163: /*----------------------------------------------------------------------
1.79 cvs 1164: ExtractTarget extract the target name from document nane.
1165: ----------------------------------------------------------------------*/
1.150 vatton 1166: void ExtractTarget (char *aName, char *target)
1.79 cvs 1167: {
1.106 cvs 1168: int lg, i;
1169: char *ptr;
1170: char *oldptr;
1.79 cvs 1171:
1172: if (!target || !aName)
1173: /* bad target */
1174: return;
1175:
1.106 cvs 1176: target[0] = EOS;
1177: lg = strlen (aName);
1.79 cvs 1178: if (lg)
1179: {
1180: /* the name is not empty */
1181: oldptr = ptr = &aName[0];
1182: do
1183: {
1.106 cvs 1184: ptr = strrchr (oldptr, '#');
1.79 cvs 1185: if (ptr)
1186: oldptr = &ptr[1];
1187: }
1188: while (ptr);
1189:
1190: i = (int) (oldptr) - (int) (aName); /* name length */
1191: if (i > 1)
1192: {
1.106 cvs 1193: aName[i - 1] = EOS;
1.79 cvs 1194: if (i != lg)
1.106 cvs 1195: strcpy (target, oldptr);
1.79 cvs 1196: }
1197: }
1198: }
1199:
1200: /*----------------------------------------------------------------------
1.90 cvs 1201: RemoveNewLines (text)
1202: Removes any '\n' chars that are found in text.
1203: Returns TRUE if it did the operation, FALSE otherwise.
1.73 cvs 1204: ----------------------------------------------------------------------*/
1.106 cvs 1205: ThotBool RemoveNewLines (char *text)
1206: {
1207: ThotBool change = FALSE;
1208: char *src;
1209: char *dest;
1.90 cvs 1210:
1211: src = text;
1212: dest = text;
1.115 kahan 1213:
1214: /* remove any preceding whitespace */
1215: while (*src && *src == ' ')
1216: {
1217: src++;
1218: change = 1;
1219: }
1220:
1.90 cvs 1221: while (*src)
1222: {
1223: switch (*src)
1224: {
1.106 cvs 1225: case '\n':
1.90 cvs 1226: /* don't copy the newline */
1227: change = 1;
1228: break;
1229: default:
1230: *dest = *src;
1231: dest++;
1232: break;
1233: }
1234: src++;
1235: }
1236: /* copy the last EOS char */
1237: *dest = *src;
1238:
1239: return (change);
1240: }
1241:
1242: /*----------------------------------------------------------------------
1243: CleanCopyFileURL
1244: Copies a file url from a src string to destination string.
1.97 cvs 1245: convertion says which type of convertion (none, %xx, URL_SEP into DIR_SEP
1246: we want to do).
1.90 cvs 1247: ----------------------------------------------------------------------*/
1.106 cvs 1248: static void CleanCopyFileURL (char *dest, char *src,
1249: ConvertionType convertion)
1.90 cvs 1250: {
1251: while (*src)
1.89 cvs 1252: {
1.90 cvs 1253: switch (*src)
1.89 cvs 1254: {
1255: #ifdef _WINDOWS
1.106 cvs 1256: case URL_SEP:
1.96 cvs 1257: /* make DIR_SEP transformation */
1.97 cvs 1258: if (convertion & AM_CONV_URL_SEP)
1.106 cvs 1259: *dest = DIR_SEP;
1.96 cvs 1260: else
1261: *dest = *src;
1.90 cvs 1262: dest++;
1.96 cvs 1263: src++;
1.90 cvs 1264: break;
1.89 cvs 1265: #endif /* _WINDOWS */
1.96 cvs 1266:
1.106 cvs 1267: case '%':
1.97 cvs 1268: if (convertion & AM_CONV_PERCENT)
1.96 cvs 1269: {
1.97 cvs 1270: /* (code adapted from libwww's HTUnEscape function */
1.96 cvs 1271: src++;
1.106 cvs 1272: if (*src != EOS)
1.97 cvs 1273: {
1274: *dest = UnEscapeChar (*src) * 16;
1275: src++;
1276: }
1.106 cvs 1277: if (*src != EOS)
1.97 cvs 1278: {
1279: *dest = *dest + UnEscapeChar (*src);
1280: src++;
1281: }
1282: dest++;
1.96 cvs 1283: }
1.97 cvs 1284: else
1.96 cvs 1285: {
1.97 cvs 1286: *dest = *src;
1287: dest++;
1.96 cvs 1288: src++;
1289: }
1290: break;
1291:
1.90 cvs 1292: default:
1293: *dest = *src;
1.89 cvs 1294: dest++;
1.96 cvs 1295: src++;
1.90 cvs 1296: break;
1.89 cvs 1297: }
1298: }
1.90 cvs 1299: /* copy the EOS char */
1300: *dest = *src;
1.73 cvs 1301: }
1.40 cvs 1302:
1303: /*----------------------------------------------------------------------
1.9 cvs 1304: NormalizeURL
1305: normalizes orgName according to a base associated with doc, and
1306: following the standard URL format rules.
1.113 cvs 1307: if doc is < 0, use as a base the URL of the document that contains
1308: (or contained) the elements that are now in the copy/cut buffer.
1.53 cvs 1309: if doc is 0 and otherPath not NULL, normalizes orgName according to this
1310: other path.
1.9 cvs 1311: The function returns the new complete and normalized URL
1.12 cvs 1312: or file name path (newName) and the name of the document (docName).
1.9 cvs 1313: N.B. If the function can't find out what's the docName, it assigns
1314: the name "noname.html".
1.4 cvs 1315: ----------------------------------------------------------------------*/
1.106 cvs 1316: void NormalizeURL (char *orgName, Document doc, char *newName,
1317: char *docName, char *otherPath)
1318: {
1319: char *basename;
1320: char tempOrgName[MAX_LENGTH];
1321: char *ptr;
1322: char used_sep;
1.84 cvs 1323: int length;
1324: ThotBool check;
1.5 cvs 1325:
1.110 cvs 1326: #ifdef _WINDOWS
1.44 cvs 1327: int ndx;
1.110 cvs 1328: #endif /* _WINDOWS */
1.44 cvs 1329:
1.5 cvs 1330: if (!newName || !docName)
1331: return;
1.18 cvs 1332:
1.113 cvs 1333: if (doc < 0)
1334: basename = TtaStrdup (SavedDocumentURL);
1335: else if (doc > 0)
1.53 cvs 1336: basename = GetBaseURL (doc);
1337: else if (otherPath != NULL)
1.108 cvs 1338: basename = TtaStrdup (otherPath);
1.32 cvs 1339: else
1.53 cvs 1340: basename = NULL;
1.32 cvs 1341:
1.18 cvs 1342: /*
1.31 cvs 1343: * Clean orgName
1344: * Make sure we have a complete orgName, without any leading or trailing
1345: * white spaces, or trailinbg new lines
1346: */
1.5 cvs 1347: ptr = orgName;
1.18 cvs 1348: /* skip leading white space and new line characters */
1.106 cvs 1349: while ((*ptr == SPACE || *ptr == EOL) && *ptr++ != EOS);
1350: strncpy (tempOrgName, ptr, MAX_LENGTH -1);
1351: tempOrgName[MAX_LENGTH -1] = EOS;
1.18 cvs 1352: /*
1.31 cvs 1353: * Make orgName a complete URL
1354: * If the URL does not include a protocol, then try to calculate
1355: * one using the doc's base element (if it exists),
1356: */
1.106 cvs 1357: if (tempOrgName[0] == EOS)
1.53 cvs 1358: {
1.106 cvs 1359: newName[0] = EOS;
1360: docName[0] = EOS;
1.53 cvs 1361: TtaFreeMemory (basename);
1362: return;
1363: }
1.49 cvs 1364:
1365: /* clean trailing white space */
1.106 cvs 1366: length = strlen (tempOrgName) - 1;
1367: while (tempOrgName[length] == SPACE && tempOrgName[length] == EOL)
1.53 cvs 1368: {
1.106 cvs 1369: tempOrgName[length] = EOS;
1.53 cvs 1370: length--;
1371: }
1.50 cvs 1372:
1.55 cvs 1373: /* remove extra dot (which dot???) */
1374: /* ugly, but faster than a strcmp */
1.106 cvs 1375: if (tempOrgName[length] == '.'
1376: && (length == 0 || tempOrgName[length-1] != '.'))
1377: tempOrgName[length] = EOS;
1.50 cvs 1378:
1.94 cvs 1379: if (IsW3Path (tempOrgName))
1.53 cvs 1380: {
1381: /* the name is complete, go to the Sixth Step */
1.106 cvs 1382: strcpy (newName, tempOrgName);
1.53 cvs 1383: SimplifyUrl (&newName);
1384: /* verify if the URL has the form "protocol://server:port" */
1.110 cvs 1385: ptr = AmayaParseUrl (newName, "", AMAYA_PARSE_ACCESS |
1386: AMAYA_PARSE_HOST |
1387: AMAYA_PARSE_PUNCTUATION);
1388: if (ptr && !strcmp (ptr, newName))
1389: /* it has this form, we complete it by adding a DIR_STR */
1.106 cvs 1390: strcat (newName, URL_STR);
1.49 cvs 1391:
1.53 cvs 1392: if (ptr)
1.50 cvs 1393: TtaFreeMemory (ptr);
1.53 cvs 1394: }
1.113 cvs 1395: else if (basename == NULL)
1.53 cvs 1396: /* the name is complete, go to the Sixth Step */
1.106 cvs 1397: strcpy (newName, tempOrgName);
1.53 cvs 1398: else
1399: {
1.31 cvs 1400: /* Calculate the absolute URL, using the base or document URL */
1.110 cvs 1401: #ifdef _WINDOWS
1.53 cvs 1402: if (!IsW3Path (basename))
1403: {
1.106 cvs 1404: length = strlen (tempOrgName);
1.53 cvs 1405: for (ndx = 0; ndx < length; ndx++)
1.106 cvs 1406: if (tempOrgName [ndx] == '/')
1407: tempOrgName [ndx] = '\\';
1.53 cvs 1408: }
1.110 cvs 1409: #endif /* _WINDOWS */
1.25 cvs 1410: ptr = AmayaParseUrl (tempOrgName, basename, AMAYA_PARSE_ALL);
1.53 cvs 1411: if (ptr)
1412: {
1413: SimplifyUrl (&ptr);
1.106 cvs 1414: strcpy (newName, ptr);
1.53 cvs 1415: TtaFreeMemory (ptr);
1416: }
1417: else
1.106 cvs 1418: newName[0] = EOS;
1.53 cvs 1419: }
1.36 cvs 1420:
1421: TtaFreeMemory (basename);
1.18 cvs 1422: /*
1.31 cvs 1423: * Prepare the docname that will refer to this ressource in the
1424: * .amaya directory. If the new URL finishes on DIR_SEP, then use
1425: * noname.html as a default ressource name
1.18 cvs 1426: */
1.106 cvs 1427: if (newName[0] != EOS)
1.53 cvs 1428: {
1.106 cvs 1429: length = strlen (newName) - 1;
1430: if (newName[length] == URL_SEP || newName[length] == DIR_SEP)
1.53 cvs 1431: {
1432: used_sep = newName[length];
1433: check = TRUE;
1434: while (check)
1435: {
1.50 cvs 1436: length--;
1437: while (length >= 0 && newName[length] != used_sep)
1.53 cvs 1438: length--;
1.106 cvs 1439: if (!strncmp (&newName[length+1], "..", 2))
1.53 cvs 1440: {
1.106 cvs 1441: newName[length+1] = EOS;
1.53 cvs 1442: /* remove also previous directory */
1443: length--;
1444: while (length >= 0 && newName[length] != used_sep)
1445: length--;
1.106 cvs 1446: if (strncmp (&newName[length+1], "//", 2))
1.131 cheyroul 1447: /* don't remove server name */
1.106 cvs 1448: newName[length+1] = EOS;
1.53 cvs 1449: }
1.106 cvs 1450: else if (!strncmp (&newName[length+1], ".", 1))
1451: newName[length+1] = EOS;
1.50 cvs 1452: else
1.53 cvs 1453: check = FALSE;
1454: }
1455: /* docname was not comprised inside the URL, so let's */
1456: /* assign the default ressource name */
1.106 cvs 1457: strcpy (docName, "noname.html");
1.53 cvs 1458: }
1459: else
1460: { /* docname is comprised inside the URL */
1.110 cvs 1461: while (length >= 0 && newName[length] != URL_SEP &&
1462: newName[length] != DIR_SEP)
1.53 cvs 1463: length--;
1464: if (length < 0)
1.106 cvs 1465: strcpy (docName, newName);
1.53 cvs 1466: else
1.106 cvs 1467: strcpy (docName, &newName[length+1]);
1.53 cvs 1468: }
1469: }
1470: else
1.106 cvs 1471: docName[0] = EOS;
1.18 cvs 1472: }
1.3 cvs 1473:
1.4 cvs 1474: /*----------------------------------------------------------------------
1.9 cvs 1475: IsSameHost
1.4 cvs 1476: ----------------------------------------------------------------------*/
1.106 cvs 1477: ThotBool IsSameHost (const char *url1, const char *url2)
1.3 cvs 1478: {
1.106 cvs 1479: char *basename_ptr1, *basename_ptr2;
1480: ThotBool result;
1.3 cvs 1481:
1.106 cvs 1482: basename_ptr1 = AmayaParseUrl (url1, "",
1483: AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
1484: basename_ptr2 = AmayaParseUrl (url2, "",
1485: AMAYA_PARSE_ACCESS | AMAYA_PARSE_HOST | AMAYA_PARSE_PUNCTUATION);
1.3 cvs 1486:
1.106 cvs 1487: if (strcmp (basename_ptr1, basename_ptr2))
1488: result = FALSE;
1489: else
1490: result = TRUE;
1491: TtaFreeMemory (basename_ptr1);
1492: TtaFreeMemory (basename_ptr2);
1493: return (result);
1.3 cvs 1494: }
1495:
1496:
1.4 cvs 1497: /*----------------------------------------------------------------------
1.22 cvs 1498: HasKnownFileSuffix
1499: returns TRUE if path points to a file ending with a suffix.
1500: ----------------------------------------------------------------------*/
1.153 vatton 1501: ThotBool HasKnownFileSuffix (const char *path)
1.106 cvs 1502: {
1503: char *root;
1504: char temppath[MAX_LENGTH];
1505: char suffix[MAX_LENGTH];
1.22 cvs 1506:
1.106 cvs 1507: if (!path || path[0] == EOS || path[strlen(path)] == DIR_SEP)
1.22 cvs 1508: return (FALSE);
1509:
1.106 cvs 1510: root = AmayaParseUrl(path, "", AMAYA_PARSE_PATH | AMAYA_PARSE_PUNCTUATION);
1.22 cvs 1511:
1512: if (root)
1513: {
1.106 cvs 1514: strcpy (temppath, root);
1.25 cvs 1515: TtaFreeMemory (root);
1.22 cvs 1516: /* Get the suffix */
1.124 vatton 1517: TtaExtractSuffix (temppath, suffix);
1.22 cvs 1518:
1.106 cvs 1519: if( suffix[0] == EOS)
1.22 cvs 1520: /* no suffix */
1521: return (FALSE);
1522:
1523: /* Normalize the suffix */
1524: ConvertToLowerCase (suffix);
1525:
1.106 cvs 1526: if (!strcmp (suffix, "gz"))
1.22 cvs 1527: /* skip the compressed suffix */
1528: {
1.124 vatton 1529: TtaExtractSuffix (temppath, suffix);
1.106 cvs 1530: if(suffix[0] == EOS)
1.22 cvs 1531: /* no suffix */
1532: return (FALSE);
1533: /* Normalize the suffix */
1534: ConvertToLowerCase (suffix);
1535: }
1536:
1.106 cvs 1537: if (strcmp (suffix, "gif") &&
1538: strcmp (suffix, "xbm") &&
1539: strcmp (suffix, "xpm") &&
1540: strcmp (suffix, "jpg") &&
1541: strcmp (suffix, "pdf") &&
1542: strcmp (suffix, "png") &&
1543: strcmp (suffix, "tgz") &&
1544: strcmp (suffix, "xpg") &&
1545: strcmp (suffix, "xpd") &&
1546: strcmp (suffix, "ps") &&
1547: strcmp (suffix, "au") &&
1548: strcmp (suffix, "html") &&
1549: strcmp (suffix, "htm") &&
1550: strcmp (suffix, "shtml") &&
1551: strcmp (suffix, "xht") &&
1552: strcmp (suffix, "xhtm") &&
1553: strcmp (suffix, "xhtml") &&
1554: strcmp (suffix, "txt") &&
1555: strcmp (suffix, "css") &&
1556: strcmp (suffix, "eps"))
1.22 cvs 1557: return (FALSE);
1558: else
1559: return (TRUE);
1560: }
1561: else
1562: return (FALSE);
1563: }
1564:
1565:
1566: /*----------------------------------------------------------------------
1.24 cvs 1567: ChopURL
1568: Gives back a URL no longer than MAX_PRINT_URL_LENGTH chars (outputURL).
1569: If inputURL is bigger than that size, outputURL receives
1570: MAX_PRINT_URL_LENGTH / 2 chars from the beginning of inputURL, "...",
1571: and MAX_PRINT_URL_LENGTH / 2 chars from the end of inputURL.
1572: If inputURL is not longer than MAX_PRINT_URL_LENGTH chars, it gets
1573: copied into outputURL.
1574: N.B.: outputURL must point to a memory block of MAX_PRINT_URL_LENGTH
1575: chars.
1576: ----------------------------------------------------------------------*/
1.106 cvs 1577: void ChopURL (char *outputURL, const char *inputURL)
1.24 cvs 1578: {
1579: int len;
1.9 cvs 1580:
1.106 cvs 1581: len = strlen (inputURL);
1.24 cvs 1582: if (len <= MAX_PRINT_URL_LENGTH)
1.106 cvs 1583: strcpy (outputURL, inputURL);
1.24 cvs 1584: else
1585: /* make a truncated urlName on the status window */
1586: {
1.106 cvs 1587: strncpy (outputURL, inputURL, MAX_PRINT_URL_LENGTH / 2);
1588: outputURL [MAX_PRINT_URL_LENGTH / 2] = EOS;
1589: strcat (outputURL, "...");
1590: strcat (outputURL, &(inputURL[len - MAX_PRINT_URL_LENGTH / 2 ]));
1.24 cvs 1591: }
1.25 cvs 1592: }
1593:
1594:
1595: /*----------------------------------------------------------------------
1596: scan
1.47 cvs 1597: Scan a filename for its constituents
1.25 cvs 1598: -----------------------------------
1599:
1600: On entry,
1601: name points to a document name which may be incomplete.
1602: On exit,
1603: absolute or relative may be nonzero (but not both).
1604: host, fragment and access may be nonzero if they were specified.
1605: Any which are nonzero point to zero terminated strings.
1606: ----------------------------------------------------------------------*/
1.106 cvs 1607: static void scan (char *name, HTURI *parts)
1.25 cvs 1608: {
1.106 cvs 1609: char * p;
1610: char * after_access = name;
1.32 cvs 1611:
1.43 cvs 1612: memset (parts, '\0', sizeof (HTURI));
1.28 cvs 1613: /* Look for fragment identifier */
1.106 cvs 1614: if ((p = strchr(name, '#')) != NULL)
1.28 cvs 1615: {
1.106 cvs 1616: *p++ = '\0';
1.28 cvs 1617: parts->fragment = p;
1.25 cvs 1618: }
1619:
1.28 cvs 1620: for (p=name; *p; p++)
1621: {
1.106 cvs 1622: if (*p == URL_SEP || *p == DIR_SEP || *p == '#' || *p == '?')
1.28 cvs 1623: break;
1.106 cvs 1624: if (*p == ':')
1.28 cvs 1625: {
1626: *p = 0;
1627: parts->access = after_access; /* Scheme has been specified */
1628:
1629: /* The combination of gcc, the "-O" flag and the HP platform is
1630: unhealthy. The following three lines is a quick & dirty fix, but is
1631: not recommended. Rather, turn off "-O". */
1632:
1633: /* after_access = p;*/
1634: /* while (*after_access == 0)*/
1635: /* after_access++;*/
1636: after_access = p+1;
1.106 cvs 1637: if (!strcasecmp("URL", parts->access))
1.28 cvs 1638: /* Ignore IETF's URL: pre-prefix */
1639: parts->access = NULL;
1640: else
1.25 cvs 1641: break;
1642: }
1643: }
1644:
1645: p = after_access;
1.43 cvs 1646: if (*p == URL_SEP || *p == DIR_SEP)
1.28 cvs 1647: {
1.43 cvs 1648: if (p[1] == URL_SEP)
1.28 cvs 1649: {
1.25 cvs 1650: parts->host = p+2; /* host has been specified */
1.28 cvs 1651: *p = 0; /* Terminate access */
1652: /* look for end of host name if any */
1.106 cvs 1653: p = strchr (parts->host, URL_SEP);
1.28 cvs 1654: if (p)
1655: {
1.106 cvs 1656: *p = EOS; /* Terminate host */
1.25 cvs 1657: parts->absolute = p+1; /* Root has been found */
1.28 cvs 1658: }
1659: }
1660: else
1661: /* Root found but no host */
1662: parts->absolute = p+1;
1663: }
1664: else
1665: {
1.25 cvs 1666: parts->relative = (*after_access) ? after_access : 0; /* zero for "" */
1.28 cvs 1667: }
1.25 cvs 1668: }
1669:
1670:
1671: /*----------------------------------------------------------------------
1.28 cvs 1672: AmayaParseUrl: parse a Name relative to another name
1673:
1674: This returns those parts of a name which are given (and requested)
1675: substituting bits from the related name where necessary.
1.25 cvs 1676:
1.28 cvs 1677: On entry,
1.25 cvs 1678: aName A filename given
1679: relatedName A name relative to which aName is to be parsed. Give
1680: it an empty string if aName is absolute.
1681: wanted A mask for the bits which are wanted.
1682:
1.28 cvs 1683: On exit,
1.25 cvs 1684: returns A pointer to a malloc'd string which MUST BE FREED
1685: ----------------------------------------------------------------------*/
1.106 cvs 1686: char *AmayaParseUrl (const char *aName, char *relatedName, int wanted)
1687: {
1688: char *return_value;
1689: char result[MAX_LENGTH];
1690: char name[MAX_LENGTH];
1691: char rel[MAX_LENGTH];
1692: char *p, *access;
1.29 cvs 1693: HTURI given, related;
1694: int len;
1.106 cvs 1695: char used_sep;
1696: char *used_str;
1.32 cvs 1697:
1.106 cvs 1698: if (strchr (aName, DIR_SEP) || strchr (relatedName, DIR_SEP))
1.33 cvs 1699: {
1.106 cvs 1700: used_str = DIR_STR;
1701: used_sep = DIR_SEP;
1.33 cvs 1702: }
1.32 cvs 1703: else
1.33 cvs 1704: {
1.106 cvs 1705: used_str = URL_STR;
1706: used_sep = URL_SEP;
1.33 cvs 1707: }
1.32 cvs 1708:
1.29 cvs 1709: /* Make working copies of input strings to cut up: */
1710: return_value = NULL;
1711: result[0] = 0; /* Clear string */
1.169 quint 1712: rel[0] = EOS;
1713: strncpy (name, aName, MAX_LENGTH - 1);
1714: name[MAX_LENGTH - 1] = EOS;
1715: if (relatedName != NULL)
1716: {
1717: strncpy (rel, relatedName, MAX_LENGTH - 1);
1718: rel[MAX_LENGTH - 1] = EOS;
1719: }
1.29 cvs 1720: else
1.106 cvs 1721: relatedName[0] = EOS;
1.29 cvs 1722:
1723: scan (name, &given);
1724: scan (rel, &related);
1725: access = given.access ? given.access : related.access;
1726: if (wanted & AMAYA_PARSE_ACCESS)
1727: if (access)
1728: {
1.106 cvs 1729: strcat (result, access);
1.29 cvs 1730: if(wanted & AMAYA_PARSE_PUNCTUATION)
1.106 cvs 1731: strcat (result, ":");
1.29 cvs 1732: }
1733:
1734: if (given.access && related.access)
1735: /* If different, inherit nothing. */
1.106 cvs 1736: if (strcmp (given.access, related.access) != 0)
1.29 cvs 1737: {
1738: related.host = 0;
1739: related.absolute = 0;
1740: related.relative = 0;
1741: related.fragment = 0;
1742: }
1743:
1744: if (wanted & AMAYA_PARSE_HOST)
1745: if(given.host || related.host)
1746: {
1747: if(wanted & AMAYA_PARSE_PUNCTUATION)
1.106 cvs 1748: strcat (result, "//");
1749: strcat (result, given.host ? given.host : related.host);
1.29 cvs 1750: }
1751:
1752: if (given.host && related.host)
1753: /* If different hosts, inherit no path. */
1.106 cvs 1754: if (strcmp (given.host, related.host) != 0)
1.29 cvs 1755: {
1756: related.absolute = 0;
1757: related.relative = 0;
1758: related.fragment = 0;
1759: }
1760:
1761: if (wanted & AMAYA_PARSE_PATH)
1762: {
1763: if (given.absolute)
1764: {
1765: /* All is given */
1766: if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106 cvs 1767: strcat (result, used_str);
1768: strcat (result, given.absolute);
1.25 cvs 1769: }
1.29 cvs 1770: else if (related.absolute)
1771: {
1772: /* Adopt path not name */
1.106 cvs 1773: strcat (result, used_str);
1774: strcat (result, related.absolute);
1.29 cvs 1775: if (given.relative)
1776: {
1777: /* Search part? */
1.106 cvs 1778: p = strchr (result, '?');
1.29 cvs 1779: if (!p)
1.106 cvs 1780: p=result+strlen(result)-1;
1.33 cvs 1781: for (; *p!=used_sep; p--); /* last / */
1.29 cvs 1782: /* Remove filename */
1783: p[1]=0;
1784: /* Add given one */
1.106 cvs 1785: strcat (result, given.relative);
1.25 cvs 1786: }
1787: }
1.29 cvs 1788: else if (given.relative)
1789: /* what we've got */
1.106 cvs 1790: strcat (result, given.relative);
1.29 cvs 1791: else if (related.relative)
1.106 cvs 1792: strcat (result, related.relative);
1.29 cvs 1793: else
1794: /* No inheritance */
1.106 cvs 1795: strcat (result, used_str);
1.25 cvs 1796: }
1.29 cvs 1797:
1798: if (wanted & AMAYA_PARSE_ANCHOR)
1799: if (given.fragment || related.fragment)
1800: {
1801: if (given.absolute && given.fragment)
1802: {
1803: /*Fixes for relURLs...*/
1804: if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106 cvs 1805: strcat (result, "#");
1806: strcat (result, given.fragment);
1.29 cvs 1807: }
1808: else if (!(given.absolute) && !(given.fragment))
1.106 cvs 1809: strcat (result, "");
1.29 cvs 1810: else
1811: {
1.110 cvs 1812: if (wanted & AMAYA_PARSE_PUNCTUATION)
1.106 cvs 1813: strcat (result, "#");
1.110 cvs 1814: strcat (result, given.fragment ? given.fragment : related.fragment);
1.29 cvs 1815: }
1816: }
1.106 cvs 1817: len = strlen (result);
1818: if ((return_value = TtaGetMemory (len + 1)) != NULL)
1819: strcpy (return_value, result);
1.29 cvs 1820: return (return_value); /* exactly the right length */
1.25 cvs 1821: }
1822:
1823: /*----------------------------------------------------------------------
1824: HTCanon
1825: Canonicalizes the URL in the following manner starting from the host
1826: pointer:
1827:
1828: 1) The host name is converted to lowercase
1829: 2) Chop off port if `:80' (http), `:70' (gopher), or `:21' (ftp)
1830:
1831: Return: OK The position of the current path part of the URL
1832: which might be the old one or a new one.
1833:
1834: ----------------------------------------------------------------------*/
1.106 cvs 1835: static char *HTCanon (char **filename, char *host)
1836: {
1837: char *newname = NULL;
1838: char used_sep;
1839: char *path;
1840: char *strptr;
1841: char *port;
1842: char *access = host-3;
1843:
1844: if (*filename && strchr (*filename, URL_SEP))
1845: used_sep = URL_SEP;
1.33 cvs 1846: else
1.106 cvs 1847: used_sep = DIR_SEP;
1.32 cvs 1848:
1.110 cvs 1849: while (access > *filename && *(access - 1) != used_sep) /* Find access method */
1.25 cvs 1850: access--;
1.110 cvs 1851: if ((path = strchr (host, used_sep)) == NULL) /* Find path */
1.106 cvs 1852: path = host + strlen (host);
1853: if ((strptr = strchr (host, '@')) != NULL && strptr < path) /* UserId */
1.82 cvs 1854: host = strptr;
1.110 cvs 1855: if ((port = strchr (host, ':')) != NULL && port > path) /* Port number */
1.82 cvs 1856: port = NULL;
1.25 cvs 1857:
1858: strptr = host; /* Convert to lower-case */
1.82 cvs 1859: while (strptr < path)
1.33 cvs 1860: {
1.123 vatton 1861: *strptr = tolower (*strptr);
1.82 cvs 1862: strptr++;
1.33 cvs 1863: }
1.25 cvs 1864:
1865: /* Does the URL contain a full domain name? This also works for a
1866: numerical host name. The domain name is already made lower-case
1867: and without a trailing dot. */
1868: {
1.106 cvs 1869: char *dot = port ? port : path;
1870: if (dot > *filename && *--dot == '.')
1.33 cvs 1871: {
1.106 cvs 1872: char *orig = dot;
1873: char *dest = dot + 1;
1.82 cvs 1874: while ((*orig++ = *dest++));
1875: if (port) port--;
1.33 cvs 1876: path--;
1.25 cvs 1877: }
1878: }
1879: /* Chop off port if `:', `:80' (http), `:70' (gopher), or `:21' (ftp) */
1.33 cvs 1880: if (port)
1881: {
1.82 cvs 1882: if (!*(port+1) || *(port+1) == used_sep)
1.33 cvs 1883: {
1884: if (!newname)
1885: {
1.106 cvs 1886: char *orig = port;
1887: char *dest = port + 1;
1.82 cvs 1888: while ((*orig++ = *dest++));
1.33 cvs 1889: }
1890: }
1.106 cvs 1891: else if ((!strncmp (access, "http", 4) &&
1892: (*(port + 1) == '8' &&
1893: *(port+2) == '0' &&
1.82 cvs 1894: (*(port+3) == used_sep || !*(port + 3)))) ||
1.106 cvs 1895: (!strncmp (access, "gopher", 6) &&
1896: (*(port+1) == '7' &&
1897: *(port+2) == '0' &&
1.82 cvs 1898: (*(port+3) == used_sep || !*(port+3)))) ||
1.106 cvs 1899: (!strncmp (access, "ftp", 3) &&
1900: (*(port+1) == '2' &&
1901: *(port + 2) == '1' &&
1.82 cvs 1902: (*(port+3) == used_sep || !*(port+3))))) {
1.33 cvs 1903: if (!newname)
1904: {
1.106 cvs 1905: char *orig = port;
1906: char *dest = port + 3;
1.33 cvs 1907: while((*orig++ = *dest++));
1908: /* Update path position, Henry Minsky */
1909: path -= 3;
1.25 cvs 1910: }
1.33 cvs 1911: }
1912: else if (newname)
1.106 cvs 1913: strncat (newname, port, (int) (path - port));
1.33 cvs 1914: }
1.25 cvs 1915:
1.33 cvs 1916: if (newname)
1917: {
1.106 cvs 1918: char *newpath = newname + strlen (newname);
1919: strcat (newname, path);
1.25 cvs 1920: path = newpath;
1.28 cvs 1921: /* Free old copy */
1922: TtaFreeMemory(*filename);
1.25 cvs 1923: *filename = newname;
1.33 cvs 1924: }
1.25 cvs 1925: return path;
1926: }
1927:
1928:
1929: /*----------------------------------------------------------------------
1.29 cvs 1930: SimplifyUrl: simplify a URI
1.32 cvs 1931: A URI is allowed to contain the sequence xxx/../ which may be
1932: replaced by "" , and the sequence "/./" which may be replaced by DIR_STR.
1.28 cvs 1933: Simplification helps us recognize duplicate URIs.
1.25 cvs 1934:
1.28 cvs 1935: Thus, /etc/junk/../fred becomes /etc/fred
1936: /etc/junk/./fred becomes /etc/junk/fred
1.25 cvs 1937:
1.28 cvs 1938: but we should NOT change
1939: http://fred.xxx.edu/../..
1.25 cvs 1940:
1941: or ../../albert.html
1942:
1.28 cvs 1943: In order to avoid empty URLs the following URLs become:
1.25 cvs 1944:
1945: /fred/.. becomes /fred/..
1946: /fred/././.. becomes /fred/..
1947: /fred/.././junk/.././ becomes /fred/..
1948:
1.28 cvs 1949: If more than one set of `://' is found (several proxies in cascade) then
1950: only the part after the last `://' is simplified.
1.25 cvs 1951:
1.28 cvs 1952: Returns: A string which might be the old one or a new one.
1.25 cvs 1953: ----------------------------------------------------------------------*/
1.106 cvs 1954: void SimplifyUrl (char **url)
1955: {
1956: char *path;
1957: char *access;
1958: char *newptr;
1959: char *p;
1960: char *orig, *dest, *end;
1.28 cvs 1961:
1.106 cvs 1962: char used_sep;
1.77 cvs 1963: ThotBool ddot_simplify; /* used to desactivate the double dot simplifcation:
1964: something/../ simplification in relative URLs when they start with a ../ */
1.32 cvs 1965:
1.28 cvs 1966: if (!url || !*url)
1967: return;
1968:
1.106 cvs 1969: if (strchr (*url, URL_SEP))
1970: used_sep = URL_SEP;
1.32 cvs 1971: else
1.106 cvs 1972: used_sep = DIR_SEP;
1.32 cvs 1973:
1.77 cvs 1974: /* should we simplify double dot? */
1975: path = *url;
1.106 cvs 1976: if (*path == '.' && *(path + 1) == '.')
1.77 cvs 1977: ddot_simplify = FALSE;
1978: else
1979: ddot_simplify = TRUE;
1980:
1.28 cvs 1981: /* Find any scheme name */
1.106 cvs 1982: if ((path = strstr (*url, "://")) != NULL)
1.33 cvs 1983: {
1984: /* Find host name */
1.28 cvs 1985: access = *url;
1.123 vatton 1986: while (access < path && (*access = tolower (*access)))
1.82 cvs 1987: access++;
1.28 cvs 1988: path += 3;
1.106 cvs 1989: while ((newptr = strstr (path, "://")) != NULL)
1.82 cvs 1990: /* For proxies */
1.106 cvs 1991: path = newptr + 3;
1.82 cvs 1992: /* We have a host name */
1.84 cvs 1993: path = HTCanon (url, path);
1.25 cvs 1994: }
1.106 cvs 1995: else if ((path = strstr (*url, ":/")) != NULL)
1.28 cvs 1996: path += 2;
1997: else
1998: path = *url;
1.84 cvs 1999: if (*path == used_sep && *(path+1) == used_sep)
1.28 cvs 2000: /* Some URLs start //<foo> */
2001: path += 1;
1.94 cvs 2002: else if (IsFilePath (path))
2003: {
2004: /* doesn't need to do anything more */
2005: return;
2006: }
1.106 cvs 2007: else if (!strncmp (path, "news:", 5))
1.28 cvs 2008: {
1.106 cvs 2009: newptr = strchr (path+5, '@');
1.28 cvs 2010: if (!newptr)
2011: newptr = path + 5;
2012: while (*newptr)
2013: {
2014: /* Make group or host lower case */
1.123 vatton 2015: *newptr = tolower (*newptr);
1.28 cvs 2016: newptr++;
1.25 cvs 2017: }
1.28 cvs 2018: /* Doesn't need to do any more */
2019: return;
1.25 cvs 2020: }
1.130 cheyroul 2021:
1.126 cheyroul 2022:
1.28 cvs 2023: if ((p = path))
2024: {
1.106 cvs 2025: if (!((end = strchr (path, ';')) || (end = strchr (path, '?')) ||
2026: (end = strchr (path, '#'))))
2027: end = path + strlen (path);
1.28 cvs 2028:
2029: /* Parse string second time to simplify */
2030: p = path;
2031: while (p < end)
2032: {
1.110 cvs 2033: /* if we're pointing to a char, it's safe to reactivate the
2034: ../ convertion */
1.106 cvs 2035: if (!ddot_simplify && *p != '.' && *p != used_sep)
1.77 cvs 2036: ddot_simplify = TRUE;
2037:
1.33 cvs 2038: if (*p==used_sep)
1.28 cvs 2039: {
1.106 cvs 2040: if (p > *url && *(p+1) == '.' && (*(p+2) == used_sep || !*(p+2)))
1.28 cvs 2041: {
2042: orig = p + 1;
1.84 cvs 2043: dest = (*(p+2) != used_sep) ? p+2 : p+3;
1.52 cvs 2044: while ((*orig++ = *dest++)); /* Remove a used_sep and a dot*/
1.28 cvs 2045: end = orig - 1;
2046: }
1.106 cvs 2047: else if (ddot_simplify && *(p+1) == '.' && *(p+2) == '.'
1.77 cvs 2048: && (*(p+3) == used_sep || !*(p+3)))
1.28 cvs 2049: {
2050: newptr = p;
1.52 cvs 2051: while (newptr>path && *--newptr!=used_sep); /* prev used_sep */
2052: if (*newptr == used_sep)
2053: orig = newptr + 1;
1.28 cvs 2054: else
1.52 cvs 2055: orig = newptr;
2056:
2057: dest = (*(p+3) != used_sep) ? p+3 : p+4;
2058: while ((*orig++ = *dest++)); /* Remove /xxx/.. */
2059: end = orig-1;
2060: /* Start again with prev slash */
2061: p = newptr;
1.28 cvs 2062: }
1.33 cvs 2063: else if (*(p+1) == used_sep)
1.28 cvs 2064: {
1.33 cvs 2065: while (*(p+1) == used_sep)
1.28 cvs 2066: {
2067: orig = p;
2068: dest = p + 1;
2069: while ((*orig++ = *dest++)); /* Remove multiple /'s */
2070: end = orig-1;
2071: }
2072: }
2073: else
1.25 cvs 2074: p++;
1.28 cvs 2075: }
2076: else
2077: p++;
1.25 cvs 2078: }
2079: }
1.51 cvs 2080: /*
2081: ** Check for host/../.. kind of things
2082: */
1.106 cvs 2083: if (*path == used_sep && *(path+1) == '.' && *(path+2) == '.'
1.77 cvs 2084: && (!*(path+3) || *(path+3) == used_sep))
1.106 cvs 2085: *(path+1) = EOS;
1.28 cvs 2086: return;
2087: }
2088:
2089:
2090: /*----------------------------------------------------------------------
1.96 cvs 2091: NormalizeFile normalizes local names.
1.28 cvs 2092: Return TRUE if target and src differ.
2093: ----------------------------------------------------------------------*/
1.106 cvs 2094: ThotBool NormalizeFile (char *src, char *target, ConvertionType convertion)
1.28 cvs 2095: {
1.110 cvs 2096: #ifndef _WINDOWS
1.106 cvs 2097: char *s;
1.93 cvs 2098: int i;
1.110 cvs 2099: #endif /* !_WINDOWS */
1.82 cvs 2100: ThotBool change;
1.90 cvs 2101: int start_index; /* the first char that we'll copy */
1.28 cvs 2102:
1.54 cvs 2103: change = FALSE;
1.90 cvs 2104: start_index = 0;
2105:
1.106 cvs 2106: if (!src || src[0] == EOS)
1.96 cvs 2107: {
1.106 cvs 2108: target[0] = EOS;
1.96 cvs 2109: return FALSE;
2110: }
1.90 cvs 2111:
2112: /* @@ do I need file: or file:/ here? */
1.106 cvs 2113: if (strncmp (src, "file:", 5) == 0)
1.28 cvs 2114: {
1.90 cvs 2115: /* remove the prefix file: */
2116: start_index += 5;
2117:
2118: /* remove the localhost prefix */
1.106 cvs 2119: if (strncmp (&src[start_index], "//localhost/", 12) == 0)
1.94 cvs 2120: start_index += 11;
2121:
2122: /* remove the first two slashes in / / /path */
2123: while (src[start_index] &&
1.106 cvs 2124: src[start_index] == '/'
2125: && src[start_index + 1] == '/')
1.94 cvs 2126: start_index++;
2127:
2128: #ifdef _WINDOWS
2129: /* remove any extra slash before the drive name */
1.106 cvs 2130: if (src[start_index] == '/'
2131: &&src[start_index+2] == ':')
1.94 cvs 2132: start_index++;
2133: #endif /* _WINDOWS */
1.90 cvs 2134:
1.106 cvs 2135: if (src[start_index] == EOS)
1.90 cvs 2136: /* if there's nothing afterwards, add a DIR_STR */
1.106 cvs 2137: strcpy (target, DIR_STR);
1.90 cvs 2138: else
1.97 cvs 2139: /* as we're inside a file: URL, we'll apply all the convertions
2140: we know */
2141: CleanCopyFileURL (target, &src[start_index], AM_CONV_ALL);
1.96 cvs 2142:
2143: change = TRUE;
2144: }
1.97 cvs 2145: else if (convertion != AM_CONV_NONE)
1.96 cvs 2146: {
2147: /* we are following a "local" relative link, we do all the
2148: convertions except for the HOME_DIR ~ one */
1.97 cvs 2149: CleanCopyFileURL (target, src, convertion);
1.28 cvs 2150: }
1.90 cvs 2151: #ifndef _WINDOWS
1.106 cvs 2152: else if (src[0] == '~')
1.53 cvs 2153: {
1.96 cvs 2154: /* it must be a URL typed in a text input field */
2155: /* do the HOME_DIR ~ substitution */
1.82 cvs 2156: s = TtaGetEnvString ("HOME");
1.106 cvs 2157: strcpy (target, s);
1.90 cvs 2158: #if 0
1.96 cvs 2159: /* JK: invalidated this part of the code as it's simpler
2160: to add the DIR_SEP whenever we have something to add
2161: to the path rather than adding it systematically */
1.106 cvs 2162: if (src[1] != DIR_SEP)
2163: strcat (target, DIR_STR);
1.90 cvs 2164: #endif
1.106 cvs 2165: i = strlen (target);
2166: strcpy (&target[i], &src[1]);
1.54 cvs 2167: change = TRUE;
1.53 cvs 2168: }
1.90 cvs 2169: #endif /* _WINDOWS */
1.28 cvs 2170: else
1.96 cvs 2171: /* leave it as it is */
1.106 cvs 2172: strcpy (target, src);
1.96 cvs 2173:
1.28 cvs 2174: /* remove /../ and /./ */
1.29 cvs 2175: SimplifyUrl (&target);
1.54 cvs 2176: if (!change)
1.106 cvs 2177: change = strcmp (src, target);
1.28 cvs 2178: return (change);
1.25 cvs 2179: }
2180:
1.28 cvs 2181:
1.25 cvs 2182: /*----------------------------------------------------------------------
1.31 cvs 2183: MakeRelativeURL: make relative name
1.25 cvs 2184:
1.28 cvs 2185: This function creates and returns a string which gives an expression of
2186: one address as related to another. Where there is no relation, an absolute
2187: address is retured.
1.25 cvs 2188:
1.28 cvs 2189: On entry,
1.25 cvs 2190: Both names must be absolute, fully qualified names of nodes
2191: (no fragment bits)
2192:
1.28 cvs 2193: On exit,
1.25 cvs 2194: The return result points to a newly allocated name which, if
2195: parsed by AmayaParseUrl relative to relatedName, will yield aName.
2196: The caller is responsible for freeing the resulting name later.
2197: ----------------------------------------------------------------------*/
1.106 cvs 2198: char *MakeRelativeURL (char *aName, char *relatedName)
2199: {
2200: char *return_value;
2201: char result[MAX_LENGTH];
2202: char *p;
2203: char *q;
2204: char *after_access;
2205: char *last_slash = NULL;
2206: int slashes, levels, len;
1.110 cvs 2207: #ifdef _WINDOWS
1.44 cvs 2208: int ndx;
1.110 cvs 2209: #endif /* _WINDOWS */
1.44 cvs 2210:
1.29 cvs 2211: if (aName == NULL || relatedName == NULL)
2212: return (NULL);
2213:
2214: slashes = 0;
2215: after_access = NULL;
2216: p = aName;
2217: q = relatedName;
1.147 vatton 2218: len = 0;
2219: for (; *p && !strncasecmp (p, q, 1); p++, q++, len++)
1.27 cvs 2220: {
2221: /* Find extent of match */
1.106 cvs 2222: if (*p == ':')
1.146 cvs 2223: {
1.168 cvs 2224: after_access = p + 1;
2225: #ifdef _WINDOWS
2226: if (len == 1)
2227: {
2228: /* it's a local Windows path like c:... */
2229: slashes+=2;
2230: }
2231: #endif /* _WINDOWS */
2232: }
2233: if (*p == DIR_SEP)
2234: {
2235: /* memorize the last slash position and count them */
2236: last_slash = p;
1.147 vatton 2237: slashes++;
1.146 cvs 2238: }
1.25 cvs 2239: }
2240:
1.31 cvs 2241: /* q, p point to the first non-matching character or zero */
1.106 cvs 2242: if (*q == EOS)
1.31 cvs 2243: {
2244: /* New name is a subset of the related name */
2245: /* exactly the right length */
1.106 cvs 2246: len = strlen (p);
2247: if ((return_value = TtaGetMemory (len + 1)) != NULL)
2248: strcpy (return_value, p);
1.31 cvs 2249: }
2250: else if ((slashes < 2 && after_access == NULL)
2251: || (slashes < 3 && after_access != NULL))
1.168 cvs 2252: {
1.31 cvs 2253: /* Two names whitout common path */
2254: /* exactly the right length */
1.106 cvs 2255: len = strlen (aName);
2256: if ((return_value = TtaGetMemory (len + 1)) != NULL)
2257: strcpy (return_value, aName);
1.31 cvs 2258: }
2259: else
2260: {
2261: /* Some path in common */
1.106 cvs 2262: if (slashes == 3 && strncmp (aName, "http:", 5) == 0)
1.31 cvs 2263: /* just the same server */
1.106 cvs 2264: strcpy (result, last_slash);
1.31 cvs 2265: else
2266: {
2267: levels= 0;
1.106 cvs 2268: for (; *q && *q != '#' && *q != ';' && *q != '?'; q++)
1.31 cvs 2269: if (*q == DIR_SEP)
2270: levels++;
2271:
1.106 cvs 2272: result[0] = EOS;
1.31 cvs 2273: for (;levels; levels--)
1.106 cvs 2274: strcat (result, "../");
2275: strcat (result, last_slash+1);
1.31 cvs 2276: }
1.52 cvs 2277:
2278: if (!*result)
1.106 cvs 2279: strcat (result, "./");
1.52 cvs 2280:
1.31 cvs 2281: /* exactly the right length */
1.106 cvs 2282: len = strlen (result);
2283: if ((return_value = TtaGetMemory (len + 1)) != NULL)
2284: strcpy (return_value, result);
1.52 cvs 2285:
1.25 cvs 2286: }
1.110 cvs 2287: #ifdef _WINDOWS
1.106 cvs 2288: len = strlen (return_value);
1.168 cvs 2289: for (ndx = 0; ndx < len; ndx ++)
1.106 cvs 2290: if (return_value[ndx] == '\\')
2291: return_value[ndx] = '/' ;
1.110 cvs 2292: #endif /* _WINDOWS */
1.29 cvs 2293: return (return_value);
1.24 cvs 2294: }
1.35 cvs 2295:
1.104 kahan 2296: /*----------------------------------------------------------------------
2297: AM_GetFileSize
2298: Returns TRUE and the filesize in the 2nd parameter.
2299: Otherwise, in case of a system error, returns FALSE, with a
2300: filesize of 0L.
2301: ---------------------------------------------------------------------*/
1.106 cvs 2302: ThotBool AM_GetFileSize (char *filename, unsigned long *file_size)
1.104 kahan 2303: {
1.106 cvs 2304: ThotFileHandle handle = ThotFile_BADHANDLE;
2305: ThotFileInfo info;
1.35 cvs 2306:
1.104 kahan 2307: *file_size = 0L;
2308: if (!TtaFileExist (filename))
2309: return FALSE;
2310:
2311: handle = TtaFileOpen (filename, ThotFile_READWRITE);
2312: if (handle == ThotFile_BADHANDLE)
2313: /* ThotFile_BADHANDLE */
2314: return FALSE;
2315: if (TtaFileStat (handle, &info) == 0)
2316: /* bad stat */
2317: info.size = 0L;
2318: TtaFileClose (handle);
2319: *file_size = (unsigned long) info.size;
2320: return TRUE;
2321: }
1.139 kahan 2322:
2323: /*----------------------------------------------------------------------
2324: AM_UseXHTMLMimeType
2325: Returns TRUE if the user has configured Amaya to use this MIME type,
2326: FALSE otherwise.
2327: ---------------------------------------------------------------------*/
2328: ThotBool AM_UseXHTMLMimeType (void)
2329: {
2330: ThotBool xhtml_mimetype;
2331:
2332: /* does the user wants to use the new MIME type? */
2333: TtaGetEnvBoolean ("ENABLE_XHTML_MIMETYPE", &xhtml_mimetype);
2334:
2335: return (xhtml_mimetype);
1.152 kahan 2336: }
2337:
1.154 kahan 2338:
2339: /********************************************
2340: The following routines were adapted from the GNU libc functions
2341: for generating a tmpnam.
2342: *********************************************/
2343:
2344: /* These are the characters used in temporary filenames. */
2345: static const char letters[] =
2346: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
2347:
2348: /* Generate a temporary file name based on TMPL. TMPL must match the
2349: rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed
2350: does not exist at the time of the call to __gen_tempname. TMPL is
2351: overwritten with the result.
2352:
2353: We use a clever algorithm to get hard-to-predict names. */
2354: void
2355: AM_gen_tempname (char *tmpl)
2356: {
2357: int len;
2358: char *XXXXXX;
2359: static uint64_t value;
2360: uint64_t random_time_bits;
2361: unsigned int count;
2362: int save_errno = errno;
2363: struct stat st;
2364:
2365: /* A lower bound on the number of temporary files to attempt to
2366: generate. The maximum total number of temporary file names that
2367: can exist for a given template is 62**6. It should never be
2368: necessary to try all these combinations. Instead if a reasonable
2369: number of names is tried (we define reasonable as 62**3) fail to
2370: give the system administrator the chance to remove the problems. */
2371: unsigned int attempts_min = 62 * 62 * 62;
2372:
2373: /* The number of times to attempt to generate a temporary file. To
2374: conform to POSIX, this must be no smaller than TMP_MAX. */
2375: unsigned int attempts = attempts_min < TMP_MAX ? TMP_MAX : attempts_min;
2376:
2377: len = strlen (tmpl);
2378: if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
2379: {
2380: /* @@ JK ? */
2381: errno = EINVAL;
2382: return;
2383: }
2384:
2385: /* This is where the Xs start. */
2386: XXXXXX = &tmpl[len - 6];
2387:
2388: /* Get some more or less random data. */
2389: #ifdef RANDOM_BITS
2390: RANDOM_BITS (random_time_bits);
2391: #else
2392: # if HAVE_GETTIMEOFDAY || _LIBC
2393: {
2394: struct timeval tv;
2395: gettimeofday (&tv, NULL);
2396: random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;
2397: }
2398: # else
2399: random_time_bits = time (NULL);
2400: # endif
2401: #endif
2402: value += random_time_bits ^ getpid ();
2403:
2404: for (count = 0; count < attempts; value += 7777, ++count)
2405: {
2406: uint64_t v = value;
2407:
2408: /* Fill in the random bits. */
2409: XXXXXX[0] = letters[v % 62];
2410: v /= 62;
2411: XXXXXX[1] = letters[v % 62];
2412: v /= 62;
2413: XXXXXX[2] = letters[v % 62];
2414: v /= 62;
2415: XXXXXX[3] = letters[v % 62];
2416: v /= 62;
2417: XXXXXX[4] = letters[v % 62];
2418: v /= 62;
2419: XXXXXX[5] = letters[v % 62];
2420:
2421: /* This case is backward from the other three. AM_gen_tempname
2422: succeeds if __xstat fails because the name does not exist.
2423: Note the continue to bypass the common logic at the bottom
2424: of the loop. */
2425: if (stat (tmpl, &st) < 0)
2426: break;
2427:
2428: continue;
2429: }
2430:
2431: if (count == attempts || errno != ENOENT)
2432: tmpl[0] = EOS;
2433: else
2434: errno = save_errno;
2435:
2436: return;
2437: }
2438:
2439: #define JOSE 1
2440:
1.152 kahan 2441: /*-----------------------------------------------------------------------
2442: GetTempName
2443: Front end to the Unix tempnam function, which is independent of the
2444: value of the TMPDIR env value
2445: Returns a dynamically allocated string with a tempname. The user
2446: must free this memory.
2447: -----------------------------------------------------------------------*/
2448: char *GetTempName (const char *dir, const char *prefix)
2449: {
1.154 kahan 2450: #ifdef JOSE
2451:
1.162 kahan 2452: static char tmpbufmem[PATH_MAX + 1];
1.154 kahan 2453: int len;
2454: int i;
2455:
1.155 cvs 2456: if (!dir || *dir == EOS || !TtaDirExists (dir))
1.154 kahan 2457: return NULL;
2458:
1.162 kahan 2459: /* make sure that the name is no bigger than PATH_MAX + the 6 tempname chars we
2460: will add */
1.154 kahan 2461:
1.156 cvs 2462: len = strlen (dir);
1.162 kahan 2463: if (len + 6 > PATH_MAX)
1.154 kahan 2464: return NULL;
2465:
2466: /* copy the dir name, and add a DIR_SEP if it's missing */
2467: if (dir[strlen (dir) - 1] == DIR_SEP)
2468: strcpy (tmpbufmem, dir);
2469: else
1.156 cvs 2470: {
1.154 kahan 2471: sprintf (tmpbufmem, "%s%c", dir, DIR_SEP);
1.156 cvs 2472: len++;
2473: }
1.154 kahan 2474:
1.161 kahan 2475: /* copy the prefix (no more than L_tmpnam chars, to respect POSIX). Save
1.156 cvs 2476: space for the 6 X and EOS chars that will become the random bits */
2477: if (prefix)
2478: {
2479: i = 0;
1.167 cheyroul 2480: while (prefix[i] != EOS && i < L_tmpnam - 8)
1.156 cvs 2481: tmpbufmem[len++] = prefix[i++];
2482: tmpbufmem[len] = EOS;
2483: }
2484:
2485: /* Add the 6 X chars */
2486: len = strlen (tmpbufmem);
2487: i = 0;
2488: while (i < 6)
2489: {
2490: tmpbufmem[len++] = 'X';
2491: i++;
2492: }
2493: tmpbufmem[len] = EOS;
1.154 kahan 2494:
2495: AM_gen_tempname (tmpbufmem);
2496:
2497: if (tmpbufmem[0] == EOS)
2498: return NULL;
2499: else
2500: return (TtaStrdup (tmpbufmem));
2501:
2502: #else
1.152 kahan 2503: char *tmpdir;
2504: char *tmp;
2505: char *name = NULL;
2506:
2507: /* save the value of TMPDIR */
2508: tmp = getenv (TMPDIR);
2509:
2510: if (tmp)
2511: {
2512: tmpdir = TtaStrdup (tmp);
2513: }
2514: else
2515: tmpdir = NULL;
2516:
2517: /* remove TMPDIR from the environment */
2518: if (tmpdir)
2519: {
2520: tmp = TtaGetMemory (strlen (tmpdir) + 2);
2521: sprintf (tmp, "%s=", TMPDIR);
2522: #ifdef _WINDOWS
2523: _putenv (tmp);
2524: #else
2525: putenv (tmp);
2526: #endif /* _WINDOWS */
2527: }
2528:
2529: /* create the tempname */
2530: #ifdef _WINDOWS
2531: /* Under Windows, _tempnam returns the same name until the file is created */
2532: {
2533: char *altprefix;
2534: name = tmpnam (NULL); /* get a possibly unique string */
2535: altprefix = TtaGetMemory(strlen (prefix) + strlen(name) + 1);
2536: sprintf (altprefix, "%s%s", prefix, name + strlen(_P_tmpdir));
2537: name = _tempnam (dir, altprefix); /* get a name that isn't yet in use */
2538: TtaFreeMemory (altprefix);
2539: }
2540: #else
2541: name = tempnam (dir, prefix);
2542: #endif /* _WINDOWS */
2543:
2544: /* restore the value of TMPDIR */
2545: if (tmpdir)
2546: {
2547: #ifdef _WINDOWS
2548: _putenv (tmpdir);
2549: #else
2550: putenv (tmpdir);
2551: #endif /* _WINDOWS */
2552: /* Shouldn't be free (see man for putenv ()) */
2553: /* TtaFreeMemory (tmpdir); */
2554: }
2555: return (name);
1.154 kahan 2556: #endif
1.139 kahan 2557: }
Webmaster