Add files

git-svn-id: svn://kolibrios.org@8193 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
maxcodehack 2020-11-14 16:51:07 +00:00
parent 42323d3960
commit 4709318319
6 changed files with 866 additions and 0 deletions

View File

@ -0,0 +1,104 @@
/*
* Backy_lib.h
* Author: JohnXenox aka Aleksandr Igorevich.
*/
#ifndef __jxl_Date_get_h__
#define __jxl_Date_get_h__
#ifdef ___TINY_C___
typedef unsigned int uint32_t;
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned long long uint64_t;
typedef char int8_t;
typedef short int int16_t;
typedef int int32_t;
typedef long long int64_t;
static inline int32_t saveFile(uint32_t nbytes, uint8_t *data, uint32_t enc, uint8_t *path)
{
int32_t val;
uint8_t dt[28]; // basic information structure.
(uint32_t) dt[0] = 2; // subfunction number.
(uint32_t) dt[4] = 0; // reserved.
(uint32_t) dt[8] = 0; // reserved.
(uint32_t) dt[12] = nbytes; // number of bytes to write.
(uint8_t *) dt[16] = data; // pointer to data.
(uint32_t) dt[20] = enc; // string encoding (0 = default, 1 = cp866, 2 = UTF-16LE, 3 = UTF-8).
(uint8_t *) dt[24] = path; // pointer to path.
__asm__ __volatile__("int $0x40":"=a"(val):"a"(80), "b"(&dt));
return val;
}
#else
#include <stdint.h>
static inline int32_t saveFile(uint32_t nbytes, uint8_t *data, uint32_t enc, uint8_t *path)
{
int32_t val;
struct file_op_t
{
uint32_t fn;
uint32_t reserved0;
uint32_t reserved1;
uint32_t number_of_bytes_to_write;
void * pointer_to_data;
char path[1];
} *file_op = calloc(sizeof(*file_op) + strlen(path) + 2, 1); // FIXME: Not works on UTF16LE enc
file_op->fn = 2;
file_op->number_of_bytes_to_write = nbytes;
file_op->pointer_to_data = data;
if (enc != 0)
{
file_op->path[0] = enc;
strcpy(file_op->path + 1, path);
}
else
{
strcpy(file_op->path, path);
}
asm volatile ("int $0x40":"=a"(val):"a"(80), "b"(file_op));
return val;
}
#endif
static inline uint32_t getDate(void)
{
uint32_t date;
__asm__ __volatile__("int $0x40":"=a"(date):"a"(29));
return date;
}
static inline uint32_t getTime(void)
{
uint32_t time;
__asm__ __volatile__("int $0x40":"=a"(time):"a"(3));
return time;
}
static inline void *openFile(uint32_t *length, const uint8_t *path)
{
uint8_t *fd;
__asm__ __volatile__ ("int $0x40":"=a"(fd), "=d"(*length):"a" (68), "b"(27),"c"(path));
return fd;
}
#endif

View File

