Source to "lock", uses locking(2)
John B. Milton
jbm at uncle.UUCP
Tue Feb 21 18:21:35 AEST 1989
Have fun! Mail me any really useful uses you come up with.
#! /bin/sh
# This is a shell archive. Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file". To overwrite existing
# files, type "sh file -c". You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g.. If this archive is complete, you
# will see the following message at the end:
# "End of shell archive."
# Contents: lock.c
# Wrapped by jbm at uncle on Tue Feb 21 03:20:35 1989
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'lock.c' -a "${1}" != "-c" ; then
echo shar: Will not clobber existing file \"'lock.c'\"
else
echo shar: Extracting \"'lock.c'\" \(1618 characters\)
sed "s/^X//" >'lock.c' <<'END_OF_FILE'
X/* vi:set ts=2 sw=2: */
X
X#include <fcntl.h>
X#include <stdio.h>
X#include <string.h>
X
Xextern int errno;
X
Xstatic char *me,Debug=0;
X
Xvoid perrorf(format,a1,a2,a3,a4,a5,a6,a7,a8,a9)
Xchar *format,*a1,*a2,*a3,*a4,*a5,*a6,*a7,*a8,*a9;
X{
X int curerrno;
X char line[200];
X
X curerrno=errno;
X sprintf(line,format,a1,a2,a3,a4,a5,a6,a7,a8,a9);
X errno=curerrno;
X perror(line);
X}
X
Xstatic char *usage="Usage: %s [-{d}ebug] [-{m}ode n] [-{o}ffset n] [-{s}ize n] [-{z}leep secs] files";
X
X#define optarg(av,i) (av[i][2]=='\0'?(av[++i]==NULL?"0":av[i]):&av[i][2])
X
Xmain(argc,argv)
Xint argc;
Xchar *argv[];
X{
X int f,i,mode=1;
X long offset=0L,size=0L;
X
X (me=strrchr(argv[0],'/'))==NULL?me=argv[0]:me++;
X for (i=1; i<argc; i++)
X if (argv[i][0]=='-')
X switch (argv[i][1]) {
X case 'd':
X Debug=1;
X break;
X case 'm':
X mode=atoi(optarg(argv,i));
X break;
X case 'o':
X offset=atoi(optarg(argv,i));
X break;
X case 's':
X size=atoi(optarg(argv,i));
X break;
X case 'z':
X if (Debug)
X fprintf(stderr,"sleeping...\n");
X sleep(atoi(optarg(argv,i)));
X break;
X default:
X fprintf(stderr,usage,me);
X exit(1);
X }
X else
X if ((f=open(argv[i],O_RDONLY))==-1)
X perrorf("%s: open \"%s\"",me,argv[i]);
X else
X if (lseek(f,offset,0)==-1)
X perrorf("%s: lseek \"%s\" to %d",me,argv[i],offset);
X else
X if (locking(f,mode,size)==-1)
X perrorf("%s: locking \"%s\" with mode %d, size %d, offset %d",
X me,argv[i],mode,size,offset);
X else
X if (Debug)
X fprintf(stderr,"Locked \"%s\" with mode %d, size %d, offset %d\n",
X argv[i],mode,size,offset);
X}
END_OF_FILE
if test 1618 -ne `wc -c <'lock.c'`; then
echo shar: \"'lock.c'\" unpacked with wrong size!
fi
# end of 'lock.c'
fi
echo shar: End of shell archive.
exit 0
--
John Bly Milton IV, jbm at uncle.UUCP, n8emr!uncle!jbm at osu-cis.cis.ohio-state.edu
(614) h:294-4823, w:764-2933; Got any good 74LS503 circuits?
More information about the Unix-pc.sources
mailing list