radare2/libr/core/syscmd.c

142 lines
3.3 KiB
C
Raw Normal View History

/* radare - LGPL - Copyright 2013-2015 - pancake */
2014-05-22 09:25:58 +00:00
#include <r_core.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;
char *nn, *u_rwx = "";
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;
case S_IFSOCK: fch = 's'; break;
}
}
#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 %-8d %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 = ".";
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;
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;
}
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);
2014-11-10 03:14:57 +00:00
if (path[strlen(path)-1] == '/')
dir = strdup (path);
else
dir = r_str_concat (strdup (path), "/");
2014-05-22 09:25:58 +00:00
int nth = 0;
2014-11-10 03:14:57 +00:00
if (printfmt == FMT_JSON) r_cons_printf("[");
r_list_foreach (files, iter, name) {
char *n = r_str_concat (strdup (dir), name);
if (!n) break;
2014-11-10 03:14:57 +00:00
if (*n) showfile (nth, n, name, printfmt);
free (n);
2014-05-22 09:25:58 +00:00
nth++;
}
2014-11-10 03:14:57 +00:00
if (printfmt == FMT_JSON) r_cons_printf("]");
free (dir);
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");
}