1 /* guestfish - the filesystem interactive shell
2 * Copyright (C) 2009-2011 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
32 /* guestfish edit command, suggested by Ján Ondrej, implemented by RWMJ */
35 run_edit (const char *cmd, size_t argc, char *argv[])
37 TMP_TEMPLATE_ON_STACK (filename);
41 struct stat oldstat, newstat;
45 fprintf (stderr, _("use '%s filename' to edit a file\n"), cmd);
49 /* Choose an editor. */
50 if (STRCASEEQ (cmd, "vi"))
52 else if (STRCASEEQ (cmd, "emacs"))
55 editor = getenv ("EDITOR");
57 editor = "vi"; /* could be cruel here and choose ed(1) */
60 /* Handle 'win:...' prefix. */
61 remotefilename = win_prefix (argv[0]);
62 if (remotefilename == NULL)
65 /* Download the file and write it to a temporary. */
66 fd = mkstemp (filename);
69 free (remotefilename);
73 snprintf (buf, sizeof buf, "/dev/fd/%d", fd);
75 if (guestfs_download (g, remotefilename, buf) == -1) {
78 free (remotefilename);
82 if (close (fd) == -1) {
85 free (remotefilename);
89 /* Get the old stat. */
90 if (stat (filename, &oldstat) == -1) {
93 free (remotefilename);
99 snprintf (buf, sizeof buf, "%s %s", editor, filename);
105 free (remotefilename);
109 /* Get the new stat. */
110 if (stat (filename, &newstat) == -1) {
113 free (remotefilename);
118 if (oldstat.st_ctime == newstat.st_ctime &&
119 oldstat.st_size == newstat.st_size) {
121 free (remotefilename);
125 /* Write new content. */
126 if (guestfs_upload (g, filename, remotefilename) == -1) {
128 free (remotefilename);
133 free (remotefilename);