@ -0,0 +1,426 @@
/*
* Programme name: Backy
* Description: The programme for backing up a file.
*
* Backy.c
* Author: JohnXenox aka Aleksandr Igorevich.
* Port to GCC: maxcodehack
*
* Works from command line, only!
*/
// Copied from TCC
char* strrev(char *p)
{
char *q = p, *res = p, z;
while(q && *q) ++q; /* find eos */
for(--q; p < q; ++p, --q)
{
z = *p;
*p = *q;
*q = z;
}
return res;
}
char* itoab(unsigned int n, char* s, int b)
{
char *ptr;
int lowbit;
ptr = s;
b >>= 1;
do {
lowbit = n & 1;
n = (n >> 1) & 0x7FFFFFFF;
*ptr = ((n % b) << 1) + lowbit;
if(*ptr < 10) *ptr += '0'; else *ptr += 55;
++ptr;
} while(n /= b);
*ptr = 0;
return strrev(s);
}
#define CREATION_DATE "2020.05.27"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "console_obj.h"
#include "Backy_lang.h"
#include "Backy_lib.h"
int date = 0;
int time = 0;
char years = 0;
char months = 0;
char days = 0;
char hours = 0;
char minutes = 0;
char seconds = 0;
char *data = 0;
int length = 0;
char path_in[4096] = {0};
char path_out[4096] = {0};
char num[3] = {0};
char full_date[25] = {0};
char ext[] = ".bak";
char flag = 0;
char state;
int main(int argc, char** argv)
{
load_console();
con_set_title("Backy");
// ============================================================ //
// preprocessing arguments from the command line. ============= //
//
// 0 argument - name of the programme.
// 1 argument - path to the file with name that need to be backup.
// 2 argument - the key (-o).
// 3 argument - path to the output directory without the name of the file.
// printf("Number of args: %d\n", argc);
// printf("Argv 0: %s\n", argv[0]);
// sprintf("Argv 1: %s\n\n", argv[1]);
// ============================================================ //
// process the command line arguments. ======================== //
if (argc > 1)
{
for (int i = 1; i < argc; i++)
{
// if found the key "-o", then copy output path into the array "path_out".
if (*argv[i] == '-') // && (*(argv[i] + 1) == 'o'))
{
// printf("Key -o is found!\n");
i++;
flag = 1;
if (i <= argc)
{
// copying of a current path into the array "path_out".
strcpy(path_out, argv[i]);
// printf("Path output is copyed!\n");
i++;
break;
}
}
// if input path is found, then copy it into the array "path_in".
if (*argv[i] == '/')
{
flag = 2;
// copying of a current path into the buffer.
strcpy(path_in, argv[i]);
if (flag != 1)
{
int idx = strlen(path_in);
while (path_in[idx] != '/')
{
idx--;
}
strncpy(path_out, path_in, idx);
}
// printf("Path input is copyed!\n");
}
// if found characters.
if ( (*argv[i] > '0') && (*argv[i] < '9') || \
(*argv[i] > 'A') && (*argv[i] < 'Z') || \
(*argv[i] > 'a') && (*argv[i] < 'z') )
{
flag = 3;
strcpy(path_in, argv[0]);
// printf("Arg 0 is copyed!\n");
int idx = strlen(path_in);
while (path_in[idx] != '/')
{
path_in[idx] = 0;
idx--;
}
idx++;
strcpy(path_out, path_in);
strcpy(&path_in[idx], argv[1]);
// printf("Arg 1 is added!\n");
}
}
// if not found the flag, then copy path from "path_in" into "path_out".
if ((flag == 0) && (flag != 2) && (flag != 3))
{
// copying the input path into the output path,
strcpy(path_out, path_in);
//printf("Path input is copyed into the path output!\n");
}
}
else
{
con_set_title("Useful info!");
#if defined (lang_en)
con_printf("\n Name: Backy");
con_printf("\n Date: %s", CREATION_DATE);
con_printf("\n Description: The programme for backing up a file.\n");
con_printf("\n Author: JohnXenox\n");
con_printf("\n Usage: backy <path1> <-o path2>\n");
con_printf(" path1 - path to a file to be backuped.\n");
con_printf(" -o path2 - path to the output directory without the name of a file.\n\n");
con_printf(" Examples:\n");
con_printf(" backy test.c\n");
con_printf(" backy test.c -o /tmp0/1/\n");
con_printf(" backy /hd0/1/test.c\n");
con_printf(" backy /hd0/1/test.c -o /tmp0/1/\n");
#elif defined (lang_ru)
con_printf("\n ˆ¬ï: Backy");
con_printf("\n „ â : %s", CREATION_DATE);
con_printf("\n Ž¯¨á ­¨¥: <20>ணࠬ¬  ¤«ï ᮧ¤ ­¨ï १¥à¢­®© ª®¯¨¨ ä ©« .\n");
con_printf("\n €¢â®à: JohnXenox\n");
con_printf("\n ˆá¯®«ì§®¢ ­¨¥: backy <path1> <-o path2>\n");
con_printf(" path1 - ¯ãâì ª ä ©«ã, ª®â®àë© ­ ¤® ᪮¯¨à®¢ âì.\n");
con_printf(" -o path2 - ¯ãâì ª ¤¨à¥ªâ®à¨¨, ¢ ª®â®àãî ¡ã¤¥â ᪮¯¨à®¢ ­  १¥à¢­ ï ª®¯¨ï ä ©« .\n\n");
con_printf(" <20>ਬ¥àë:\n");
con_printf(" backy test.c\n");
con_printf(" backy test.c -o /tmp0/1/\n");
con_printf(" backy /hd0/1/test.c\n");
con_printf(" backy /hd0/1/test.c -o /tmp0/1/\n");
#endif
return 0;
}
//printf("Path_in: %s\n", path_in);
//printf("Path_out: %s\n", path_out);
// ============================================================ //
// getting the time in BCD. =================================== //
time = getTime(); // time = 0x00SSMMHH.
hours = (char)time;
minutes = (char)(time >> 8);
seconds = (char)(time >> 16);
// ============================================================ //
// getting the date in BCD. =================================== //
date = getDate(); // date = 0x00DDMMYY.
years = (char)date;
months = (char)(date >> 8);
days = (char)(date >> 16);
// ============================================================ //
// fills the array with the date in ASCII. ==================== //
char ofs = 0;
char *dta = 0;
for (char i = 0; i < 3; i++)
{
if (i == 0)
{
dta = &years;
full_date[ofs] = '2';
ofs++;
full_date[ofs] = '0';
ofs++;
}
if (i == 1)
{
dta = &months;
}
if (i == 2)
{
dta = &days;
}
itoab(*dta, num, 16);
if (num[1] == 0)
{
full_date[ofs] = '0';
ofs++;
full_date[ofs] = num[0];
}
else
{
full_date[ofs] = num[0];
ofs++;
full_date[ofs] = num[1];
}
ofs++;
if (i != 2)
{
full_date[ofs] = '.';
ofs++;
}
}
full_date[ofs] = '_';
ofs++;
// ============================================================ //
// fills the array with the time in ASCII. ==================== //
ofs = 11;
dta = 0;
for (char i = 0; i < 3; i++)
{
if (i == 0)
dta = &hours;
if (i == 1)
dta = &minutes;
if (i == 2)
dta = &seconds;
itoab(*dta, num, 16);
if (num[1] == 0)
{
full_date[ofs] = '0';
ofs++;
full_date[ofs] = num[0];
}
else
{
full_date[ofs] = num[0];
ofs++;
full_date[ofs] = num[1];
}
ofs++;
if (i < 2)
{
full_date[ofs] = '.';
}
//else
//{
// full_date[ofs] = '_';
//}
ofs++;
}
// ============================================================ //
// adding the name of the input file to the output path. ====== //
int i = 0;
int y = 0;
// searching for a zero terminator in the input path.
while (path_in[i] != 0)
{
i++;
}
// searching for a slash in the input path.
while (path_in[i] != '/')
{
i--;
}
// searching for a zero terminator in the output path.
while (path_out[y] != 0)
{
y++;
}
// searching for a slash in the output path.
if (path_out[y - 1] == '/')
{
y--;
}
// copying the input name of the file into the output path,
strcpy(&path_out[y], &path_in[i]);
// ============================================================ //
// adding the extension and full date to the path. ============ //
i = 0;
// searching for a zero terminator in the output path.
while (path_out[i] != 0)
{
i++;
}
path_out[i] = '_';
i++;
// adding full date.
strcpy(&path_out[i], full_date);
i += strlen(full_date);
// adding the extension to a path.
strncpy(&path_out[i], ext, 4);
//printf("Path_in: %s\n", path_in);
//printf("Path_out: %s\n", path_out);
data = openFile(&length, path_in);
if(data == 0)
{
#if defined (lang_en)
con_printf("\nThe file isn't found!\n");
#elif defined (lang_ru)
con_printf("\n” ©« ­¥ ­ ©¤¥­!\n");
#endif
return 13;
}
return checkStateOnSave(saveFile(length, data, 0, path_out));
}

