#include "stat.h" /* ls [-l] [dir]: list a directory (default: current). * -l adds the type, link count and size - the terminal is 40 columns, so the * long form is "d 1 256 name", not the BSD/GNU sprawl. */ static void pad(unsigned int n, unsigned char width) { unsigned char d = 1; unsigned int v = n; while (v >= 10) { v = v / 10; d++; } while (d++ < width) putc(' '); putu(n); } void main(void) { char *argv[4]; char name[16]; char path[40]; struct gstat st; unsigned char argc = argv_parse(argv, 4); unsigned char lng = hasflag(argv, argc, 'l'); char dir[32]; unsigned char i, j, k, havedir = 0; /* Copy the directory argument onto the stack before anything else: argv points into the command line at $A000, and gstat()'s request block is a static - which lives at $A000 too. The first stat would otherwise eat the argument, and every later entry would stat a garbage path. */ for (i = 0; i < argc; i++) if (argv[i][0] != '-') { for (j = 0; argv[i][j] && j < 31; j++) dir[j] = argv[i][j]; dir[j] = 0; havedir = 1; break; } if (!havedir) { dir[0] = '.'; dir[1] = 0; } if (opendir(dir)) { puts("ls: no such directory"); nl(); sexit(1); } for (i = 0; flist(i, name); i++) { name[15] = 0; if (!lng) { puts(name); nl(); continue; } /* stat wants a path, and flist only hands back the leaf name */ j = 0; if (havedir && !(dir[0] == '.' && dir[1] == 0)) { for (k = 0; dir[k] && j < 30; k++) path[j++] = dir[k]; if (j && path[j - 1] != '/') path[j++] = '/'; } for (k = 0; name[k] && j < 38; k++) path[j++] = name[k]; path[j] = 0; if (!gstat(path, &st)) { puts("? "); puts(name); nl(); continue; } putc(st.type == ST_DIR ? 'd' : '-'); putc(' '); pad(st.nlink, 2); putc(' '); pad(st.size, 5); putc(' '); puts(name); nl(); } }