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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
39 /* for O_DIRECTORY and O_DIRECT */
45 #include <sys/types.h>
52 #include <libcfs/libcfs.h>
53 #include <lustre/lustre_user.h>
55 typedef struct flag_mapping {
60 FLAG_MAPPING flag_table[] = {
61 {"O_RDONLY", O_RDONLY},
62 {"O_WRONLY", O_WRONLY},
66 {"O_NOCTTY", O_NOCTTY},
68 {"O_APPEND", O_APPEND},
69 {"O_NONBLOCK", O_NONBLOCK},
70 {"O_NDELAY", O_NDELAY},
73 {"O_DIRECT", O_DIRECT},
75 {"O_LARGEFILE", O_LARGEFILE},
76 {"O_DIRECTORY", O_DIRECTORY},
77 {"O_NOFOLLOW", O_NOFOLLOW},
78 {"O_LOV_DELAY_CREATE", O_LOV_DELAY_CREATE},
82 void Usage_and_abort(void)
84 fprintf(stderr, "Usage: openfile -f flags [ -m mode ] filename \n");
85 fprintf(stderr, "e.g. openfile -f O_RDWR:O_CREAT -m 0755 /etc/passwd\n");
89 int main(int argc, char** argv)
100 char *cloned_flags = NULL;
105 while ((c = getopt (argc, argv, "f:m:")) != -1) {
110 cloned_flags = (char *)malloc(strlen(optarg)+1);
111 if (cloned_flags == NULL) {
112 fprintf(stderr, "Insufficient memory.\n");
117 strncpy(cloned_flags, optarg, strlen(optarg)+1);
118 flags = atoi(cloned_flags);
122 printf("flags = %d\n",flags);
128 for (tmp = strtok(cloned_flags, ":|"); tmp;
129 tmp = strtok(NULL, ":|")) {
132 printf("flags = %s\n",tmp);
135 for (i = 0; flag_table[i].flag != -1; i++) {
136 if (!strcmp(tmp, flag_table[i].string)){
137 flags |= flag_table[i].flag;
142 if (flag_table[i].flag == -1) {
143 fprintf(stderr, "No such flag: %s\n",
150 printf("flags = %x\n", flags);
156 printf("mode = %s\n", optarg);
158 mode = strtol(optarg, NULL, 8);
161 printf("mode = %o\n", mode);
165 fprintf(stderr, "Bad parameters.\n");
171 if (optind == argc) {
172 fprintf(stderr, "Bad parameters.\n");
177 fname = argv[optind];
180 fprintf(stderr, "Missing flag or file-name\n");
187 fd = open(fname, flags, mode);
189 fd = open(fname, flags);
194 printf("Succeed in opening file \"%s\"(flags=%s",
195 fname, cloned_flags);
198 printf(", mode=%o", mode);
202 fprintf(stderr, "Error in opening file \"%s\"(flags=%s",
203 fname, cloned_flags);
205 fprintf(stderr, ", mode=%o", mode);
206 fprintf(stderr, ") %d: %s\n", save_errno, strerror(save_errno));