View File

@ -0,0 +1,161 @@
/*
* Backy_lib.h
* Author: JohnXenox aka Aleksandr Igorevich.
*/
#ifndef __Backy_lib_h__
#define __Backy_lib_h__
/*
typedef unsigned int uint32_t;
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned long long uint64_t;
typedef char int8_t;
typedef short int int16_t;
typedef int int32_t;
typedef long long int64_t;
*/
static inline uint32_t getDate(void)
{
uint32_t date;
__asm__ __volatile__("int $0x40":"=a"(date):"a"(29));
return date;
}
static inline uint32_t getTime(void)
{
uint32_t time;
__asm__ __volatile__("int $0x40":"=a"(time):"a"(3));
return time;
}
static inline void *openFile(uint32_t *length, const uint8_t *path)
{
uint8_t *fd;
__asm__ __volatile__ ("int $0x40":"=a"(fd), "=d"(*length):"a" (68), "b"(27),"c"(path));
return fd;
}
// FILE SAVING NOT WORKING
/*
struct saveFile__
{
int num;
int res_;
int res;
int nbyte;
char* data_;
int enc_;
char* path_;
};
*/
static inline int32_t saveFile(uint32_t nbytes, uint8_t *data, uint32_t enc, uint8_t *path)
{
int32_t val;
/*
struct saveFile__ dt;
//uint8_t dt[28]; // basic information structure.
dt.num = 2; // subfunction number.
dt.res_= 0; // reserved.
dt.res = 0; // reserved.
dt.nbyte = nbytes; // number of bytes to write.
dt.data_ = data; // pointer to data.
dt.enc_ = enc; // string encoding (0 = default, 1 = cp866, 2 = UTF-16LE, 3 = UTF-8).
dt.path_ = path; // pointer to path.
__asm__ __volatile__("int $0x40":"=a"(val):"a"(80), "b"(&dt));
*/
return val;
}
int checkStateOnSave(int state)
{
#if defined (lang_en)
switch(state)
{
case 2: con_printf("\nThe function isn't supported for the given file system!\n");
return 2;
case 3: con_printf("\nUnknown file system!\n");
return 3;
case 5: con_printf("\nFile isn't found!\n");
return 5;
case 6: con_printf("\nEnd of a file, EOF!\n");
return 6;
case 7: con_printf("\nPointer lies outside of application memory!\n");
return 7;
case 8: con_printf("\nDisk is full!\n");
return 8;
case 9: con_printf("\nFile system error!\n");
return 9;
case 10: con_printf("\nAccess denied!\n");
return 10;
case 11: con_printf("\nDevice error!\n");
return 11;
case 12: con_printf("\nFile system requires more memory!\n");
return 12;
}
#elif defined (lang_ru)
switch(state)
{
case 2: con_printf("\n”ã­ªæ¨ï ­¥ ¯®¤¤¥à¦¨¢ ¥âáï ¤«ï ¤ ­­®© ä ©«®¢®© á¨á⥬ë!\n");
return 2;
case 3: con_printf("\n<EFBFBD>¥¨§¢¥áâ­ ï ä ©«®¢ ï á¨á⥬ !\n");
return 3;
case 5: con_printf("\n” ©« ­¥ ­ ©¤¥­!\n");
return 5;
case 6: con_printf("\n” ©« § ª®­ç¨«áï!\n");
return 6;
case 7: con_printf("\n“ª § â¥«ì ¢­¥ ¯ ¬ï⨠¯à¨«®¦¥­¨ï!\n");
return 7;
case 8: con_printf("\n„¨áª § ¯®«­¥­!\n");
return 8;
case 9: con_printf("\nŽè¨¡ª  ä ©«®¢®© á¨á⥬ë!\n");
return 9;
case 10: con_printf("\n„®áâ㯠§ ¯à¥éñ­!\n");
return 10;
case 11: con_printf("\nŽè¨¡ª  ãáâனá⢠!\n");
return 11;
case 12: con_printf("\n” ©«®¢®© á¨á⥬¥ ­¥¤®áâ â®ç­® ®¯¥à â¨¢­®© ¯ ¬ïâ¨!\n");
return 12;
}
#endif
}
#endif

