1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=4:tabstop=4:
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 (c) 2008, 2010, Oracle and/or its affiliates. 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 #include <libcfs/libcfs.h>
45 DWORD Time = 1000 * time;
49 void print_last_error(char* Prefix)
54 FORMAT_MESSAGE_ALLOCATE_BUFFER |
55 FORMAT_MESSAGE_FROM_SYSTEM |
56 FORMAT_MESSAGE_IGNORE_INSERTS,
65 printf("%s %s", Prefix, (LPTSTR) lpMsgBuf);
71 int gethostname(char * name, int namelen)
82 printf("hello, world\n");
88 * getopt structures & routines
92 /* Data type for reentrant functions. */
95 /* These have exactly the same meaning as the corresponding global
96 variables, except that they are used for the reentrant
97 versions of getopt. */
103 /* Internal members. */
105 /* True if the internal members have been initialized. */
108 /* The next char to be scanned in the option-element
109 in which the last option character we returned was found.
110 This allows us to pick up the scan where we left off.
112 If this is zero, or a null string, it means resume the scan
113 by advancing to the next ARGV-element. */
116 /* Describe how to deal with options that follow non-option ARGV-elements.
118 If the caller did not specify anything,
119 the default is REQUIRE_ORDER if the environment variable
120 POSIXLY_CORRECT is defined, PERMUTE otherwise.
122 REQUIRE_ORDER means don't recognize them as options;
123 stop option processing when the first non-option is seen.
124 This is what Unix does.
125 This mode of operation is selected by either setting the environment
126 variable POSIXLY_CORRECT, or using `+' as the first character
127 of the list of option characters.
129 PERMUTE is the default. We permute the contents of ARGV as we
130 scan, so that eventually all the non-options are at the end.
131 This allows options to be given in any order, even with programs
132 that were not written to expect this.
134 RETURN_IN_ORDER is an option available to programs that were
135 written to expect options and other ARGV-elements in any order
136 and that care about the ordering of the two. We describe each
137 non-option ARGV-element as if it were the argument of an option
138 with character code 1. Using `-' as the first character of the
139 list of option characters selects this mode of operation.
141 The special argument `--' forces an end of option-scanning regardless
142 of the value of `ordering'. In the case of RETURN_IN_ORDER, only
143 `--' can cause `getopt' to return -1 with `optind' != ARGC. */
147 REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
150 /* If the POSIXLY_CORRECT environment variable is set. */
151 int __posixly_correct;
154 /* Handle permutation of arguments. */
156 /* Describe the part of ARGV that contains non-options that have
157 been skipped. `first_nonopt' is the index in ARGV of the first
158 of them; `last_nonopt' is the index after the last of them. */
164 /* For communication from `getopt' to the caller.
165 When `getopt' finds an option that takes an argument,
166 the argument value is returned here.
167 Also, when `ordering' is RETURN_IN_ORDER,
168 each non-option ARGV-element is returned here. */
172 /* Index in ARGV of the next element to be scanned.
173 This is used for communication to and from the caller
174 and for communication between successive calls to `getopt'.
176 On entry to `getopt', zero means this is the first call; initialize.
178 When `getopt' returns -1, this is the index of the first of the
179 non-option elements that the caller should itself scan.
181 Otherwise, `optind' communicates from one call to the next
182 how much of ARGV has been scanned so far. */
184 /* 1003.2 says this must be 1 before any call. */
187 /* Callers store zero here to inhibit the error message
188 for unrecognized options. */
192 /* Set to an option character which was unrecognized.
193 This must be initialized on some systems to avoid linking in the
194 system's own getopt implementation. */
198 /* Keep a global copy of all internal members of getopt_data. */
200 static struct _getopt_data getopt_data;
203 /* Initialize the internal data when the first call is made. */
206 _getopt_initialize (int argc, char *const *argv, const char *optstring,
207 struct _getopt_data *d)
209 /* Start processing options with ARGV-element 1 (since ARGV-element 0
210 is the program name); the sequence of previously skipped
211 non-option ARGV-elements is empty. */
213 d->__first_nonopt = d->__last_nonopt = d->optind;
215 d->__nextchar = NULL;
217 d->__posixly_correct = 0;
219 /* Determine how to handle the ordering of options and nonoptions. */
221 if (optstring[0] == '-')
223 d->__ordering = RETURN_IN_ORDER;
226 else if (optstring[0] == '+')
228 d->__ordering = REQUIRE_ORDER;
231 else if (d->__posixly_correct)
232 d->__ordering = REQUIRE_ORDER;
234 d->__ordering = PERMUTE;
239 /* Scan elements of ARGV (whose length is ARGC) for option characters
242 If an element of ARGV starts with '-', and is not exactly "-" or "--",
243 then it is an option element. The characters of this element
244 (aside from the initial '-') are option characters. If `getopt'
245 is called repeatedly, it returns successively each of the option characters
246 from each of the option elements.
248 If `getopt' finds another option character, it returns that character,
249 updating `optind' and `nextchar' so that the next call to `getopt' can
250 resume the scan with the following option character or ARGV-element.
252 If there are no more option characters, `getopt' returns -1.
253 Then `optind' is the index in ARGV of the first ARGV-element
254 that is not an option. (The ARGV-elements have been permuted
255 so that those that are not options now come last.)
257 OPTSTRING is a string containing the legitimate option characters.
258 If an option character is seen that is not listed in OPTSTRING,
259 return '?' after printing an error message. If you set `opterr' to
260 zero, the error message is suppressed but we still return '?'.
262 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
263 so the following text in the same ARGV-element, or the text of the following
264 ARGV-element, is returned in `optarg'. Two colons mean an option that
265 wants an optional arg; if there is text in the current ARGV-element,
266 it is returned in `optarg', otherwise `optarg' is set to zero.
268 If OPTSTRING starts with `-' or `+', it requests different methods of
269 handling the non-option ARGV-elements.
270 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
272 Long-named options begin with `--' instead of `-'.
273 Their names may be abbreviated as long as the abbreviation is unique
274 or is an exact match for some defined option. If they have an
275 argument, it follows the option name in the same ARGV-element, separated
276 from the option name by a `=', or else the in next ARGV-element.
277 When `getopt' finds a long-named option, it returns 0 if that option's
278 `flag' field is nonzero, the value of the option's `val' field
279 if the `flag' field is zero.
281 The elements of ARGV aren't really const, because we permute them.
282 But we pretend they're const in the prototype to be compatible
285 LONGOPTS is a vector of `struct option' terminated by an
286 element containing a name which is zero.
288 LONGIND returns the index in LONGOPT of the long-named option found.
289 It is only valid when a long-named option has been found by the most
292 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
293 long-named options. */
295 /* Exchange two adjacent subsequences of ARGV.
296 One subsequence is elements [first_nonopt,last_nonopt)
297 which contains all the non-options that have been skipped so far.
298 The other is elements [last_nonopt,optind), which contains all
299 the options processed since those non-options were skipped.
301 `first_nonopt' and `last_nonopt' are relocated so that they describe
302 the new indices of the non-options in ARGV after they are moved. */
304 #define SWAP_FLAGS(ch1, ch2)
307 exchange (char **argv, struct _getopt_data *d)
309 int bottom = d->__first_nonopt;
310 int middle = d->__last_nonopt;
314 /* Exchange the shorter segment with the far end of the longer segment.
315 That puts the shorter segment into the right place.
316 It leaves the longer segment in the right place overall,
317 but it consists of two parts that need to be swapped next. */
319 while (top > middle && middle > bottom)
321 if (top - middle > middle - bottom)
323 /* Bottom segment is the short one. */
324 int len = middle - bottom;
327 /* Swap it with the top part of the top segment. */
328 for (i = 0; i < len; i++)
330 tem = argv[bottom + i];
331 argv[bottom + i] = argv[top - (middle - bottom) + i];
332 argv[top - (middle - bottom) + i] = tem;
333 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
335 /* Exclude the moved bottom segment from further swapping. */
340 /* Top segment is the short one. */
341 int len = top - middle;
344 /* Swap it with the bottom part of the bottom segment. */
345 for (i = 0; i < len; i++)
347 tem = argv[bottom + i];
348 argv[bottom + i] = argv[middle + i];
349 argv[middle + i] = tem;
350 SWAP_FLAGS (bottom + i, middle + i);
352 /* Exclude the moved top segment from further swapping. */
357 /* Update records for the slots the non-options now occupy. */
359 d->__first_nonopt += (d->optind - d->__last_nonopt);
360 d->__last_nonopt = d->optind;
364 _getopt_internal_r (int argc, char *const *argv, const char *optstring,
365 const struct option *longopts, int *longind,
366 int long_only, struct _getopt_data *d)
368 int print_errors = d->opterr;
369 if (optstring[0] == ':')
377 if (d->optind == 0 || !d->__initialized)
380 d->optind = 1; /* Don't scan ARGV[0], the program name. */
381 optstring = _getopt_initialize (argc, argv, optstring, d);
382 d->__initialized = 1;
385 /* Test whether ARGV[optind] points to a non-option argument.
386 Either it does not have option syntax, or there is an environment flag
387 from the shell indicating it is not an option. The later information
388 is only used when the used in the GNU libc. */
390 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
392 if (d->__nextchar == NULL || *d->__nextchar == '\0')
394 /* Advance to the next ARGV-element. */
396 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
397 moved back by the user (who may also have changed the arguments). */
398 if (d->__last_nonopt > d->optind)
399 d->__last_nonopt = d->optind;
400 if (d->__first_nonopt > d->optind)
401 d->__first_nonopt = d->optind;
403 if (d->__ordering == PERMUTE)
405 /* If we have just processed some options following some non-options,
406 exchange them so that the options come first. */
408 if (d->__first_nonopt != d->__last_nonopt
409 && d->__last_nonopt != d->optind)
410 exchange ((char **) argv, d);
411 else if (d->__last_nonopt != d->optind)
412 d->__first_nonopt = d->optind;
414 /* Skip any additional non-options
415 and extend the range of non-options previously skipped. */
417 while (d->optind < argc && NONOPTION_P)
419 d->__last_nonopt = d->optind;
422 /* The special ARGV-element `--' means premature end of options.
423 Skip it like a null option,
424 then exchange with previous non-options as if it were an option,
425 then skip everything else like a non-option. */
427 if (d->optind != argc && !strcmp (argv[d->optind], "--"))
431 if (d->__first_nonopt != d->__last_nonopt
432 && d->__last_nonopt != d->optind)
433 exchange ((char **) argv, d);
434 else if (d->__first_nonopt == d->__last_nonopt)
435 d->__first_nonopt = d->optind;
436 d->__last_nonopt = argc;
441 /* If we have done all the ARGV-elements, stop the scan
442 and back over any non-options that we skipped and permuted. */
444 if (d->optind == argc)
446 /* Set the next-arg-index to point at the non-options
447 that we previously skipped, so the caller will digest them. */
448 if (d->__first_nonopt != d->__last_nonopt)
449 d->optind = d->__first_nonopt;
453 /* If we have come to a non-option and did not permute it,
454 either stop the scan or describe it to the caller and pass it by. */
458 if (d->__ordering == REQUIRE_ORDER)
460 d->optarg = argv[d->optind++];
464 /* We have found another option-ARGV-element.
465 Skip the initial punctuation. */
467 d->__nextchar = (argv[d->optind] + 1
468 + (longopts != NULL && argv[d->optind][1] == '-'));
471 /* Decode the current option-ARGV-element. */
473 /* Check whether the ARGV-element is a long option.
475 If long_only and the ARGV-element has the form "-f", where f is
476 a valid short option, don't consider it an abbreviated form of
477 a long option that starts with f. Otherwise there would be no
478 way to give the -f short option.
480 On the other hand, if there's a long option "fubar" and
481 the ARGV-element is "-fu", do consider that an abbreviation of
482 the long option, just like "--fu", and not "-f" with arg "u".
484 This distinction seems to be the most useful approach. */
487 && (argv[d->optind][1] == '-'
488 || (long_only && (argv[d->optind][2]
489 || !strchr (optstring, argv[d->optind][1])))))
492 const struct option *p;
493 const struct option *pfound = NULL;
499 for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
502 /* Test all long options for either exact match
503 or abbreviated matches. */
504 for (p = longopts, option_index = 0; p->name; p++, option_index++)
505 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
507 if ((unsigned int) (nameend - d->__nextchar)
508 == (unsigned int) strlen (p->name))
510 /* Exact match found. */
512 indfound = option_index;
516 else if (pfound == NULL)
518 /* First nonexact match found. */
520 indfound = option_index;
523 || pfound->has_arg != p->has_arg
524 || pfound->flag != p->flag
525 || pfound->val != p->val)
526 /* Second or later nonexact match found. */
534 fprintf (stderr, "%s: option '%s' is ambiguous\n",
535 argv[0], argv[d->optind]);
537 d->__nextchar += strlen (d->__nextchar);
545 option_index = indfound;
549 /* Don't test has_arg with >, because some C compilers don't
550 allow it to be used on enums. */
552 d->optarg = nameend + 1;
558 if (argv[d->optind - 1][1] == '-')
561 fprintf (stderr, "%s: option '--%s' doesn't allow an argument\n",
562 argv[0], pfound->name);
566 /* +option or -option */
567 fprintf (stderr, "%s: option '%c%s' doesn't allow an argument\n",
568 argv[0], argv[d->optind - 1][0],
574 d->__nextchar += strlen (d->__nextchar);
576 d->optopt = pfound->val;
580 else if (pfound->has_arg == 1)
582 if (d->optind < argc)
583 d->optarg = argv[d->optind++];
589 "%s: option '%s' requires an argument\n",
590 argv[0], argv[d->optind - 1]);
592 d->__nextchar += strlen (d->__nextchar);
593 d->optopt = pfound->val;
594 return optstring[0] == ':' ? ':' : '?';
597 d->__nextchar += strlen (d->__nextchar);
599 *longind = option_index;
602 *(pfound->flag) = pfound->val;
608 /* Can't find it as a long option. If this is not getopt_long_only,
609 or the option starts with '--' or is not a valid short
610 option, then it's an error.
611 Otherwise interpret it as a short option. */
612 if (!long_only || argv[d->optind][1] == '-'
613 || strchr (optstring, *d->__nextchar) == NULL)
617 if (argv[d->optind][1] == '-')
621 fprintf (stderr, "%s: unrecognized option '--%s'\n",
622 argv[0], d->__nextchar);
626 /* +option or -option */
627 fprintf (stderr, "%s: unrecognized option '%c%s'\n",
628 argv[0], argv[d->optind][0], d->__nextchar);
633 d->__nextchar = (char *) "";
640 /* Look at and handle the next short option-character. */
643 char c = *d->__nextchar++;
644 char *temp = strchr (optstring, c);
646 /* Increment `optind' when we start to process its last character. */
647 if (*d->__nextchar == '\0')
650 if (temp == NULL || c == ':')
654 fprintf (stderr, "%s: invalid option -- '%c'\n", argv[0], c);
659 /* Convenience. Treat POSIX -W foo same as long option --foo */
660 if (temp[0] == 'W' && temp[1] == ';')
663 const struct option *p;
664 const struct option *pfound = NULL;
670 /* This is an option that requires an argument. */
671 if (*d->__nextchar != '\0')
673 d->optarg = d->__nextchar;
674 /* If we end this ARGV-element by taking the rest as an arg,
675 we must advance to the next element now. */
678 else if (d->optind == argc)
683 "%s: option requires an argument -- '%c'\n",
687 if (optstring[0] == ':')
694 /* We already incremented `d->optind' once;
695 increment it again when taking next ARGV-elt as argument. */
696 d->optarg = argv[d->optind++];
698 /* optarg is now the argument, see if it's in the
699 table of longopts. */
701 for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
705 /* Test all long options for either exact match
706 or abbreviated matches. */
707 for (p = longopts, option_index = 0; p->name; p++, option_index++)
708 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
710 if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
712 /* Exact match found. */
714 indfound = option_index;
718 else if (pfound == NULL)
720 /* First nonexact match found. */
722 indfound = option_index;
725 /* Second or later nonexact match found. */
732 fprintf (stderr, "%s: option '-W %s' is ambiguous\n",
733 argv[0], argv[d->optind]);
735 d->__nextchar += strlen (d->__nextchar);
741 option_index = indfound;
744 /* Don't test has_arg with >, because some C compilers don't
745 allow it to be used on enums. */
747 d->optarg = nameend + 1;
753 %s: option '-W %s' doesn't allow an argument\n",
754 argv[0], pfound->name);
757 d->__nextchar += strlen (d->__nextchar);
761 else if (pfound->has_arg == 1)
763 if (d->optind < argc)
764 d->optarg = argv[d->optind++];
771 "%s: option '%s' requires an argument\n",
772 argv[0], argv[d->optind - 1]);
774 d->__nextchar += strlen (d->__nextchar);
775 return optstring[0] == ':' ? ':' : '?';
778 d->__nextchar += strlen (d->__nextchar);
780 *longind = option_index;
783 *(pfound->flag) = pfound->val;
788 d->__nextchar = NULL;
789 return 'W'; /* Let the application handle it. */
795 /* This is an option that accepts an argument optionally. */
796 if (*d->__nextchar != '\0')
798 d->optarg = d->__nextchar;
803 d->__nextchar = NULL;
807 /* This is an option that requires an argument. */
808 if (*d->__nextchar != '\0')
810 d->optarg = d->__nextchar;
811 /* If we end this ARGV-element by taking the rest as an arg,
812 we must advance to the next element now. */
815 else if (d->optind == argc)
820 "%s: option requires an argument -- '%c'\n",
824 if (optstring[0] == ':')
830 /* We already incremented `optind' once;
831 increment it again when taking next ARGV-elt as argument. */
832 d->optarg = argv[d->optind++];
833 d->__nextchar = NULL;
841 _getopt_internal (int argc, char *const *argv, const char *optstring,
842 const struct option *longopts, int *longind, int long_only)
846 getopt_data.optind = optind;
847 getopt_data.opterr = opterr;
849 result = _getopt_internal_r (argc, argv, optstring, longopts,
850 longind, long_only, &getopt_data);
852 optind = getopt_data.optind;
853 optarg = getopt_data.optarg;
854 optopt = getopt_data.optopt;
860 getopt_long (int argc, char *const *argv, const char *options,
861 const struct option *long_options, int *opt_index)
863 return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
866 #define TOLOWER(c) tolower(c)
867 typedef unsigned chartype;
870 strcasestr (phaystack, pneedle)
871 const char *phaystack;
874 register const unsigned char *haystack, *needle;
875 register chartype b, c;
878 haystack = (const unsigned char *) phaystack;
879 needle = (const unsigned char *) pneedle;
881 b = TOLOWER (*needle);
884 haystack--; /* possible ANSI violation */
891 while (TOLOWER (c) != (int) b);
893 c = TOLOWER (*++needle);
902 register const unsigned char *rhaystack, *rneedle;
909 if (TOLOWER (a) == (int) b)
917 while (TOLOWER (a) != (int) b);
919 jin: a = *++haystack;
923 if (TOLOWER (a) != (int) c)
926 rhaystack = haystack-- + 1;
928 a = TOLOWER (*rneedle);
930 if (TOLOWER (*rhaystack) == (int) a)
936 a = TOLOWER (*++needle);
937 if (TOLOWER (*rhaystack) != (int) a)
942 a = TOLOWER (*++needle);
944 while (TOLOWER (*rhaystack) == (int) a);
946 needle = rneedle; /* took the register-poor approach */
953 return (char*) haystack;
958 int glob (const char * __pattern, int __flags,
959 int (*__errfunc) (const char *, int),
962 cfs_enter_debugger();
966 void globfree(glob_t *__pglog)
970 int setenv(const char *envname, const char *envval, int overwrite)
974 if (GetEnvironmentVariable(envname, NULL, 0) == 0) {
979 rc = SetEnvironmentVariable(envname, envval);
980 rc = rc > 0 ? 0 : -1;
987 int uname(struct utsname *uts)
989 OSVERSIONINFO OsVerInfo;
991 /* query computer name */
992 memset(uts, 0, sizeof(struct utsname));
993 strcpy(uts->sysname, "winnt");
994 strcpy(uts->release, "winnt");
996 /* query system version */
997 OsVerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
998 GetVersionEx(&OsVerInfo);
1000 if (OsVerInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) {
1001 if (OsVerInfo.dwMajorVersion == 6 &&
1002 OsVerInfo.dwBuildNumber > 3790) {
1003 strcpy(uts->release, "Vista");
1010 sprintf(uts->version, "%d.%d", OsVerInfo.dwMajorVersion,
1011 OsVerInfo.dwMinorVersion);
1015 struct passwd * getpwuid(uid_t uid)
1017 static struct passwd generic_passwd = {0, "root"};
1018 return &generic_passwd;
1021 void* pgalloc(size_t factor)
1025 page = VirtualAlloc(NULL, CFS_PAGE_SIZE << factor,
1026 MEM_COMMIT, PAGE_READWRITE);
1030 void pgfree(void * page)
1032 _ASSERT(page != NULL);
1033 VirtualFree(page, 0, MEM_RELEASE);
1036 #endif /* !__KERNEL__ */