Save the history in the specified file. On success 0 is returned * otherwise -1 is returned. */
| 585 | /* Save the history in the specified file. On success 0 is returned |
| 586 | * otherwise -1 is returned. */ |
| 587 | int linenoiseHistorySave(const char* filename) { |
| 588 | #ifdef _WIN32 |
| 589 | long old_umask = umask(_S_IREAD | _S_IWRITE); |
| 590 | FILE* fp = fopen(filename, "wb"); |
| 591 | #else |
| 592 | mode_t old_umask = umask(S_IXUSR | S_IRWXG | S_IRWXO); |
| 593 | FILE* fp = fopen(filename, "w"); |
| 594 | #endif |
| 595 | int j; |
| 596 | |
| 597 | umask(old_umask); |
| 598 | if (fp == NULL) |
| 599 | return -1; |
| 600 | #ifdef _WIN32 |
| 601 | chmod(filename, _S_IREAD | _S_IWRITE); |
| 602 | #else |
| 603 | chmod(filename, S_IRUSR | S_IWUSR); |
| 604 | #endif |
| 605 | for (j = 0; j < history_len; j++) |
| 606 | fprintf(fp, "%s\n", history[j]); |
| 607 | fclose(fp); |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | /* Load the history from the specified file. If the file does not exist |
| 612 | * zero is returned and no operation is performed. |