foundation2_fluffos_v1/
foundation2_fluffos_v1/bin/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/ChangeLog.old/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/Win32/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/compat/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/compat/simuls/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/include/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/clone/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/command/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/data/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/etc/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/include/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/inherit/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/inherit/master/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/log/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/single/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/single/tests/compiler/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/single/tests/efuns/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/single/tests/operators/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/u/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/tmp/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/windows/
foundation2_fluffos_v1/lib/cfg/
foundation2_fluffos_v1/lib/cmds/adm/
foundation2_fluffos_v1/lib/daemon/save/
foundation2_fluffos_v1/lib/daemon/services/
foundation2_fluffos_v1/lib/daemon/soul/
foundation2_fluffos_v1/lib/doc/build/
foundation2_fluffos_v1/lib/doc/classes/
foundation2_fluffos_v1/lib/doc/driver/
foundation2_fluffos_v1/lib/doc/driver/applies/
foundation2_fluffos_v1/lib/doc/driver/applies/interactive/
foundation2_fluffos_v1/lib/doc/driver/concepts/
foundation2_fluffos_v1/lib/doc/driver/driver/
foundation2_fluffos_v1/lib/doc/driver/efuns/arrays/
foundation2_fluffos_v1/lib/doc/driver/efuns/buffers/
foundation2_fluffos_v1/lib/doc/driver/efuns/calls/
foundation2_fluffos_v1/lib/doc/driver/efuns/compile/
foundation2_fluffos_v1/lib/doc/driver/efuns/filesystem/
foundation2_fluffos_v1/lib/doc/driver/efuns/floats/
foundation2_fluffos_v1/lib/doc/driver/efuns/functions/
foundation2_fluffos_v1/lib/doc/driver/efuns/general/
foundation2_fluffos_v1/lib/doc/driver/efuns/mappings/
foundation2_fluffos_v1/lib/doc/driver/efuns/numbers/
foundation2_fluffos_v1/lib/doc/driver/efuns/parsing/
foundation2_fluffos_v1/lib/doc/driver/lpc/constructs/
foundation2_fluffos_v1/lib/doc/driver/lpc/types/
foundation2_fluffos_v1/lib/doc/driver/platforms/
foundation2_fluffos_v1/lib/doc/efun/
foundation2_fluffos_v1/lib/doc/etc/
foundation2_fluffos_v1/lib/doc/faq/
foundation2_fluffos_v1/lib/doc/help/creator/
foundation2_fluffos_v1/lib/doc/help/player/
foundation2_fluffos_v1/lib/doc/lpc/basic/
foundation2_fluffos_v1/lib/doc/lpc/data_types/
foundation2_fluffos_v1/lib/doc/lpc/etc/
foundation2_fluffos_v1/lib/doc/lpc/intermediate/
foundation2_fluffos_v1/lib/doc/lpc/types/
foundation2_fluffos_v1/lib/doc/mudlib/
foundation2_fluffos_v1/lib/doc/mudlib/Features/
foundation2_fluffos_v1/lib/domains/Examples/armour/
foundation2_fluffos_v1/lib/domains/Examples/etc/
foundation2_fluffos_v1/lib/domains/Examples/npc/
foundation2_fluffos_v1/lib/domains/Examples/room/
foundation2_fluffos_v1/lib/domains/Examples/virtual/
foundation2_fluffos_v1/lib/domains/Examples/virtual/exaA/
foundation2_fluffos_v1/lib/domains/Examples/virtual/exaB/
foundation2_fluffos_v1/lib/domains/Examples/weapon/
foundation2_fluffos_v1/lib/domains/School/doc/
foundation2_fluffos_v1/lib/domains/School/room/
foundation2_fluffos_v1/lib/domains/School/room/Classes/
foundation2_fluffos_v1/lib/domains/School/room/Offices/
foundation2_fluffos_v1/lib/domains/Standard/
foundation2_fluffos_v1/lib/domains/Standard/pools/
foundation2_fluffos_v1/lib/domains/Standard/std/
foundation2_fluffos_v1/lib/domains/Standard/xtra/
foundation2_fluffos_v1/lib/include/
foundation2_fluffos_v1/lib/lib/obj/
foundation2_fluffos_v1/lib/news/
foundation2_fluffos_v1/lib/save/
foundation2_fluffos_v1/lib/secure/cfg/
foundation2_fluffos_v1/lib/secure/cmds/player/
foundation2_fluffos_v1/lib/secure/lib/
foundation2_fluffos_v1/old/
foundation2_fluffos_v1/win32/
/*    /cmd *    from Foundation II
 *    a faster, newer ls command
 *    created by Descartes of Borg 940724
 */

