radare2/libr/core/syscmd.c

196 lines
4.5 KiB
C
Raw Normal View History

/* radare - LGPL - Copyright 2013-2015 - pancake */
2014-05-22 09:25:58 +00:00
#include <r_core.h>
#include <errno.h>
2014-11-10 03:14:57 +00:00
#define FMT_RAW 1
#define FMT_JSON 2
static void showfile(const int nth, const char *fpath, const char *name, int printfmt) {
#if __UNIX__
2014-05-22 09:25:58 +00:00
struct stat sb;
#endif
2014-11-10 03:14:57 +00:00
const char *n = fpath;
2015-01-14 01:53:34 +00:00
char *nn, *u_rwx = NULL;
2014-05-22 09:25:58 +00:00
int sz = r_file_size (n);
int perm, isdir, uid = 0, gid = 0;
int fch = '-';
2014-11-10 03:14:57 +00:00
if (!strncmp (fpath, "./", 2))
fpath = fpath+2;
2014-05-22 09:25:58 +00:00
if (r_file_is_directory (n)) {
2014-11-10 03:14:57 +00:00
nn = r_str_concat (strdup (fpath), "/");
2014-05-22 09:25:58 +00:00
isdir = 1;
} else {
2014-11-10 03:14:57 +00:00
nn = strdup (fpath);
2014-05-22 09:25:58 +00:00
isdir = 0;
}
if (!*nn) {
free (nn);
return;
}
perm = isdir? 0755: 0644;
2014-11-10 03:14:57 +00:00
if (!printfmt) {
2014-05-22 09:25:58 +00:00
r_cons_printf ("%18s%s", nn, (nth%4)?" ":"\n");
2014-05-26 22:09:17 +00:00
free (nn);
2014-05-22 09:25:58 +00:00
return;
}
// TODO: escape non-printable chars in filenames
// TODO: Implement more real info in ls -l
// TODO: handle suid
#if __UNIX__
if (lstat (n, &sb) != -1) {
ut32 ifmt = sb.st_mode & S_IFMT;
uid = sb.st_uid;
gid = sb.st_gid;
perm = sb.st_mode & 0777;
if (!(u_rwx = strdup(r_str_rwx_i(perm>>6)))) {
free(nn);
return;
}
if (sb.st_mode & S_ISUID)
u_rwx[2] = (sb.st_mode & S_IXUSR) ? 's' : 'S';
2014-05-22 09:25:58 +00:00
if (isdir) fch = 'd';
else
switch (ifmt) {
case S_IFCHR: fch = 'c'; break;
case S_IFBLK: fch = 'b'; break;
case S_IFLNK: fch = 'l'; break;
case S_IFIFO: fch = 'p'; break;
2015-02-05 06:10:30 +00:00
#ifdef S_IFSOCK
2014-05-22 09:25:58 +00:00
case S_IFSOCK: fch = 's'; break;
2015-02-05 06:10:30 +00:00
#endif
2014-05-22 09:25:58 +00:00
}
}
#else
fch = isdir? 'd': '-';
#endif
2014-11-10 03:14:57 +00:00
if (printfmt == FMT_RAW) {
r_cons_printf ("%c%s%s%s 1 %4d:%-4d %-10d %s\n",
2014-05-22 09:25:58 +00:00
isdir?'d':fch,
u_rwx,
2014-05-22 09:25:58 +00:00
r_str_rwx_i ((perm>>3)&7),
r_str_rwx_i (perm&7),
uid, gid, sz, nn);
2014-11-10 03:14:57 +00:00
} else if (printfmt == FMT_JSON) {
if (nth > 0) r_cons_printf(",");
r_cons_printf("{\"name\":\"%s\",\"size\":%d,\"uid\":%d,"
"\"gid\":%d,\"perm\":%d,\"isdir\":%s}",
name, sz, uid, gid, perm, isdir?"true":"false");
}
2014-05-26 22:09:17 +00:00
free (nn);
free (u_rwx);
2014-05-22 09:25:58 +00:00
}
// TODO: Move into r_util .. r_print maybe? r_cons dep is anoying
R_API void r_core_syscmd_ls(const char *input) {
const char *path = ".";
2015-04-17 14:43:10 +00:00
char *d = NULL;
char *p = NULL;
char *homepath = NULL;
2015-04-16 11:11:21 +00:00
char *pattern = NULL;
2014-11-10 03:14:57 +00:00
int printfmt = 0;
RListIter *iter;
RList *files;
2014-05-22 09:25:58 +00:00
char *name;
char *dir;
2015-04-16 11:11:21 +00:00
int off;
2014-06-14 00:59:12 +00:00
if (r_sandbox_enable (0)) {
eprintf ("Sandbox forbids listing directories\n");
return;
}
if (input[1]==' ') {
2014-11-10 03:14:57 +00:00
if ((!strncmp (input+2, "-l", 2)) || (!strncmp (input+2, "-j", 2))) {
if (input[3]) {
2014-11-10 03:14:57 +00:00
printfmt = (input[3] == 'j') ? FMT_JSON : FMT_RAW;
path = input+4;
while (*path==' ') path++;
if (!*path) path = ".";
}
} else path = input+2;
}
2015-04-17 14:43:10 +00:00
if (*path == '~') {
homepath = r_str_home (path+2);
path = (const char *)homepath;
}
else if (*path == '$'){
if (!strncmp (path+1, "home", 4) || !strncmp (path+1, "HOME", 4)){
if (!*(path+6))
homepath = r_str_home (NULL);
else homepath = r_str_home (path+6);
path = (const char *)homepath;
}
}
2015-04-16 11:11:21 +00:00
if (!r_file_is_directory (path)){
p = strrchr(path, '/');
if (p){
off = p - path;
2015-04-17 14:43:10 +00:00
d = (char *) calloc (1, off+1);
2015-04-16 11:11:21 +00:00
if (!d) return;
memcpy (d, path, off);
path = (const char *)d;
pattern = strdup (p+1);
}else {
pattern = strdup (path);
path = ".";
}
} else pattern = strdup ("*");
2014-05-22 09:25:58 +00:00
if (r_file_is_regular (path)) {
2014-11-10 03:14:57 +00:00
showfile (0, path, path, printfmt);
2014-05-22 09:25:58 +00:00
return;
}
files = r_sys_dir (path);
if (path[strlen (path)-1] == '/')
2014-11-10 03:14:57 +00:00
dir = strdup (path);
else
dir = r_str_concat (strdup (path), "/");
2014-05-22 09:25:58 +00:00
int nth = 0;
if (printfmt == FMT_JSON) r_cons_printf ("[");
r_list_foreach (files, iter, name) {
char *n = r_str_concat (strdup (dir), name);
if (!n) break;
2015-04-16 11:11:21 +00:00
if (r_str_glob(name, pattern)){
if (*n) showfile (nth, n, name, printfmt);
nth++;
}
free (n);
}
if (printfmt == FMT_JSON) r_cons_printf ("]");
free (dir);
2015-04-16 11:11:21 +00:00
if (d) free (d);
2015-04-17 14:43:10 +00:00
if (homepath) free (homepath);
2015-04-16 11:11:21 +00:00
free (pattern);
r_list_free (files);
}
2014-08-06 22:37:44 +00:00
R_API void r_core_syscmd_cat(const char *file) {
int sz;
2014-08-06 22:37:44 +00:00
const char *p = strchr (file, ' ');
if (p) {
char *data, *filename = strdup (p+1);
filename = r_str_chop (filename);
data = r_file_slurp (filename, &sz);
if (data) {
r_cons_memcat (data, sz);
free (data);
} else eprintf ("No such file or directory\n");
2014-08-17 18:30:28 +00:00
free (filename);
2014-08-06 22:37:44 +00:00
} else eprintf ("Usage: cat [file]\n");
}
R_API void r_core_syscmd_mkdir(const char *dir) {
const char *p = strchr (dir, ' ');
if (p) {
char *dirname = strdup (p+1);
dirname = r_str_chop (dirname);
if (!r_sys_mkdir (dirname)) {
if (r_sys_mkdir_failed ())
eprintf ("Cannot create \"%s\"\n", dirname);
}
free (dirname);
} else eprintf ("Usage: mkdir [directory]\n");
}