4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
27 * This file is part of Lustre, http://www.lustre.org/
30 /* for O_DIRECTORY and O_DIRECT */
36 #include <sys/types.h>
43 #include <linux/lustre/lustre_user.h>
45 typedef struct flag_mapping {
50 FLAG_MAPPING flag_table[] = {
51 {"O_RDONLY", O_RDONLY},
52 {"O_WRONLY", O_WRONLY},
56 {"O_NOCTTY", O_NOCTTY},
58 {"O_APPEND", O_APPEND},
59 {"O_NONBLOCK", O_NONBLOCK},
60 {"O_NDELAY", O_NDELAY},
63 {"O_DIRECT", O_DIRECT},
65 {"O_LARGEFILE", O_LARGEFILE},
66 {"O_DIRECTORY", O_DIRECTORY},
67 {"O_NOFOLLOW", O_NOFOLLOW},
68 {"O_LOV_DELAY_CREATE", O_LOV_DELAY_CREATE},
72 void Usage_and_abort(void)
74 fprintf(stderr, "Usage: openfile -f flags [ -m mode ] filename\n");
76 "e.g. openfile -f O_RDWR:O_CREAT -m 0755 /etc/passwd\n");
80 int main(int argc, char **argv)
91 char *cloned_flags = NULL;
96 while ((c = getopt(argc, argv, "f:m:")) != -1) {
101 cloned_flags = strdup(optarg);
103 fprintf(stderr, "Insufficient memory.\n");
108 flags = atoi(cloned_flags);
112 printf("flags = %d\n", flags);
119 for (tmp = strtok(cloned_flags, ":|"); tmp;
120 tmp = strtok(NULL, ":|")) {
123 printf("flags = %s\n", tmp);
126 for (i = 0; flag_table[i].flag != -1; i++) {
128 flag_table[i].string)) {
129 flags |= flag_table[i].flag;
134 if (flag_table[i].flag == -1) {
135 fprintf(stderr, "No such flag: %s\n",
142 printf("flags = %x\n", flags);
148 printf("mode = %s\n", optarg);
150 mode = strtol(optarg, NULL, 8);
153 printf("mode = %o\n", mode);
157 fprintf(stderr, "Bad parameters.\n");
163 if (optind == argc) {
164 fprintf(stderr, "Bad parameters.\n");
169 fname = argv[optind];
172 fprintf(stderr, "Missing flag or file-name\n");
178 fd = open(fname, flags, mode);
180 fd = open(fname, flags);
185 printf("Succeed in opening file \"%s\"(flags=%s",
186 fname, cloned_flags);
189 printf(", mode=%o", mode);
193 fprintf(stderr, "Error in opening file \"%s\"(flags=%s",
194 fname, cloned_flags);
196 fprintf(stderr, ", mode=%o", mode);
197 fprintf(stderr, ") %d: %s\n", save_errno, strerror(save_errno));