#include <lib.h>

inherit LIB_DAEMON;

static private mapping file_mapping(string *files);
static private string display_ls(mixed targ, int aflag, int lflag, int tflag,
  int nflag, int bflag, int sflag);
static private string long_list(string dir, mixed *files);
static private string short_list(string dir, mixed *files, int n, int s);

int cmd(string str) {
    string *args, *paths, *options, *files, *tmp, *dirs;
    string show;
    int moref, brief, size;
    int all_files, long_details, time_sort, no_load_info, i, x, maxi;

    if(str == "" || !str) str = (string)previous_object()->query_cwd();
    if(!str) return notify_fail("No current working directory.\n");
    i = sizeof(args = explode(str, " "));
    if(args[0][0] == '-') {
        if((x = strlen(args[0])) > 1) options = explode(args[0][1..x-1], "");
        else options = ({});
        if(i == 1) paths = ({ (string)previous_object()->query_cwd() });
        else paths = args[1..i-1];
    }
    else {
        options = ({});
        paths = args;
    }
    i = sizeof(options);
    while(i--) {
        switch(options[i]) {
            case "a": all_files = 1; break;
            case "l": long_details = 1; break;
            case "t": time_sort = 1; break;
	    case "m": moref = 1; break;
            case "n": no_load_info = 1; break;
            case "b": brief = 1; break;
            case "s": size = 1; break;
        }
    }
    for(i=0, maxi = sizeof(paths), files = ({}); i<maxi; i++)
      if(tmp = (string *)previous_object()->wild_card(paths[i])) files += tmp;
    if(!sizeof(files)) { 
        message("error", "No such file or directory.", this_player());
        return 1;
    }
    dirs = filter(files, "is_dir", this_object());
    if(sizeof(files = files - dirs))
      show = display_ls(file_mapping(files),all_files,long_details, time_sort,
        no_load_info, brief, size);
    else show = "";
    if(!(maxi = sizeof(dirs))) {
        if(moref) previous_object()->more(explode(show, "\n"), "system");
        else message("system", show, previous_object());
        return 1;
    }
    for(i=0; i<maxi; i++)
      show += display_ls(dirs[i], all_files, long_details, time_sort, 
        no_load_info, brief, size);
    if(moref) previous_object()->more(explode(show, "\n"), "system");
    else message("system", show, previous_object());
    return 1;
}

static int is_dir(string str) { return (file_size(str) == -2); }

static private mapping file_mapping(string *files) {
    mapping borg;
    string *tmp;
    string dir;
    int i, maxi, x;

    for(i=0, maxi = sizeof(files), borg = ([]); i<maxi; i++) {
        x = sizeof(tmp = explode(files[i], "/"));
        if(x == 1) dir = "/";
        else dir = "/"+implode(tmp[0..x-2], "/")+"/";
        if(borg[dir]) borg[dir] += get_dir(dir+tmp[x-1], -1);
        else borg[dir] = get_dir(dir+tmp[x-1], -1);
    }
    return borg;
}

static private string display_ls(mixed targ, int aflag, int lflag, int tflag,
  int nflag, int bflag, int sflag) {
    string *cles;
    string ret;
    int i, maxi;

    if(stringp(targ) && targ == "/") targ = ([ "/" : get_dir("/", -1) ]);
    else if(stringp(targ)) targ = ([ targ+"/" : get_dir(targ+"/", -1) ]);
    for(i=0, maxi = sizeof(cles = keys(targ)); i<maxi; i++) {
        if(!bflag) ret = cles[i]+":\n";
        if(!aflag) targ[cles[i]] = filter(targ[cles[i]], "filter_dots",
          this_object());
        if(tflag) 
          targ[cles[i]]=sort_array(targ[cles[i]],"time_sort",this_object());
        if(lflag) ret += long_list(cles[i], targ[cles[i]]);
        else ret += short_list(cles[i], targ[cles[i]], nflag, sflag);
        ret += "\n";
    }
    return ret;
}

