~ubuntu-branches/ubuntu/trusty/util-linux/trusty-proposed

« back to all changes in this revision

Viewing changes to sys-utils/flock.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones
  • Date: 2011-11-03 15:38:23 UTC
  • mto: (4.5.5 sid) (1.6.4)
  • mto: This revision was merged to the branch mainline in revision 85.
  • Revision ID: package-import@ubuntu.com-20111103153823-10sx16jprzxlhkqf
ImportĀ upstreamĀ versionĀ 2.20.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
#include <sys/file.h>
40
40
#include <sys/time.h>
41
41
#include <sys/wait.h>
 
42
#include <sys/stat.h>
 
43
#include <fcntl.h>
42
44
 
43
45
#include "nls.h"
44
46
 
60
62
 
61
63
static void usage(int ex)
62
64
{
63
 
  fputs("flock (" PACKAGE_STRING ")\n", stderr);
 
65
  fputs(_("\nUsage:\n"), stderr);
64
66
  fprintf(stderr,
65
 
        _("Usage: %1$s [-sxun][-w #] fd#\n"
66
 
          "       %1$s [-sxon][-w #] file [-c] command...\n"
67
 
          "       %1$s [-sxon][-w #] directory [-c] command...\n"
68
 
          "  -s  --shared     Get a shared lock\n"
69
 
          "  -x  --exclusive  Get an exclusive lock\n"
70
 
          "  -u  --unlock     Remove a lock\n"
71
 
          "  -n  --nonblock   Fail rather than wait\n"
72
 
          "  -w  --timeout    Wait for a limited amount of time\n"
73
 
          "  -o  --close      Close file descriptor before running command\n"
74
 
          "  -c  --command    Run a single command string through the shell\n"
75
 
          "  -h  --help       Display this text\n"
76
 
          "  -V  --version    Display version\n"),
77
 
          program);
 
67
        _(" %1$s [-sxun][-w #] fd#\n"
 
68
          " %1$s [-sxon][-w #] file [-c] command...\n"
 
69
          " %1$s [-sxon][-w #] directory [-c] command...\n"), program);
 
70
 
 
71
  fputs(_("\nOptions:\n"), stderr);
 
72
  fputs(_(" -s  --shared     Get a shared lock\n"
 
73
          " -x  --exclusive  Get an exclusive lock\n"
 
74
          " -u  --unlock     Remove a lock\n"
 
75
          " -n  --nonblock   Fail rather than wait\n"
 
76
          " -w  --timeout    Wait for a limited amount of time\n"
 
77
          " -o  --close      Close file descriptor before running command\n"
 
78
          " -c  --command    Run a single command string through the shell\n"
 
79
          " -h  --help       Display this text\n"
 
80
          " -V  --version    Display version\n\n"), stderr);
78
81
  exit(ex);
79
82
}
80
83
 
126
129
  int have_timeout = 0;
127
130
  int type = LOCK_EX;
128
131
  int block = 0;
 
132
  int open_accmode;
129
133
  int fd = -1;
130
134
  int opt, ix;
131
135
  int do_close = 0;
208
212
    }
209
213
 
210
214
    filename = argv[optind];
211
 
    fd = open(filename, O_RDONLY|O_NOCTTY|O_CREAT, 0666);
 
215
    open_accmode = ((type == LOCK_SH || access(filename, R_OK|W_OK) < 0) ?
 
216
                    O_RDONLY : O_RDWR);
 
217
    fd = open(filename, open_accmode|O_NOCTTY|O_CREAT, 0666);
212
218
    /* Linux doesn't like O_CREAT on a directory, even though it should be a
213
 
       no-op */
 
219
       no-op; POSIX doesn't allow O_RDWR or O_WRONLY */
214
220
    if (fd < 0 && errno == EISDIR)
215
221
        fd = open(filename, O_RDONLY|O_NOCTTY);
216
222