* tmp means we are writing a tmp file in a secure fashion. We use
* it when spell checking or dumping the file on an error.
*
- * append means, not surprisingly, whether we are appending instead
- * of overwriting.
+ * append == 1 means we are appending instead of overwriting.
+ * append == 2 means we are prepending instead of overwriting.
*
* nonamechange means don't change the current filename, it is ignored
* if tmp == 1.
int write_file(char *name, int tmp, int append, int nonamechange)
{
long size, lineswritten = 0;
- char *buf = NULL;
+ char *buf = NULL, prechar;
filestruct *fileptr;
- int fd, mask = 0, realexists, anyexists;
+ int fd, fd2, mask = 0, realexists, anyexists;
struct stat st, lst;
char *realname = NULL;
if (realexists == -1 || tmp || (!ISSET(FOLLOW_SYMLINKS) &&
S_ISLNK(lst.st_mode))) {
to reflect whether or not to link/unlink/rename the file */
- else if (ISSET(FOLLOW_SYMLINKS) || !S_ISLNK(lst.st_mode) || tmp) {
+ else if (append != 2 && (ISSET(FOLLOW_SYMLINKS) || !S_ISLNK(lst.st_mode)
+ || tmp)) {
/* Use O_EXCL if tmp == 1. This is now copied from joe, because
wiggy says so *shrug*. */
if (append)
return -1;
}
+ /* if we're prepending, open the real file, and append it here */
+ if (append == 2) {
+ if ((fd = open(buf, O_WRONLY | O_APPEND, (S_IRUSR|S_IWUSR))) == -1) {
+ statusbar(_("Could not reopen %s: %s"), buf, strerror(errno));
+ return -1;
+ }
+ if ((fd2 = open(realname, O_RDONLY)) == -1) {
+ statusbar(_("Could open %s for prepend: %s"), realname, strerror(errno));
+ return -1;
+ }
+
+ while (read(fd2, &prechar, 1) > 0)
+ write(fd, &prechar, 1);
+
+ close(fd);
+ close(fd2);
+
+ }
+
+
if (realexists == -1 || tmp ||
(!ISSET(FOLLOW_SYMLINKS) && S_ISLNK(lst.st_mode))) {
/* Use permissions from file we are overwriting. */
mask = st.st_mode;
- if (!tmp && (!ISSET(FOLLOW_SYMLINKS) && S_ISLNK(lst.st_mode))) {
+ if (append == 2 ||
+ (!tmp && (!ISSET(FOLLOW_SYMLINKS) && S_ISLNK(lst.st_mode)))) {
if (unlink(realname) == -1) {
if (errno != ENOENT) {
statusbar(_("Could not open %s for writing: %s"),
/* Be nice to the translation folks */
if (ISSET(MARK_ISSET) && !exiting) {
- if (append)
+ if (append == 2)
+ i = statusq(1, writefile_list, "",
+ "%s%s", _("Prepend Selection to File"), formatstr);
+ else if (append)
i = statusq(1, writefile_list, "",
"%s%s", _("Append Selection to File"), formatstr);
else
} else
#endif
{
- if (append)
+ if (append == 2)
+ i = statusq(1, writefile_list, answer,
+ "%s%s", _("File Name to Prepend"), formatstr);
+ else if (append)
i = statusq(1, writefile_list, answer,
"%s%s", _("File Name to Append"), formatstr);
else
UNSET(DOS_FILE);
TOGGLE(MAC_FILE);
return(do_writeout(answer, exiting, append));
- } else if (i == NANO_APPEND_KEY)
- return(do_writeout(answer, exiting, 1 - append));
+ } else if (i == NANO_PREPEND_KEY)
+ return(do_writeout(answer, exiting, 2));
+ else if (i == NANO_APPEND_KEY && append != 1)
+ return(do_writeout(answer, exiting, 1));
+ else if (i == NANO_APPEND_KEY)
+ return(do_writeout(answer, exiting, 0));
#ifdef DEBUG
fprintf(stderr, _("filename is %s"), answer);
"", *nano_backspace_msg = "", *nano_tab_msg =
"", *nano_enter_msg = "", *nano_cancel_msg =
"", *nano_unjustify_msg = "", *nano_append_msg =
- "";
+ "", *nano_prepend_msg = "";
#ifdef ENABLE_MULTIBUFFER
char *nano_openprev_msg = "", *nano_opennext_msg = "";
nano_gotodir_msg = _("Goto Directory");
nano_cancel_msg = _("Cancel the current function");
nano_append_msg = _("Append to the current file");
+ nano_prepend_msg = _("Prepend to the current file");
nano_reverse_msg = _("Search backwards");
nano_dos_msg = _("Write file out in DOS format");
nano_mac_msg = _("Write file out in Mac format");
NANO_APPEND_KEY, _("Append"),
nano_append_msg, 0, 0, 0, NOVIEW, 0);
+ sc_init_one(&writefile_list,
+ NANO_PREPEND_KEY, _("Prepend"),
+ nano_prepend_msg, 0, 0, 0, NOVIEW, 0);
+
sc_init_one(&writefile_list, NANO_CANCEL_KEY,
_("Cancel"), nano_cancel_msg, 0, 0, 0, VIEW, 0);