Skip to content

Instantly share code, notes, and snippets.

@markogresak
Created December 7, 2014 16:15
Show Gist options
  • Select an option

  • Save markogresak/4b9c0ca67f225c04a584 to your computer and use it in GitHub Desktop.

Select an option

Save markogresak/4b9c0ca67f225c04a584 to your computer and use it in GitHub Desktop.
#include <sys/file.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#define E_O_I "odpiranju vhodne"
#define E_L_I "zaklepanju vhodne"
#define E_UL_I "odklepanju vhodne"
#define E_O_O "odpiranju vhodne"
#define E_L_O "zaklepanju vhodne"
#define E_UL_O "odklepanju vhodne"
#define E_WR "zapisovanju"
#define M "Napaka pri %s datoteke"
#define N_A "-"
#define BLEN 1024
#define O open
#define L flock
#define R read
#define W write
#define C close
#define E(x) return x
#define F sprintf
#define P perror
int e(int d,char* m){if(d==-1){char s[40];F(s,M,m);P(s);exit(errno);}E(d);}
int main(int c,char *v[]){
int i=e((c<2||!strcmp(v[1],N_A))?STDIN_FILENO:O(v[1],O_RDONLY),E_O_I),
o=e((c<3)?STDOUT_FILENO:O(v[2],O_WRONLY),E_O_O),len;char b[BLEN];
e(L(i,LOCK_SH),E_L_I);e(L(o,LOCK_EX),E_L_O);
while((len=R(i,&b,BLEN))>0)e(W(o,&b,len)==len?0:1,E_WR);
e(L(i,LOCK_UN),E_UL_I);e(L(o,LOCK_UN),E_UL_O);C(i);C(o);E(0);}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment