1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
41 /* for O_DIRECTORY and O_DIRECT */
47 #include <sys/types.h>
54 #include <libcfs/libcfs.h>
55 #include <lustre/lustre_user.h>
57 typedef struct flag_mapping {
62 FLAG_MAPPING flag_table[] = {
63 {"O_RDONLY", O_RDONLY},
64 {"O_WRONLY", O_WRONLY},
68 {"O_NOCTTY", O_NOCTTY},
70 {"O_APPEND", O_APPEND},
71 {"O_NONBLOCK", O_NONBLOCK},
72 {"O_NDELAY", O_NDELAY},
75 {"O_DIRECT", O_DIRECT},
77 {"O_LARGEFILE", O_LARGEFILE},
78 {"O_DIRECTORY", O_DIRECTORY},
79 {"O_NOFOLLOW", O_NOFOLLOW},
80 {"O_LOV_DELAY_CREATE", O_LOV_DELAY_CREATE},
84 void Usage_and_abort(void)
86 fprintf(stderr, "Usage: openfile -f flags [ -m mode ] filename \n");
87 fprintf(stderr, "e.g. openfile -f O_RDWR:O_CREAT -m 0755 /etc/passwd\n");
91 int main(int argc, char** argv)
102 char *cloned_flags = NULL;
107 while ((c = getopt (argc, argv, "f:m:")) != -1) {
112 cloned_flags = (char *)malloc(strlen(optarg)+1);
113 if (cloned_flags == NULL) {
114 fprintf(stderr, "Insufficient memory.\n");
119 strncpy(cloned_flags, optarg, strlen(optarg)+1);
120 flags = atoi(cloned_flags);
124 printf("flags = %d\n",flags);
130 for (tmp = strtok(cloned_flags, ":|"); tmp;
131 tmp = strtok(NULL, ":|")) {
134 printf("flags = %s\n",tmp);
137 for (i = 0; flag_table[i].flag != -1; i++) {
138 if (!strcmp(tmp, flag_table[i].string)){
139 flags |= flag_table[i].flag;
144 if (flag_table[i].flag == -1) {
145 fprintf(stderr, "No such flag: %s\n",
152 printf("flags = %x\n", flags);
158 printf("mode = %s\n", optarg);
160 mode = strtol(optarg, NULL, 8);
163 printf("mode = %o\n", mode);
167 fprintf(stderr, "Bad parameters.\n");
173 if (optind == argc) {
174 fprintf(stderr, "Bad parameters.\n");
179 fname = argv[optind];
182 fprintf(stderr, "Missing flag or file-name\n");
189 fd = open(fname, flags, mode);
191 fd = open(fname, flags);
196 printf("Succeed in opening file \"%s\"(flags=%s",
197 fname, cloned_flags);
200 printf(", mode=%o", mode);
204 fprintf(stderr, "Error in opening file \"%s\"(flags=%s",
205 fname, cloned_flags);
207 fprintf(stderr, ", mode=%o", mode);
208 fprintf(stderr, ") %d: %s\n", save_errno, strerror(save_errno));