Skip to content

Instantly share code, notes, and snippets.

#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <linux/fd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#define BLKSSZGET _IO(0x12,104)/* get block device sector size */
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kthread.h>
struct task_struct* ts;
static int thread_func(void)
{
while (1)
{
@khajavi
khajavi / Makefile
Created September 8, 2013 10:44
simple flex example
count_words: count_words.o lexer.o -lfl
gcc count_words.o lexer.o -lfl -o count_words
count_words.o: count_words.c
gcc -c count_words.c
lexer.o: lexer.c
gcc -c lexer.c
lexer.c: lexer.l
@khajavi
khajavi / hello.c
Created September 7, 2013 13:32
simple Hello, Wolrd makefile
#include <stdio.h>
int main() { printf("Hello, World!\n"); }
@khajavi
khajavi / getenv.c
Created September 7, 2013 13:04
getenv example.
#include <stdlib.h> //getenv, atoi
#include <stdio.h> //printf
int main() {
char* reps_text = getenv("reps");
int reps = reps_text ? atoi( reps_text ) : 10;
char* msg = getenv( "msg" );
if (!msg) msg = "Hello.";
@khajavi
khajavi / env2.c
Created September 7, 2013 12:52
get environment var in c
#include <stdlib.h>
#include <stdio.h>
int main( int argc, char **argv, char **envp ) {
char **env;
for( env = envp; *env; env++ )
printf( "%s\n", *env );
@khajavi
khajavi / env.c
Created September 7, 2013 12:51
get envrionment variable in c
#include <stdio.h>
extern char **environ;
void main()
{
for (char **env = environ; *env; ++env)
printf("%s\n", *env);
}
@khajavi
khajavi / list_contents.c
Created June 16, 2013 13:13
Example of reading elements of archive file by using libarchive library.
#include <archive.h>
#include <archive_entry.h>
int main() {
struct archive* a;
struct archive_entry* entry;
int r;
a = archive_read_new();
archive_read_support_filter_all( a );
@khajavi
khajavi / Makefile
Created June 1, 2013 13:30
Example of creating new GObject class in Vala
#prototype: Prototype.vala main.vala
# valac -C Prototype.vala main.vala
main: main.c Prototype.c
gcc main.c Prototype.c -o main.bin `pkg-config glib-2.0 gtk+-2.0 --cflags --libs`
@khajavi
khajavi / Makefile
Created June 1, 2013 11:21
Example of getting attribute value of xml node by using libxml2 library.
CC = gcc
CLIBS = `pkg-config libxml-2.0 --cflags --libs`
retrieve_attribute_value: retrieve_attribute_value_example.c
$(CC) retrieve_attribute_value_example.c -o retrieve_attribute_value_example.bin $(CLIBS)
clean:
rm -f *.o *.bin