2 * client cmdline interface command "cd" for libcaptive
3 * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; exactly version 2 of June 1991 is required
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <glib/gmessages.h>
23 #include <glib/gerror.h>
27 #include <captive/client-directory.h>
29 #include <captive/client.h>
31 #include "cmd_cd.h" /* self */
36 #define STATICS_NUM (5)
39 const gchar *cmdline_cwd;
42 GQuark cmdline_cmd_cd_error_quark(void)
47 r=g_quark_from_static_string("cmdline-cmd-cd");
53 const struct poptOption cmd_cd_table[]={
59 /* Returns: statically allocated absolute pathname string */
60 G_CONST_RETURN gchar *cmdline_path_from_cwd(const gchar *relative)
62 static gchar *statics[STATICS_NUM];
63 static int staticsi=0;
66 /* 'relative' may be NULL for the '.' meaning */
68 if (!cmdline_cwd || (relative && *relative==G_DIR_SEPARATOR)) { /* bootstrap or absolute */
69 g_assert(g_path_is_absolute(relative));
73 s=g_strdup(cmdline_cwd);
75 s=g_build_filename(cmdline_cwd,relative,NULL);
76 g_assert(g_path_is_absolute(s));
78 r=captive_path_normalize(s);
81 g_free(statics[staticsi]);
82 statics[staticsi++]=r;
83 staticsi%=G_N_ELEMENTS(statics);
89 void cmd_cd_internal(const gchar *targetdir,GError **errp)
91 CaptiveDirectoryObject *captive_directory_object;
93 g_return_if_fail(targetdir!=NULL);
94 g_return_if_fail(!errp || !*errp);
96 if (!errvfsresult_to_gerr(errp,captive_directory_new_open(
97 &captive_directory_object, /* captive_directory_object_return */
98 cmdline_captive_vfs_object, /* captive_vfs_object */
99 targetdir))) { /* pathname */
101 g_set_error(errp,CMDLINE_CMD_CD_ERROR,CMDLINE_CMD_CD_ERROR_CANNOT_OPEN_DIRECTORY,
102 _("Cannot open directory: %s"),targetdir);
106 g_object_unref(captive_directory_object);
108 g_free((/*de-const*/ gchar *)cmdline_cwd);
109 cmdline_cwd=g_strdup(targetdir);
110 g_assert(g_path_is_absolute(cmdline_cwd));
114 void cmd_cd(const char **cmd_argv,GError **errp)
116 g_return_if_fail(!errp || !*errp);
119 cmd_cd_internal(cmdline_path_from_cwd(cmd_argv[0]),errp);
121 printf("Guest-OS CWD: %s\n",cmdline_cwd);