2 ** Copyright 2001, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
5 #include <sys/syscalls.h>
11 #include <newos/tty_priv.h>
14 #include "file_utils.h"
15 #include "shell_defs.h"
17 struct command cmds
[] = {
20 {"mkdir", &cmd_mkdir
},
21 {"touch", &cmd_touch
},
29 int cmd_exec(int argc
, char *argv
[])
31 return cmd_create_proc(argc
- 1,argv
+1);
34 int cmd_create_proc(int argc
,char *argv
[])
41 char filename
[SCAN_SIZE
+1];
44 printf("not enough args to exec\n");
50 if( !find_file_in_path(argv
[0],filename
,SCAN_SIZE
)){
51 printf("can't find '%s' \n",argv
[0]);
56 // a hack to support the unix '&'
58 arg_len
= strlen(tmp
);
72 pid
= _kern_proc_create_proc(filename
,filename
, argv
, argc
, 5, PROC_FLAG_SUSPENDED
|PROC_FLAG_NEW_PGROUP
);
77 ioctl(0, _TTY_IOCTL_SET_PGRP
, &pid
, sizeof(pgrp_id
));
78 _kern_send_proc_signal(pid
, SIGCONT
);
79 _kern_proc_wait_on_proc(pid
, &retcode
);
81 ioctl(0, _TTY_IOCTL_SET_PGRP
, &pid
, sizeof(pgrp_id
));
83 _kern_send_proc_signal(pid
, SIGCONT
);
86 printf("Error: cannot execute '%s'\n", filename
);
87 return 0; // should be -1, but the shell would exit
93 int cmd_mkdir(int argc
, char *argv
[])
98 printf("not enough arguments to mkdir\n");
102 rc
= _kern_mkdir(argv
[1]);
104 printf("_kern_mkdir() returned error: %s\n", strerror(rc
));
106 printf("%s successfully created.\n", argv
[1]);
112 int cmd_touch(int argc
, char *argv
[])
117 printf("not enough arguments to touch\n");
121 rc
= _kern_create(argv
[1]);
123 printf("_kern_create() returned error: %s\n", strerror(rc
));
125 printf("%s successfully created.\n", argv
[1]);
131 int cmd_cat(int argc
, char *argv
[])
138 printf("not enough arguments to cat\n");
142 fd
= open(argv
[1], 0);
144 printf("cat: open() returned error: %s!\n", strerror(fd
));
149 rc
= read(fd
, buf
, sizeof(buf
));
161 int cmd_cd(int argc
, char *argv
[])
166 printf("not enough arguments to cd\n");
172 printf("cd: chdir() returned error: %s!\n", strerror(rc
));
178 int cmd_pwd(int argc
, char *argv
[])
184 printf("cd: getcwd() returned error: %s!\n", "xx"); //strerror(rc));
186 printf("pwd: cwd=\'%s\'\n", cwd
);
194 int cmd_stat(int argc
, char *argv
[])
197 struct file_stat stat
;
200 printf("not enough arguments to stat\n");
204 rc
= _kern_rstat(argv
[1], &stat
);
206 printf("stat of file '%s': \n", argv
[1]);
207 printf("vnid 0x%x\n", (unsigned int)stat
.vnid
);
208 printf("type %d\n", stat
.type
);
209 printf("size %d\n", (int)stat
.size
);
211 printf("stat failed for file '%s'\n", argv
[1]);
216 int cmd_help(int argc
, char *argv
[])
218 printf("command list:\n\n");
219 printf("exit : quits this copy of the shell\n");
220 printf("exec <file> : load this file as a binary and run it\n");
221 printf("mkdir <path> : makes a directory at <path>\n");
222 printf("touch <path> : creates a file at <path>\n");
223 printf("cd <path> : sets the current working directory at <path>\n");
224 printf("ls <path> : directory list of <path>. If no path given it lists the current dir\n");
225 printf("stat <file> : gives detailed file statistics of <file>\n");
226 printf("help : this command\n");
227 printf("cat <file> : dumps the file to stdout\n");
228 printf("mount <path> <device> <fsname> : tries to mount <device> at <path>\n");
229 printf("unmount <path> : tries to unmount at <path>\n");