static int filter_dots(mixed *file) { return (file[0][0] != '.'); }

static int time_sort(mixed *alpha, mixed *beta) {
    if(alpha[2] < beta[2]) return 1;
    else if(alpha[2] > beta[2]) return -1;
    else return 0;
}

static private string long_list(string dir, mixed *files) {
    string ret, acc, loaded;
    int i, maxi;

    if(!(maxi = sizeof(files))) return "";
    else ret = "";
    if((int)master()->valid_read(dir, previous_object())) acc = "r";
    else acc = "-";
    if((int)master()->valid_write(dir, previous_object())) acc += "w";
    else acc += "-";
    if(member_array(dir[0..strlen(dir)-2],
      (string *)previous_object()->GetSearchPath()) != -1) acc += "x";
    else acc += "-";
    for(i=0, maxi=sizeof(files); i<maxi; i++) {
        if(files[i][1] == -2) loaded = "";
        else loaded = (find_object(dir+files[i][0]) ? "*" : "");
        ret += sprintf("%:-3s%:-5s%:-30s%:-10d%s", 
          loaded, acc, ctime(files[i][2]),
          files[i][1], files[i][0]);
        if(files[i][1] == -2) ret += "/\n";
        else ret += "\n";
    }
    return ret;
}

static private string short_list(string dir, mixed *files, int n, int s) {
    string *newfiles;
    string ret, tmp;
    int i, j, max, x, long, ind, cols, rows;

    i = sizeof(newfiles=map_array(files,"map_files",this_object(),({dir,n,s})));
    while(i--) if((x=strlen(newfiles[i])) > long) long = x;
    tmp = (string)previous_object()->GetEnvVar("SCREEN");
    if(!tmp || !sscanf(tmp, "%d", x) || !x) x = 80;
    x = x-2;
    if(long > x/3-3) long = x/3-3;
    cols = x/(long+3);
    rows = (max = sizeof(newfiles))/cols;
    if(max % cols) rows++;
    ret = "";
    for(i=0; i<rows; i++) {
        for(j=0; j<cols; j++) {
            ind = (rows * j) + i;
            if(ind < max) ret += sprintf("%:-"+(long+3)+"s", newfiles[ind]);
            else ret += sprintf("%:-"+(long+3)+"s", "");
        }
        ret += "\n";
    }
    return ret;
}

static string map_files(mixed *file, int *flags) {
    string tmp;

    if(flags[1] && flags[2]) {
        if(file[1] == -2) return file[0]+"/";
        else return file[0];
    }
#if 0
    if(!flags[1]) {
        if(find_object((string)flags[0]+file[0]) && file[1] != -2) tmp = "*";
        else tmp = " ";
    }
#endif
    else tmp = "";
    if(!flags[2]) {
        if(file[1] == -2) tmp = tmp + "    ";
        else if(!file[1]) tmp = tmp + "0   ";
        else if(file[1] < 1024) tmp = " 1   ";
        else tmp = sprintf("%s%:-3d ", tmp, (file[1]/1024));
    }
    return tmp + file[0] + ((file[1] == -2) ? "/" : "");
}

void help() {
    message("help", "Syntax: <ls [-ablmnst] (directories|files)>\n\n"
      "If you pass a single directory as an argument, it will list all "
      "files and directories in that directory.  If you list a single "
      "file, then information about that file will be displayed.  If you "
      "use special characters like wild cards, then relevant information "
      "regarding those files and/or directories will be displayed.  The "
      "options have the following meanings:\n"
      "    -a List all files, including files beginning with a '.'\n"
      "    -b Brief listing, leaving out the directory name\n"
      "    -l Long listing of all details about a file\n"
      "    -m Page the output through more\n"
      "    -n No display of loaded object information\n"
      "    -s No display of size information\n"
      "    -t Sort directory listings by time last modified\n\n"
      "The -l option overrides the -n and -s options.  The columns "
      "in the -l listing break down in the following manner:\n"
      "    * if loaded, blank space if not loaded\n"
      "    Access permissions, in the form of rwx\n"
      "    Time last modified\n"
      "    Size of the file\n"
      "    File name\n\nSee also: cd, mkdir, mv, pwd, rm, rmdir", 
      this_player());
}