102 lines (92 with data), 3.0 kB
#define MAX_LENGTH 100
#define MAX_CHAR 10000
int
cat(string name, int start, int length)
{
string words;
array lines;
int size;
size = file_size(name);
if (size < 0) return -1;
if (!length || length > 6000) length = 6000;
if (start >= 0)
words = grab_file(name, 0, MAX_CHAR);
else {
words = grab_file(name, size - MAX_CHAR, size);
}
if (!words) return -1;
lines = explode(words, "\n");
if (start < 0) start = sizeof(lines) - start - length;
if (start < 0) start = 0;
write(implode(lines[start..(start+length)], "\n"));
write("\n");
return length;
}
int
scat((int|string) words, int start, int length)
{
array lines;
if (!words) return -1;
if (!length || length > 6000) length = 6000;
lines = explode(words, "\n");
if (start < 0) start = sizeof(lines) - start - length;
if (start < 0) start = 0;
write(implode(lines[start..(start+length)], "\n"));
write("\n");
return length;
}
static string space = " ";
/* If the show_sizes is specified then show file sizes, otherwise not. */
string
format_ls (array ret,int width ,int show_sizes, int show_dates) {
int count=0,i, col, pad, r, height, j,du =0,max;
array list;
/* Allow a space before the size, (if show_sizes)5 spaces for size,
* one more space then the file
*/
if (show_dates) {
/* One file per line */
list = allocate (sizeof (ret[0]));
for (i = sizeof(list); i--; ) {
list [i] = "" + ret[1][i] + " " ;
list [i] = space[..7-sizeof(list[i])]+ list[i];
list [i] += ctime(ret[2][i]) + " " + ret[0][i];
}
return implode (list,"\n");
}
width -= 2; /* Get rid of the \n */
if (!ret || !sizeof (ret[0])) return "No matching files.\n";
for (i = sizeof(ret[0]);i--; ) {
if (sizeof(ret[0][i]) > max) max = sizeof(ret[0][i]);
}
if (show_sizes) col = width / (max + 7);
else col = width / (max +2);
pad = width / col;
height = (sizeof(ret[0])+col-1) / col;
list = allocate (col*height);
/* write("Col = "+col+" Pad = "+pad+" Height = "+height+" Max = " + max+"\n"); */
for (i = 0; i < height; i++) {
for (j = 0 ; j < col;j++ ) {
r= (j*height) + i;
/* write ("count : " + count + " r : " + r + "\n"); */
if (r >= sizeof (ret [1])) {
list [count] = (j+1 == col) ? "\n": "";
count ++;
continue;
}
if (show_sizes) {
list[count] = ret[1][r] + " ";
du += ret[1][r];
list[count] = space[.. 7-sizeof (list[count])] + list[count];
}
else
list[count] = " ";
list[count] += ret [0][r];
if (j+1 < col) list[count] += space[..pad- sizeof(list[count])];
else list[count] += "\n";
count ++;
}
}
if (show_sizes) return "Total space: " + du + " k\n" + implode (list,"") ;
return implode (list,"");
}