Skip to content

Instantly share code, notes, and snippets.

View alsamitech's full-sized avatar

Sami Alameddine alsamitech

  • Alsami Technologies
  • United States of America
View GitHub Profile
@alsamitech
alsamitech / dns_nm_to_addr.c
Created April 8, 2021 02:15
Returns IP adress of URL
typedef struct hostent hostent_t;
typedef struct in_addr inaddr_t;
char* dns_nm_to_addr(char* nm){
hostent_t* hostinfo;
inaddr_t* addr;
hostinfo=gethostbyname(nm);
return inet_ntoa(*(inaddr_t*)(hostinfo->h_addr));
}
@alsamitech
alsamitech / getline.c
Created April 10, 2021 06:24
Gets a specified line from a file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Types and stuff
#include <ctype.h>
#include <stdint.h>
typedef struct _arr{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifndef __unix__
#error Please use unix or replace the unix syscalls with functions/syscalls available on your system. After you have done that, remove this error
#endif
#include <unistd.h>
unsigned char starts_with(char* _in, char* startsw){
long unsigned int fma=strlen(_in);
for(unsigned int i=0;i<(fma-1);i++){
if(_in[i]==0x0||startsw[i]==0x0){
break;
}
if(_in[i]!=startsw[i]){
return 1;
}
}
@alsamitech
alsamitech / lines_from_file.c
Last active April 22, 2021 06:24
Requires: atokl aka compound_tokenizer, <stdio.h>, <stdlib.h>, <string.h>, and <ctype.h>
typedef struct _arr{
char* arr;
size_t len;
}arr_t;
arr_t lines_from_file(char* filenm){
arr_t filedata;
filedata.arr=read_file(filenm, &filedata.len);
if(!filedata.arr){
arr_t a={0, 0};
@alsamitech
alsamitech / getline.c
Last active May 22, 2021 19:12
getline - Gets a line from a stream
char* getline(FILE* stream){
size_t bytes=0;
unsigned int capacity=64;
char* buf=malloc(capacity);
char c;
#ifdef __unix__
while((c=fgetc(stream))!=EOF&&c!='\n')
#else
while((c=fgetc(stream))!=EOF&&c!='\n'&&c!='\r')
#endif // __unix__
@alsamitech
alsamitech / unix_read_file_old.c
Last active April 30, 2021 22:54
Requires <unistd.h>, <fcntl.h>. It is recommended that you contain the result as an arr_t or use a wrapper for that.
char* unix_read_file(char* filenm, long unsigned int* len){
int fd=open(filenm, O_RDONLY);
char* buf=0x0;
if(fd>0){
struct stat stats;
fstat(fd, &stats);
buf=malloc(stats.st_size+1);
if(buf){
read(fd, buf, stats.st_size);
buf[stats.st_size]=0x0;
@alsamitech
alsamitech / fast_atokl.c
Created April 22, 2021 06:39
The Compound Tokenizer, but faster.
char** atokl(char* InC, char* delim, long unsigned int* len){
long unsigned int capacity=32;
char** tok=(char**)malloc(capacity*sizeof(char**));
//printf("%p\n", tok);
tok[0]=strtok(strdup(InC), delim);
{
long unsigned int i=1;
while(tok[i-1]!=NULL){
if(i+2>=capacity){
@alsamitech
alsamitech / execute_async.c
Created April 23, 2021 05:46
Executes another program with a child process received from fork. Instead of waiting, one process just returns the function and the program can just continue whatever it was doing originally.
// Requires <unistd.h> and <stdlib.h>
void ExecuteAsync(char** argv){
pid_t pid=fork();
if(pid==0){
// child process
execv(argv[0], argv);
exit(127);
}
@alsamitech
alsamitech / clock.c
Created April 23, 2021 08:26
Like time, but more accurate
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifndef __unix__
#error only supports unix!
#endif // __unix__