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) 2008, 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 #include <libcfs/libcfs.h>
43 DWORD Time = 1000 * time;
47 void print_last_error(char* Prefix)
52 FORMAT_MESSAGE_ALLOCATE_BUFFER |
53 FORMAT_MESSAGE_FROM_SYSTEM |
54 FORMAT_MESSAGE_IGNORE_INSERTS,
63 printf("%s %s", Prefix, (LPTSTR) lpMsgBuf);
69 int gethostname(char * name, int namelen)
80 printf("hello, world\n");
86 * getopt structures & routines
90 /* Data type for reentrant functions. */
93 /* These have exactly the same meaning as the corresponding global
94 variables, except that they are used for the reentrant
95 versions of getopt. */
101 /* Internal members. */
103 /* True if the internal members have been initialized. */
106 /* The next char to be scanned in the option-element
107 in which the last option character we returned was found.
108 This allows us to pick up the scan where we left off.
110 If this is zero, or a null string, it means resume the scan
111 by advancing to the next ARGV-element. */
114 /* Describe how to deal with options that follow non-option ARGV-elements.
116 If the caller did not specify anything,
117 the default is REQUIRE_ORDER if the environment variable
118 POSIXLY_CORRECT is defined, PERMUTE otherwise.
120 REQUIRE_ORDER means don't recognize them as options;
121 stop option processing when the first non-option is seen.
122 This is what Unix does.
123 This mode of operation is selected by either setting the environment
124 variable POSIXLY_CORRECT, or using `+' as the first character
125 of the list of option characters.
127 PERMUTE is the default. We permute the contents of ARGV as we
128 scan, so that eventually all the non-options are at the end.
129 This allows options to be given in any order, even with programs
130 that were not written to expect this.
132 RETURN_IN_ORDER is an option available to programs that were
133 written to expect options and other ARGV-elements in any order
134 and that care about the ordering of the two. We describe each
135 non-option ARGV-element as if it were the argument of an option
136 with character code 1. Using `-' as the first character of the
137 list of option characters selects this mode of operation.
139 The special argument `--' forces an end of option-scanning regardless
140 of the value of `ordering'. In the case of RETURN_IN_ORDER, only
141 `--' can cause `getopt' to return -1 with `optind' != ARGC. */
145 REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
148 /* If the POSIXLY_CORRECT environment variable is set. */
149 int __posixly_correct;
152 /* Handle permutation of arguments. */
154 /* Describe the part of ARGV that contains non-options that have
155 been skipped. `first_nonopt' is the index in ARGV of the first
156 of them; `last_nonopt' is the index after the last of them. */
162 /* For communication from `getopt' to the caller.
163 When `getopt' finds an option that takes an argument,
164 the argument value is returned here.
165 Also, when `ordering' is RETURN_IN_ORDER,
166 each non-option ARGV-element is returned here. */
170 /* Index in ARGV of the next element to be scanned.
171 This is used for communication to and from the caller
172 and for communication between successive calls to `getopt'.
174 On entry to `getopt', zero means this is the first call; initialize.
176 When `getopt' returns -1, this is the index of the first of the
177 non-option elements that the caller should itself scan.
179 Otherwise, `optind' communicates from one call to the next
180 how much of ARGV has been scanned so far. */
182 /* 1003.2 says this must be 1 before any call. */
185 /* Callers store zero here to inhibit the error message
186 for unrecognized options. */
190 /* Set to an option character which was unrecognized.
191 This must be initialized on some systems to avoid linking in the
192 system's own getopt implementation. */
196 /* Keep a global copy of all internal members of getopt_data. */
198 static struct _getopt_data getopt_data;
201 /* Initialize the internal data when the first call is made. */
204 _getopt_initialize (int argc, char *const *argv, const char *optstring,
205 struct _getopt_data *d)
207 /* Start processing options with ARGV-element 1 (since ARGV-element 0
208 is the program name); the sequence of previously skipped
209 non-option ARGV-elements is empty. */
211 d->__first_nonopt = d->__last_nonopt = d->optind;
213 d->__nextchar = NULL;
215 d->__posixly_correct = 0;
217 /* Determine how to handle the ordering of options and nonoptions. */
219 if (optstring[0] == '-')
221 d->__ordering = RETURN_IN_ORDER;
224 else if (optstring[0] == '+')
226 d->__ordering = REQUIRE_ORDER;
229 else if (d->__posixly_correct)
230 d->__ordering = REQUIRE_ORDER;
232 d->__ordering = PERMUTE;
237 /* Scan elements of ARGV (whose length is ARGC) for option characters
240 If an element of ARGV starts with '-', and is not exactly "-" or "--",
241 then it is an option element. The characters of this element
242 (aside from the initial '-') are option characters. If `getopt'
243 is called repeatedly, it returns successively each of the option characters
244 from each of the option elements.
246 If `getopt' finds another option character, it returns that character,
247 updating `optind' and `nextchar' so that the next call to `getopt' can
248 resume the scan with the following option character or ARGV-element.
250 If there are no more option characters, `getopt' returns -1.
251 Then `optind' is the index in ARGV of the first ARGV-element
252 that is not an option. (The ARGV-elements have been permuted
253 so that those that are not options now come last.)
255 OPTSTRING is a string containing the legitimate option characters.
256 If an option character is seen that is not listed in OPTSTRING,
257 return '?' after printing an error message. If you set `opterr' to
258 zero, the error message is suppressed but we still return '?'.
260 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
261 so the following text in the same ARGV-element, or the text of the following
262 ARGV-element, is returned in `optarg'. Two colons mean an option that
263 wants an optional arg; if there is text in the current ARGV-element,
264 it is returned in `optarg', otherwise `optarg' is set to zero.
266 If OPTSTRING starts with `-' or `+', it requests different methods of
267 handling the non-option ARGV-elements.
268 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
270 Long-named options begin with `--' instead of `-'.
271 Their names may be abbreviated as long as the abbreviation is unique
272 or is an exact match for some defined option. If they have an
273 argument, it follows the option name in the same ARGV-element, separated
274 from the option name by a `=', or else the in next ARGV-element.
275 When `getopt' finds a long-named option, it returns 0 if that option's
276 `flag' field is nonzero, the value of the option's `val' field
277 if the `flag' field is zero.
279 The elements of ARGV aren't really const, because we permute them.
280 But we pretend they're const in the prototype to be compatible
283 LONGOPTS is a vector of `struct option' terminated by an
284 element containing a name which is zero.
286 LONGIND returns the index in LONGOPT of the long-named option found.
287 It is only valid when a long-named option has been found by the most
290 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
291 long-named options. */
293 /* Exchange two adjacent subsequences of ARGV.
294 One subsequence is elements [first_nonopt,last_nonopt)
295 which contains all the non-options that have been skipped so far.
296 The other is elements [last_nonopt,optind), which contains all
297 the options processed since those non-options were skipped.
299 `first_nonopt' and `last_nonopt' are relocated so that they describe
300 the new indices of the non-options in ARGV after they are moved. */
302 #define SWAP_FLAGS(ch1, ch2)
305 exchange (char **argv, struct _getopt_data *d)
307 int bottom = d->__first_nonopt;
308 int middle = d->__last_nonopt;
312 /* Exchange the shorter segment with the far end of the longer segment.
313 That puts the shorter segment into the right place.
314 It leaves the longer segment in the right place overall,
315 but it consists of two parts that need to be swapped next. */
317 while (top > middle && middle > bottom)
319 if (top - middle > middle - bottom)
321 /* Bottom segment is the short one. */
322 int len = middle - bottom;
325 /* Swap it with the top part of the top segment. */
326 for (i = 0; i < len; i++)
328 tem = argv[bottom + i];
329 argv[bottom + i] = argv[top - (middle - bottom) + i];
330 argv[top - (middle - bottom) + i] = tem;
331 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
333 /* Exclude the moved bottom segment from further swapping. */
338 /* Top segment is the short one. */
339 int len = top - middle;
342 /* Swap it with the bottom part of the bottom segment. */
343 for (i = 0; i < len; i++)
345 tem = argv[bottom + i];
346 argv[bottom + i] = argv[middle + i];
347 argv[middle + i] = tem;
348 SWAP_FLAGS (bottom + i, middle + i);
350 /* Exclude the moved top segment from further swapping. */
355 /* Update records for the slots the non-options now occupy. */
357 d->__first_nonopt += (d->optind - d->__last_nonopt);
358 d->__last_nonopt = d->optind;
362 _getopt_internal_r (int argc, char *const *argv, const char *optstring,
363 const struct option *longopts, int *longind,
364 int long_only, struct _getopt_data *d)
366 int print_errors = d->opterr;
367 if (optstring[0] == ':')
375 if (d->optind == 0 || !d->__initialized)
378 d->optind = 1; /* Don't scan ARGV[0], the program name. */
379 optstring = _getopt_initialize (argc, argv, optstring, d);
380 d->__initialized = 1;
383 /* Test whether ARGV[optind] points to a non-option argument.
384 Either it does not have option syntax, or there is an environment flag
385 from the shell indicating it is not an option. The later information
386 is only used when the used in the GNU libc. */
388 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
390 if (d->__nextchar == NULL || *d->__nextchar == '\0')
392 /* Advance to the next ARGV-element. */
394 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
395 moved back by the user (who may also have changed the arguments). */
396 if (d->__last_nonopt > d->optind)
397 d->__last_nonopt = d->optind;
398 if (d->__first_nonopt > d->optind)
399 d->__first_nonopt = d->optind;
401 if (d->__ordering == PERMUTE)
403 /* If we have just processed some options following some non-options,
404 exchange them so that the options come first. */
406 if (d->__first_nonopt != d->__last_nonopt
407 && d->__last_nonopt != d->optind)
408 exchange ((char **) argv, d);
409 else if (d->__last_nonopt != d->optind)
410 d->__first_nonopt = d->optind;
412 /* Skip any additional non-options
413 and extend the range of non-options previously skipped. */
415 while (d->optind < argc && NONOPTION_P)
417 d->__last_nonopt = d->optind;
420 /* The special ARGV-element `--' means premature end of options.
421 Skip it like a null option,
422 then exchange with previous non-options as if it were an option,
423 then skip everything else like a non-option. */
425 if (d->optind != argc && !strcmp (argv[d->optind], "--"))
429 if (d->__first_nonopt != d->__last_nonopt
430 && d->__last_nonopt != d->optind)
431 exchange ((char **) argv, d);
432 else if (d->__first_nonopt == d->__last_nonopt)
433 d->__first_nonopt = d->optind;
434 d->__last_nonopt = argc;
439 /* If we have done all the ARGV-elements, stop the scan
440 and back over any non-options that we skipped and permuted. */
442 if (d->optind == argc)
444 /* Set the next-arg-index to point at the non-options
445 that we previously skipped, so the caller will digest them. */
446 if (d->__first_nonopt != d->__last_nonopt)
447 d->optind = d->__first_nonopt;
451 /* If we have come to a non-option and did not permute it,
452 either stop the scan or describe it to the caller and pass it by. */
456 if (d->__ordering == REQUIRE_ORDER)
458 d->optarg = argv[d->optind++];
462 /* We have found another option-ARGV-element.
463 Skip the initial punctuation. */
465 d->__nextchar = (argv[d->optind] + 1
466 + (longopts != NULL && argv[d->optind][1] == '-'));
469 /* Decode the current option-ARGV-element. */
471 /* Check whether the ARGV-element is a long option.
473 If long_only and the ARGV-element has the form "-f", where f is
474 a valid short option, don't consider it an abbreviated form of
475 a long option that starts with f. Otherwise there would be no
476 way to give the -f short option.
478 On the other hand, if there's a long option "fubar" and
479 the ARGV-element is "-fu", do consider that an abbreviation of
480 the long option, just like "--fu", and not "-f" with arg "u".
482 This distinction seems to be the most useful approach. */
485 && (argv[d->optind][1] == '-'
486 || (long_only && (argv[d->optind][2]
487 || !strchr (optstring, argv[d->optind][1])))))
490 const struct option *p;
491 const struct option *pfound = NULL;
497 for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
500 /* Test all long options for either exact match
501 or abbreviated matches. */
502 for (p = longopts, option_index = 0; p->name; p++, option_index++)
503 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
505 if ((unsigned int) (nameend - d->__nextchar)
506 == (unsigned int) strlen (p->name))
508 /* Exact match found. */
510 indfound = option_index;
514 else if (pfound == NULL)
516 /* First nonexact match found. */
518 indfound = option_index;
521 || pfound->has_arg != p->has_arg
522 || pfound->flag != p->flag
523 || pfound->val != p->val)
524 /* Second or later nonexact match found. */
532 fprintf (stderr, "%s: option '%s' is ambiguous\n",
533 argv[0], argv[d->optind]);
535 d->__nextchar += strlen (d->__nextchar);
543 option_index = indfound;
547 /* Don't test has_arg with >, because some C compilers don't
548 allow it to be used on enums. */
550 d->optarg = nameend + 1;
556 if (argv[d->optind - 1][1] == '-')
559 fprintf (stderr, "%s: option '--%s' doesn't allow an argument\n",
560 argv[0], pfound->name);
564 /* +option or -option */
565 fprintf (stderr, "%s: option '%c%s' doesn't allow an argument\n",
566 argv[0], argv[d->optind - 1][0],
572 d->__nextchar += strlen (d->__nextchar);
574 d->optopt = pfound->val;
578 else if (pfound->has_arg == 1)
580 if (d->optind < argc)
581 d->optarg = argv[d->optind++];
587 "%s: option '%s' requires an argument\n",
588 argv[0], argv[d->optind - 1]);
590 d->__nextchar += strlen (d->__nextchar);
591 d->optopt = pfound->val;
592 return optstring[0] == ':' ? ':' : '?';
595 d->__nextchar += strlen (d->__nextchar);
597 *longind = option_index;
600 *(pfound->flag) = pfound->val;
606 /* Can't find it as a long option. If this is not getopt_long_only,
607 or the option starts with '--' or is not a valid short
608 option, then it's an error.
609 Otherwise interpret it as a short option. */
610 if (!long_only || argv[d->optind][1] == '-'
611 || strchr (optstring, *d->__nextchar) == NULL)
615 if (argv[d->optind][1] == '-')
619 fprintf (stderr, "%s: unrecognized option '--%s'\n",
620 argv[0], d->__nextchar);
624 /* +option or -option */
625 fprintf (stderr, "%s: unrecognized option '%c%s'\n",
626 argv[0], argv[d->optind][0], d->__nextchar);
631 d->__nextchar = (char *) "";
638 /* Look at and handle the next short option-character. */
641 char c = *d->__nextchar++;
642 char *temp = strchr (optstring, c);
644 /* Increment `optind' when we start to process its last character. */
645 if (*d->__nextchar == '\0')
648 if (temp == NULL || c == ':')
652 fprintf (stderr, "%s: invalid option -- '%c'\n", argv[0], c);
657 /* Convenience. Treat POSIX -W foo same as long option --foo */
658 if (temp[0] == 'W' && temp[1] == ';')
661 const struct option *p;
662 const struct option *pfound = NULL;
668 /* This is an option that requires an argument. */
669 if (*d->__nextchar != '\0')
671 d->optarg = d->__nextchar;
672 /* If we end this ARGV-element by taking the rest as an arg,
673 we must advance to the next element now. */
676 else if (d->optind == argc)
681 "%s: option requires an argument -- '%c'\n",
685 if (optstring[0] == ':')
692 /* We already incremented `d->optind' once;
693 increment it again when taking next ARGV-elt as argument. */
694 d->optarg = argv[d->optind++];
696 /* optarg is now the argument, see if it's in the
697 table of longopts. */
699 for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
703 /* Test all long options for either exact match
704 or abbreviated matches. */
705 for (p = longopts, option_index = 0; p->name; p++, option_index++)
706 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
708 if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
710 /* Exact match found. */
712 indfound = option_index;
716 else if (pfound == NULL)
718 /* First nonexact match found. */
720 indfound = option_index;
723 /* Second or later nonexact match found. */
730 fprintf (stderr, "%s: option '-W %s' is ambiguous\n",
731 argv[0], argv[d->optind]);
733 d->__nextchar += strlen (d->__nextchar);
739 option_index = indfound;
742 /* Don't test has_arg with >, because some C compilers don't
743 allow it to be used on enums. */
745 d->optarg = nameend + 1;
751 %s: option '-W %s' doesn't allow an argument\n",
752 argv[0], pfound->name);
755 d->__nextchar += strlen (d->__nextchar);
759 else if (pfound->has_arg == 1)
761 if (d->optind < argc)
762 d->optarg = argv[d->optind++];
769 "%s: option '%s' requires an argument\n",
770 argv[0], argv[d->optind - 1]);
772 d->__nextchar += strlen (d->__nextchar);
773 return optstring[0] == ':' ? ':' : '?';
776 d->__nextchar += strlen (d->__nextchar);
778 *longind = option_index;
781 *(pfound->flag) = pfound->val;
786 d->__nextchar = NULL;
787 return 'W'; /* Let the application handle it. */
793 /* This is an option that accepts an argument optionally. */
794 if (*d->__nextchar != '\0')
796 d->optarg = d->__nextchar;
801 d->__nextchar = NULL;
805 /* This is an option that requires an argument. */
806 if (*d->__nextchar != '\0')
808 d->optarg = d->__nextchar;
809 /* If we end this ARGV-element by taking the rest as an arg,
810 we must advance to the next element now. */
813 else if (d->optind == argc)
818 "%s: option requires an argument -- '%c'\n",
822 if (optstring[0] == ':')
828 /* We already incremented `optind' once;
829 increment it again when taking next ARGV-elt as argument. */
830 d->optarg = argv[d->optind++];
831 d->__nextchar = NULL;
839 _getopt_internal (int argc, char *const *argv, const char *optstring,
840 const struct option *longopts, int *longind, int long_only)
844 getopt_data.optind = optind;
845 getopt_data.opterr = opterr;
847 result = _getopt_internal_r (argc, argv, optstring, longopts,
848 longind, long_only, &getopt_data);
850 optind = getopt_data.optind;
851 optarg = getopt_data.optarg;
852 optopt = getopt_data.optopt;
858 getopt_long (int argc, char *const *argv, const char *options,
859 const struct option *long_options, int *opt_index)
861 return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
864 #define TOLOWER(c) tolower(c)
865 typedef unsigned chartype;
868 strcasestr (phaystack, pneedle)
869 const char *phaystack;
872 register const unsigned char *haystack, *needle;
873 register chartype b, c;
876 haystack = (const unsigned char *) phaystack;
877 needle = (const unsigned char *) pneedle;
879 b = TOLOWER (*needle);
882 haystack--; /* possible ANSI violation */
889 while (TOLOWER (c) != (int) b);
891 c = TOLOWER (*++needle);
900 register const unsigned char *rhaystack, *rneedle;
907 if (TOLOWER (a) == (int) b)
915 while (TOLOWER (a) != (int) b);
917 jin: a = *++haystack;
921 if (TOLOWER (a) != (int) c)
924 rhaystack = haystack-- + 1;
926 a = TOLOWER (*rneedle);
928 if (TOLOWER (*rhaystack) == (int) a)
934 a = TOLOWER (*++needle);
935 if (TOLOWER (*rhaystack) != (int) a)
940 a = TOLOWER (*++needle);
942 while (TOLOWER (*rhaystack) == (int) a);
944 needle = rneedle; /* took the register-poor approach */
951 return (char*) haystack;
956 int glob (const char * __pattern, int __flags,
957 int (*__errfunc) (const char *, int),
960 cfs_enter_debugger();
964 void globfree(glob_t *__pglog)
968 int setenv(const char *envname, const char *envval, int overwrite)
972 if (GetEnvironmentVariable(envname, NULL, 0) == 0) {
977 rc = SetEnvironmentVariable(envname, envval);
978 rc = rc > 0 ? 0 : -1;
985 int uname(struct utsname *uts)
987 OSVERSIONINFO OsVerInfo;
989 /* query computer name */
990 memset(uts, 0, sizeof(struct utsname));
991 strcpy(uts->sysname, "winnt");
992 strcpy(uts->release, "winnt");
994 /* query system version */
995 OsVerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
996 GetVersionEx(&OsVerInfo);
998 if (OsVerInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) {
999 if (OsVerInfo.dwMajorVersion == 6 &&
1000 OsVerInfo.dwBuildNumber > 3790) {
1001 strcpy(uts->release, "Vista");
1008 sprintf(uts->version, "%d.%d", OsVerInfo.dwMajorVersion,
1009 OsVerInfo.dwMinorVersion);
1013 struct passwd * getpwuid(uid_t uid)
1015 static struct passwd generic_passwd = {0, "root"};
1016 return &generic_passwd;
1019 void* pgalloc(size_t factor)
1023 page = VirtualAlloc(NULL, PAGE_CACHE_SIZE << factor,
1024 MEM_COMMIT, PAGE_READWRITE);
1028 void pgfree(void * page)
1030 _ASSERT(page != NULL);
1031 VirtualFree(page, 0, MEM_RELEASE);
1034 #endif /* !__KERNEL__ */