Update skipped files in previous commit

git-svn-id: svn://kolibrios.org@9766 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
turbocat
2022-04-15 09:11:49 +00:00
parent cde4fa851d
commit 99922e4c2c
51 changed files with 1967 additions and 2487 deletions
@@ -1,9 +1,10 @@
#include <assert.h>
#include <stdio.h>
int a=431;
int b=532;
int a = 431;
int b = 532;
int main(){
assert(a!=b);
assert(a==b);
int main()
{
assert(a != b);
assert(a == b);
}
@@ -1,7 +1,7 @@
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/ksys.h>
ksys_colors_table_t sys_color_table;
@@ -10,8 +10,7 @@ char statusbar[255];
ksys_thread_t proc_info;
char text_line[255];
enum BUTTONS
{
enum BUTTONS {
BTN_QUIT = 1,
BTN_POP = 10,
BTN_UNLOCK = 11
@@ -23,40 +22,43 @@ enum BUTTONS
void draw_window()
{
int win_hight, win_width, i, pos_y = _ksys_get_skin_height() + 36; // 60 == 24+36
int win_hight, win_width, i, pos_y = _ksys_get_skin_height() + 36; // 60 == 24+36
// start redraw
_ksys_start_draw();
// define&draw window
// define&draw window
_ksys_create_window(10, 40, 600, 400, "My window", sys_color_table.work_area, 0x13);
_ksys_thread_info(&proc_info, -1);
F
win_width = proc_info.winx_size;
win_width
= proc_info.winx_size;
win_hight = proc_info.winy_size;
_ksys_define_button(10, 30, 70, 20, BTN_POP, sys_color_table.work_button);
_ksys_draw_text("BUTTON1", 15, 34, 0, 0x90000000 | sys_color_table.work_button_text); //0x80000000 asciiz
_ksys_draw_text("BUTTON1", 15, 34, 0, 0x90000000 | sys_color_table.work_button_text); // 0x80000000 asciiz
_ksys_define_button(100, 30, 80, 20, BTN_UNLOCK, sys_color_table.work_button);
_ksys_draw_text("BUTTTON2", 110, 34, 0, 0x90000000 | sys_color_table.work_button_text);
// display statusbar
_ksys_draw_bar(6, win_hight - 17, win_width - 11, 12, 0x80000000 | sys_color_table.work_area); //0x80000000 gradient
_ksys_draw_bar(6, win_hight - 17, win_width - 11, 12, 0x80000000 | sys_color_table.work_area); // 0x80000000 gradient
_ksys_draw_text(statusbar, 10, win_hight - 15, 0, 0x80000000 | sys_color_table.work_text);
// display strings
for (i = LINES; i > 0; i--){
snprintf (text_line, sizeof text_line, "Line[%d]<<Just a text>>", i);
for (i = LINES; i > 0; i--) {
snprintf(text_line, sizeof text_line, "Line[%d]<<Just a text>>", i);
text_line[(win_width - 10 - 5) / FONT_W + 1] = '\0'; // clip text size, seems to big lines crashing OS, and form len by window size
_ksys_draw_text(text_line, 5, pos_y, 0, 0x90000000 | sys_color_table.work_text);
pos_y += FONT_H;
if(pos_y + 29 > win_hight) break; // 12 font + 12 statusbar + 5 border
if (pos_y + 29 > win_hight)
break; // 12 font + 12 statusbar + 5 border
}
// end redraw
_ksys_end_draw();
}
int main()
{
int gui_event;
@@ -67,30 +69,28 @@ int main()
_ksys_get_system_colors(&sys_color_table);
_ksys_set_event_mask(0xC0000027); // mouse events only when focused window and mouse inside
do{
gui_event = _ksys_get_event();
switch(gui_event)
{
do {
gui_event = _ksys_get_event();
switch (gui_event) {
case KSYS_EVENT_NONE:
break;
break;
case KSYS_EVENT_REDRAW:
draw_window();
break;
break;
case KSYS_EVENT_KEY:
break;
break;
case KSYS_EVENT_BUTTON:
pressed_button = _ksys_get_button();
switch (pressed_button)
{
case BTN_POP:
switch (pressed_button) {
case BTN_POP:
strcpy(statusbar, "POP pressed....");
draw_window();
break;
case BTN_UNLOCK:
case BTN_UNLOCK:
strcpy(statusbar, "UNLOCK pressed....");
draw_window();
break;
case BTN_QUIT:
case BTN_QUIT:
return 0;
break;
}
@@ -99,17 +99,18 @@ int main()
mouse_pos = _ksys_get_mouse_pos(KSYS_MOUSE_WINDOW_POS); // window relative
mouse_button = _ksys_get_mouse_eventstate();
debug_printf("mouse ev (%d,%d)%x\n", mouse_pos.x, mouse_pos.y, mouse_button);
if (mouse_button & (1<<24)) // double click
if (mouse_button & (1 << 24)) // double click
{
int n = (mouse_pos.y - 60) / FONT_H;
if (n < 0 || n >= LINES) break;
if (n < 0 || n >= LINES)
break;
debug_printf("click on str(%d), clip slot(%d)\n", n, LINES - n - 1);
sprintf(statusbar, "click on str(%d), clip slot(%d)\n", n, LINES - n - 1);
draw_window();
}
break;
}
} while(1) ; /* End of main activity loop */
} while (1); /* End of main activity loop */
return 0;
return 0;
}
@@ -1,11 +1,11 @@
// BOXLIB EXAMPLE (scrollbar, progressbar, editbox and checkbox)
// Writed by maxcodehack and superturbocat2001
#include <sys/ksys.h>
#include <stdlib.h>
#include <string.h>
#include <clayer/boxlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ksys.h>
#define WIN_W 640
#define WIN_H 563
@@ -16,75 +16,77 @@
#define SCROLL_MAX_LEN 215
#define BLACK 0x000000
#define WHITE 0xFFFFFF
#define BLUE 0x0000FF
#define X_W(X, W) ((X<<16)+W)
#define BLUE 0x0000FF
#define X_W(X, W) ((X << 16) + W)
#define Y_H X_W
uint32_t wheels;
char* title = "Boxlib example";
char ed_buff[ED_BUFF_LEN];
scrollbar scroll = {15, WIN_W - 26, WIN_H - 29, 0, 0, 2, 215, SCROLL_BUTTON_SIZE, 0,0x707070,0xD2CED0,0x555555};
progressbar pg = {0, 10, 10, 270, 35, 1, 0, 200, 0xB4B4B4, 0x2728FF, 0xA9A9A9};
edit_box ed={WIN_W-140,10,60,0xFFFFFF,0x6a9480,0,0x6a9480, BLACK | TEXT_SIZE, ED_BUFF_LEN, ed_buff,NULL,ed_focus};
check_box output_off={X_W(10, 15), Y_H(120,15), 10, WHITE, BLUE, BLACK | TEXT_SIZE, "Disable duplicate output",0};
void draw_window(){
_ksys_start_draw();
_ksys_create_window(215,100,WIN_W,WIN_H,title, 0x858585, 0x34);
edit_box_draw(&ed);
check_box_draw2(&output_off);
if(!output_off.flags){
_ksys_draw_text(ed_buff, 10, 90, strlen(ed_buff), BLACK | TEXT_SIZE);
}
scrollbar_v_draw(&scroll);
scrollbar scroll = { 15, WIN_W - 26, WIN_H - 29, 0, 0, 2, 215, SCROLL_BUTTON_SIZE, 0, 0x707070, 0xD2CED0, 0x555555 };
progressbar pg = { 0, 10, 10, 270, 35, 1, 0, 200, 0xB4B4B4, 0x2728FF, 0xA9A9A9 };
edit_box ed = { WIN_W - 140, 10, 60, 0xFFFFFF, 0x6a9480, 0, 0x6a9480, BLACK | TEXT_SIZE, ED_BUFF_LEN, ed_buff, NULL, ed_focus };
check_box output_off = { X_W(10, 15), Y_H(120, 15), 10, WHITE, BLUE, BLACK | TEXT_SIZE, "Disable duplicate output", 0 };
void draw_window()
{
_ksys_start_draw();
_ksys_create_window(215, 100, WIN_W, WIN_H, title, 0x858585, 0x34);
edit_box_draw(&ed);
check_box_draw2(&output_off);
if (!output_off.flags) {
_ksys_draw_text(ed_buff, 10, 90, strlen(ed_buff), BLACK | TEXT_SIZE);
}
scrollbar_v_draw(&scroll);
progressbar_draw(&pg);
_ksys_end_draw();
_ksys_end_draw();
}
int main()
{
init_checkbox2(&output_off);
_ksys_set_event_mask(KSYS_EVM_REDRAW + KSYS_EVM_KEY + KSYS_EVM_BUTTON + KSYS_EVM_MOUSE+ KSYS_EVM_MOUSE_FILTER);
while(1){
switch(_ksys_get_event()){
case KSYS_EVENT_BUTTON:
if (_ksys_get_button() == 1) return 0;
break;
case KSYS_EVENT_KEY:
edit_box_key_safe(&ed, _ksys_get_key());
init_checkbox2(&output_off);
_ksys_set_event_mask(KSYS_EVM_REDRAW + KSYS_EVM_KEY + KSYS_EVM_BUTTON + KSYS_EVM_MOUSE + KSYS_EVM_MOUSE_FILTER);
while (1) {
switch (_ksys_get_event()) {
case KSYS_EVENT_BUTTON:
if (_ksys_get_button() == 1)
return 0;
break;
case KSYS_EVENT_KEY:
edit_box_key_safe(&ed, _ksys_get_key());
draw_window();
break;
case KSYS_EVENT_REDRAW:
draw_window();
break;
case KSYS_EVENT_MOUSE:
edit_box_mouse(&ed);
scrollbar_v_mouse(&scroll);
pg.value = scroll.position;
progressbar_draw(&pg);
check_box_mouse2(&output_off);
unsigned int scroll_strong = 10;
break;
case KSYS_EVENT_REDRAW:
draw_window();
break;
case KSYS_EVENT_MOUSE:
edit_box_mouse(&ed);
scrollbar_v_mouse(&scroll);
pg.value = scroll.position;
progressbar_draw(&pg);
check_box_mouse2(&output_off);
unsigned int scroll_strong = 10;
wheels = _ksys_get_mouse_wheels();
if(wheels & 0xFFFF){
if((short)wheels > 0){
scroll.position += scroll_strong;
if(scroll.position>scroll.max_area-scroll.cur_area){
scroll.position=scroll.max_area-scroll.cur_area;
}
}else if((short)wheels < 0 && scroll.position > 0){
scroll.position -= scroll_strong;
if((int)scroll.position<0){
scroll.position=0;
}
}
scrollbar_v_draw(&scroll);
if (wheels & 0xFFFF) {
if ((short)wheels > 0) {
scroll.position += scroll_strong;
if (scroll.position > scroll.max_area - scroll.cur_area) {
scroll.position = scroll.max_area - scroll.cur_area;
}
} else if ((short)wheels < 0 && scroll.position > 0) {
scroll.position -= scroll_strong;
if ((int)scroll.position < 0) {
scroll.position = 0;
}
}
scrollbar_v_draw(&scroll);
}
break;
}
}
return 0;
break;
}
}
return 0;
}
@@ -1,28 +1,28 @@
#include <sys/ksys.h>
#include <clayer/dialog.h>
#include <stdio.h>
#include <sys/ksys.h>
int main()
{
open_dialog *dlg_open = kolibri_new_open_dialog(OPEN, 10, 10, 420, 320); // create opendialog struct
OpenDialog_init(dlg_open); // Initializing an open dialog box.
OpenDialog_start(dlg_open); // Show open dialog box
color_dialog *color_select = kolibri_new_color_dialog(SELECT, 10, 10,420,320); // create colordialog struct
ColorDialog_init(color_select); // Initializing an color dialog box.
ColorDialog_start(color_select); // Show color dialog
if(dlg_open->status == SUCCESS){
printf("File selected '%s'\n",dlg_open->openfile_path);
}else{
open_dialog* dlg_open = kolibri_new_open_dialog(OPEN, 10, 10, 420, 320); // create opendialog struct
OpenDialog_init(dlg_open); // Initializing an open dialog box.
OpenDialog_start(dlg_open); // Show open dialog box
color_dialog* color_select = kolibri_new_color_dialog(SELECT, 10, 10, 420, 320); // create colordialog struct
ColorDialog_init(color_select); // Initializing an color dialog box.
ColorDialog_start(color_select); // Show color dialog
if (dlg_open->status == SUCCESS) {
printf("File selected '%s'\n", dlg_open->openfile_path);
} else {
puts("No file selected!");
}
if(color_select->status == SUCCESS){
printf("Color selected: #%06X\n",color_select->color);
if (color_select->status == SUCCESS) {
printf("Color selected: #%06X\n", color_select->color);
rgb_t color_rgb = (rgb_t)color_select->color;
printf("Red:%d Green:%d Blue:%d", color_rgb.red, color_rgb.green, color_rgb.blue);
}else{
printf("Red:%d Green:%d Blue:%d", color_rgb.red, color_rgb.green, color_rgb.blue);
} else {
puts("No color selected!");
}
free(dlg_open);
@@ -1,23 +1,24 @@
/* Written by turbocat2001 (Logaev Maxim) */
#include <clayer/libimg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <clayer/libimg.h>
#include <sys/ksys.h>
#define NEW_IMG_H 128
#define NEW_IMG_H 128
#define NEW_IMG_W 128
#define IMG_H 256
#define IMG_W 256
Image *image_blend; // Create image struct
Image* image_blend; // Create image struct
ksys_colors_table_t sys_color_table; // Create system colors table
ksys_colors_table_t sys_color_table; // Create system colors table
void* load_img(char* fname, uint32_t* read_sz){ // Image file upload function
FILE *f = fopen(fname, "rb");
void* load_img(char* fname, uint32_t* read_sz)
{ // Image file upload function
FILE* f = fopen(fname, "rb");
if (!f) {
printf("Can't open file: %s\n", fname);
return NULL;
@@ -29,7 +30,7 @@ void* load_img(char* fname, uint32_t* read_sz){ // Image file upload function
int filesize = ftell(f);
rewind(f);
void* fdata = malloc(filesize);
if(!fdata) {
if (!fdata) {
printf("No memory for file %s\n", fname);
return NULL;
}
@@ -42,55 +43,57 @@ void* load_img(char* fname, uint32_t* read_sz){ // Image file upload function
return fdata;
}
void draw_gui(){
void draw_gui()
{
_ksys_start_draw();
_ksys_create_window(10, 40, (IMG_W+NEW_IMG_W)+50, IMG_H+50, "Libimg", sys_color_table.work_area, 0x34);
img_draw(image_blend, 10, 10, IMG_W*2, IMG_H , 0, 0); // Draw blended image to window
_ksys_create_window(10, 40, (IMG_W + NEW_IMG_W) + 50, IMG_H + 50, "Libimg", sys_color_table.work_area, 0x34);
img_draw(image_blend, 10, 10, IMG_W * 2, IMG_H, 0, 0); // Draw blended image to window
_ksys_end_draw();
}
int main(){
int main()
{
_ksys_get_system_colors(&sys_color_table); // Get system colors theme
_ksys_set_event_mask(0xC0000027);
uint32_t img_size;
void *file_data = load_img("logo.png", &img_size); // Get RAW data and size
if(!file_data){
uint32_t img_size;
void* file_data = load_img("logo.png", &img_size); // Get RAW data and size
if (!file_data) {
return 1;
}
Image* image = img_decode(file_data, img_size, 0); // Decode RAW data to Image data
if (image->Type != IMAGE_BPP32) {
if (image->Type != IMAGE_BPP32) {
image = img_convert(image, NULL, IMAGE_BPP32, 0, 0); // Convert image to format BPP32
if (!image) {
printf("Сonvert error!: \n");
printf("Сonvert error!: \n");
return 1;
}
}
image_blend = img_create(IMG_W+NEW_IMG_W, IMG_H, IMAGE_BPP32); // Create an empty layer
img_fill_color(image_blend, IMG_W+NEW_IMG_W, IMG_H, sys_color_table.work_area); // Fill the layer with one color
img_blend(image_blend, image, 0, 0, 0, 0, IMG_W, IMG_H); // Blending images to display the alpha channel.
image_blend = img_create(IMG_W + NEW_IMG_W, IMG_H, IMAGE_BPP32); // Create an empty layer
img_fill_color(image_blend, IMG_W + NEW_IMG_W, IMG_H, sys_color_table.work_area); // Fill the layer with one color
img_blend(image_blend, image, 0, 0, 0, 0, IMG_W, IMG_H); // Blending images to display the alpha channel.
/* Reduce image size from 256x256 to 128x128 */
image = img_scale(image, 0, 0, IMG_W, IMG_H, NULL, LIBIMG_SCALE_STRETCH , LIBIMG_INTER_BILINEAR, NEW_IMG_W, NEW_IMG_H);
img_blend(image_blend, image, 256, 0, 0, 0, NEW_IMG_W, NEW_IMG_H);
img_destroy(image); // Destroy image structure
free(file_data); // Free allocated file_data buffer
image = img_scale(image, 0, 0, IMG_W, IMG_H, NULL, LIBIMG_SCALE_STRETCH, LIBIMG_INTER_BILINEAR, NEW_IMG_W, NEW_IMG_H);
img_blend(image_blend, image, 256, 0, 0, 0, NEW_IMG_W, NEW_IMG_H);
img_destroy(image); // Destroy image structure
free(file_data); // Free allocated file_data buffer
/* Main event loop */
while (1) {
switch(_ksys_get_event()){
case KSYS_EVENT_REDRAW:
draw_gui();
break;
case KSYS_EVENT_BUTTON:
if (_ksys_get_button()==1){
return 0;
}
break;
while (1) {
switch (_ksys_get_event()) {
case KSYS_EVENT_REDRAW:
draw_gui();
break;
case KSYS_EVENT_BUTTON:
if (_ksys_get_button() == 1) {
return 0;
}
break;
}
}
return 0;
}
return 0;
}
@@ -2,7 +2,7 @@
int main()
{
msgbox *msg1 = NULL;
msgbox* msg1 = NULL;
msg1 = kolibri_new_msgbox("Title", "Text in window", 0, "Ok");
kolibri_start_msgbox(msg1, NULL);
}
@@ -1,8 +1,7 @@
#include <stdlib.h>
#include <clayer/rasterworks.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ksys.h>
#include <clayer/rasterworks.h>
// Sizes
int x_size = 768, y_size = 256;
@@ -14,38 +13,39 @@ int main()
{
// Count length
int ln_str = countUTF8Z(string, -1);
// Create image buffer
void *buffi = malloc(x_size * y_size * 3 * sizeof(char) + 8);
void* buffi = malloc(x_size * y_size * 3 * sizeof(char) + 8);
// Set sizes
*((int*)buffi) = x_size;
*((int*)buffi+1) = y_size;
*((int*)buffi + 1) = y_size;
// Fill color
memset((char*)buffi + 8, 0xFF, x_size * y_size * 3);
// Draw text on buffer
drawText(buffi, 5, 0, string, ln_str, 0xFF000000, 0x30C18);
drawText(buffi, 5, 32, string, ln_str, 0xFF000000, 0x1030C18);
drawText(buffi, 5, 64, string, ln_str, 0xFF000000, 0x2030C18);
drawText(buffi, 5, 96, string, ln_str, 0xFF000000, 0x4030C18);
drawText(buffi, 5, 0, string, ln_str, 0xFF000000, 0x30C18);
drawText(buffi, 5, 32, string, ln_str, 0xFF000000, 0x1030C18);
drawText(buffi, 5, 64, string, ln_str, 0xFF000000, 0x2030C18);
drawText(buffi, 5, 96, string, ln_str, 0xFF000000, 0x4030C18);
drawText(buffi, 5, 128, string, ln_str, 0xFF000000, 0x8030C18);
drawText(buffi, 5, 160, string, ln_str, 0xFF000000, 0x0F031428);
while (1) {
switch (_ksys_get_event()){
case KSYS_EVENT_REDRAW:
_ksys_start_draw();
_ksys_create_window(50, 50, 800, 300, "Rasterworks Example", 0x999999, 0x34);
_ksys_draw_bitmap(buffi + 8, 10, 10, 768, 256);
_ksys_end_draw();
break;
case KSYS_EVENT_BUTTON:
if(_ksys_get_button() == 1) exit(0);
break;
};
while (1) {
switch (_ksys_get_event()) {
case KSYS_EVENT_REDRAW:
_ksys_start_draw();
_ksys_create_window(50, 50, 800, 300, "Rasterworks Example", 0x999999, 0x34);
_ksys_draw_bitmap(buffi + 8, 10, 10, 768, 256);
_ksys_end_draw();
break;
case KSYS_EVENT_BUTTON:
if (_ksys_get_button() == 1)
exit(0);
break;
};
}
return 0;
}
@@ -5,13 +5,14 @@
int main()
{
if(con_init()) return 1; // init fail
(*con_set_title)("Console colors");
if (con_init())
return 1; // init fail
(*con_set_tsitle)("Console colors");
// con_write_asciiz("\033[0;31;42m test \n"); // red on green bk
// con_write_asciiz("\033[0;31;42m test \n"); // red on green bk
for(int i = 30; i < 48; i++){
(*con_printf)("\033[%dmColor 0x%02X: ", i, i);
for (int i = 30; i < 48; i++) {
(*con_printf)("\033[%dmColor 0x%02X: ", i, i);
(*con_write_asciiz)("Text sample.");
(*con_printf)(" printf %s test %d\n", "small", i);
}
@@ -1,7 +1,7 @@
#include "stddef.h"
#include <libgen.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>
#include <string.h>
#include <sys/ksys.h>
@@ -12,38 +12,40 @@ enum ARGV_FILE {
OUT = 2
};
void show_help(void){
void show_help(void)
{
puts("Usage: defgen [lib.obj] [lib.def]");
}
int main(int argc, char** argv){
int main(int argc, char** argv)
{
if(argc!=ARGC_VALID){
if (argc != ARGC_VALID) {
show_help();
return 0;
}
ksys_dll_t *obj_dll = _ksys_dlopen(argv[IN]);
ksys_dll_t* obj_dll = _ksys_dlopen(argv[IN]);
FILE* outfile = fopen(argv[OUT], "w");
if(!obj_dll){
if (!obj_dll) {
printf("File '%s' not found!\n", argv[IN]);
return 1;
}
if(!outfile){
if (!outfile) {
printf("Unable to create file:'%s'!\n", argv[OUT]);
return 2;
}
fprintf(outfile, "LIBRARY %s\n\n", basename(argv[IN]));
fputs("EXPORTS\n", outfile);
int i=0;
while(obj_dll[i].func_name){
fprintf(outfile,"%s\n", obj_dll[i].func_name);
int i = 0;
while (obj_dll[i].func_name) {
fprintf(outfile, "%s\n", obj_dll[i].func_name);
i++;
}
fclose(outfile);
fclose(outfile);
return 0;
}
@@ -1,45 +1,44 @@
#include <sys/dirent.h>
#include <sys/dir.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <sys/dir.h>
#include <sys/dirent.h>
const char *folder_type = "Folder";
const char *file_type = "File";
const char* folder_type = "Folder";
const char* file_type = "File";
int main()
{
char *path=getcwd(NULL, PATH_MAX);
printf("Current directory: %s\n", path);
if(mkdir("test")){
puts("Test folder created!");
}
else{
char* path = getcwd(NULL, PATH_MAX);
printf("Current directory: %s\n", path);
if (mkdir("test")) {
puts("Test folder created!");
} else {
puts("Error creating folder!");
}
DIR *mydir = opendir(path);
if(!mydir){
DIR* mydir = opendir(path);
if (!mydir) {
puts("File system error.");
return -1;
}
struct dirent *file_info;
char *str_type=NULL;
struct dirent* file_info;
char* str_type = NULL;
putc(' ');
while((file_info = readdir(mydir))!=NULL){
if(file_info->d_type==IS_FOLDER){
while ((file_info = readdir(mydir)) != NULL) {
if (file_info->d_type == IS_FOLDER) {
(*con_set_flags)(CON_COLOR_GREEN);
str_type = (char*)folder_type;
}else {
} else {
(*con_set_flags)(7);
str_type = (char*)file_type;
}
printf("%3d %20s %s\n ", file_info->d_ino ,file_info->d_name, str_type);
printf("%3d %20s %s\n ", file_info->d_ino, file_info->d_name, str_type);
};
setcwd("/sys/develop");
path=getcwd(NULL, PATH_MAX);
path = getcwd(NULL, PATH_MAX);
printf("Move to the directory: %s\n", path);
free(path);
}
@@ -1,50 +1,50 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define READ_MAX 255
static char test_str1[] = "123454567890abcdefghijklmnopqrstufvwxyz";
static char test_str2[READ_MAX];
int main(int argc, char **argv)
int main(int argc, char** argv)
{
int i=0;
FILE *f;
int i = 0;
FILE* f;
//write to file
debug_printf("Write file...\n");
f=fopen("testfile.txt","w");
// write to file
debug_printf("Write file...\n");
f = fopen("testfile.txt", "w");
while(test_str1[i]!='a'){
fputc(test_str1[i],f);
i++;
}
fclose(f);
while (test_str1[i] != 'a') {
fputc(test_str1[i], f);
i++;
}
fclose(f);
//append to file
debug_printf("Apend file...\n");
f=fopen("testfile.txt","a");
fputs(test_str1+i,f);
char null_term = '\0';
fwrite(&null_term, sizeof(char), 1, f);
printf("Error: %s\n",strerror(errno));
fclose(f);
// append to file
debug_printf("Apend file...\n");
f = fopen("testfile.txt", "a");
fputs(test_str1 + i, f);
char null_term = '\0';
fwrite(&null_term, sizeof(char), 1, f);
printf("Error: %s\n", strerror(errno));
fclose(f);
//copy from testfile.txt to copyfile.txt
debug_printf("Read file...\n");
f=fopen("testfile.txt","r");
i=0;
while((test_str2[i]=fgetc(f))!=EOF && i<READ_MAX){
fputc(test_str2[i], stdout);
i++;
}
printf("\n%s\n", test_str1);
if(!strcmp(test_str2, test_str1)){
puts("TEST: OK!");
}else{
puts("TEST: FAIL!");
}
fclose(f);
// copy from testfile.txt to copyfile.txt
debug_printf("Read file...\n");
f = fopen("testfile.txt", "r");
i = 0;
while ((test_str2[i] = fgetc(f)) != EOF && i < READ_MAX) {
fputc(test_str2[i], stdout);
i++;
}
printf("\n%s\n", test_str1);
if (!strcmp(test_str2, test_str1)) {
puts("TEST: OK!");
} else {
puts("TEST: FAIL!");
}
fclose(f);
}
@@ -1,25 +1,27 @@
#include <clayer/network.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char *host = "kolibrios.org";
int main()
{
char* host = "kolibrios.org";
int port = 80;
printf("Connecting to %s on port %d\n", host, port);
struct addrinfo *addr_info;
char port_str[16]; sprintf(port_str, "%d", port);
struct addrinfo* addr_info;
char port_str[16];
sprintf(port_str, "%d", port);
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; // IPv4 or IPv6 doesnt matter
hints.ai_family = AF_UNSPEC; // IPv4 or IPv6 doesnt matter
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
if (getaddrinfo(host, port_str, 0, &addr_info) != 0) {
printf("Host %s not found!\n", host);
freeaddrinfo(addr_info);
exit(-1);
}
printf("IP address of %s is %s\n", host, inet_ntoa(addr_info->ai_addr->sin_addr));
//printf("Host port = %d\n", addr_info->ai_addr->sin_port >> 8);
printf("IP address of %s is %s\n", host, inet_ntoa(addr_info->ai_addr->sin_addr));
// printf("Host port = %d\n", addr_info->ai_addr->sin_port >> 8);
char request[256];
sprintf(request, "GET /en/ HTTP/1.1\r\nHost: %s\r\n\r\n", host);
@@ -29,22 +31,22 @@ int main() {
puts("Connecting...\n");
if (connect(sock, addr_info->ai_addr, addr_info->ai_addrlen) != 0) {
printf("Connection failed, errno = %d\n", errno);
exit(errno);
printf("Connection failed, errno = %d\n", errno);
exit(errno);
}
puts("Connected successfully\n");
puts("Sending request...\n");
if (send(sock, request, strlen(request), MSG_NOFLAG) == -1) {
printf("Sending failed, errno = %d\n", errno);
exit(errno);
printf("Sending failed, errno = %d\n", errno);
exit(errno);
}
puts("Request sended successfully, waiting for response...\n");
char buf[512 + 1];
if (recv(sock, buf, 512, MSG_NOFLAG) == -1) {
printf("Receive failed, errno = %d\n", errno);
exit(errno);
printf("Receive failed, errno = %d\n", errno);
exit(errno);
}
printf("Response = %s\n", buf);
@@ -1,15 +1,17 @@
#include <sys/ksys.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ksys.h>
#include <time.h>
int comp(void *a, void *b) {
int comp(void* a, void* b)
{
return *(int*)a - *(int*)b;
}
int main(){
int main()
{
puts("Start testing.");
assert(NULL == ((void*)0));
assert(RAND_MAX == 65535);
@@ -45,11 +47,11 @@ int main(){
assert(output3.quot == 6);
assert(output3.rem == 3);
char *st2 = malloc(sizeof(char)*2);
char* st2 = malloc(sizeof(char) * 2);
assert(st2 != NULL);
st2[0] = 'H';
st2[1] = 'i';
st2 = realloc(st2, sizeof(char)*3);
st2 = realloc(st2, sizeof(char) * 3);
st2[2] = '!';
assert(!strcmp(st2, "Hi!"));
free(st2);
@@ -61,8 +63,8 @@ int main(){
assert(!strcmp(st2, "Hi"));
free(st2);
char *start = "100.00 Rub";
char *end;
char* start = "100.00 Rub";
char* end;
assert(strtol(start, &end, 10) == 100L);
assert(!strcmp(end, ".00 Rub"));
@@ -70,17 +72,17 @@ int main(){
assert(strtod(start, &end) == 100.0);
assert(!strcmp(end, " Rub"));
char *st3 = "21.3e3Hello World!";
char* st3 = "21.3e3Hello World!";
assert(atof(st3) == 21300.0);
int nums[10] = {5, 3, 9, 1, 8, 4, 2, 0, 7, 6};
qsort(nums, 10, sizeof(int), (int(*) (const void *, const void *))comp);
for (int i = 0; i < 10; i++) {
int nums[10] = { 5, 3, 9, 1, 8, 4, 2, 0, 7, 6 };
qsort(nums, 10, sizeof(int), (int (*)(const void*, const void*))comp);
for (int i = 0; i < 10; i++) {
assert(nums[i] == i);
}
time_t libc_time = time(NULL);
struct tm *libc_tm = localtime(&libc_time);
struct tm* libc_tm = localtime(&libc_time);
printf(asctime(libc_tm));
puts("End testing.");
@@ -1,43 +1,41 @@
#include <stdio.h>
#include <math.h>
#include <stdio.h>
int main()
{
int i;
for (i = 0; i < 20; i++)
{
printf("------------------------------------------------------\n");
// printf ( "remainder of 5.3 / 2 is %f\n", remainder (5.3,2) );
// printf ( "remainder of 18.5 / 4.2 is %f\n", remainder (18.5,4.2) );
//remainder of 5.3 / 2 is -0.700000
//remainder of 18.5 / 4.2 is 1.700000
{
int i;
for (i = 0; i < 20; i++) {
printf("------------------------------------------------------\n");
// printf ( "remainder of 5.3 / 2 is %f\n", remainder (5.3,2) );
// printf ( "remainder of 18.5 / 4.2 is %f\n", remainder (18.5,4.2) );
// remainder of 5.3 / 2 is -0.700000
// remainder of 18.5 / 4.2 is 1.700000
printf ( "fmod of 5.3 / 2 is %f\n", fmod (5.3,2) );
printf ( "fmod of 18.5 / 4.2 is %f\n", fmod (18.5,4.2) );
// fmod of 5.3 / 2 is 1.300000
// fmod of 18.5 / 4.2 is 1.700000
printf("fmod of 5.3 / 2 is %f\n", fmod(5.3, 2));
printf("fmod of 18.5 / 4.2 is %f\n", fmod(18.5, 4.2));
// fmod of 5.3 / 2 is 1.300000
// fmod of 18.5 / 4.2 is 1.700000
double param, fractpart, intpart, result;
int n;
double param, fractpart, intpart, result;
int n;
param = 3.14159265;
fractpart = modf (param , &intpart);
printf ("%f = %f + %f \n", param, intpart, fractpart);
//3.141593 = 3.000000 + 0.141593
param = 3.14159265;
fractpart = modf(param, &intpart);
printf("%f = %f + %f \n", param, intpart, fractpart);
// 3.141593 = 3.000000 + 0.141593
param = 0.95;
n = 4;
result = ldexp (param , n);
printf ("%f * 2^%d = %f\n", param, n, result);
//0.950000 * 2^4 = 15.200000
param = 0.95;
n = 4;
result = ldexp(param, n);
printf("%f * 2^%d = %f\n", param, n, result);
// 0.950000 * 2^4 = 15.200000
param = 8.0;
result = frexp (param , &n);
printf ("%f = %f * 2^%d\n", param, result, n);
//8.000000 = 0.500000 * 2^4
param = 50;
result = frexp (param , &n);
printf ("%f = %f * 2^%d\n", param, result, n);
}
param = 8.0;
result = frexp(param, &n);
printf("%f = %f * 2^%d\n", param, result, n);
// 8.000000 = 0.500000 * 2^4
param = 50;
result = frexp(param, &n);
printf("%f = %f * 2^%d\n", param, result, n);
}
}
@@ -1,22 +1,23 @@
/*
* This is an example program for sending a message through a "pipe".
* This is an example program for sending a message through a "pipe".
* Created by turbocat (Maxim Logaev) 2022.
*/
*/
#include <sys/ksys.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <sys/ksys.h>
#define TH_STACK_SIZE 1024
#define MESSAGE_SIZE 12
#define MESSAGE_SIZE 12
ksys_colors_table_t sys_colors;
int pipefd[2];
char *send_message = "HELLO PIPE!";
char* send_message = "HELLO PIPE!";
void tmain() {
void tmain()
{
char recv_message[MESSAGE_SIZE];
_ksys_posix_read(pipefd[0], recv_message, MESSAGE_SIZE);
printf("RECV: %s\n", recv_message);
@@ -25,14 +26,15 @@ void tmain() {
exit(0);
}
void create_thread(void){
unsigned tid; // New thread ID
void *th_stack = malloc(TH_STACK_SIZE); // Allocate memory for thread stack
void create_thread(void)
{
unsigned tid; // New thread ID
void* th_stack = malloc(TH_STACK_SIZE); // Allocate memory for thread stack
if (!th_stack) {
puts("Memory allocation error for thread!");
return;
}
tid = _ksys_create_thread(tmain, th_stack+TH_STACK_SIZE); // Create new thread with entry "main"
tid = _ksys_create_thread(tmain, th_stack + TH_STACK_SIZE); // Create new thread with entry "main"
if (tid == -1) {
puts("Unable to create a new thread!");
return;
@@ -40,7 +42,8 @@ void create_thread(void){
printf("New thread created (TID=%u)\n", tid);
}
void main() {
void main()
{
if (_ksys_posix_pipe2(pipefd, 0)) {
puts("Pipe creation error!");
return;
@@ -5,75 +5,69 @@
#define HEIGHT 480
#define BPP 4
#define DEPTH 32
void setpixel(SDL_Surface *screen, int x, int y, Uint8 r, Uint8 g, Uint8 b)
void setpixel(SDL_Surface* screen, int x, int y, Uint8 r, Uint8 g, Uint8 b)
{
Uint32 *pixmem32;
Uint32 colour;
colour = SDL_MapRGB( screen->format, r, g, b );
pixmem32 = (Uint32*) screen->pixels + y + x;
Uint32* pixmem32;
Uint32 colour;
colour = SDL_MapRGB(screen->format, r, g, b);
pixmem32 = (Uint32*)screen->pixels + y + x;
*pixmem32 = colour;
}
void DrawScreen(SDL_Surface* screen, int h)
{
int x, y, ytimesw;
if(SDL_MUSTLOCK(screen))
{
if(SDL_LockSurface(screen) < 0) return;
if (SDL_MUSTLOCK(screen)) {
if (SDL_LockSurface(screen) < 0)
return;
}
for(y = 0; y < screen->h; y++ )
{
ytimesw = y*screen->pitch/BPP;
for( x = 0; x < screen->w; x++ )
{
setpixel(screen, x, ytimesw, (x*x)/256+3*y+h, (y*y)/256+x+h, h);
for (y = 0; y < screen->h; y++) {
ytimesw = y * screen->pitch / BPP;
for (x = 0; x < screen->w; x++) {
setpixel(screen, x, ytimesw, (x * x) / 256 + 3 * y + h, (y * y) / 256 + x + h, h);
}
}
if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
if (SDL_MUSTLOCK(screen))
SDL_UnlockSurface(screen);
SDL_Flip(screen);
}
int main(int argc, char* argv[])
{
SDL_Surface *screen;
SDL_Surface* screen;
SDL_Event event;
int keypress = 0;
int h=0;
if (SDL_Init(SDL_INIT_VIDEO) < 0 ) return 1;
if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, DEPTH, SDL_FULLSCREEN|SDL_HWSURFACE)))
{
int h = 0;
if (SDL_Init(SDL_INIT_VIDEO) < 0)
return 1;
if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, DEPTH, SDL_FULLSCREEN | SDL_HWSURFACE))) {
SDL_Quit();
return 1;
}
while(!keypress)
{
DrawScreen(screen,h++);
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
keypress = 1;
break;
case SDL_KEYDOWN:
keypress = 1;
break;
}
}
while (!keypress) {
DrawScreen(screen, h++);
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
keypress = 1;
break;
case SDL_KEYDOWN:
keypress = 1;
break;
}
}
}
SDL_Quit();
return 0;
}
@@ -1,17 +1,18 @@
#include "../include/shell_api.h"
#include "../include/shell_api.h"
char string[256];
int main(){
int main()
{
shell_cls();
shell_printf("SHELL PID=%d\n\r", shell_get_pid());
shell_puts("This is a test console application for Shell\n\r");
shell_puts("Type a string (255 symbols max): ");
shell_gets(string, 255);
shell_printf("You typed: %s\n\r", string);
shell_puts("Press any key: ");
string[0] = shell_getc();
shell_printf("\n\rYou pressed: %c", string[0]);
@@ -1,12 +1,13 @@
#include <sys/ksys.h>
#include <stdio.h>
#include <limits.h>
#include <stdio.h>
#include <sys/ksys.h>
char* test_string1 = "Hello world!";
int a, b;
int main(int argc, char** argv){
sscanf("43 53","%d %d",&a, &b);
int main(int argc, char** argv)
{
sscanf("43 53", "%d %d", &a, &b);
printf("(43 53) = (%d %d)\n", a, b);
printf("Hello world! = %s\n", test_string1);
printf("345.358980 = %f\n", 345.35898);
@@ -1,17 +1,18 @@
#include <ctype.h>
#include <errno.h>
#include <sys/ksys.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <sys/ksys.h>
int main(int argc, char** argv){
char hello1[]="Hello, KolibriOS!";
int main(int argc, char** argv)
{
char hello1[] = "Hello, KolibriOS!";
char hello2[20];
memcpy(hello1, hello2, strlen(hello1));
if(!__strcmp(hello1, hello2)){
if (!__strcmp(hello1, hello2)) {
printf("memcpy: Successfully!\n");
return 0;
} else{
} else {
printf("memcpy: Failure\n");
return -1;
}
@@ -1,18 +1,18 @@
/*
* An example of using threads to create a copy of a window.
* An example of using threads to create a copy of a window.
* Built on top of the /programs/develop/examples/thread/trunk/thread.asm example.
*
*
* Created by turbocat (Maxim Logaev) 2021.
*/
*/
#include <sys/ksys.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ksys.h>
#define TH_STACK_SIZE 1024
enum BUTTONS{
BTN_QUIT = 1,
enum BUTTONS {
BTN_QUIT = 1,
BTN_CREATE_TH = 2,
};
@@ -20,43 +20,46 @@ ksys_colors_table_t sys_colors;
extern int main();
void redraw_window(void){
ksys_pos_t mouse_pos = _ksys_get_mouse_pos(KSYS_MOUSE_SCREEN_POS);
void redraw_window(void)
{
ksys_pos_t mouse_pos = _ksys_get_mouse_pos(KSYS_MOUSE_SCREEN_POS);
_ksys_start_draw();
_ksys_create_window(mouse_pos.x, mouse_pos.y, 140, 60, "Threads", sys_colors.work_area, 0x14);
_ksys_define_button(10, 30, 120, 20, BTN_CREATE_TH, sys_colors.work_button);
_ksys_define_button(10, 30, 120, 20, BTN_CREATE_TH, sys_colors.work_button);
_ksys_draw_text("Create thread!", 15, 34, 0, 0x90000000 | sys_colors.work_button_text);
_ksys_end_draw();
}
void create_thread(void){
unsigned tid; // New thread ID
void *th_stack = malloc(TH_STACK_SIZE); // Allocate memory for thread stack
if(!th_stack){
_ksys_debug_puts("Memory allocation error for thread!");
return;
void create_thread(void)
{
unsigned tid; // New thread ID
void* th_stack = malloc(TH_STACK_SIZE); // Allocate memory for thread stack
if (!th_stack) {
_ksys_debug_puts("Memory allocation error for thread!");
return;
}
tid = _ksys_create_thread(main, th_stack+TH_STACK_SIZE); // Create new thread with entry "main"
if(tid==-1){
tid = _ksys_create_thread(main, th_stack + TH_STACK_SIZE); // Create new thread with entry "main"
if (tid == -1) {
_ksys_debug_puts("Unable to create a new thread!");
return;
}
debug_printf("New thread created (TID=%u)\n", tid);
}
int main(){
int main()
{
_ksys_get_system_colors(&sys_colors);
int gui_event;
redraw_window();
while(1){
gui_event = _ksys_get_event();
switch(gui_event){
while (1) {
gui_event = _ksys_get_event();
switch (gui_event) {
case KSYS_EVENT_REDRAW:
redraw_window();
break;
case KSYS_EVENT_BUTTON:
switch (_ksys_get_button()){
switch (_ksys_get_button()) {
case BTN_CREATE_TH:
create_thread();
break;
@@ -64,6 +67,6 @@ int main(){
_ksys_exit();
}
break;
}
}
}
}
}
@@ -1,42 +1,43 @@
#include "stddef.h"
#include <sys/ksys.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ksys.h>
#define DEV_ADD_DISK 1
#define TMPDISK_SIZE 10 //Mb
#define TMPDISK_SIZE 10 // Mb
#pragma pack(push, 1)
struct{
unsigned disk_size;
unsigned char disk_id;
}tmpdisk_add;
struct {
unsigned disk_size;
unsigned char disk_id;
} tmpdisk_add;
#pragma pack(pop)
char *tmpdisk_res_text[]={
"TmpDisk operation completed successfully",
"Unknown IOCTL code, wrong input/output size...",
"DiskId must be from 0 to 9",
"DiskSize is too large",
"DiskSize is too small, might be too little free RAM",
"Memory allocation failed",
"Unknown error O_o",
0};
char* tmpdisk_res_text[] = {
"TmpDisk operation completed successfully",
"Unknown IOCTL code, wrong input/output size...",
"DiskId must be from 0 to 9",
"DiskSize is too large",
"DiskSize is too small, might be too little free RAM",
"Memory allocation failed",
"Unknown error O_o",
0
};
int main(){
int main()
{
ksys_drv_hand_t tmpdisk_drv = _ksys_load_driver("tmpdisk");
if(!tmpdisk_drv){
if (!tmpdisk_drv) {
puts("tmpdisk.sys driver not load!");
exit(0);
}else{
} else {
puts("tmpdisk.sys driver is load!");
}
tmpdisk_add.disk_size = TMPDISK_SIZE*1024*1024/512;
tmpdisk_add.disk_size = TMPDISK_SIZE * 1024 * 1024 / 512;
tmpdisk_add.disk_id = 5;
ksys_ioctl_t ioctl;
ioctl.func_num = DEV_ADD_DISK;
ioctl.handler = tmpdisk_drv;
@@ -46,10 +47,10 @@ int main(){
ioctl.out_data_size = 0;
printf("Create '/tmp%u/' disk a %u Mb size...\n", tmpdisk_add.disk_id, TMPDISK_SIZE);
unsigned status =_ksys_driver_control(&ioctl);
if(status<7){
unsigned status = _ksys_driver_control(&ioctl);
if (status < 7) {
puts(tmpdisk_res_text[status]);
}else{
} else {
puts(tmpdisk_res_text[6]);
}
exit(0);
@@ -4,19 +4,19 @@ The main code is taken from the site:
https://www.binarytides.com/whois-client-code-in-c-with-linux-sockets/
*/
#include <errno.h>
#include "sys/ksys.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <clayer/network.h>
#include <conio.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
FILE *out=stdout;
FILE* out = stdout;
#ifdef DEBUG
FILE *out=stderr;
FILE* out = stderr;
#endif
#define EXIT_SUCCESS 0
@@ -26,273 +26,273 @@ FILE *out=stderr;
void show_help()
{
puts("Usage: whois <host> [-f <file>]\n");
puts(" host Connect to server host");
puts(" -f file Redirecting output to file\n");
puts("Example: whois google.com -f my.txt");
puts("Usage: whois <host> [-f <file>]\n");
puts(" host Connect to server host");
puts(" -f file Redirecting output to file\n");
puts("Example: whois google.com -f my.txt");
}
int get_whois_data(char * , char **);
int hostname_to_ip(char * , char *);
int whois_query(char * , char * , char **);
char *str_replace(char *search , char *replace , char *subject );
int get_whois_data(char*, char**);
int hostname_to_ip(char*, char*);
int whois_query(char*, char*, char**);
char* str_replace(char* search, char* replace, char* subject);
char* str_copy(char*);
int main(int argc , char *argv[])
int main(int argc, char* argv[])
{
char *domain , *data = NULL;
int f_flag=0;
char *domain, *data = NULL;
int f_flag = 0;
if(argc==2){
domain=strdup(argv[1]);
}
if (argc == 2) {
domain = strdup(argv[1]);
}
else if(!strcmp(argv[2], "-f") && argc==4){
domain=strdup(argv[1]);
if((out=fopen(argv[3],"w"))==NULL){
printf("Error writing to file: '%s' !\n", argv[3]);
exit(0);
}
}else{
show_help();
exit(0);
}
if(out==stdout){
con_init();
(*con_set_title)("Whois");
}
get_whois_data(domain , &data);
exit(0);
else if (!strcmp(argv[2], "-f") && argc == 4) {
domain = strdup(argv[1]);
if ((out = fopen(argv[3], "w")) == NULL) {
printf("Error writing to file: '%s' !\n", argv[3]);
exit(0);
}
} else {
show_help();
exit(0);
}
if (out == stdout) {
con_init();
(*con_set_title)("Whois");
}
get_whois_data(domain, &data);
exit(0);
}
/*
Get the whois data of a domain
*/
int get_whois_data(char *domain , char **data)
int get_whois_data(char* domain, char** data)
{
char ext[1024] , *pch , *response = NULL , *response_2 = NULL , *wch , *dt;
char ext[1024], *pch, *response = NULL, *response_2 = NULL, *wch, *dt;
//remove "http://" and "www."
domain = str_replace("http://" , "" , domain);
domain = str_replace("www." , "" , domain);
// remove "http://" and "www."
domain = str_replace("http://", "", domain);
domain = str_replace("www.", "", domain);
//get the extension , com , org , edu
dt = strdup(domain);
// get the extension , com , org , edu
dt = strdup(domain);
if(dt == NULL){
fprintf(out, "strdup failed");
}
pch = (char*)strtok(dt , ".");
while(pch != NULL){
strcpy(ext , pch);
pch = strtok(NULL , ".");
}
// This will tell the whois server for the particular TLD like com , org
if( whois_query("whois.iana.org" , ext , &response) == EXIT_FAILURE){
fprintf(out, "Whois query failed");
if (dt == NULL) {
fprintf(out, "strdup failed");
}
pch = (char*)strtok(dt, ".");
while (pch != NULL) {
strcpy(ext, pch);
pch = strtok(NULL, ".");
}
// This will tell the whois server for the particular TLD like com , org
if (whois_query("whois.iana.org", ext, &response) == EXIT_FAILURE) {
fprintf(out, "Whois query failed");
return 1;
}
fprintf(out, "\n\nResponse is:\n\n");
fprintf(out, "%s", response);
}
fprintf(out, "\n\nResponse is:\n\n");
fprintf(out, "%s", response);
// Now analysze the response
pch = strtok(response , "\n");
while(pch != NULL){
// Check if whois line
wch = strstr(pch , "whois.");
if(wch != NULL){
break;
}
// Next line please
pch = strtok(NULL , "\n");
}
// Now we have the TLD whois server in wch , query again
//This will provide minimal whois information along with the parent whois server of the specific domain :)
wch = strdup(wch);
free(response);
//This should not be necessary , but segmentation fault without this , why ?
response = NULL;
if(wch != NULL){
fprintf(out,"\nTLD Whois server is : %s" , wch);
if( whois_query(wch , domain , &response) == EXIT_FAILURE){
fprintf(out, "Whois query failed\n");
return EXIT_FAILURE;
}
}else{
fprintf(out, "\nTLD whois server for %s not found\n" , ext);
return EXIT_SUCCESS;
}
response_2 = strdup(response);
// Again search for a whois server in this response. :)
pch = strtok(response , "\n");
while(pch != NULL){
// Check if whois line
wch = strstr(pch , "whois.");
if(wch != NULL){
break;
}
//Next line please
pch = strtok(NULL , "\n");
}
/*
If a registrar whois server is found then query it
*/
if(wch){
// Now we have the registrar whois server , this has the direct full information of the particular domain
// so lets query again
fprintf(out, "\nRegistrar Whois server is : %s" , wch);
if( whois_query(wch , domain , &response) == EXIT_FAILURE ){
fprintf(out, "Whois query failed");
}else{
fprintf(out, "\n%s" , response);
// Now analysze the response
pch = strtok(response, "\n");
while (pch != NULL) {
// Check if whois line
wch = strstr(pch, "whois.");
if (wch != NULL) {
break;
}
}
/*
otherwise echo the output from the previous whois result
*/
else{
fprintf(out, "%s" , response_2);
}
return 0;
// Next line please
pch = strtok(NULL, "\n");
}
// Now we have the TLD whois server in wch , query again
// This will provide minimal whois information along with the parent whois server of the specific domain :)
wch = strdup(wch);
free(response);
// This should not be necessary , but segmentation fault without this , why ?
response = NULL;
if (wch != NULL) {
fprintf(out, "\nTLD Whois server is : %s", wch);
if (whois_query(wch, domain, &response) == EXIT_FAILURE) {
fprintf(out, "Whois query failed\n");
return EXIT_FAILURE;
}
} else {
fprintf(out, "\nTLD whois server for %s not found\n", ext);
return EXIT_SUCCESS;
}
response_2 = strdup(response);
// Again search for a whois server in this response. :)
pch = strtok(response, "\n");
while (pch != NULL) {
// Check if whois line
wch = strstr(pch, "whois.");
if (wch != NULL) {
break;
}
// Next line please
pch = strtok(NULL, "\n");
}
/*
If a registrar whois server is found then query it
*/
if (wch) {
// Now we have the registrar whois server , this has the direct full information of the particular domain
// so lets query again
fprintf(out, "\nRegistrar Whois server is : %s", wch);
if (whois_query(wch, domain, &response) == EXIT_FAILURE) {
fprintf(out, "Whois query failed");
} else {
fprintf(out, "\n%s", response);
}
}
/*
otherwise echo the output from the previous whois result
*/
else {
fprintf(out, "%s", response_2);
}
return 0;
}
/*
Perform a whois query to a server and record the response
*/
int whois_query(char *server , char *query , char **response)
int whois_query(char* server, char* query, char** response)
{
char ip[32] , message[100] , buffer[1500];
int sock , read_size , total_size = 0;
int WHOIS_PORT = 43;
char ip[32], message[100], buffer[1500];
int sock, read_size, total_size = 0;
int WHOIS_PORT = 43;
struct sockaddr dest;
sock = socket(AF_INET4 , SOCK_STREAM , IPPROTO_TCP);
//Prepare connection structures :)
memset(&dest , 0 , sizeof(dest) );
dest.sin_family = AF_INET;
sock = socket(AF_INET4, SOCK_STREAM, IPPROTO_TCP);
// Prepare connection structures :)
memset(&dest, 0, sizeof(dest));
dest.sin_family = AF_INET;
server = str_copy(server);
server[strcspn(server, "\r\n")] = '\0';
fprintf(out, "\nResolving: %s ...\n" , server);
if(hostname_to_ip(server , ip) == EXIT_FAILURE ){
fprintf(out, "\nResolving: %s ...\n", server);
if (hostname_to_ip(server, ip) == EXIT_FAILURE) {
fprintf(out, "Failed\n");
return EXIT_FAILURE;
}
fprintf(out, "Found ip: %s \n" , ip);
}
fprintf(out, "Found ip: %s \n", ip);
dest.sin_addr = inet_addr(ip);
dest.sin_port = PORT(WHOIS_PORT);
dest.sin_port = PORT(WHOIS_PORT);
; //Now connect to remote server
if(connect(sock , (const struct sockaddr*) &dest , sizeof(dest)) < 0){
perror("connect failed");
perror(strerror(errno));
; // Now connect to remote server
if (connect(sock, (const struct sockaddr*)&dest, sizeof(dest)) < 0) {
perror("connect failed");
perror(strerror(errno));
return EXIT_FAILURE;
}
}
//Now send some data or message
fprintf(out, "\nQuerying for: %s ...\n" , query);
sprintf(message , "%s\r\n" , query);
if( send(sock , message , strlen(message) , 0) < 0){
perror("send failed");
// Now send some data or message
fprintf(out, "\nQuerying for: %s ...\n", query);
sprintf(message, "%s\r\n", query);
if (send(sock, message, strlen(message), 0) < 0) {
perror("send failed");
return EXIT_FAILURE;
}
//Now receive the response
while((read_size = recv(sock, buffer, sizeof(buffer), 0))){
*response = realloc(*response , read_size + total_size);
if(*response == NULL){
fprintf(out, "realloc failed");
}
// Now receive the response
while ((read_size = recv(sock, buffer, sizeof(buffer), 0))) {
*response = realloc(*response, read_size + total_size);
if (*response == NULL) {
fprintf(out, "realloc failed");
return EXIT_FAILURE;
}
memcpy(*response + total_size , buffer , read_size);
total_size += read_size;
}
fprintf(out, "Done\n");
*response = realloc(*response , total_size + 1);
*(*response + total_size) = '\0';
close(sock);
}
memcpy(*response + total_size, buffer, read_size);
total_size += read_size;
}
fprintf(out, "Done\n");
*response = realloc(*response, total_size + 1);
*(*response + total_size) = '\0';
close(sock);
return EXIT_SUCCESS;
}
/*
Get the ip address of a given hostname
*/
int hostname_to_ip(char *hostname , char *ip)
int hostname_to_ip(char* hostname, char* ip)
{
struct addrinfo *addr_info;
char port_str[16]; sprintf(port_str, "%d", 80);
struct addrinfo* addr_info;
char port_str[16];
sprintf(port_str, "%d", 80);
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; // IPv4 or IPv6 doesnt matter
hints.ai_family = AF_UNSPEC; // IPv4 or IPv6 doesnt matter
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
if (getaddrinfo(hostname, port_str, 0, &addr_info) != 0) {
freeaddrinfo(addr_info);
return EXIT_FAILURE;
}else{
strcpy(ip, inet_ntoa(addr_info->ai_addr->sin_addr));
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
} else {
strcpy(ip, inet_ntoa(addr_info->ai_addr->sin_addr));
return EXIT_SUCCESS;
}
}
/*
Search and replace a string with another string , in a string
*/
char *str_replace(char *search , char *replace , char *subject)
char* str_replace(char* search, char* replace, char* subject)
{
char *p = NULL , *old = NULL , *new_subject = NULL ;
int c = 0 , search_size;
search_size = strlen(search);
//Count how many occurences
for(p = strstr(subject , search) ; p != NULL ; p = strstr(p + search_size , search)){
c++;
}
//Final size
c = ( strlen(replace) - search_size )*c + strlen(subject);
//New subject with new size
new_subject = malloc( c );
//Set it to blank
strcpy(new_subject , "");
//The start position
old = subject;
for(p = strstr(subject , search) ; p != NULL ; p = strstr(p + search_size , search)){
//move ahead and copy some text from original subject , from a certain position
strncpy(new_subject + strlen(new_subject) , old , p - old);
//move ahead and copy the replacement text
strcpy(new_subject + strlen(new_subject) , replace);
//The new start position after this search match
old = p + search_size;
}
//Copy the part after the last search match
strcpy(new_subject + strlen(new_subject) , old);
return new_subject;
char *p = NULL, *old = NULL, *new_subject = NULL;
int c = 0, search_size;
search_size = strlen(search);
// Count how many occurences
for (p = strstr(subject, search); p != NULL; p = strstr(p + search_size, search)) {
c++;
}
// Final size
c = (strlen(replace) - search_size) * c + strlen(subject);
// New subject with new size
new_subject = malloc(c);
// Set it to blank
strcpy(new_subject, "");
// The start position
old = subject;
for (p = strstr(subject, search); p != NULL; p = strstr(p + search_size, search)) {
// move ahead and copy some text from original subject , from a certain position
strncpy(new_subject + strlen(new_subject), old, p - old);
// move ahead and copy the replacement text
strcpy(new_subject + strlen(new_subject), replace);
// The new start position after this search match
old = p + search_size;
}
// Copy the part after the last search match
strcpy(new_subject + strlen(new_subject), old);
return new_subject;
}
char* str_copy(char *source)
char* str_copy(char* source)
{
char *copy = malloc(strlen(source) + 1);
if(copy){
char* copy = malloc(strlen(source) + 1);
if (copy) {
strcpy(copy, source);
}
return copy;
}
}