X-Git-Url: http://dolda2000.com/gitweb/?p=utils.git;a=blobdiff_plain;f=serialize.c;fp=serialize.c;h=4a851b1683ec74cf05b3e40a8cf4df2991404689;hp=0000000000000000000000000000000000000000;hb=21e62b992c09d307c38a58dbfc6ae9a23de82128;hpb=d740b990e5e6202dc9f4b5976de67342504add62 diff --git a/serialize.c b/serialize.c new file mode 100644 index 0000000..4a851b1 --- /dev/null +++ b/serialize.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include + +int main(int argc, unsigned char **argv) +{ + int c; + int fd, locktype; + pid_t pid; + int status; + + locktype = LOCK_EX; + while((c = getopt(argc, argv, "se")) > 0) + { + switch(c) + { + case 's': + locktype = LOCK_SH; + break; + case 'e': + locktype = LOCK_EX; + break; + default: + return(1); + } + } + if(argc - optind < 2) + { + fprintf(stderr, "Wrong number of arguments\n"); + return(1); + } + if((fd = open(argv[optind], O_RDWR | O_CREAT, 0600)) < 0) + { + perror(argv[optind]); + return(1); + } + optind++; + flock(fd, locktype); + if((pid = fork()) < 0) + { + perror("fork"); + return(1); + } else if(!pid) { + execvp(argv[optind], argv + optind); + perror(argv[optind]); + exit(127); + } + while(wait(&status) != pid); + flock(fd, LOCK_UN); + return(status); +}