Squash commits for public release
This commit is contained in:
5
userland/utilities/cat/.info.mk
Normal file
5
userland/utilities/cat/.info.mk
Normal file
@@ -0,0 +1,5 @@
|
||||
APPS += CAT
|
||||
|
||||
CAT_NAME = cat
|
||||
CAT_LIBS = c
|
||||
CAT_INSTALL_PATH = bin/
|
||||
8
userland/utilities/cat/BUILD.gn
Normal file
8
userland/utilities/cat/BUILD.gn
Normal file
@@ -0,0 +1,8 @@
|
||||
import("//build/userland/TEMPLATE.gni")
|
||||
|
||||
xOS_executable("cat") {
|
||||
install_path = "bin/"
|
||||
sources = [ "main.c" ]
|
||||
configs = [ "//build/userland:userland_flags" ]
|
||||
deplibs = [ "libc" ]
|
||||
}
|
||||
37
userland/utilities/cat/main.c
Normal file
37
userland/utilities/cat/main.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define BUF_SIZE 512
|
||||
char buf[BUF_SIZE];
|
||||
|
||||
void cat(int fd)
|
||||
{
|
||||
int n = 0;
|
||||
while ((n = read(fd, buf, sizeof(buf))) > 0) {
|
||||
if (fwrite(buf, n, 1, stdout) != n) {
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int fd, i;
|
||||
|
||||
if (argc <= 1) {
|
||||
cat(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (i = 1; i < argc; i++) {
|
||||
if ((fd = open(argv[i], O_RDONLY)) < 0) {
|
||||
printf("cat: cannot open %s\n", argv[i]);
|
||||
return 1;
|
||||
}
|
||||
cat(fd);
|
||||
close(fd);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
5
userland/utilities/edit/.info.mk
Normal file
5
userland/utilities/edit/.info.mk
Normal file
@@ -0,0 +1,5 @@
|
||||
APPS += EDIT
|
||||
|
||||
EDIT_NAME = edit
|
||||
EDIT_LIBS = c
|
||||
EDIT_INSTALL_PATH = bin/
|
||||
176
userland/utilities/edit/file.c
Normal file
176
userland/utilities/edit/file.c
Normal file
@@ -0,0 +1,176 @@
|
||||
#include "file.h"
|
||||
#include "viewer.h"
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
file_view_area_t view_area;
|
||||
|
||||
static int _file_get_size_of_first_line()
|
||||
{
|
||||
int res = 0;
|
||||
for (int i = view_area.start; i < view_area.data_len; i++) {
|
||||
res++;
|
||||
if (view_area.buffer[i] == '\n') {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
return view_area.data_len - view_area.start;
|
||||
}
|
||||
|
||||
static void _file_find_start(uint32_t old_offset)
|
||||
{
|
||||
old_offset -= 2;
|
||||
while (old_offset > view_area.offset) {
|
||||
if (view_area.buffer[old_offset - view_area.offset] == '\n') {
|
||||
view_area.start = old_offset - view_area.offset + 1;
|
||||
return;
|
||||
}
|
||||
old_offset--;
|
||||
}
|
||||
view_area.start = 0;
|
||||
}
|
||||
|
||||
static inline int _file_remaining_size()
|
||||
{
|
||||
return view_area.data_len - view_area.start;
|
||||
}
|
||||
|
||||
static inline int _file_is_last_line_in_buf()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
int _file_buf_flush()
|
||||
{
|
||||
/* FIXME: for now we think that we have all file in our buffer */
|
||||
lseek(view_area.fd, view_area.offset, SEEK_SET);
|
||||
view_area.data_len = write(view_area.fd, view_area.buffer, view_area.data_len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int _file_buf_reload(int offset_in_file)
|
||||
{
|
||||
/* TODO: Get real file size */
|
||||
view_area.buf_len = 4000;
|
||||
view_area.buffer = (char*)malloc(view_area.buf_len);
|
||||
|
||||
view_area.start = 0;
|
||||
view_area.offset = offset_in_file;
|
||||
view_area.was_changed = 0;
|
||||
lseek(view_area.fd, view_area.offset, SEEK_SET);
|
||||
view_area.data_len = read(view_area.fd, view_area.buffer, view_area.buf_len);
|
||||
if (view_area.data_len < 0) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int file_line_len(int offset)
|
||||
{
|
||||
int res = 0;
|
||||
for (int i = offset; i < view_area.data_len; i++) {
|
||||
res++;
|
||||
if (view_area.buffer[i] == '\n') {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
return view_area.data_len - view_area.start;
|
||||
}
|
||||
|
||||
int file_line_len_backwards(int offset)
|
||||
{
|
||||
|
||||
int res = 1;
|
||||
for (int i = offset - 1; i >= view_area.offset; i--) {
|
||||
if (view_area.buffer[i] == '\n') {
|
||||
return res;
|
||||
}
|
||||
res++;
|
||||
}
|
||||
return offset;
|
||||
}
|
||||
|
||||
int file_open(char* path)
|
||||
{
|
||||
int n;
|
||||
if ((view_area.fd = open(path, O_RDWR)) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
_file_buf_reload(0);
|
||||
if (view_area.data_len < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
viewer_display(view_area.buffer, view_area.start, _file_remaining_size());
|
||||
|
||||
return view_area.fd;
|
||||
}
|
||||
|
||||
void file_exit()
|
||||
{
|
||||
close(view_area.fd);
|
||||
}
|
||||
|
||||
/**
|
||||
* file_paste_char pastes and flush updates to disk.
|
||||
*/
|
||||
int file_paste_char(char c, int offset)
|
||||
{
|
||||
view_area.was_changed = 1;
|
||||
view_area.data_len++;
|
||||
|
||||
memmove(view_area.buffer + offset + 1, view_area.buffer + offset, view_area.data_len - offset - 1);
|
||||
view_area.buffer[offset] = c;
|
||||
viewer_cursor_next();
|
||||
viewer_display(view_area.buffer, view_area.start, _file_remaining_size());
|
||||
}
|
||||
|
||||
/**
|
||||
* file_paste_char pastes and flush updates to disk.
|
||||
*/
|
||||
int file_save()
|
||||
{
|
||||
if (view_area.was_changed) {
|
||||
_file_buf_flush();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Scroll functions update file buffer, to contain line scrolled to.
|
||||
*/
|
||||
int file_scroll_up()
|
||||
{
|
||||
if (view_area.line == 0 || view_area.start == 0) {
|
||||
return -1;
|
||||
}
|
||||
view_area.line--;
|
||||
int old_offset_in_file = view_area.offset + view_area.start;
|
||||
int offset_in_file = view_area.offset + view_area.start - SCREEN_X;
|
||||
if (offset_in_file < 0) {
|
||||
offset_in_file = 0;
|
||||
}
|
||||
|
||||
if (offset_in_file >= view_area.offset) {
|
||||
view_area.start = offset_in_file - view_area.offset;
|
||||
_file_find_start(old_offset_in_file);
|
||||
viewer_display(view_area.buffer, view_area.start, _file_remaining_size());
|
||||
} else {
|
||||
_file_buf_reload(offset_in_file);
|
||||
_file_find_start(old_offset_in_file);
|
||||
viewer_display(view_area.buffer, view_area.start, _file_remaining_size());
|
||||
}
|
||||
}
|
||||
|
||||
int file_scroll_down()
|
||||
{
|
||||
int line_size = _file_get_size_of_first_line();
|
||||
view_area.start += line_size;
|
||||
if (!_file_is_last_line_in_buf()) {
|
||||
_file_buf_reload(view_area.offset + view_area.start);
|
||||
}
|
||||
viewer_display(view_area.buffer, view_area.start, _file_remaining_size());
|
||||
view_area.line++;
|
||||
}
|
||||
31
userland/utilities/edit/file.h
Normal file
31
userland/utilities/edit/file.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef _USERLAND_EDIT_FILE_H
|
||||
#define _USERLAND_EDIT_FILE_H
|
||||
|
||||
#ifdef __xOS__
|
||||
#include <sys/types.h>
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
|
||||
struct file_view_area {
|
||||
char* buffer;
|
||||
int start; /* offset in buffer where the line starts */
|
||||
int offset; /* offset in file */
|
||||
int data_len;
|
||||
int buf_len;
|
||||
int line;
|
||||
char was_changed;
|
||||
int fd;
|
||||
};
|
||||
typedef struct file_view_area file_view_area_t;
|
||||
|
||||
int file_open(char* path);
|
||||
void file_exit();
|
||||
int file_save();
|
||||
int file_line_len(int offset);
|
||||
int file_line_len_backwards(int offset);
|
||||
int file_paste_char(char c, int offset);
|
||||
int file_scroll_up();
|
||||
int file_scroll_down();
|
||||
|
||||
#endif
|
||||
32
userland/utilities/edit/lifetime.c
Normal file
32
userland/utilities/edit/lifetime.c
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "lifetime.h"
|
||||
#ifndef __xOS__
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
|
||||
struct termios orig_term;
|
||||
|
||||
void enable_raw_mode()
|
||||
{
|
||||
struct termios raw;
|
||||
tcgetattr(STDIN, &raw);
|
||||
orig_term = raw;
|
||||
raw.c_lflag &= ~(ECHO | ICANON);
|
||||
tcsetattr(STDIN, TCSAFLUSH, &raw);
|
||||
}
|
||||
|
||||
void restore_termios()
|
||||
{
|
||||
tcsetattr(STDIN, TCSAFLUSH, &orig_term);
|
||||
}
|
||||
|
||||
void exit_app()
|
||||
{
|
||||
restore_termios();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void crash_app()
|
||||
{
|
||||
restore_termios();
|
||||
exit(1);
|
||||
}
|
||||
14
userland/utilities/edit/lifetime.h
Normal file
14
userland/utilities/edit/lifetime.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef _USERLAND_EDIT_LIFETIME_H
|
||||
#define _USERLAND_EDIT_LIFETIME_H
|
||||
|
||||
#include "viewer.h"
|
||||
#include <termios.h>
|
||||
|
||||
extern struct termios orig_term;
|
||||
|
||||
void enable_raw_mode();
|
||||
void restore_termios();
|
||||
void exit_app();
|
||||
void crash_app();
|
||||
|
||||
#endif
|
||||
80
userland/utilities/edit/main.c
Normal file
80
userland/utilities/edit/main.c
Normal file
@@ -0,0 +1,80 @@
|
||||
#include "file.h"
|
||||
#include "lifetime.h"
|
||||
#include "menu.h"
|
||||
#include "mode.h"
|
||||
#include "viewer.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define STDIN 0
|
||||
#define STDOUT 1
|
||||
|
||||
uint32_t mode;
|
||||
mode_disc_t mode_disc;
|
||||
|
||||
void viewing_accept_key(char key)
|
||||
{
|
||||
if (key == ':') {
|
||||
menu_enter_mode();
|
||||
return;
|
||||
}
|
||||
if (key == 'w') {
|
||||
viewer_cursor_up();
|
||||
return;
|
||||
}
|
||||
if (key == 's') {
|
||||
viewer_cursor_down();
|
||||
return;
|
||||
}
|
||||
if (key == 'a') {
|
||||
viewer_cursor_left();
|
||||
return;
|
||||
}
|
||||
if (key == 'd') {
|
||||
viewer_cursor_right();
|
||||
return;
|
||||
}
|
||||
|
||||
file_paste_char(key, viewer_get_cursor_offset_in_file());
|
||||
}
|
||||
|
||||
char get_key()
|
||||
{
|
||||
char c;
|
||||
if (read(STDIN, &c, 1) != 1) {
|
||||
crash_app();
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
void process_input()
|
||||
{
|
||||
for (;;) {
|
||||
char key = get_key();
|
||||
mode_disc.accept_key(key);
|
||||
}
|
||||
}
|
||||
|
||||
void display_file(char* path)
|
||||
{
|
||||
int fd = file_open(path);
|
||||
}
|
||||
|
||||
void init_mode()
|
||||
{
|
||||
mode = VIEWING;
|
||||
mode_disc.accept_key = viewing_accept_key;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc < 2) {
|
||||
return 0;
|
||||
}
|
||||
enable_raw_mode();
|
||||
init_mode();
|
||||
display_file(argv[1]);
|
||||
process_input();
|
||||
exit_app();
|
||||
}
|
||||
2
userland/utilities/edit/makefile
Normal file
2
userland/utilities/edit/makefile
Normal file
@@ -0,0 +1,2 @@
|
||||
all : *.c
|
||||
gcc $^ -o edit
|
||||
49
userland/utilities/edit/menu.c
Normal file
49
userland/utilities/edit/menu.c
Normal file
@@ -0,0 +1,49 @@
|
||||
#include "menu.h"
|
||||
#include "file.h"
|
||||
#include "lifetime.h"
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
char cmd[SCREEN_X];
|
||||
int pos = 0;
|
||||
|
||||
void menu_run_cmd(char* str, int len)
|
||||
{
|
||||
if (str[0] == 'q' && len == 1) {
|
||||
exit_app();
|
||||
}
|
||||
if (str[0] == 'w' && len == 1) {
|
||||
file_save();
|
||||
}
|
||||
if (str[0] == 'w' && str[1] == 'q' && len == 2) {
|
||||
file_save();
|
||||
exit_app();
|
||||
}
|
||||
}
|
||||
|
||||
void menu_enter_mode()
|
||||
{
|
||||
mode = MENU;
|
||||
mode_disc.enter_mode = menu_enter_mode;
|
||||
mode_disc.leave_mode = menu_leave_mode;
|
||||
mode_disc.accept_key = menu_accept_key;
|
||||
viewer_enter_menu_mode();
|
||||
write(STDOUT, ": ", 2);
|
||||
|
||||
pos = 0;
|
||||
}
|
||||
|
||||
void menu_leave_mode()
|
||||
{
|
||||
}
|
||||
|
||||
void menu_accept_key(char key)
|
||||
{
|
||||
if (key == '\n') {
|
||||
menu_run_cmd(cmd, pos);
|
||||
pos = 0;
|
||||
return;
|
||||
}
|
||||
cmd[pos++] = key;
|
||||
write(STDOUT, &key, 1);
|
||||
}
|
||||
11
userland/utilities/edit/menu.h
Normal file
11
userland/utilities/edit/menu.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef _USERLAND_EDIT_MENU_H
|
||||
#define _USERLAND_EDIT_MENU_H
|
||||
|
||||
#include "mode.h"
|
||||
#include "viewer.h"
|
||||
|
||||
void menu_enter_mode();
|
||||
void menu_leave_mode();
|
||||
void menu_accept_key(char key);
|
||||
|
||||
#endif
|
||||
27
userland/utilities/edit/mode.h
Normal file
27
userland/utilities/edit/mode.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef _USERLAND_EDIT_MODE_H
|
||||
#define _USERLAND_EDIT_MODE_H
|
||||
|
||||
#include "viewer.h"
|
||||
#ifdef __xOS__
|
||||
#include <sys/types.h>
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
|
||||
struct mode_disc {
|
||||
void (*enter_mode)();
|
||||
void (*leave_mode)();
|
||||
void (*accept_key)(char key);
|
||||
};
|
||||
typedef struct mode_disc mode_disc_t;
|
||||
|
||||
enum APP_MODE {
|
||||
VIEWING = 0,
|
||||
MENU,
|
||||
EDITING,
|
||||
};
|
||||
|
||||
extern uint32_t mode;
|
||||
extern mode_disc_t mode_disc;
|
||||
|
||||
#endif
|
||||
209
userland/utilities/edit/viewer.c
Normal file
209
userland/utilities/edit/viewer.c
Normal file
@@ -0,0 +1,209 @@
|
||||
#include "viewer.h"
|
||||
#include "file.h"
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int out_x, out_y;
|
||||
int cursor_x, cursor_y;
|
||||
char* displaying_buffer;
|
||||
int working_with_offset;
|
||||
|
||||
static int _viewer_get_offset(int cx, int cy)
|
||||
{
|
||||
return cy * SCREEN_X + cx;
|
||||
}
|
||||
|
||||
void viewer_clear_screen()
|
||||
{
|
||||
out_x = out_y = 0;
|
||||
write(STDOUT, "\x1b[2J", 4);
|
||||
write(STDOUT, "\x1b[H", 3);
|
||||
}
|
||||
|
||||
void _viewer_set_cursor(int col, int row)
|
||||
{
|
||||
char buf[8];
|
||||
buf[0] = '\x1b';
|
||||
buf[1] = '[';
|
||||
int id = 2;
|
||||
col++, row++;
|
||||
if (row < 10) {
|
||||
buf[id++] = row + '0';
|
||||
} else {
|
||||
buf[id++] = (row / 10) + '0';
|
||||
buf[id++] = (row % 10) + '0';
|
||||
}
|
||||
buf[id++] = ';';
|
||||
if (col < 10) {
|
||||
buf[id++] = col + '0';
|
||||
} else {
|
||||
buf[id++] = (col / 10) + '0';
|
||||
buf[id++] = (col % 10) + '0';
|
||||
}
|
||||
buf[id++] = 'H';
|
||||
write(STDOUT, buf, id);
|
||||
}
|
||||
|
||||
void _viewer_set_cursor_to_start()
|
||||
{
|
||||
write(STDOUT, "\x1b[H", 3);
|
||||
}
|
||||
|
||||
void _viewer_new_line()
|
||||
{
|
||||
write(STDOUT, "\n", 1);
|
||||
}
|
||||
|
||||
char _viewer_has_space_to_print(char c)
|
||||
{
|
||||
if (c == '\n') {
|
||||
if (out_y == SCREEN_Y - 1) {
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
if (out_x == SCREEN_X - 1 && out_y == SCREEN_Y - 1) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
char _viewer_display_one(char c)
|
||||
{
|
||||
if (_viewer_has_space_to_print(c)) {
|
||||
if (c == '\n') {
|
||||
out_x = 0;
|
||||
out_y++;
|
||||
_viewer_new_line();
|
||||
} else {
|
||||
write(STDOUT, &c, 1);
|
||||
out_x++;
|
||||
if (out_x == SCREEN_X) {
|
||||
out_x = 0;
|
||||
out_y++;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void _log_dec(uint32_t dec)
|
||||
{
|
||||
uint32_t pk = 1000000000;
|
||||
char was_not_zero = 0;
|
||||
while (pk > 0) {
|
||||
uint32_t pp = dec / pk;
|
||||
if (was_not_zero || pp > 0) {
|
||||
char ccp = pp + '0';
|
||||
write(1, &ccp, 1);
|
||||
was_not_zero = 1;
|
||||
}
|
||||
dec -= pp * pk;
|
||||
pk /= 10;
|
||||
}
|
||||
if (!was_not_zero) {
|
||||
write(1, "0", 1);
|
||||
}
|
||||
}
|
||||
|
||||
void viewer_display(char* buf, int start, int len)
|
||||
{
|
||||
displaying_buffer = buf;
|
||||
int last_cx = 0;
|
||||
viewer_clear_screen();
|
||||
for (int i = start; i < start + len; i++) {
|
||||
if (out_x <= cursor_x && out_y == cursor_y) {
|
||||
working_with_offset = i;
|
||||
last_cx = out_x;
|
||||
}
|
||||
if (!_viewer_display_one(buf[i])) {
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
|
||||
while (out_y < SCREEN_Y - 1) {
|
||||
out_y++;
|
||||
write(STDOUT, "\n~", 2);
|
||||
}
|
||||
out:
|
||||
cursor_x = last_cx;
|
||||
_viewer_set_cursor(cursor_x, cursor_y);
|
||||
}
|
||||
|
||||
void viewer_cursor_left()
|
||||
{
|
||||
if (cursor_x) {
|
||||
cursor_x--;
|
||||
working_with_offset--;
|
||||
_viewer_set_cursor(cursor_x, cursor_y);
|
||||
}
|
||||
}
|
||||
|
||||
void viewer_cursor_right()
|
||||
{
|
||||
if (displaying_buffer) {
|
||||
if (displaying_buffer[working_with_offset] != '\n') {
|
||||
cursor_x++;
|
||||
working_with_offset++;
|
||||
_viewer_set_cursor(cursor_x, cursor_y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void viewer_cursor_up()
|
||||
{
|
||||
if (!cursor_y) {
|
||||
file_scroll_up();
|
||||
} else {
|
||||
working_with_offset -= file_line_len_backwards(working_with_offset);
|
||||
cursor_y--;
|
||||
int new_line_len = file_line_len_backwards(working_with_offset) - 1;
|
||||
if (cursor_x >= new_line_len) {
|
||||
cursor_x = (new_line_len) % SCREEN_X;
|
||||
}
|
||||
working_with_offset -= (new_line_len);
|
||||
working_with_offset += cursor_x;
|
||||
_viewer_set_cursor(cursor_x, cursor_y);
|
||||
}
|
||||
}
|
||||
|
||||
void viewer_cursor_down()
|
||||
{
|
||||
if (cursor_y == SCREEN_Y - 1) {
|
||||
file_scroll_down();
|
||||
} else {
|
||||
working_with_offset += file_line_len(working_with_offset);
|
||||
cursor_y++;
|
||||
if (cursor_x >= file_line_len(working_with_offset)) {
|
||||
cursor_x = file_line_len(working_with_offset) - 1;
|
||||
}
|
||||
working_with_offset += cursor_x;
|
||||
_viewer_set_cursor(cursor_x, cursor_y);
|
||||
}
|
||||
}
|
||||
|
||||
void viewer_cursor_next()
|
||||
{
|
||||
cursor_x++;
|
||||
if (cursor_x == SCREEN_X) {
|
||||
cursor_y++;
|
||||
cursor_x = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int viewer_get_cursor_offset_in_file()
|
||||
{
|
||||
return working_with_offset;
|
||||
}
|
||||
|
||||
void viewer_enter_menu_mode()
|
||||
{
|
||||
write(STDOUT, "\x1b[25;1H", 7);
|
||||
}
|
||||
|
||||
void viewer_leave_menu_mode()
|
||||
{
|
||||
/* FIXME: Restore cursor to prev position */
|
||||
_viewer_set_cursor_to_start();
|
||||
}
|
||||
19
userland/utilities/edit/viewer.h
Normal file
19
userland/utilities/edit/viewer.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef _USERLAND_EDIT_VIEWER_H
|
||||
#define _USERLAND_EDIT_VIEWER_H
|
||||
|
||||
#define STDIN 0
|
||||
#define STDOUT 1
|
||||
#define SCREEN_X 80
|
||||
#define SCREEN_Y 24
|
||||
|
||||
void viewer_clear_screen();
|
||||
void viewer_display(char* buf, int start, int len);
|
||||
void viewer_enter_menu_mode();
|
||||
void viewer_cursor_left();
|
||||
void viewer_cursor_right();
|
||||
void viewer_cursor_up();
|
||||
void viewer_cursor_down();
|
||||
void viewer_cursor_next();
|
||||
int viewer_get_cursor_offset_in_file();
|
||||
|
||||
#endif
|
||||
8
userland/utilities/kill/BUILD.gn
Normal file
8
userland/utilities/kill/BUILD.gn
Normal file
@@ -0,0 +1,8 @@
|
||||
import("//build/userland/TEMPLATE.gni")
|
||||
|
||||
xOS_executable("kill") {
|
||||
install_path = "bin/"
|
||||
sources = [ "main.c" ]
|
||||
configs = [ "//build/userland:userland_flags" ]
|
||||
deplibs = [ "libc" ]
|
||||
}
|
||||
16
userland/utilities/kill/main.c
Normal file
16
userland/utilities/kill/main.c
Normal file
@@ -0,0 +1,16 @@
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc <= 2) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
printf("%d %d", atoi(argv[1]), atoi(argv[2]));
|
||||
kill(atoi(argv[1]), atoi(argv[2]));
|
||||
return 0;
|
||||
}
|
||||
5
userland/utilities/ls/.info.mk
Normal file
5
userland/utilities/ls/.info.mk
Normal file
@@ -0,0 +1,5 @@
|
||||
APPS += LS
|
||||
|
||||
LS_NAME = ls
|
||||
LS_LIBS = c
|
||||
LS_INSTALL_PATH = bin/
|
||||
8
userland/utilities/ls/BUILD.gn
Normal file
8
userland/utilities/ls/BUILD.gn
Normal file
@@ -0,0 +1,8 @@
|
||||
import("//build/userland/TEMPLATE.gni")
|
||||
|
||||
xOS_executable("ls") {
|
||||
install_path = "bin/"
|
||||
sources = [ "main.c" ]
|
||||
configs = [ "//build/userland:userland_flags" ]
|
||||
deplibs = [ "libc" ]
|
||||
}
|
||||
72
userland/utilities/ls/main.c
Normal file
72
userland/utilities/ls/main.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <dirent.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define BUF_SIZE 1024
|
||||
|
||||
struct linux_dirent {
|
||||
uint32_t inode;
|
||||
uint16_t rec_len;
|
||||
uint8_t name_len;
|
||||
uint8_t file_type;
|
||||
char* name;
|
||||
};
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int fd, nread;
|
||||
char buf[BUF_SIZE];
|
||||
struct linux_dirent* d;
|
||||
int bpos;
|
||||
char d_type;
|
||||
char has_path = 0;
|
||||
char show_inodes = 0;
|
||||
char show_private = 0;
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (memcmp(argv[i], "-i", 3) == 0) {
|
||||
show_inodes = 1;
|
||||
} else if (memcmp(argv[i], "-a", 3) == 0) {
|
||||
show_private = 1;
|
||||
} else {
|
||||
has_path = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (has_path) {
|
||||
fd = open(argv[1], O_RDONLY | O_DIRECTORY);
|
||||
} else {
|
||||
fd = open(".", O_RDONLY | O_DIRECTORY);
|
||||
}
|
||||
|
||||
if (fd < 0) {
|
||||
printf("ls: can't open file\n");
|
||||
return -1;
|
||||
}
|
||||
for (;;) {
|
||||
nread = getdents(fd, buf, BUF_SIZE);
|
||||
if (nread < 0) {
|
||||
printf("ls: can't read dir\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (nread == 0)
|
||||
break;
|
||||
|
||||
for (bpos = 0; bpos < nread;) {
|
||||
d = (struct linux_dirent*)(buf + bpos);
|
||||
if (((char*)&d->name)[0] != '.' || show_private) {
|
||||
printf("%s", (char*)&d->name);
|
||||
if (show_inodes) {
|
||||
printf(" %d", d->inode);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
bpos += d->rec_len;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
5
userland/utilities/mkdir/.info.mk
Normal file
5
userland/utilities/mkdir/.info.mk
Normal file
@@ -0,0 +1,5 @@
|
||||
APPS += MKDIR
|
||||
|
||||
MKDIR_NAME = mkdir
|
||||
MKDIR_LIBS = c
|
||||
MKDIR_INSTALL_PATH = bin/
|
||||
8
userland/utilities/mkdir/BUILD.gn
Normal file
8
userland/utilities/mkdir/BUILD.gn
Normal file
@@ -0,0 +1,8 @@
|
||||
import("//build/userland/TEMPLATE.gni")
|
||||
|
||||
xOS_executable("mkdir") {
|
||||
install_path = "bin/"
|
||||
sources = [ "main.c" ]
|
||||
configs = [ "//build/userland:userland_flags" ]
|
||||
deplibs = [ "libc" ]
|
||||
}
|
||||
19
userland/utilities/mkdir/main.c
Normal file
19
userland/utilities/mkdir/main.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc < 2) {
|
||||
write(1, "Usage: mkdir files...\n", 22);
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (mkdir(argv[i]) < 0) {
|
||||
write(1, "mkdir: failed to create\n", 26);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
5
userland/utilities/rm/.info.mk
Normal file
5
userland/utilities/rm/.info.mk
Normal file
@@ -0,0 +1,5 @@
|
||||
APPS += RM
|
||||
|
||||
RM_NAME = rm
|
||||
RM_LIBS = c
|
||||
RM_INSTALL_PATH = bin/
|
||||
8
userland/utilities/rm/BUILD.gn
Normal file
8
userland/utilities/rm/BUILD.gn
Normal file
@@ -0,0 +1,8 @@
|
||||
import("//build/userland/TEMPLATE.gni")
|
||||
|
||||
xOS_executable("rm") {
|
||||
install_path = "bin/"
|
||||
sources = [ "main.c" ]
|
||||
configs = [ "//build/userland:userland_flags" ]
|
||||
deplibs = [ "libc" ]
|
||||
}
|
||||
18
userland/utilities/rm/main.c
Normal file
18
userland/utilities/rm/main.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc < 2) {
|
||||
write(1, "Usage: rm files...\n", 21);
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (unlink(argv[i]) < 0) {
|
||||
write(1, "rm: failed to delete\n", 21);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
5
userland/utilities/rmdir/.info.mk
Normal file
5
userland/utilities/rmdir/.info.mk
Normal file
@@ -0,0 +1,5 @@
|
||||
APPS += RMDIR
|
||||
|
||||
RMDIR_NAME = rmdir
|
||||
RMDIR_LIBS = c
|
||||
RMDIR_INSTALL_PATH = bin/
|
||||
8
userland/utilities/rmdir/BUILD.gn
Normal file
8
userland/utilities/rmdir/BUILD.gn
Normal file
@@ -0,0 +1,8 @@
|
||||
import("//build/userland/TEMPLATE.gni")
|
||||
|
||||
xOS_executable("rmdir") {
|
||||
install_path = "bin/"
|
||||
sources = [ "main.c" ]
|
||||
configs = [ "//build/userland:userland_flags" ]
|
||||
deplibs = [ "libc" ]
|
||||
}
|
||||
19
userland/utilities/rmdir/main.c
Normal file
19
userland/utilities/rmdir/main.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc < 2) {
|
||||
printf("Usage: rmdir files...\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (rmdir(argv[i]) < 0) {
|
||||
printf("rmdir: failed to delete\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
8
userland/utilities/sudo/BUILD.gn
Normal file
8
userland/utilities/sudo/BUILD.gn
Normal file
@@ -0,0 +1,8 @@
|
||||
import("//build/userland/TEMPLATE.gni")
|
||||
|
||||
xOS_executable("sudo") {
|
||||
install_path = "bin/"
|
||||
sources = [ "main.c" ]
|
||||
configs = [ "//build/userland:userland_flags" ]
|
||||
deplibs = [ "libc" ]
|
||||
}
|
||||
50
userland/utilities/sudo/main.c
Normal file
50
userland/utilities/sudo/main.c
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <fcntl.h>
|
||||
#include <pwd.h>
|
||||
#include <shadow.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
char pswd[64];
|
||||
|
||||
int auth(char* uname)
|
||||
{
|
||||
spwd_t* spwd;
|
||||
spwd = getspnam(uname);
|
||||
if (spwd == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("Password: ");
|
||||
fflush(stdout);
|
||||
scanf("%s", pswd);
|
||||
int res = strcmp(pswd, spwd->sp_pwdp);
|
||||
endspent();
|
||||
return res;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
char* envp[] = { "PATH=/bin:/usr/bin", NULL };
|
||||
if (auth("root") != 0) {
|
||||
printf("Incorrect password\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Sign as root
|
||||
passwd_t* pwd = getpwnam("root");
|
||||
if (pwd == NULL) {
|
||||
printf("unable to retrieve user info");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (setuid(pwd->pw_uid) != 0) {
|
||||
printf("setuid failure");
|
||||
return -1;
|
||||
}
|
||||
|
||||
execvpe(argv[1], &argv[1], envp);
|
||||
return 0;
|
||||
}
|
||||
5
userland/utilities/touch/.info.mk
Normal file
5
userland/utilities/touch/.info.mk
Normal file
@@ -0,0 +1,5 @@
|
||||
APPS += TOUCH
|
||||
|
||||
TOUCH_NAME = touch
|
||||
TOUCH_LIBS = c
|
||||
TOUCH_INSTALL_PATH = bin/
|
||||
8
userland/utilities/touch/BUILD.gn
Normal file
8
userland/utilities/touch/BUILD.gn
Normal file
@@ -0,0 +1,8 @@
|
||||
import("//build/userland/TEMPLATE.gni")
|
||||
|
||||
xOS_executable("touch") {
|
||||
install_path = "bin/"
|
||||
sources = [ "main.c" ]
|
||||
configs = [ "//build/userland:userland_flags" ]
|
||||
deplibs = [ "libc" ]
|
||||
}
|
||||
25
userland/utilities/touch/main.c
Normal file
25
userland/utilities/touch/main.c
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int fd;
|
||||
|
||||
if (argc < 2) {
|
||||
write(1, "Usage: touch files...\n", 22);
|
||||
return 0;
|
||||
}
|
||||
|
||||
mode_t std_mode = S_IFREG | S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if ((fd = creat(argv[i], std_mode)) < 0) {
|
||||
write(1, "touch: failed to create\n", 24);
|
||||
break;
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
5
userland/utilities/uname/.info.mk
Normal file
5
userland/utilities/uname/.info.mk
Normal file
@@ -0,0 +1,5 @@
|
||||
APPS += UNAME
|
||||
|
||||
UNAME_NAME = uname
|
||||
UNAME_LIBS = c
|
||||
UNAME_INSTALL_PATH = bin/
|
||||
8
userland/utilities/uname/BUILD.gn
Normal file
8
userland/utilities/uname/BUILD.gn
Normal file
@@ -0,0 +1,8 @@
|
||||
import("//build/userland/TEMPLATE.gni")
|
||||
|
||||
xOS_executable("uname") {
|
||||
install_path = "bin/"
|
||||
sources = [ "main.c" ]
|
||||
configs = [ "//build/userland:userland_flags" ]
|
||||
deplibs = [ "libc" ]
|
||||
}
|
||||
68
userland/utilities/uname/main.c
Normal file
68
userland/utilities/uname/main.c
Normal file
@@ -0,0 +1,68 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int fd, i;
|
||||
|
||||
utsname_t uts;
|
||||
int rc = uname(&uts);
|
||||
if (rc < 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int flag_s = 0;
|
||||
int flag_n = 0;
|
||||
int flag_r = 0;
|
||||
int flag_m = 0;
|
||||
if (argc == 1) {
|
||||
flag_s = 1;
|
||||
} else {
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
if (argv[i][0] == '-') {
|
||||
for (const char* o = &argv[i][1]; *o; ++o) {
|
||||
switch (*o) {
|
||||
case 's':
|
||||
flag_s = 1;
|
||||
break;
|
||||
case 'n':
|
||||
flag_n = 1;
|
||||
break;
|
||||
case 'r':
|
||||
flag_r = 1;
|
||||
break;
|
||||
case 'm':
|
||||
flag_m = 1;
|
||||
break;
|
||||
case 'a':
|
||||
flag_s = flag_n = flag_r = flag_m = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!flag_s && !flag_n && !flag_r && !flag_m) {
|
||||
flag_s = 1;
|
||||
}
|
||||
|
||||
if (flag_s) {
|
||||
printf("%s ", uts.sysname);
|
||||
}
|
||||
if (flag_n) {
|
||||
printf("%s ", uts.nodename);
|
||||
// write(1, uts.nodename, strlen(uts.nodename));
|
||||
}
|
||||
if (flag_r) {
|
||||
printf("%s ", uts.release);
|
||||
// write(1, uts.release, strlen(uts.release));
|
||||
}
|
||||
if (flag_m) {
|
||||
printf("%s ", uts.machine);
|
||||
// write(1, uts.machine, strlen(uts.machine));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
8
userland/utilities/whoami/BUILD.gn
Normal file
8
userland/utilities/whoami/BUILD.gn
Normal file
@@ -0,0 +1,8 @@
|
||||
import("//build/userland/TEMPLATE.gni")
|
||||
|
||||
xOS_executable("whoami") {
|
||||
install_path = "bin/"
|
||||
sources = [ "main.c" ]
|
||||
configs = [ "//build/userland:userland_flags" ]
|
||||
deplibs = [ "libc" ]
|
||||
}
|
||||
10
userland/utilities/whoami/main.c
Normal file
10
userland/utilities/whoami/main.c
Normal file
@@ -0,0 +1,10 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
puts(getlogin());
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user