/* * As its sole author, I explicitly place this program in the public domain. * It may be used by anyone in any way for any purpose, though I would * appreciate credit where it's due. * der Mouse, mouse@rodents.montreal.qc.ca, 1998-08-05 */ #include #include #include #include #include extern const char *__progname; static int fd; static int have_fd = 0; static int op = 0; static int vflag = 0; static void handleargs(int ac, char **av) { int skip; int errs; char *ep; skip = 0; errs = 0; for (ac--,av++;ac;ac--,av++) { if (skip > 0) { skip --; continue; } if (**av != '-') { if (! have_fd) { fd = strtol(*av,&ep,0); if ((fd < 0) || *ep || (ep == *av)) { fprintf(stderr,"%s: invalid file descriptor argument `%s'\n",__progname,*av); errs ++; } else { have_fd = 1; } } else { fprintf(stderr,"%s: extra argument `%s'\n",__progname,*av); errs ++; } continue; } if (!strcmp(*av,"-sh")) { op |= LOCK_SH; continue; } if (!strcmp(*av,"-ex")) { op |= LOCK_EX; continue; } if (!strcmp(*av,"-nb")) { op |= LOCK_NB; continue; } if (!strcmp(*av,"-un")) { op |= LOCK_UN; continue; } if (!strcmp(*av,"-v")) { vflag = 1; continue; } fprintf(stderr,"%s: unrecognized option `%s'\n",__progname,*av); errs ++; } if (errs) exit(2); } int main(int, char **); int main(int ac, char **av) { int ec; handleargs(ac,av); if (flock(fd,op) >= 0) exit(0); switch (errno) { #ifdef EAGAIN case EAGAIN: #endif #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EAGAIN != EWOULDBLOCK)) case EWOULDBLOCK: #endif ec = 1; break; default: ec = 2; break; } if (vflag || (ec != 1)) fprintf(stderr,"%s: %s\n",__progname,strerror(errno)); exit(ec); }