View File

@ -0,0 +1,27 @@
CC = kos32-gcc
LD = kos32-ld
SDK_DIR = $(abspath ../../../../contrib/sdk)
LDFLAGS = -call_shared -nostdlib -T $(SDK_DIR)/sources/newlib/app-dynamic.lds --image-base 0
CFLAGS = -c -fno-ident -O2 -fomit-frame-pointer -fno-ident -U__WIN32__ -U_Win32 -U_WIN32 -U__MINGW32__ -UWIN32
INCLUDES = -I $(SDK_DIR)/sources/newlib/libc/include
LIBPATH = -L $(SDK_DIR)/lib -L /home/autobuild/tools/win32/mingw32/lib
ifndef LANG_backy
LANG_backy = lang_en
endif
default: backy
backy: $(OBJECTS) Makefile
echo "#define $(LANG_backy)" > Backy_lang.h
$(CC) $(CFLAGS) $(INCLUDES) -o Backy.o Backy.c
$(LD) $(LDFLAGS) $(LIBPATH) --subsystem console -o Backy Backy.o -lgcc -lc.dll
kos32-strip -s Backy -o Backy
objcopy Backy -O binary
rm Backy.o
rm Backy_lang.h
clean:
rm Backy

View File

@ -0,0 +1,141 @@
// Console.obj loading for kos32-gcc
// Writed by rgimad and maxcodehack
#include <string.h>
#include <stdlib.h>
#ifndef CONSOLE_OBJ_H
#define CONSOLE_OBJ_H
#ifdef __cplusplus
extern "C" {
#endif
#ifndef NULL
#define NULL 0
#endif
#ifndef cdecl
#define cdecl __attribute__ ((cdecl))
#endif
#ifndef stdcall
#define stdcall __attribute__ ((stdcall))
#endif
typedef unsigned int dword;
typedef unsigned short word;
const char* imports[] = {
"START", "version", "con_init", "con_write_asciiz", "con_write_string",
"con_printf", "con_exit", "con_get_flags", "con_set_flags", "con_kbhit",
"con_getch", "con_getch2", "con_gets", "con_gets2", "con_get_font_height",
"con_get_cursor_height", "con_set_cursor_height", "con_cls",
"con_get_cursor_pos", "con_set_cursor_pos", "con_set_title",
(char*)0
};
dword *version;
typedef int (stdcall * con_gets2_callback)(int keycode, char** pstr, int* pn,
int* ppos);
void stdcall (*con_init)(dword wnd_width, dword wnd_height, dword scr_width, dword scr_height, const char* title) = 0;
void stdcall (*con_exit)(int bCloseWindow) = 0;
void stdcall (*con_set_title)(const char* title) = 0;
void stdcall (*con_write_asciiz)(const char* str) = 0;
void stdcall (*con_write_string)(const char* str, dword length) = 0;
int cdecl (*con_printf)(const char* format, ...) = 0;
dword stdcall (*con_get_flags)(void) = 0;
dword stdcall (*con_set_flags)(dword new_flags) = 0;
int stdcall (*con_get_font_height)(void) = 0;
int stdcall (*con_get_cursor_height)(void) = 0;
int stdcall (*con_set_cursor_height)(int new_height) = 0;
int stdcall (*con_getch)(void) = 0;
word stdcall (*con_getch2)(void) = 0;
int stdcall (*con_kbhit)(void) = 0;
char* stdcall (*con_gets)(char* str, int n) = 0;
char* stdcall (*con_gets2)(con_gets2_callback callback, char* str, int n) = 0;
void stdcall (*con_cls)() = 0;
void stdcall (*con_get_cursor_pos)(int* px, int* py) = 0;
void stdcall (*con_set_cursor_pos)(int x, int y) = 0;
const char lib_path[] = "/sys/lib/console.obj";
void* load_library(const char *name)
{
void *table;
__asm__ __volatile__(
"int $0x40"
:"=a"(table)
:"a"(68), "b"(19), "c"(name));
return table;
}
void *load_library_procedure(void *exports, const char *name)
{
if (exports == NULL) { return 0; }
while (*(dword*)exports != 0)
{
char *str1 = (char*)(*(dword*)exports);
if (strcmp(str1, name) == 0)
{
void *ptr = (void*)*(dword*)(exports + 4);
return ptr;
}
exports += 8;
}
return 0;
}
void output_debug_string(const char *s)
{
unsigned int i = 0;
while(*(s + i))
{
asm volatile ("int $0x40"::"a"(63), "b"(1), "c"(*(s + i)));
i++;
}
}
void load_console()
{
void *lib = load_library(lib_path);
if (!lib)
{
output_debug_string("Console.obj loading error\r\n");
exit(1);
}
dword (*start_lib)(dword) = (dword(*)(dword))load_library_procedure(lib, imports[0]);
version = (dword*)load_library_procedure(lib, imports[1]);
con_init = (void stdcall(*)(dword,dword,dword,dword,const char*))load_library_procedure(lib, imports[2]);
con_write_asciiz = (void stdcall(*)(const char*))load_library_procedure(lib, imports[3]);
con_write_string = (void stdcall(*)(const char*,dword))load_library_procedure(lib, imports[4]);
con_printf = (int cdecl(*)(const char*,...))load_library_procedure(lib, imports[5]);
con_exit = (void stdcall(*)(int))load_library_procedure(lib, imports[6]);
con_get_flags = (dword stdcall(*)(void))load_library_procedure(lib, imports[7]);
con_set_flags = (dword stdcall(*)(dword))load_library_procedure(lib, imports[8]);
con_kbhit = (int stdcall(*)(void))load_library_procedure(lib, imports[9]);
con_getch = (int stdcall(*)(void))load_library_procedure(lib, imports[10]);
con_getch2 = (word stdcall(*)(void))load_library_procedure(lib, imports[11]);
con_gets = (char* stdcall(*)(char*,int))load_library_procedure(lib, imports[12]);
con_gets2 = (char* stdcall(*)(con_gets2_callback,char*,int))load_library_procedure(lib, imports[13]);
con_get_font_height = (int stdcall(*)(void))load_library_procedure(lib, imports[14]);
con_get_cursor_height = (int stdcall(*)(void))load_library_procedure(lib, imports[15]);
con_set_cursor_height = (int stdcall(*)(int))load_library_procedure(lib, imports[16]);
con_cls = (void stdcall(*)(void))load_library_procedure(lib, imports[17]);
con_get_cursor_pos = (void stdcall(*)(int*,int*))load_library_procedure(lib, imports[18]);
con_set_cursor_pos = (void stdcall(*)(int,int))load_library_procedure(lib, imports[19]);
con_set_title = (void stdcall(*)(const char*))load_library_procedure(lib, imports[20]);
}
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,7 @@
Port backy to gcc
Files not working yet
Languages:
Eng:
make
Rus:
env LANG_backy=lang_ru make