Whamcloud - gitweb
merge b_devel into HEAD, which will become 0.7.3
[fs/lustre-release.git] / lustre / tests / openfile.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4
5 #if 0
6 #define DEBUG
7 #endif
8
9 #define _GNU_SOURCE
10
11 #include <stdio.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <errno.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19
20 typedef struct flag_mapping {
21        const char *string;
22        const int  flag;
23 } FLAG_MAPPING;
24
25 FLAG_MAPPING flag_table[] = {
26        {"O_RDONLY", O_RDONLY},
27        {"O_WRONLY", O_WRONLY},
28        {"O_RDWR", O_RDWR},
29        {"O_CREAT", O_CREAT},
30        {"O_EXCL", O_EXCL},
31        {"O_NOCTTY", O_NOCTTY},
32        {"O_TRUNC", O_TRUNC},
33        {"O_APPEND", O_APPEND},
34        {"O_NONBLOCK", O_NONBLOCK},
35        {"O_NDELAY", O_NDELAY},
36        {"O_SYNC", O_SYNC},
37        {"O_NOFOLLOW", O_NOFOLLOW},
38        {"O_DIRECTORY", O_DIRECTORY},
39        {"O_LARGEFILE", O_LARGEFILE},
40        {"", -1}
41 };
42
43 void Usage_and_abort(void)
44 {
45        fprintf(stderr, "Usage: openfile -f flags [ -m mode ] filename \n");
46        fprintf(stderr, "e.g. openfile -f O_RDWR:O_CREAT -m 0755 /etc/passwd\n");
47        exit(-1);
48 }
49
50 int main(int argc, char** argv)
51 {
52         int    fd;
53         int    flags=0;
54         mode_t mode=0;
55         char*  fname=NULL;
56         int    mode_set=0;
57         int    flag_set=0;
58         int    file_set=0;
59         char   c;
60         char*  cloned_flags;
61
62         if (argc == 1)
63                 Usage_and_abort();
64
65         while ((c = getopt (argc, argv, "f:m:")) != -1) {
66                 switch (c) {
67                 case 'f': {
68                         char *tmp;
69
70                         cloned_flags = (char *)malloc(strlen(optarg)+1);
71                         if (cloned_flags == NULL) {
72                                 fprintf(stderr, "Insufficient memory.\n");
73                                 exit(-1);
74                         }
75
76                         strncpy(cloned_flags, optarg, strlen(optarg)+1);
77                         for (tmp = strtok(optarg, ":|"); tmp;
78                              tmp = strtok(NULL, ":|")) {
79                                 int i = 0;
80 #ifdef DEBUG
81                                 printf("flags = %s\n",tmp);
82 #endif
83                                 flag_set = 1;
84                                 for (i = 0; flag_table[i].flag != -1; i++) {
85                                         if (!strcmp(tmp, flag_table[i].string)){
86                                                 flags |= flag_table[i].flag;
87                                                 break;
88                                         }
89                                 }
90
91                                 if (flag_table[i].flag == -1) {
92                                         fprintf(stderr, "No such flag: %s\n",
93                                                 tmp);
94                                         exit(-1);
95                                 }
96                         }
97 #ifdef DEBUG
98                         printf("flags = %x\n", flags);
99 #endif
100                         break;
101                 }
102                 case 'm':
103 #ifdef DEBUG
104                         printf("mode = %s\n", optarg);
105 #endif
106                         mode = strtol(optarg, NULL, 8);
107                         mode_set = 1;
108 #ifdef DEBUG
109                         printf("mode = %o\n", mode);
110 #endif
111                         break;
112                 default:
113                         fprintf(stderr, "Bad parameters.\n");
114                         Usage_and_abort();
115                 }
116         }
117
118         if (optind == argc) {
119                 fprintf(stderr, "Bad parameters.\n");
120                 Usage_and_abort();
121         }
122
123         fname = argv[optind];
124         file_set = 1;
125
126         if (!flag_set || !file_set) {
127                 fprintf(stderr, "Missing flag or file-name\n");
128                 exit(-1);
129         }
130
131
132         if (mode_set)
133                 fd = open(fname, flags, mode);
134         else
135                 fd = open(fname, flags);
136
137         if (fd != -1) {
138                 fprintf(stderr, "Succeed in opening file \"%s\"(flags=%s",
139                         fname, cloned_flags);
140
141                 if (mode_set)
142                         fprintf(stderr, ", mode=%o", mode);
143                 fprintf(stderr, ")\n");
144                 close(fd);
145         } else {
146                 fprintf(stderr, "Error in opening file \"%s\"(flags=%s",
147                         fname, cloned_flags);
148                 if (mode_set)
149                         fprintf(stderr, ", mode=%o", mode);
150                 fprintf(stderr, ") %d: %s\n", errno, strerror(errno));
151         }
152         return errno;
153 }