Skip to content

Instantly share code, notes, and snippets.

View alsamitech's full-sized avatar

Sami Alameddine alsamitech

View GitHub Profile
@alsamitech
alsamitech / isnum.c
Created April 26, 2021 07:46
My implementation of isnum.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define s_foreach(str, index) for(size_t index=0;str[index]!=0;index++)
char isnum(char* str){
s_foreach(str, i){
switch(str[i]){
case '1': break;
double ExecuteAC(char** argv){
pid_t pid=fork();
if(pid==0){
// child process
execv(argv[0], argv);
exit(127);
}else{
// parent process
long before=clock();
waitpid(pid, 0, 0);
@alsamitech
alsamitech / basic_table.c
Created April 30, 2021 05:30
libtable_basic, requires <stdlib.h> and <ctype.h>
typedef struct _table{
size_t x;
size_t y;
char** table;
}table_t;
table_t new_table(size_t x, size_t y, size_t s){
table_t table;
table.x=x;
table.y=y;
@alsamitech
alsamitech / unix_read_file.c
Created April 30, 2021 22:56
Requires <unistd.h>, <sys/stat.h>, and <fcntl.h>
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);
*len=stats.st_size;
if(buf){
read(fd, buf, stats.st_size);
@alsamitech
alsamitech / unix_get_filesz.c
Created April 30, 2021 23:06
Gets the size of a file. Requires <unistd.h>, <fcntl.h>, and <sys/stats.h>
size_t unix_get_filesz(char* filenm){
size_t sz=0;
int fd=open(filenm, O_RDONLY);
if(fd>0){
struct stat stats;
fstat(fd, &stats);
sz=stats.st_size;
close(fd);
}
@alsamitech
alsamitech / fast_atokl_r0.c
Created May 3, 2021 08:11
Requires <stdlib.h> and <string.h>
char** atokl(char* InC, char* delim, long unsigned int* len){
long unsigned int capacity=32;
char** tok=(char**)malloc((capacity+1)*sizeof(char**));
//printf("%p\n", tok);
char* pass=strdup(InC);
{
long unsigned int i=0;
while(tok[i-1]!=NULL){
if(i+2>=capacity){
char ismapped(void* ptr){
int fd[2];
char valid=1;
pipe(fd);
if(write(fd[1], ptr, 1)<0){
if(errno==EFAULT){
valid=0;
}
}
@alsamitech
alsamitech / new_table.c
Created May 5, 2021 23:24
Heap-allocates a table in one malloc call. Requires <stdlib.h> and <ctype.h>
typedef struct _table{
size_t x, y;
char** table;
}table_t;
table_t new_table(size_t x, size_t y, size_t s){
table_t table;
table.x=x;
table.y=y;
table.table=malloc(x*sizeof(void*));
@alsamitech
alsamitech / ltoa.c
Created May 7, 2021 04:19
My implementation of ltoa() because some compilers just won't provide one.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define BUFSIZE (sizeof(long) * 8 + 1)
ldiv_t ldiv (long int numer, long int denom);
char *ltoa(long N, char *str, int base)
@alsamitech
alsamitech / onesep_s.c
Created May 7, 2021 04:49
what strtok_r is to strtok. For seplib.
char* onesep_s(char* in, char sep, char** save_ptr){
long unsigned int until=until_byte(in, strlen(in), sep);
if(until==strlen(in))return 0;
*save_ptr+=until+1;
return strndupx(in, until);
}