* xmlShellWrite: * @ctxt: the shell context * @filename: the file name * @node: a node in the tree * @node2: unused * * Implements the XML shell function "write" * Write the current node to the filename, it saves the serialization * of the subtree under the @node specified * * Returns 0 or -1 in case of error */
| 2496 | * Returns 0 or -1 in case of error |
| 2497 | */ |
| 2498 | int |
| 2499 | xmlShellWrite(xmlShellCtxtPtr ctxt, char *filename, xmlNodePtr node, |
| 2500 | xmlNodePtr node2 ATTRIBUTE_UNUSED) |
| 2501 | { |
| 2502 | if (node == NULL) |
| 2503 | return (-1); |
| 2504 | if ((filename == NULL) || (filename[0] == 0)) { |
| 2505 | return (-1); |
| 2506 | } |
| 2507 | #ifdef W_OK |
| 2508 | if (access((char *) filename, W_OK)) { |
| 2509 | fprintf(ctxt->output, |
| 2510 | "Cannot write to %s\n", filename); |
| 2511 | return (-1); |
| 2512 | } |
| 2513 | #endif |
| 2514 | switch (node->type) { |
| 2515 | case XML_DOCUMENT_NODE: |
| 2516 | if (xmlSaveFile((char *) filename, ctxt->doc) < -1) { |
| 2517 | fprintf(ctxt->output, |
| 2518 | "Failed to write to %s\n", filename); |
| 2519 | return (-1); |
| 2520 | } |
| 2521 | break; |
| 2522 | case XML_HTML_DOCUMENT_NODE: |
| 2523 | #ifdef LIBXML_HTML_ENABLED |
| 2524 | if (htmlSaveFile((char *) filename, ctxt->doc) < 0) { |
| 2525 | fprintf(ctxt->output, |
| 2526 | "Failed to write to %s\n", filename); |
| 2527 | return (-1); |
| 2528 | } |
| 2529 | #else |
| 2530 | if (xmlSaveFile((char *) filename, ctxt->doc) < -1) { |
| 2531 | fprintf(ctxt->output, |
| 2532 | "Failed to write to %s\n", filename); |
| 2533 | return (-1); |
| 2534 | } |
| 2535 | #endif /* LIBXML_HTML_ENABLED */ |
| 2536 | break; |
| 2537 | default:{ |
| 2538 | FILE *f; |
| 2539 | |
| 2540 | f = fopen((char *) filename, "w"); |
| 2541 | if (f == NULL) { |
| 2542 | fprintf(ctxt->output, |
| 2543 | "Failed to write to %s\n", filename); |
| 2544 | return (-1); |
| 2545 | } |
| 2546 | xmlElemDump(f, ctxt->doc, node); |
| 2547 | fclose(f); |
| 2548 | } |
| 2549 | } |
| 2550 | return (0); |
| 2551 | } |
| 2552 | |
| 2553 | /** |
| 2554 | * xmlShellSave: |
no test coverage detected