2b0c7624ff33433c482e8c126e3b10ea3c401758
[captive.git] / src / client / cmdline / cmd_cd.c
1 /* $Id$
2  * client cmdline interface command "cd" for libcaptive
3  * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
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
8  * 
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.
13  * 
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
17  */
18
19
20 #include "config.h"
21
22 #include <glib/gmessages.h>
23 #include <glib/gerror.h>
24 #include <popt.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <captive/client-directory.h>
28 #include <stdio.h>
29 #include <captive/client.h>
30
31 #include "cmd_cd.h"     /* self */
32 #include "main.h"
33
34
35 /* Config: */
36 #define STATICS_NUM (5)
37
38
39 const gchar *cmdline_cwd;
40
41
42 GQuark cmdline_cmd_cd_error_quark(void)
43 {
44 GQuark r=0;
45
46         if (!r)
47                 r=g_quark_from_static_string("cmdline-cmd-cd");
48
49         return r;
50 }
51
52
53 const struct poptOption cmd_cd_table[]={
54                 CMDLINE_POPT_AUTOHELP
55                 POPT_TABLEEND
56                 };
57
58
59 /* Returns: statically allocated absolute pathname string */
60 G_CONST_RETURN gchar *cmdline_path_from_cwd(const gchar *relative)
61 {
62 static gchar *statics[STATICS_NUM];
63 static int staticsi=0;
64 gchar *r,*s;
65
66         /* 'relative' may be NULL for the '.' meaning */
67
68         if (!cmdline_cwd || (relative && *relative==G_DIR_SEPARATOR)) { /* bootstrap or absolute */
69                 g_assert(g_path_is_absolute(relative));
70                 s=g_strdup(relative);
71                 }
72         else if (!relative)
73                 s=g_strdup(cmdline_cwd);
74         else
75                 s=g_build_filename(cmdline_cwd,relative,NULL);
76         g_assert(g_path_is_absolute(s));
77
78         r=captive_path_normalize(s);
79         g_free(s);
80
81         g_free(statics[staticsi]);
82         statics[staticsi++]=r;
83         staticsi%=G_N_ELEMENTS(statics);
84
85         return r;
86 }
87
88
89 void cmd_cd_internal(const gchar *targetdir,GError **errp)
90 {
91 CaptiveDirectoryObject *captive_directory_object;
92
93         g_return_if_fail(targetdir!=NULL);
94         g_return_if_fail(!errp || !*errp);
95
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 */
100                 err_cleanup(errp);
101                 g_set_error(errp,CMDLINE_CMD_CD_ERROR,CMDLINE_CMD_CD_ERROR_CANNOT_OPEN_DIRECTORY,
102                                 _("Cannot open directory: %s"),targetdir);
103                 return;
104                 }
105
106         g_object_unref(captive_directory_object);
107
108         g_free((/*de-const*/ gchar *)cmdline_cwd);
109         cmdline_cwd=g_strdup(targetdir);
110         g_assert(g_path_is_absolute(cmdline_cwd));
111 }
112
113
114 void cmd_cd(const char **cmd_argv,GError **errp)
115 {
116         g_return_if_fail(!errp || !*errp);
117
118         if (cmd_argv[0])
119                 cmd_cd_internal(cmdline_path_from_cwd(cmd_argv[0]),errp);
120
121         printf("Guest-OS CWD: %s\n",cmdline_cwd);
122 }