mirror of
https://github.com/zdharma-continuum/zconvey.git
synced 2026-09-10 07:06:21 -04:00
Initial commit: flock.c compiles and runs on Darwin
This commit is contained in:
commit
167723b800
339
LICENSE
Normal file
339
LICENSE
Normal file
|
|
@ -0,0 +1,339 @@
|
||||||
|
GNU GENERAL PUBLIC LICENSE
|
||||||
|
Version 2, June 1991
|
||||||
|
|
||||||
|
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
|
||||||
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
Everyone is permitted to copy and distribute verbatim copies
|
||||||
|
of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
|
Preamble
|
||||||
|
|
||||||
|
The licenses for most software are designed to take away your
|
||||||
|
freedom to share and change it. By contrast, the GNU General Public
|
||||||
|
License is intended to guarantee your freedom to share and change free
|
||||||
|
software--to make sure the software is free for all its users. This
|
||||||
|
General Public License applies to most of the Free Software
|
||||||
|
Foundation's software and to any other program whose authors commit to
|
||||||
|
using it. (Some other Free Software Foundation software is covered by
|
||||||
|
the GNU Lesser General Public License instead.) You can apply it to
|
||||||
|
your programs, too.
|
||||||
|
|
||||||
|
When we speak of free software, we are referring to freedom, not
|
||||||
|
price. Our General Public Licenses are designed to make sure that you
|
||||||
|
have the freedom to distribute copies of free software (and charge for
|
||||||
|
this service if you wish), that you receive source code or can get it
|
||||||
|
if you want it, that you can change the software or use pieces of it
|
||||||
|
in new free programs; and that you know you can do these things.
|
||||||
|
|
||||||
|
To protect your rights, we need to make restrictions that forbid
|
||||||
|
anyone to deny you these rights or to ask you to surrender the rights.
|
||||||
|
These restrictions translate to certain responsibilities for you if you
|
||||||
|
distribute copies of the software, or if you modify it.
|
||||||
|
|
||||||
|
For example, if you distribute copies of such a program, whether
|
||||||
|
gratis or for a fee, you must give the recipients all the rights that
|
||||||
|
you have. You must make sure that they, too, receive or can get the
|
||||||
|
source code. And you must show them these terms so they know their
|
||||||
|
rights.
|
||||||
|
|
||||||
|
We protect your rights with two steps: (1) copyright the software, and
|
||||||
|
(2) offer you this license which gives you legal permission to copy,
|
||||||
|
distribute and/or modify the software.
|
||||||
|
|
||||||
|
Also, for each author's protection and ours, we want to make certain
|
||||||
|
that everyone understands that there is no warranty for this free
|
||||||
|
software. If the software is modified by someone else and passed on, we
|
||||||
|
want its recipients to know that what they have is not the original, so
|
||||||
|
that any problems introduced by others will not reflect on the original
|
||||||
|
authors' reputations.
|
||||||
|
|
||||||
|
Finally, any free program is threatened constantly by software
|
||||||
|
patents. We wish to avoid the danger that redistributors of a free
|
||||||
|
program will individually obtain patent licenses, in effect making the
|
||||||
|
program proprietary. To prevent this, we have made it clear that any
|
||||||
|
patent must be licensed for everyone's free use or not licensed at all.
|
||||||
|
|
||||||
|
The precise terms and conditions for copying, distribution and
|
||||||
|
modification follow.
|
||||||
|
|
||||||
|
GNU GENERAL PUBLIC LICENSE
|
||||||
|
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||||
|
|
||||||
|
0. This License applies to any program or other work which contains
|
||||||
|
a notice placed by the copyright holder saying it may be distributed
|
||||||
|
under the terms of this General Public License. The "Program", below,
|
||||||
|
refers to any such program or work, and a "work based on the Program"
|
||||||
|
means either the Program or any derivative work under copyright law:
|
||||||
|
that is to say, a work containing the Program or a portion of it,
|
||||||
|
either verbatim or with modifications and/or translated into another
|
||||||
|
language. (Hereinafter, translation is included without limitation in
|
||||||
|
the term "modification".) Each licensee is addressed as "you".
|
||||||
|
|
||||||
|
Activities other than copying, distribution and modification are not
|
||||||
|
covered by this License; they are outside its scope. The act of
|
||||||
|
running the Program is not restricted, and the output from the Program
|
||||||
|
is covered only if its contents constitute a work based on the
|
||||||
|
Program (independent of having been made by running the Program).
|
||||||
|
Whether that is true depends on what the Program does.
|
||||||
|
|
||||||
|
1. You may copy and distribute verbatim copies of the Program's
|
||||||
|
source code as you receive it, in any medium, provided that you
|
||||||
|
conspicuously and appropriately publish on each copy an appropriate
|
||||||
|
copyright notice and disclaimer of warranty; keep intact all the
|
||||||
|
notices that refer to this License and to the absence of any warranty;
|
||||||
|
and give any other recipients of the Program a copy of this License
|
||||||
|
along with the Program.
|
||||||
|
|
||||||
|
You may charge a fee for the physical act of transferring a copy, and
|
||||||
|
you may at your option offer warranty protection in exchange for a fee.
|
||||||
|
|
||||||
|
2. You may modify your copy or copies of the Program or any portion
|
||||||
|
of it, thus forming a work based on the Program, and copy and
|
||||||
|
distribute such modifications or work under the terms of Section 1
|
||||||
|
above, provided that you also meet all of these conditions:
|
||||||
|
|
||||||
|
a) You must cause the modified files to carry prominent notices
|
||||||
|
stating that you changed the files and the date of any change.
|
||||||
|
|
||||||
|
b) You must cause any work that you distribute or publish, that in
|
||||||
|
whole or in part contains or is derived from the Program or any
|
||||||
|
part thereof, to be licensed as a whole at no charge to all third
|
||||||
|
parties under the terms of this License.
|
||||||
|
|
||||||
|
c) If the modified program normally reads commands interactively
|
||||||
|
when run, you must cause it, when started running for such
|
||||||
|
interactive use in the most ordinary way, to print or display an
|
||||||
|
announcement including an appropriate copyright notice and a
|
||||||
|
notice that there is no warranty (or else, saying that you provide
|
||||||
|
a warranty) and that users may redistribute the program under
|
||||||
|
these conditions, and telling the user how to view a copy of this
|
||||||
|
License. (Exception: if the Program itself is interactive but
|
||||||
|
does not normally print such an announcement, your work based on
|
||||||
|
the Program is not required to print an announcement.)
|
||||||
|
|
||||||
|
These requirements apply to the modified work as a whole. If
|
||||||
|
identifiable sections of that work are not derived from the Program,
|
||||||
|
and can be reasonably considered independent and separate works in
|
||||||
|
themselves, then this License, and its terms, do not apply to those
|
||||||
|
sections when you distribute them as separate works. But when you
|
||||||
|
distribute the same sections as part of a whole which is a work based
|
||||||
|
on the Program, the distribution of the whole must be on the terms of
|
||||||
|
this License, whose permissions for other licensees extend to the
|
||||||
|
entire whole, and thus to each and every part regardless of who wrote it.
|
||||||
|
|
||||||
|
Thus, it is not the intent of this section to claim rights or contest
|
||||||
|
your rights to work written entirely by you; rather, the intent is to
|
||||||
|
exercise the right to control the distribution of derivative or
|
||||||
|
collective works based on the Program.
|
||||||
|
|
||||||
|
In addition, mere aggregation of another work not based on the Program
|
||||||
|
with the Program (or with a work based on the Program) on a volume of
|
||||||
|
a storage or distribution medium does not bring the other work under
|
||||||
|
the scope of this License.
|
||||||
|
|
||||||
|
3. You may copy and distribute the Program (or a work based on it,
|
||||||
|
under Section 2) in object code or executable form under the terms of
|
||||||
|
Sections 1 and 2 above provided that you also do one of the following:
|
||||||
|
|
||||||
|
a) Accompany it with the complete corresponding machine-readable
|
||||||
|
source code, which must be distributed under the terms of Sections
|
||||||
|
1 and 2 above on a medium customarily used for software interchange; or,
|
||||||
|
|
||||||
|
b) Accompany it with a written offer, valid for at least three
|
||||||
|
years, to give any third party, for a charge no more than your
|
||||||
|
cost of physically performing source distribution, a complete
|
||||||
|
machine-readable copy of the corresponding source code, to be
|
||||||
|
distributed under the terms of Sections 1 and 2 above on a medium
|
||||||
|
customarily used for software interchange; or,
|
||||||
|
|
||||||
|
c) Accompany it with the information you received as to the offer
|
||||||
|
to distribute corresponding source code. (This alternative is
|
||||||
|
allowed only for noncommercial distribution and only if you
|
||||||
|
received the program in object code or executable form with such
|
||||||
|
an offer, in accord with Subsection b above.)
|
||||||
|
|
||||||
|
The source code for a work means the preferred form of the work for
|
||||||
|
making modifications to it. For an executable work, complete source
|
||||||
|
code means all the source code for all modules it contains, plus any
|
||||||
|
associated interface definition files, plus the scripts used to
|
||||||
|
control compilation and installation of the executable. However, as a
|
||||||
|
special exception, the source code distributed need not include
|
||||||
|
anything that is normally distributed (in either source or binary
|
||||||
|
form) with the major components (compiler, kernel, and so on) of the
|
||||||
|
operating system on which the executable runs, unless that component
|
||||||
|
itself accompanies the executable.
|
||||||
|
|
||||||
|
If distribution of executable or object code is made by offering
|
||||||
|
access to copy from a designated place, then offering equivalent
|
||||||
|
access to copy the source code from the same place counts as
|
||||||
|
distribution of the source code, even though third parties are not
|
||||||
|
compelled to copy the source along with the object code.
|
||||||
|
|
||||||
|
4. You may not copy, modify, sublicense, or distribute the Program
|
||||||
|
except as expressly provided under this License. Any attempt
|
||||||
|
otherwise to copy, modify, sublicense or distribute the Program is
|
||||||
|
void, and will automatically terminate your rights under this License.
|
||||||
|
However, parties who have received copies, or rights, from you under
|
||||||
|
this License will not have their licenses terminated so long as such
|
||||||
|
parties remain in full compliance.
|
||||||
|
|
||||||
|
5. You are not required to accept this License, since you have not
|
||||||
|
signed it. However, nothing else grants you permission to modify or
|
||||||
|
distribute the Program or its derivative works. These actions are
|
||||||
|
prohibited by law if you do not accept this License. Therefore, by
|
||||||
|
modifying or distributing the Program (or any work based on the
|
||||||
|
Program), you indicate your acceptance of this License to do so, and
|
||||||
|
all its terms and conditions for copying, distributing or modifying
|
||||||
|
the Program or works based on it.
|
||||||
|
|
||||||
|
6. Each time you redistribute the Program (or any work based on the
|
||||||
|
Program), the recipient automatically receives a license from the
|
||||||
|
original licensor to copy, distribute or modify the Program subject to
|
||||||
|
these terms and conditions. You may not impose any further
|
||||||
|
restrictions on the recipients' exercise of the rights granted herein.
|
||||||
|
You are not responsible for enforcing compliance by third parties to
|
||||||
|
this License.
|
||||||
|
|
||||||
|
7. If, as a consequence of a court judgment or allegation of patent
|
||||||
|
infringement or for any other reason (not limited to patent issues),
|
||||||
|
conditions are imposed on you (whether by court order, agreement or
|
||||||
|
otherwise) that contradict the conditions of this License, they do not
|
||||||
|
excuse you from the conditions of this License. If you cannot
|
||||||
|
distribute so as to satisfy simultaneously your obligations under this
|
||||||
|
License and any other pertinent obligations, then as a consequence you
|
||||||
|
may not distribute the Program at all. For example, if a patent
|
||||||
|
license would not permit royalty-free redistribution of the Program by
|
||||||
|
all those who receive copies directly or indirectly through you, then
|
||||||
|
the only way you could satisfy both it and this License would be to
|
||||||
|
refrain entirely from distribution of the Program.
|
||||||
|
|
||||||
|
If any portion of this section is held invalid or unenforceable under
|
||||||
|
any particular circumstance, the balance of the section is intended to
|
||||||
|
apply and the section as a whole is intended to apply in other
|
||||||
|
circumstances.
|
||||||
|
|
||||||
|
It is not the purpose of this section to induce you to infringe any
|
||||||
|
patents or other property right claims or to contest validity of any
|
||||||
|
such claims; this section has the sole purpose of protecting the
|
||||||
|
integrity of the free software distribution system, which is
|
||||||
|
implemented by public license practices. Many people have made
|
||||||
|
generous contributions to the wide range of software distributed
|
||||||
|
through that system in reliance on consistent application of that
|
||||||
|
system; it is up to the author/donor to decide if he or she is willing
|
||||||
|
to distribute software through any other system and a licensee cannot
|
||||||
|
impose that choice.
|
||||||
|
|
||||||
|
This section is intended to make thoroughly clear what is believed to
|
||||||
|
be a consequence of the rest of this License.
|
||||||
|
|
||||||
|
8. If the distribution and/or use of the Program is restricted in
|
||||||
|
certain countries either by patents or by copyrighted interfaces, the
|
||||||
|
original copyright holder who places the Program under this License
|
||||||
|
may add an explicit geographical distribution limitation excluding
|
||||||
|
those countries, so that distribution is permitted only in or among
|
||||||
|
countries not thus excluded. In such case, this License incorporates
|
||||||
|
the limitation as if written in the body of this License.
|
||||||
|
|
||||||
|
9. The Free Software Foundation may publish revised and/or new versions
|
||||||
|
of the General Public License from time to time. Such new versions will
|
||||||
|
be similar in spirit to the present version, but may differ in detail to
|
||||||
|
address new problems or concerns.
|
||||||
|
|
||||||
|
Each version is given a distinguishing version number. If the Program
|
||||||
|
specifies a version number of this License which applies to it and "any
|
||||||
|
later version", you have the option of following the terms and conditions
|
||||||
|
either of that version or of any later version published by the Free
|
||||||
|
Software Foundation. If the Program does not specify a version number of
|
||||||
|
this License, you may choose any version ever published by the Free Software
|
||||||
|
Foundation.
|
||||||
|
|
||||||
|
10. If you wish to incorporate parts of the Program into other free
|
||||||
|
programs whose distribution conditions are different, write to the author
|
||||||
|
to ask for permission. For software which is copyrighted by the Free
|
||||||
|
Software Foundation, write to the Free Software Foundation; we sometimes
|
||||||
|
make exceptions for this. Our decision will be guided by the two goals
|
||||||
|
of preserving the free status of all derivatives of our free software and
|
||||||
|
of promoting the sharing and reuse of software generally.
|
||||||
|
|
||||||
|
NO WARRANTY
|
||||||
|
|
||||||
|
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
||||||
|
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
||||||
|
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
||||||
|
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
||||||
|
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||||
|
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
||||||
|
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
||||||
|
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
||||||
|
REPAIR OR CORRECTION.
|
||||||
|
|
||||||
|
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||||
|
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
||||||
|
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
||||||
|
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
||||||
|
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
||||||
|
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
||||||
|
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
||||||
|
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
||||||
|
POSSIBILITY OF SUCH DAMAGES.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
How to Apply These Terms to Your New Programs
|
||||||
|
|
||||||
|
If you develop a new program, and you want it to be of the greatest
|
||||||
|
possible use to the public, the best way to achieve this is to make it
|
||||||
|
free software which everyone can redistribute and change under these terms.
|
||||||
|
|
||||||
|
To do so, attach the following notices to the program. It is safest
|
||||||
|
to attach them to the start of each source file to most effectively
|
||||||
|
convey the exclusion of warranty; and each file should have at least
|
||||||
|
the "copyright" line and a pointer to where the full notice is found.
|
||||||
|
|
||||||
|
<one line to give the program's name and a brief idea of what it does.>
|
||||||
|
Copyright (C) <year> <name of author>
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation; either version 2 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License along
|
||||||
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
|
||||||
|
Also add information on how to contact you by electronic and paper mail.
|
||||||
|
|
||||||
|
If the program is interactive, make it output a short notice like this
|
||||||
|
when it starts in an interactive mode:
|
||||||
|
|
||||||
|
Gnomovision version 69, Copyright (C) year name of author
|
||||||
|
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||||
|
This is free software, and you are welcome to redistribute it
|
||||||
|
under certain conditions; type `show c' for details.
|
||||||
|
|
||||||
|
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||||
|
parts of the General Public License. Of course, the commands you use may
|
||||||
|
be called something other than `show w' and `show c'; they could even be
|
||||||
|
mouse-clicks or menu items--whatever suits your program.
|
||||||
|
|
||||||
|
You should also get your employer (if you work as a programmer) or your
|
||||||
|
school, if any, to sign a "copyright disclaimer" for the program, if
|
||||||
|
necessary. Here is a sample; alter the names:
|
||||||
|
|
||||||
|
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
|
||||||
|
`Gnomovision' (which makes passes at compilers) written by James Hacker.
|
||||||
|
|
||||||
|
<signature of Ty Coon>, 1 April 1989
|
||||||
|
Ty Coon, President of Vice
|
||||||
|
|
||||||
|
This General Public License does not permit incorporating your program into
|
||||||
|
proprietary programs. If your program is a subroutine library, you may
|
||||||
|
consider it more useful to permit linking proprietary applications with the
|
||||||
|
library. If this is what you want to do, use the GNU Lesser General
|
||||||
|
Public License instead of this License.
|
||||||
4
myflock/Makefile
Normal file
4
myflock/Makefile
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
all: flock
|
||||||
|
|
||||||
|
flock: flock.c c.h nls.h timer.h
|
||||||
|
gcc -Wall flock.c -o flock
|
||||||
356
myflock/c.h
Normal file
356
myflock/c.h
Normal file
|
|
@ -0,0 +1,356 @@
|
||||||
|
/*
|
||||||
|
* Fundamental C definitions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef UTIL_LINUX_C_H
|
||||||
|
#define UTIL_LINUX_C_H
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#ifdef HAVE_ERR_H
|
||||||
|
# include <err.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef HAVE_SYS_SYSMACROS_H
|
||||||
|
# include <sys/sysmacros.h> /* for major, minor */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Compiler-specific stuff
|
||||||
|
*/
|
||||||
|
#ifndef __GNUC_PREREQ
|
||||||
|
# if defined __GNUC__ && defined __GNUC_MINOR__
|
||||||
|
# define __GNUC_PREREQ(maj, min) \
|
||||||
|
((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
|
||||||
|
# else
|
||||||
|
# define __GNUC_PREREQ(maj, min) 0
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __GNUC__
|
||||||
|
|
||||||
|
/* &a[0] degrades to a pointer: a different type from an array */
|
||||||
|
# define __must_be_array(a) \
|
||||||
|
UL_BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(__typeof__(a), __typeof__(&a[0])))
|
||||||
|
|
||||||
|
# define ignore_result(x) __extension__ ({ \
|
||||||
|
__typeof__(x) __dummy __attribute__((__unused__)) = (x); (void) __dummy; \
|
||||||
|
})
|
||||||
|
|
||||||
|
#else /* !__GNUC__ */
|
||||||
|
# define __must_be_array(a) 0
|
||||||
|
# define __attribute__(_arg_)
|
||||||
|
# define ignore_result(x) ((void) (x))
|
||||||
|
#endif /* !__GNUC__ */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Function attributes
|
||||||
|
*/
|
||||||
|
#ifndef __ul_alloc_size
|
||||||
|
# if __GNUC_PREREQ (4, 3)
|
||||||
|
# define __ul_alloc_size(s) __attribute__((alloc_size(s), warn_unused_result))
|
||||||
|
# else
|
||||||
|
# define __ul_alloc_size(s)
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef __ul_calloc_size
|
||||||
|
# if __GNUC_PREREQ (4, 3)
|
||||||
|
# define __ul_calloc_size(n, s) __attribute__((alloc_size(n, s), warn_unused_result))
|
||||||
|
# else
|
||||||
|
# define __ul_calloc_size(n, s)
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Force a compilation error if condition is true, but also produce a
|
||||||
|
* result (of value 0 and type size_t), so the expression can be used
|
||||||
|
* e.g. in a structure initializer (or wherever else comma expressions
|
||||||
|
* aren't permitted).
|
||||||
|
*/
|
||||||
|
#define UL_BUILD_BUG_ON_ZERO(e) __extension__ (sizeof(struct { int:-!!(e); }))
|
||||||
|
#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
|
||||||
|
|
||||||
|
#ifndef ARRAY_SIZE
|
||||||
|
# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef PATH_MAX
|
||||||
|
# define PATH_MAX 4096
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef TRUE
|
||||||
|
# define TRUE 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef FALSE
|
||||||
|
# define FALSE 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef min
|
||||||
|
# define min(x, y) __extension__ ({ \
|
||||||
|
__typeof__(x) _min1 = (x); \
|
||||||
|
__typeof__(y) _min2 = (y); \
|
||||||
|
(void) (&_min1 == &_min2); \
|
||||||
|
_min1 < _min2 ? _min1 : _min2; })
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef max
|
||||||
|
# define max(x, y) __extension__ ({ \
|
||||||
|
__typeof__(x) _max1 = (x); \
|
||||||
|
__typeof__(y) _max2 = (y); \
|
||||||
|
(void) (&_max1 == &_max2); \
|
||||||
|
_max1 > _max2 ? _max1 : _max2; })
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef cmp_numbers
|
||||||
|
# define cmp_numbers(x, y) __extension__ ({ \
|
||||||
|
__typeof__(x) _a = (x); \
|
||||||
|
__typeof__(y) _b = (y); \
|
||||||
|
(void) (&_a == &_b); \
|
||||||
|
_a == _b ? 0 : _a > _b ? 1 : -1; })
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef offsetof
|
||||||
|
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef container_of
|
||||||
|
#define container_of(ptr, type, member) __extension__ ({ \
|
||||||
|
const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
|
||||||
|
(type *)( (char *)__mptr - offsetof(type,member) );})
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME
|
||||||
|
# ifdef HAVE___PROGNAME
|
||||||
|
extern char *__progname;
|
||||||
|
# define program_invocation_short_name __progname
|
||||||
|
# else
|
||||||
|
# ifdef HAVE_GETEXECNAME
|
||||||
|
# define program_invocation_short_name \
|
||||||
|
prog_inv_sh_nm_from_file(getexecname(), 0)
|
||||||
|
# else
|
||||||
|
# define program_invocation_short_name \
|
||||||
|
prog_inv_sh_nm_from_file(__FILE__, 1)
|
||||||
|
# endif
|
||||||
|
static char prog_inv_sh_nm_buf[256];
|
||||||
|
static inline char *
|
||||||
|
prog_inv_sh_nm_from_file(char *f, char stripext)
|
||||||
|
{
|
||||||
|
char *t;
|
||||||
|
|
||||||
|
if ((t = strrchr(f, '/')) != NULL)
|
||||||
|
t++;
|
||||||
|
else
|
||||||
|
t = f;
|
||||||
|
|
||||||
|
strncpy(prog_inv_sh_nm_buf, t, sizeof(prog_inv_sh_nm_buf) - 1);
|
||||||
|
prog_inv_sh_nm_buf[sizeof(prog_inv_sh_nm_buf) - 1] = '\0';
|
||||||
|
|
||||||
|
if (stripext && (t = strrchr(prog_inv_sh_nm_buf, '.')) != NULL)
|
||||||
|
*t = '\0';
|
||||||
|
|
||||||
|
return prog_inv_sh_nm_buf;
|
||||||
|
}
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef HAVE_ERR_H
|
||||||
|
static inline void
|
||||||
|
errmsg(char doexit, int excode, char adderr, const char *fmt, ...)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s: ", program_invocation_short_name);
|
||||||
|
if (fmt != NULL) {
|
||||||
|
va_list argp;
|
||||||
|
va_start(argp, fmt);
|
||||||
|
vfprintf(stderr, fmt, argp);
|
||||||
|
va_end(argp);
|
||||||
|
if (adderr)
|
||||||
|
fprintf(stderr, ": ");
|
||||||
|
}
|
||||||
|
if (adderr)
|
||||||
|
fprintf(stderr, "%m");
|
||||||
|
fprintf(stderr, "\n");
|
||||||
|
if (doexit)
|
||||||
|
exit(excode);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef HAVE_ERR
|
||||||
|
# define err(E, FMT...) errmsg(1, E, 1, FMT)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef HAVE_ERRX
|
||||||
|
# define errx(E, FMT...) errmsg(1, E, 0, FMT)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef HAVE_WARN
|
||||||
|
# define warn(FMT...) errmsg(0, 0, 1, FMT)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef HAVE_WARNX
|
||||||
|
# define warnx(FMT...) errmsg(0, 0, 0, FMT)
|
||||||
|
#endif
|
||||||
|
#endif /* !HAVE_ERR_H */
|
||||||
|
|
||||||
|
|
||||||
|
static inline __attribute__((const)) int is_power_of_2(unsigned long num)
|
||||||
|
{
|
||||||
|
return (num != 0 && ((num & (num - 1)) == 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifndef HAVE_LOFF_T
|
||||||
|
typedef int64_t loff_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(HAVE_DIRFD) && (!defined(HAVE_DECL_DIRFD) || HAVE_DECL_DIRFD == 0) && defined(HAVE_DIR_DD_FD)
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
static inline int dirfd(DIR *d)
|
||||||
|
{
|
||||||
|
return d->dd_fd;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Fallback defines for old versions of glibc
|
||||||
|
*/
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
#ifdef O_CLOEXEC
|
||||||
|
#define UL_CLOEXECSTR "e"
|
||||||
|
#else
|
||||||
|
#define UL_CLOEXECSTR ""
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef O_CLOEXEC
|
||||||
|
#define O_CLOEXEC 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __FreeBSD_kernel__
|
||||||
|
#ifndef F_DUPFD_CLOEXEC
|
||||||
|
#define F_DUPFD_CLOEXEC 17 /* Like F_DUPFD, but FD_CLOEXEC is set */
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef AI_ADDRCONFIG
|
||||||
|
#define AI_ADDRCONFIG 0x0020
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef IUTF8
|
||||||
|
#define IUTF8 0040000
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* MAXHOSTNAMELEN replacement
|
||||||
|
*/
|
||||||
|
static inline size_t get_hostname_max(void)
|
||||||
|
{
|
||||||
|
long len = sysconf(_SC_HOST_NAME_MAX);
|
||||||
|
|
||||||
|
if (0 < len)
|
||||||
|
return len;
|
||||||
|
|
||||||
|
#ifdef MAXHOSTNAMELEN
|
||||||
|
return MAXHOSTNAMELEN;
|
||||||
|
#elif HOST_NAME_MAX
|
||||||
|
return HOST_NAME_MAX;
|
||||||
|
#endif
|
||||||
|
return 64;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The usleep function was marked obsolete in POSIX.1-2001 and was removed
|
||||||
|
* in POSIX.1-2008. It was replaced with nanosleep() that provides more
|
||||||
|
* advantages (like no interaction with signals and other timer functions).
|
||||||
|
*/
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Constant strings for usage() functions. For more info see
|
||||||
|
* Documentation/howto-usage-function.txt and disk-utils/delpart.c
|
||||||
|
*/
|
||||||
|
#define USAGE_HEADER _("\nUsage:\n")
|
||||||
|
#define USAGE_OPTIONS _("\nOptions:\n")
|
||||||
|
#define USAGE_SEPARATOR "\n"
|
||||||
|
#define USAGE_HELP _(" -h, --help display this help and exit\n")
|
||||||
|
#define USAGE_VERSION _(" -V, --version output version information and exit\n")
|
||||||
|
#define USAGE_MAN_TAIL(_man) _("\nFor more details see %s.\n"), _man
|
||||||
|
|
||||||
|
#define UTIL_LINUX_VERSION _("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING
|
||||||
|
|
||||||
|
/*
|
||||||
|
* scanf modifiers for "strings allocation"
|
||||||
|
*/
|
||||||
|
#ifdef HAVE_SCANF_MS_MODIFIER
|
||||||
|
#define UL_SCNsA "%ms"
|
||||||
|
#elif defined(HAVE_SCANF_AS_MODIFIER)
|
||||||
|
#define UL_SCNsA "%as"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* seek stuff
|
||||||
|
*/
|
||||||
|
#ifndef SEEK_DATA
|
||||||
|
# define SEEK_DATA 3
|
||||||
|
#endif
|
||||||
|
#ifndef SEEK_HOLE
|
||||||
|
# define SEEK_HOLE 4
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Macros to convert #define'itions to strings, for example
|
||||||
|
* #define XYXXY 42
|
||||||
|
* printf ("%s=%s\n", stringify(XYXXY), stringify_value(XYXXY));
|
||||||
|
*/
|
||||||
|
#define stringify_value(s) stringify(s)
|
||||||
|
#define stringify(s) #s
|
||||||
|
|
||||||
|
/*
|
||||||
|
* UL_ASAN_BLACKLIST is a macro to tell AddressSanitizer (a compile-time
|
||||||
|
* instrumentation shipped with Clang and GCC) to not instrument the
|
||||||
|
* annotated function. Furthermore, it will prevent the compiler from
|
||||||
|
* inlining the function because inlining currently breaks the blacklisting
|
||||||
|
* mechanism of AddressSanitizer.
|
||||||
|
*/
|
||||||
|
#if defined(__has_feature)
|
||||||
|
# if __has_feature(address_sanitizer)
|
||||||
|
# define UL_ASAN_BLACKLIST __attribute__((noinline)) __attribute__((no_sanitize_memory)) __attribute__((no_sanitize_address))
|
||||||
|
# else
|
||||||
|
# define UL_ASAN_BLACKLIST /* nothing */
|
||||||
|
# endif
|
||||||
|
#else
|
||||||
|
# define UL_ASAN_BLACKLIST /* nothing */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Note that sysconf(_SC_GETPW_R_SIZE_MAX) returns *initial* suggested size for
|
||||||
|
* pwd buffer and in some cases it is not large enough. See POSIX and
|
||||||
|
* getpwnam_r man page for more details.
|
||||||
|
*/
|
||||||
|
#define UL_GETPW_BUFSIZ (16 * 1024)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Darwin or other BSDs may only have MAP_ANON. To get it on Darwin we must
|
||||||
|
* define _DARWIN_C_SOURCE before including sys/mman.h. We do this in config.h.
|
||||||
|
*/
|
||||||
|
#if !defined MAP_ANONYMOUS && defined MAP_ANON
|
||||||
|
# define MAP_ANONYMOUS (MAP_ANON)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* UTIL_LINUX_C_H */
|
||||||
318
myflock/flock.c
Normal file
318
myflock/flock.c
Normal file
|
|
@ -0,0 +1,318 @@
|
||||||
|
/* Copyright 2003-2005 H. Peter Anvin - All Rights Reserved
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person
|
||||||
|
* obtaining a copy of this software and associated documentation
|
||||||
|
* files (the "Software"), to deal in the Software without
|
||||||
|
* restriction, including without limitation the rights to use,
|
||||||
|
* copy, modify, merge, publish, distribute, sublicense, and/or
|
||||||
|
* sell copies of the Software, and to permit persons to whom
|
||||||
|
* the Software is furnished to do so, subject to the following
|
||||||
|
* conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall
|
||||||
|
* be included in all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||||
|
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||||
|
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||||
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||||
|
* OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <getopt.h>
|
||||||
|
#include <paths.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sysexits.h>
|
||||||
|
#include <sys/file.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "c.h"
|
||||||
|
#include "nls.h"
|
||||||
|
//#include "strutils.h"
|
||||||
|
//#include "closestream.h"
|
||||||
|
//#include "monotonic.h"
|
||||||
|
|
||||||
|
static void __attribute__((__noreturn__)) usage(int ex)
|
||||||
|
{
|
||||||
|
fprintf(stderr, USAGE_HEADER);
|
||||||
|
fprintf(stderr,
|
||||||
|
_(" %1$s [options] <file>|<directory> <command> [<argument>...]\n"
|
||||||
|
" %1$s [options] <file>|<directory> -c <command>\n"
|
||||||
|
" %1$s [options] <file descriptor number>\n"),
|
||||||
|
program_invocation_short_name);
|
||||||
|
|
||||||
|
fputs(USAGE_SEPARATOR, stderr);
|
||||||
|
fputs(_("Manage file locks from shell scripts.\n"), stderr);
|
||||||
|
|
||||||
|
fputs(USAGE_OPTIONS, stderr);
|
||||||
|
fputs(_( " -s, --shared get a shared lock\n"), stderr);
|
||||||
|
fputs(_( " -x, --exclusive get an exclusive lock (default)\n"), stderr);
|
||||||
|
fputs(_( " -u, --unlock remove a lock\n"), stderr);
|
||||||
|
fputs(_( " -n, --nonblock fail rather than wait\n"), stderr);
|
||||||
|
fputs(_( " -o, --close close file descriptor before running command\n"), stderr);
|
||||||
|
fputs(_( " -c, --command <command> run a single command string through the shell\n"), stderr);
|
||||||
|
fputs(_( " -F, --no-fork execute command without forking\n"), stderr);
|
||||||
|
fputs(_( " --verbose increase verbosity\n"), stderr);
|
||||||
|
fprintf(stderr, USAGE_SEPARATOR);
|
||||||
|
fprintf(stderr, USAGE_HELP);
|
||||||
|
fprintf(stderr, USAGE_VERSION);
|
||||||
|
fprintf(stderr, USAGE_MAN_TAIL("flock(1)"));
|
||||||
|
exit(ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
static sig_atomic_t timeout_expired = 0;
|
||||||
|
|
||||||
|
static void timeout_handler(int sig __attribute__((__unused__)),
|
||||||
|
siginfo_t *info,
|
||||||
|
void *context __attribute__((__unused__)))
|
||||||
|
{
|
||||||
|
if (info->si_code == SI_TIMER)
|
||||||
|
timeout_expired = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int open_file(const char *filename, int *flags)
|
||||||
|
{
|
||||||
|
|
||||||
|
int fd;
|
||||||
|
int fl = *flags == 0 ? O_RDONLY : *flags;
|
||||||
|
|
||||||
|
errno = 0;
|
||||||
|
fl |= O_NOCTTY | O_CREAT;
|
||||||
|
fd = open(filename, fl, 0666);
|
||||||
|
|
||||||
|
/* Linux doesn't like O_CREAT on a directory, even though it
|
||||||
|
* should be a no-op; POSIX doesn't allow O_RDWR or O_WRONLY
|
||||||
|
*/
|
||||||
|
if (fd < 0 && errno == EISDIR) {
|
||||||
|
fl = O_RDONLY | O_NOCTTY;
|
||||||
|
fd = open(filename, fl);
|
||||||
|
}
|
||||||
|
if (fd < 0) {
|
||||||
|
warn(_("cannot open lock file %s"), filename);
|
||||||
|
if (errno == ENOMEM || errno == EMFILE || errno == ENFILE)
|
||||||
|
exit(EX_OSERR);
|
||||||
|
if (errno == EROFS || errno == ENOSPC)
|
||||||
|
exit(EX_CANTCREAT);
|
||||||
|
exit(EX_NOINPUT);
|
||||||
|
}
|
||||||
|
*flags = fl;
|
||||||
|
return fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int have_timeout = 0;
|
||||||
|
int type = LOCK_EX;
|
||||||
|
int block = 0;
|
||||||
|
int open_flags = 0;
|
||||||
|
int fd = -1;
|
||||||
|
int opt, ix;
|
||||||
|
int do_close = 0;
|
||||||
|
int no_fork = 0;
|
||||||
|
int status;
|
||||||
|
int verbose = 0;
|
||||||
|
struct timeval time_start, time_done;
|
||||||
|
/*
|
||||||
|
* The default exit code for lock conflict or timeout
|
||||||
|
* is specified in man flock.1
|
||||||
|
*/
|
||||||
|
int conflict_exit_code = 101;
|
||||||
|
char **cmd_argv = NULL, *sh_c_argv[4];
|
||||||
|
const char *filename = NULL;
|
||||||
|
enum {
|
||||||
|
OPT_VERBOSE = CHAR_MAX + 1
|
||||||
|
};
|
||||||
|
static const struct option long_options[] = {
|
||||||
|
{"shared", no_argument, NULL, 's'},
|
||||||
|
{"exclusive", no_argument, NULL, 'x'},
|
||||||
|
{"unlock", no_argument, NULL, 'u'},
|
||||||
|
{"nonblocking", no_argument, NULL, 'n'},
|
||||||
|
{"nb", no_argument, NULL, 'n'},
|
||||||
|
{"wait", required_argument, NULL, 'w'},
|
||||||
|
{"conflict-exit-code", required_argument, NULL, 'E'},
|
||||||
|
{"close", no_argument, NULL, 'o'},
|
||||||
|
{"no-fork", no_argument, NULL, 'F'},
|
||||||
|
{"verbose", no_argument, NULL, OPT_VERBOSE},
|
||||||
|
{"help", no_argument, NULL, 'h'},
|
||||||
|
{"version", no_argument, NULL, 'V'},
|
||||||
|
{NULL, 0, NULL, 0}
|
||||||
|
};
|
||||||
|
|
||||||
|
setlocale(LC_ALL, "");
|
||||||
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
||||||
|
textdomain(PACKAGE);
|
||||||
|
|
||||||
|
if (argc < 2)
|
||||||
|
usage(EX_USAGE);
|
||||||
|
|
||||||
|
optopt = 0;
|
||||||
|
while ((opt =
|
||||||
|
getopt_long(argc, argv, "+sexnoFuw:E:hV?", long_options,
|
||||||
|
&ix)) != EOF) {
|
||||||
|
switch (opt) {
|
||||||
|
case 's':
|
||||||
|
type = LOCK_SH;
|
||||||
|
break;
|
||||||
|
case 'e':
|
||||||
|
case 'x':
|
||||||
|
type = LOCK_EX;
|
||||||
|
break;
|
||||||
|
case 'u':
|
||||||
|
type = LOCK_UN;
|
||||||
|
break;
|
||||||
|
case 'o':
|
||||||
|
do_close = 1;
|
||||||
|
break;
|
||||||
|
case 'F':
|
||||||
|
no_fork = 1;
|
||||||
|
break;
|
||||||
|
case 'n':
|
||||||
|
block = LOCK_NB;
|
||||||
|
break;
|
||||||
|
case OPT_VERBOSE:
|
||||||
|
verbose = 1;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
/* optopt will be set if this was an unrecognized
|
||||||
|
* option, i.e. *not* 'h' or '?
|
||||||
|
*/
|
||||||
|
usage(optopt ? EX_USAGE : 0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (no_fork && do_close)
|
||||||
|
errx(EX_USAGE,
|
||||||
|
_("the --no-fork and --close options are incompatible"));
|
||||||
|
|
||||||
|
if (argc > optind + 1) {
|
||||||
|
/* Run command */
|
||||||
|
if (!strcmp(argv[optind + 1], "-c") ||
|
||||||
|
!strcmp(argv[optind + 1], "--command")) {
|
||||||
|
if (argc != optind + 3)
|
||||||
|
errx(EX_USAGE,
|
||||||
|
_("%s requires exactly one command argument"),
|
||||||
|
argv[optind + 1]);
|
||||||
|
cmd_argv = sh_c_argv;
|
||||||
|
cmd_argv[0] = getenv("SHELL");
|
||||||
|
if (!cmd_argv[0] || !*cmd_argv[0])
|
||||||
|
cmd_argv[0] = _PATH_BSHELL;
|
||||||
|
cmd_argv[1] = "-c";
|
||||||
|
cmd_argv[2] = argv[optind + 2];
|
||||||
|
cmd_argv[3] = 0;
|
||||||
|
} else {
|
||||||
|
cmd_argv = &argv[optind + 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
filename = argv[optind];
|
||||||
|
fd = open_file(filename, &open_flags);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
/* Bad options */
|
||||||
|
errx(EX_USAGE, _("requires file descriptor, file or directory"));
|
||||||
|
}
|
||||||
|
|
||||||
|
while (flock(fd, type | block)) {
|
||||||
|
switch (errno) {
|
||||||
|
case EWOULDBLOCK:
|
||||||
|
/* -n option set and failed to lock. */
|
||||||
|
if (verbose)
|
||||||
|
warnx(_("failed to get lock"));
|
||||||
|
exit(conflict_exit_code);
|
||||||
|
case EINTR:
|
||||||
|
/* Signal received */
|
||||||
|
if (timeout_expired) {
|
||||||
|
/* -w option set and failed to lock. */
|
||||||
|
if (verbose)
|
||||||
|
warnx(_("timeout while waiting to get lock"));
|
||||||
|
exit(conflict_exit_code);
|
||||||
|
}
|
||||||
|
/* otherwise try again */
|
||||||
|
continue;
|
||||||
|
case EIO:
|
||||||
|
case EBADF: /* since Linux 3.4 (commit 55725513) */
|
||||||
|
/* Probably NFSv4 where flock() is emulated by fcntl().
|
||||||
|
* Let's try to reopen in read-write mode.
|
||||||
|
*/
|
||||||
|
if (!(open_flags & O_RDWR) &&
|
||||||
|
type != LOCK_SH &&
|
||||||
|
filename &&
|
||||||
|
access(filename, R_OK | W_OK) == 0) {
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
open_flags = O_RDWR;
|
||||||
|
fd = open_file(filename, &open_flags);
|
||||||
|
|
||||||
|
if (open_flags & O_RDWR)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
/* go through */
|
||||||
|
default:
|
||||||
|
/* Other errors */
|
||||||
|
if (filename)
|
||||||
|
warn("%s", filename);
|
||||||
|
else
|
||||||
|
warn("%d", fd);
|
||||||
|
exit((errno == ENOLCK
|
||||||
|
|| errno == ENOMEM) ? EX_OSERR : EX_DATAERR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
status = EX_OK;
|
||||||
|
|
||||||
|
if (cmd_argv) {
|
||||||
|
pid_t w, f;
|
||||||
|
/* Clear any inherited settings */
|
||||||
|
signal(SIGCHLD, SIG_DFL);
|
||||||
|
if (verbose)
|
||||||
|
printf(_("%s: executing %s\n"), program_invocation_short_name, cmd_argv[0]);
|
||||||
|
|
||||||
|
if (!no_fork) {
|
||||||
|
f = fork();
|
||||||
|
if (f < 0) {
|
||||||
|
err(EX_OSERR, _("fork failed"));
|
||||||
|
if (f != 0) {
|
||||||
|
do {
|
||||||
|
w = waitpid(f, &status, 0);
|
||||||
|
if (w == -1 && errno != EINTR)
|
||||||
|
break;
|
||||||
|
} while (w != f);
|
||||||
|
|
||||||
|
if (w == -1) {
|
||||||
|
status = EXIT_FAILURE;
|
||||||
|
warn(_("waitpid failed"));
|
||||||
|
} else if (WIFEXITED(status))
|
||||||
|
status = WEXITSTATUS(status);
|
||||||
|
else if (WIFSIGNALED(status))
|
||||||
|
status = WTERMSIG(status) + 128;
|
||||||
|
else
|
||||||
|
/* WTF? */
|
||||||
|
status = EX_OSERR;
|
||||||
|
}
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (do_close)
|
||||||
|
close(fd);
|
||||||
|
execvp(cmd_argv[0], cmd_argv);
|
||||||
|
/* execvp() failed */
|
||||||
|
warn(_("failed to execute %s"), cmd_argv[0]);
|
||||||
|
_exit((errno == ENOMEM) ? EX_OSERR : EX_UNAVAILABLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
out:
|
||||||
|
return status;
|
||||||
|
}
|
||||||
115
myflock/nls.h
Normal file
115
myflock/nls.h
Normal file
|
|
@ -0,0 +1,115 @@
|
||||||
|
#ifndef UTIL_LINUX_NLS_H
|
||||||
|
#define UTIL_LINUX_NLS_H
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]);
|
||||||
|
|
||||||
|
#ifndef LOCALEDIR
|
||||||
|
#define LOCALEDIR "/usr/share/locale"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef HAVE_LOCALE_H
|
||||||
|
# include <locale.h>
|
||||||
|
#else
|
||||||
|
# undef setlocale
|
||||||
|
# define setlocale(Category, Locale) /* empty */
|
||||||
|
struct lconv
|
||||||
|
{
|
||||||
|
char *decimal_point;
|
||||||
|
};
|
||||||
|
# undef localeconv
|
||||||
|
# define localeconv() NULL
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef ENABLE_NLS
|
||||||
|
# include <libintl.h>
|
||||||
|
# define _(Text) gettext (Text)
|
||||||
|
# ifdef gettext_noop
|
||||||
|
# define N_(String) gettext_noop (String)
|
||||||
|
# else
|
||||||
|
# define N_(String) (String)
|
||||||
|
# endif
|
||||||
|
# define P_(Singular, Plural, n) ngettext (Singular, Plural, n)
|
||||||
|
#else
|
||||||
|
# undef bindtextdomain
|
||||||
|
# define bindtextdomain(Domain, Directory) /* empty */
|
||||||
|
# undef textdomain
|
||||||
|
# define textdomain(Domain) /* empty */
|
||||||
|
# define _(Text) (Text)
|
||||||
|
# define N_(Text) (Text)
|
||||||
|
# define P_(Singular, Plural, n) ((n) == 1 ? (Singular) : (Plural))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef HAVE_LANGINFO_H
|
||||||
|
# include <langinfo.h>
|
||||||
|
#else
|
||||||
|
|
||||||
|
typedef int nl_item;
|
||||||
|
extern char *langinfo_fallback(nl_item item);
|
||||||
|
|
||||||
|
# define nl_langinfo langinfo_fallback
|
||||||
|
|
||||||
|
enum {
|
||||||
|
CODESET = 1,
|
||||||
|
RADIXCHAR,
|
||||||
|
THOUSEP,
|
||||||
|
D_T_FMT,
|
||||||
|
D_FMT,
|
||||||
|
T_FMT,
|
||||||
|
T_FMT_AMPM,
|
||||||
|
AM_STR,
|
||||||
|
PM_STR,
|
||||||
|
|
||||||
|
DAY_1,
|
||||||
|
DAY_2,
|
||||||
|
DAY_3,
|
||||||
|
DAY_4,
|
||||||
|
DAY_5,
|
||||||
|
DAY_6,
|
||||||
|
DAY_7,
|
||||||
|
|
||||||
|
ABDAY_1,
|
||||||
|
ABDAY_2,
|
||||||
|
ABDAY_3,
|
||||||
|
ABDAY_4,
|
||||||
|
ABDAY_5,
|
||||||
|
ABDAY_6,
|
||||||
|
ABDAY_7,
|
||||||
|
|
||||||
|
MON_1,
|
||||||
|
MON_2,
|
||||||
|
MON_3,
|
||||||
|
MON_4,
|
||||||
|
MON_5,
|
||||||
|
MON_6,
|
||||||
|
MON_7,
|
||||||
|
MON_8,
|
||||||
|
MON_9,
|
||||||
|
MON_10,
|
||||||
|
MON_11,
|
||||||
|
MON_12,
|
||||||
|
|
||||||
|
ABMON_1,
|
||||||
|
ABMON_2,
|
||||||
|
ABMON_3,
|
||||||
|
ABMON_4,
|
||||||
|
ABMON_5,
|
||||||
|
ABMON_6,
|
||||||
|
ABMON_7,
|
||||||
|
ABMON_8,
|
||||||
|
ABMON_9,
|
||||||
|
ABMON_10,
|
||||||
|
ABMON_11,
|
||||||
|
ABMON_12,
|
||||||
|
|
||||||
|
ERA_D_FMT,
|
||||||
|
ERA_D_T_FMT,
|
||||||
|
ERA_T_FMT,
|
||||||
|
ALT_DIGITS,
|
||||||
|
CRNCYSTR,
|
||||||
|
YESEXPR,
|
||||||
|
NOEXPR
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* !HAVE_LANGINFO_H */
|
||||||
|
|
||||||
|
#endif /* UTIL_LINUX_NLS_H */
|
||||||
11
myflock/timer.h
Normal file
11
myflock/timer.h
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
#ifndef UTIL_LINUX_TIMER_H
|
||||||
|
#define UTIL_LINUX_TIMER_H
|
||||||
|
|
||||||
|
#include <signal.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
|
||||||
|
extern int setup_timer(timer_t * t_id, struct itimerval *timeout,
|
||||||
|
void (*timeout_handler)(int, siginfo_t *, void *));
|
||||||
|
extern void cancel_timer(timer_t * t_id);
|
||||||
|
|
||||||
|
#endif /* UTIL_LINUX_TIMER_H */
|
||||||
41
zconvey.plugin.zsh
Normal file
41
zconvey.plugin.zsh
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
#
|
||||||
|
# No plugin manager is needed to use this file. All that is needed is adding:
|
||||||
|
# source {where-zconvey-is}/zconvey.plugin.zsh
|
||||||
|
#
|
||||||
|
# to ~/.zshrc.
|
||||||
|
#
|
||||||
|
|
||||||
|
0="${(%):-%N}" # this gives immunity to functionargzero being unset
|
||||||
|
ZCONVEY_REPO_DIR="${0%/*}"
|
||||||
|
ZCONVEY_CONFIG_DIR="$HOME/.config/zconvey"
|
||||||
|
|
||||||
|
#
|
||||||
|
# Update FPATH if:
|
||||||
|
# 1. Not loading with Zplugin
|
||||||
|
# 2. Not having fpath already updated (that would equal: using other plugin manager)
|
||||||
|
#
|
||||||
|
|
||||||
|
if [[ -z "$ZPLG_CUR_PLUGIN" && "${fpath[(r)$ZCONVEY_REPO_DIR]}" != $ZCONVEY_REPO_DIR ]]; then
|
||||||
|
fpath+=( "$ZCONVEY_REPO_DIR" )
|
||||||
|
fi
|
||||||
|
|
||||||
|
typeset -gi ZCONV_ID
|
||||||
|
typeset -hH ZCONV_FD
|
||||||
|
|
||||||
|
# Acquire ID
|
||||||
|
() {
|
||||||
|
mkdir -p "${ZCONVEY_CONFIG_DIR}/locks"
|
||||||
|
integer idx possible_id=1
|
||||||
|
local fd
|
||||||
|
|
||||||
|
# Supported are 100 shells - acquire takes ~330 ms
|
||||||
|
for (( idx=1; idx <= 100; idx ++ )); do
|
||||||
|
:
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
function __convey_preexec() {
|
||||||
|
}
|
||||||
|
|
||||||
|
autoload add-zsh-hook
|
||||||
|
add-zsh-hook preexec __convey_preexec
|
||||||
Loading…
Reference in a new issue