Skip to content

Instantly share code, notes, and snippets.

@josephok
josephok / change_pending_signals_disposition.c
Last active June 15, 2016 08:37
Write a program that shows that when the disposition of a pending signal is changed to be SIG_IGN , the program never sees (catches) the signal.
// Write a program that shows that when the disposition of a pending signal is
// changed to be SIG_IGN , the program never sees (catches) the signal.
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
void printSigset(FILE *of, const char *prefix, const sigset_t *sigset)
{
int sig, cnt;
@josephok
josephok / quicksort.c
Created June 14, 2016 04:58
quick sort
#include <stdio.h>
void quicksort(int *arr, int low, int high)
{
int pivot, i, j, temp;
if(low < high) {
pivot = low; // select a pivot element
i = low;
j = high;
while(i < j) {
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
int mydup2(int oldfd, int newfd) {
int fd = fcntl(oldfd, F_GETFL);
if (fd == -1) {
errno = EBADF;
return -1;
@josephok
josephok / vimrc
Last active May 10, 2016 10:26
my vim config
set nocompatible " required
filetype off " required
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')
@josephok
josephok / Dockerfile
Created May 1, 2016 10:51
Ubuntu 14.04 Dockerfile
FROM ubuntu:14.04
MAINTAINER Joseph <[email protected]>
COPY sources.list /etc/apt/
RUN apt-get update
RUN apt-get install -y vim
@josephok
josephok / env.c
Created April 19, 2016 13:24
puts env list
#include <unistd.h>
extern char **environ;
int main(int argc, char const *argv[]) {
char **ptr;
for (ptr = environ; *ptr != NULL; ptr++) {
puts(*ptr);
}
return 0;
@josephok
josephok / dup2.c
Created April 19, 2016 05:06
implements dup and dup2 using fcntl.
// implements dup and dup2 using fcntl.
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <assert.h>
#include <errno.h>
int mydup(int oldfd);
int mydup2(int oldfd, int newfd);
@josephok
josephok / copy.c
Created April 18, 2016 08:18
copy a file
// Copy a file.
// Removed all error hanlders for simplicity.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#ifndef BUF_SIZE
#define BUF_SIZE 4096
@josephok
josephok / file_hole.c
Created April 18, 2016 08:00
Add hole to a file
// Removed all error hanlder for simplicity
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main(int argc, char const *argv[]) {
int flags = O_RDWR;
@josephok
josephok / name.sh
Last active December 14, 2018 07:31
Generate a random name based on https://randomuser.me/api/
#!/usr/bin/env bash
which jq &> /dev/null
if [[ $? -ne 0 ]]
then
echo "Please install jq first, see https://stedolan.github.io/jq/"
exit 1
fi
name=$(curl -s https://randomuser.me/api/)