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"
28 #include <glib/gprintf.h>
30 #include "cmd_cd.h" /* self */
35 #define STATICS_NUM (5)
38 const gchar *cmdline_cwd;
41 GQuark cmdline_cmd_cd_error_quark(void)
46 r=g_quark_from_static_string("cmdline-cmd-cd");
52 const struct poptOption cmd_cd_table[]={
58 /* Returns: statically allocated absolute pathname string */
59 G_CONST_RETURN gchar *cmdline_path_from_cwd(const gchar *relative)
61 static gchar *statics[STATICS_NUM];
62 static int staticsi=0;
63 gchar *r,*r_end,*s,*d;
65 /* 'relative' may be NULL for the '.' meaning */
67 if (!cmdline_cwd || (relative && *relative==G_DIR_SEPARATOR)) { /* bootstrap or absolute */
68 g_assert(g_path_is_absolute(relative));
72 r=g_strdup(cmdline_cwd);
74 r=g_build_filename(cmdline_cwd,relative,NULL);
75 g_assert(g_path_is_absolute(r));
79 if (*s==G_DIR_SEPARATOR && d>r && d[-1]==G_DIR_SEPARATOR)
84 if (d>(r+1) && d[-1]==G_DIR_SEPARATOR)
89 /* 'r' is NOT NULL-terminated here! */
91 for (d=s=r+1;s<r_end;) {
92 if (!strncmp(s,"./",2)) {
96 if (!strncmp(s,"../",3)) {
98 g_assert(d[-1]==G_DIR_SEPARATOR);
102 } while (d[-1]!=G_DIR_SEPARATOR);
108 g_assert(d[-1]==G_DIR_SEPARATOR); /* trailing '/' */
109 if (d>r+1) /* leave at least "/" */
113 g_assert(g_path_is_absolute(r));
115 g_free(statics[staticsi]);
116 statics[staticsi++]=r;
117 staticsi%=G_N_ELEMENTS(statics);
123 void cmd_cd(const char **cmd_argv,GError **errp)
125 CaptiveDirectoryObject *captive_directory_object;
127 g_return_if_fail(!errp || !*errp);
130 const gchar *targetdir=cmdline_path_from_cwd(cmd_argv[0]);
132 if (!errvfsresult_to_gerr(errp,captive_directory_new_open(
133 &captive_directory_object, /* captive_directory_object_return */
134 targetdir))) { /* pathname */
136 g_set_error(errp,CMDLINE_CMD_CD_ERROR,CMDLINE_CMD_CD_ERROR_CANNOT_OPEN_DIRECTORY,
137 _("Cannot open directory: %s"),targetdir);
141 g_object_unref(captive_directory_object);
143 g_free((/*de-const*/ gchar *)cmdline_cwd);
144 cmdline_cwd=g_strdup(targetdir);
145 g_assert(g_path_is_absolute(cmdline_cwd));
148 g_printf("Guest-OS CWD: %s\n",cmdline_cwd);