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/
28 * Lustre is a trademark of Sun Microsystems, Inc.
31 /* for O_DIRECTORY and O_DIRECT */
37 #include <sys/types.h>
44 #include <linux/lustre/lustre_user.h>
46 typedef struct flag_mapping {
51 FLAG_MAPPING flag_table[] = {
52 {"O_RDONLY", O_RDONLY},
53 {"O_WRONLY", O_WRONLY},
57 {"O_NOCTTY", O_NOCTTY},
59 {"O_APPEND", O_APPEND},
60 {"O_NONBLOCK", O_NONBLOCK},
61 {"O_NDELAY", O_NDELAY},
64 {"O_DIRECT", O_DIRECT},
66 {"O_LARGEFILE", O_LARGEFILE},
67 {"O_DIRECTORY", O_DIRECTORY},
68 {"O_NOFOLLOW", O_NOFOLLOW},
69 {"O_LOV_DELAY_CREATE", O_LOV_DELAY_CREATE},
73 void Usage_and_abort(void)
75 fprintf(stderr, "Usage: openfile -f flags [ -m mode ] filename\n");
77 "e.g. openfile -f O_RDWR:O_CREAT -m 0755 /etc/passwd\n");
81 int main(int argc, char **argv)
92 char *cloned_flags = NULL;
97 while ((c = getopt(argc, argv, "f:m:")) != -1) {
102 cloned_flags = strdup(optarg);
104 fprintf(stderr, "Insufficient memory.\n");
109 flags = atoi(cloned_flags);
113 printf("flags = %d\n", flags);
120 for (tmp = strtok(cloned_flags, ":|"); tmp;
121 tmp = strtok(NULL, ":|")) {
124 printf("flags = %s\n", tmp);
127 for (i = 0; flag_table[i].flag != -1; i++) {
129 flag_table[i].string)) {
130 flags |= flag_table[i].flag;
135 if (flag_table[i].flag == -1) {
136 fprintf(stderr, "No such flag: %s\n",
143 printf("flags = %x\n", flags);
149 printf("mode = %s\n", optarg);
151 mode = strtol(optarg, NULL, 8);
154 printf("mode = %o\n", mode);
158 fprintf(stderr, "Bad parameters.\n");
164 if (optind == argc) {
165 fprintf(stderr, "Bad parameters.\n");
170 fname = argv[optind];
173 fprintf(stderr, "Missing flag or file-name\n");
179 fd = open(fname, flags, mode);
181 fd = open(fname, flags);
186 printf("Succeed in opening file \"%s\"(flags=%s",
187 fname, cloned_flags);
190 printf(", mode=%o", mode);
194 fprintf(stderr, "Error in opening file \"%s\"(flags=%s",
195 fname, cloned_flags);
197 fprintf(stderr, ", mode=%o", mode);
198 fprintf(stderr, ") %d: %s\n", save_errno, strerror(save_errno));