Whamcloud - gitweb
b=16150
[fs/lustre-release.git] / libcfs / libcfs / winnt / winnt-usr.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=4:tabstop=4:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #ifndef __KERNEL__
38
39 #define _NTDDK_
40 #include <windows.h>
41 #include <libcfs/libcfs.h>
42
43 void sleep(int time)
44 {
45     DWORD Time = 1000 * time;
46     Sleep(Time);
47 }
48
49 void print_last_error(char* Prefix)
50 {
51     LPVOID lpMsgBuf;
52
53     FormatMessage( 
54         FORMAT_MESSAGE_ALLOCATE_BUFFER |
55         FORMAT_MESSAGE_FROM_SYSTEM |
56         FORMAT_MESSAGE_IGNORE_INSERTS,
57         NULL,
58         GetLastError(),
59         0,
60         (LPTSTR) &lpMsgBuf,
61         0,
62         NULL
63         );
64
65     printf("%s %s", Prefix, (LPTSTR) lpMsgBuf);
66
67     LocalFree(lpMsgBuf);
68 }
69
70
71 int gethostname(char * name, int namelen)
72 {
73     return 0;
74 }
75
76 int ioctl (
77     int handle,
78     int cmd,
79     void *buffer
80     )
81 {
82     printf("hello, world\n");
83     return 0;
84 }
85
86
87 /*
88  * getopt structures & routines
89  */
90
91
92 /* Data type for reentrant functions.  */
93 struct _getopt_data
94 {
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.  */
98   int optind;
99   int opterr;
100   int optopt;
101   char *optarg;
102
103   /* Internal members.  */
104
105   /* True if the internal members have been initialized.  */
106   int __initialized;
107
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.
111
112      If this is zero, or a null string, it means resume the scan
113      by advancing to the next ARGV-element.  */
114   char *__nextchar;
115
116   /* Describe how to deal with options that follow non-option ARGV-elements.
117
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.
121
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.
128
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.
133
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.
140
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.  */
144
145   enum
146     {
147       REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
148     } __ordering;
149
150   /* If the POSIXLY_CORRECT environment variable is set.  */
151   int __posixly_correct;
152
153
154   /* Handle permutation of arguments.  */
155
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.  */
159
160   int __first_nonopt;
161   int __last_nonopt;
162 };
163
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.  */
169
170 char *optarg;
171
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'.
175
176    On entry to `getopt', zero means this is the first call; initialize.
177
178    When `getopt' returns -1, this is the index of the first of the
179    non-option elements that the caller should itself scan.
180
181    Otherwise, `optind' communicates from one call to the next
182    how much of ARGV has been scanned so far.  */
183
184 /* 1003.2 says this must be 1 before any call.  */
185 int optind = 1;
186
187 /* Callers store zero here to inhibit the error message
188    for unrecognized options.  */
189
190 int opterr = 1;
191
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.  */
195
196 int optopt = '?';
197
198 /* Keep a global copy of all internal members of getopt_data.  */
199
200 static struct _getopt_data getopt_data;
201
202
203 /* Initialize the internal data when the first call is made.  */
204
205 static const char *
206 _getopt_initialize (int argc, char *const *argv, const char *optstring,
207                     struct _getopt_data *d)
208 {
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.  */
212
213   d->__first_nonopt = d->__last_nonopt = d->optind;
214
215   d->__nextchar = NULL;
216
217   d->__posixly_correct = 0;
218
219   /* Determine how to handle the ordering of options and nonoptions.  */
220
221   if (optstring[0] == '-')
222     {
223       d->__ordering = RETURN_IN_ORDER;
224       ++optstring;
225     }
226   else if (optstring[0] == '+')
227     {
228       d->__ordering = REQUIRE_ORDER;
229       ++optstring;
230     }
231   else if (d->__posixly_correct)
232     d->__ordering = REQUIRE_ORDER;
233   else
234     d->__ordering = PERMUTE;
235
236   return optstring;
237 }
238
239 /* Scan elements of ARGV (whose length is ARGC) for option characters
240    given in OPTSTRING.
241
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.
247
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.
251
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.)
256
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 '?'.
261
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.
267
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.
271
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.
280
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
283    with other systems.
284
285    LONGOPTS is a vector of `struct option' terminated by an
286    element containing a name which is zero.
287
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
290    recent call.
291
292    If LONG_ONLY is nonzero, '-' as well as '--' can introduce
293    long-named options.  */
294
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.
300
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.  */
303
304 #define SWAP_FLAGS(ch1, ch2)
305
306 static void
307 exchange (char **argv, struct _getopt_data *d)
308 {
309   int bottom = d->__first_nonopt;
310   int middle = d->__last_nonopt;
311   int top = d->optind;
312   char *tem;
313
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.  */
318
319   while (top > middle && middle > bottom)
320     {
321       if (top - middle > middle - bottom)
322         {
323           /* Bottom segment is the short one.  */
324           int len = middle - bottom;
325           register int i;
326
327           /* Swap it with the top part of the top segment.  */
328           for (i = 0; i < len; i++)
329             {
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);
334             }
335           /* Exclude the moved bottom segment from further swapping.  */
336           top -= len;
337         }
338       else
339         {
340           /* Top segment is the short one.  */
341           int len = top - middle;
342           register int i;
343
344           /* Swap it with the bottom part of the bottom segment.  */
345           for (i = 0; i < len; i++)
346             {
347               tem = argv[bottom + i];
348               argv[bottom + i] = argv[middle + i];
349               argv[middle + i] = tem;
350               SWAP_FLAGS (bottom + i, middle + i);
351             }
352           /* Exclude the moved top segment from further swapping.  */
353           bottom += len;
354         }
355     }
356
357   /* Update records for the slots the non-options now occupy.  */
358
359   d->__first_nonopt += (d->optind - d->__last_nonopt);
360   d->__last_nonopt = d->optind;
361 }
362
363 int
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)
367 {
368   int print_errors = d->opterr;
369   if (optstring[0] == ':')
370     print_errors = 0;
371
372   if (argc < 1)
373     return -1;
374
375   d->optarg = NULL;
376
377   if (d->optind == 0 || !d->__initialized)
378     {
379       if (d->optind == 0)
380         d->optind = 1;  /* Don't scan ARGV[0], the program name.  */
381       optstring = _getopt_initialize (argc, argv, optstring, d);
382       d->__initialized = 1;
383     }
384
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.  */
389
390 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
391
392   if (d->__nextchar == NULL || *d->__nextchar == '\0')
393     {
394       /* Advance to the next ARGV-element.  */
395
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;
402
403       if (d->__ordering == PERMUTE)
404         {
405           /* If we have just processed some options following some non-options,
406              exchange them so that the options come first.  */
407
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;
413
414           /* Skip any additional non-options
415              and extend the range of non-options previously skipped.  */
416
417           while (d->optind < argc && NONOPTION_P)
418             d->optind++;
419           d->__last_nonopt = d->optind;
420         }
421
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.  */
426
427       if (d->optind != argc && !strcmp (argv[d->optind], "--"))
428         {
429           d->optind++;
430
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;
437
438           d->optind = argc;
439         }
440
441       /* If we have done all the ARGV-elements, stop the scan
442          and back over any non-options that we skipped and permuted.  */
443
444       if (d->optind == argc)
445         {
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;
450           return -1;
451         }
452
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.  */
455
456       if (NONOPTION_P)
457         {
458           if (d->__ordering == REQUIRE_ORDER)
459             return -1;
460           d->optarg = argv[d->optind++];
461           return 1;
462         }
463
464       /* We have found another option-ARGV-element.
465          Skip the initial punctuation.  */
466
467       d->__nextchar = (argv[d->optind] + 1
468                   + (longopts != NULL && argv[d->optind][1] == '-'));
469     }
470
471   /* Decode the current option-ARGV-element.  */
472
473   /* Check whether the ARGV-element is a long option.
474
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.
479
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".
483
484      This distinction seems to be the most useful approach.  */
485
486   if (longopts != NULL
487       && (argv[d->optind][1] == '-'
488           || (long_only && (argv[d->optind][2]
489                             || !strchr (optstring, argv[d->optind][1])))))
490     {
491       char *nameend;
492       const struct option *p;
493       const struct option *pfound = NULL;
494       int exact = 0;
495       int ambig = 0;
496       int indfound = -1;
497       int option_index;
498
499       for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
500         /* Do nothing.  */ ;
501
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))
506           {
507             if ((unsigned int) (nameend - d->__nextchar)
508                 == (unsigned int) strlen (p->name))
509               {
510                 /* Exact match found.  */
511                 pfound = p;
512                 indfound = option_index;
513                 exact = 1;
514                 break;
515               }
516             else if (pfound == NULL)
517               {
518                 /* First nonexact match found.  */
519                 pfound = p;
520                 indfound = option_index;
521               }
522             else if (long_only
523                      || pfound->has_arg != p->has_arg
524                      || pfound->flag != p->flag
525                      || pfound->val != p->val)
526               /* Second or later nonexact match found.  */
527               ambig = 1;
528           }
529
530       if (ambig && !exact)
531         {
532           if (print_errors)
533             {
534               fprintf (stderr, "%s: option '%s' is ambiguous\n",
535                        argv[0], argv[d->optind]);
536             }
537           d->__nextchar += strlen (d->__nextchar);
538           d->optind++;
539           d->optopt = 0;
540           return '?';
541         }
542
543       if (pfound != NULL)
544         {
545           option_index = indfound;
546           d->optind++;
547           if (*nameend)
548             {
549               /* Don't test has_arg with >, because some C compilers don't
550                  allow it to be used on enums.  */
551               if (pfound->has_arg)
552                 d->optarg = nameend + 1;
553               else
554                 {
555                   if (print_errors)
556                     {
557
558                       if (argv[d->optind - 1][1] == '-')
559                         {
560                           /* --option */
561                           fprintf (stderr, "%s: option '--%s' doesn't allow an argument\n",
562                                    argv[0], pfound->name);
563                         }
564                       else
565                         {
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],
569                                    pfound->name);
570                         }
571
572                     }
573
574                   d->__nextchar += strlen (d->__nextchar);
575
576                   d->optopt = pfound->val;
577                   return '?';
578                 }
579             }
580           else if (pfound->has_arg == 1)
581             {
582               if (d->optind < argc)
583                 d->optarg = argv[d->optind++];
584               else
585                 {
586                   if (print_errors)
587                     {
588                       fprintf (stderr,
589                                "%s: option '%s' requires an argument\n",
590                                argv[0], argv[d->optind - 1]);
591                     }
592                   d->__nextchar += strlen (d->__nextchar);
593                   d->optopt = pfound->val;
594                   return optstring[0] == ':' ? ':' : '?';
595                 }
596             }
597           d->__nextchar += strlen (d->__nextchar);
598           if (longind != NULL)
599             *longind = option_index;
600           if (pfound->flag)
601             {
602               *(pfound->flag) = pfound->val;
603               return 0;
604             }
605           return pfound->val;
606         }
607
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)
614         {
615           if (print_errors)
616             {
617               if (argv[d->optind][1] == '-')
618                 {
619                   /* --option */
620
621                   fprintf (stderr, "%s: unrecognized option '--%s'\n",
622                            argv[0], d->__nextchar);
623                 }
624               else
625                 {
626                   /* +option or -option */
627                   fprintf (stderr, "%s: unrecognized option '%c%s'\n",
628                            argv[0], argv[d->optind][0], d->__nextchar);
629                 }
630
631
632             }
633           d->__nextchar = (char *) "";
634           d->optind++;
635           d->optopt = 0;
636           return '?';
637         }
638     }
639
640   /* Look at and handle the next short option-character.  */
641
642   {
643     char c = *d->__nextchar++;
644     char *temp = strchr (optstring, c);
645
646     /* Increment `optind' when we start to process its last character.  */
647     if (*d->__nextchar == '\0')
648       ++d->optind;
649
650     if (temp == NULL || c == ':')
651       {
652         if (print_errors)
653           {
654             fprintf (stderr, "%s: invalid option -- '%c'\n", argv[0], c);
655           }
656         d->optopt = c;
657         return '?';
658       }
659     /* Convenience. Treat POSIX -W foo same as long option --foo */
660     if (temp[0] == 'W' && temp[1] == ';')
661       {
662         char *nameend;
663         const struct option *p;
664         const struct option *pfound = NULL;
665         int exact = 0;
666         int ambig = 0;
667         int indfound = 0;
668         int option_index;
669
670         /* This is an option that requires an argument.  */
671         if (*d->__nextchar != '\0')
672           {
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.  */
676             d->optind++;
677           }
678         else if (d->optind == argc)
679           {
680             if (print_errors)
681               {
682                 fprintf (stderr,
683                          "%s: option requires an argument -- '%c'\n",
684                          argv[0], c);
685               }
686             d->optopt = c;
687             if (optstring[0] == ':')
688               c = ':';
689             else
690               c = '?';
691             return c;
692           }
693         else
694           /* We already incremented `d->optind' once;
695              increment it again when taking next ARGV-elt as argument.  */
696           d->optarg = argv[d->optind++];
697
698         /* optarg is now the argument, see if it's in the
699            table of longopts.  */
700
701         for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
702              nameend++)
703           /* Do nothing.  */ ;
704
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))
709             {
710               if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
711                 {
712                   /* Exact match found.  */
713                   pfound = p;
714                   indfound = option_index;
715                   exact = 1;
716                   break;
717                 }
718               else if (pfound == NULL)
719                 {
720                   /* First nonexact match found.  */
721                   pfound = p;
722                   indfound = option_index;
723                 }
724               else
725                 /* Second or later nonexact match found.  */
726                 ambig = 1;
727             }
728         if (ambig && !exact)
729           {
730             if (print_errors)
731               {
732                 fprintf (stderr, "%s: option '-W %s' is ambiguous\n",
733                          argv[0], argv[d->optind]);
734               }
735             d->__nextchar += strlen (d->__nextchar);
736             d->optind++;
737             return '?';
738           }
739         if (pfound != NULL)
740           {
741             option_index = indfound;
742             if (*nameend)
743               {
744                 /* Don't test has_arg with >, because some C compilers don't
745                    allow it to be used on enums.  */
746                 if (pfound->has_arg)
747                   d->optarg = nameend + 1;
748                 else
749                   {
750                     if (print_errors)
751                       {
752                         fprintf (stderr, "\
753 %s: option '-W %s' doesn't allow an argument\n",
754                                  argv[0], pfound->name);
755                       }
756
757                     d->__nextchar += strlen (d->__nextchar);
758                     return '?';
759                   }
760               }
761             else if (pfound->has_arg == 1)
762               {
763                 if (d->optind < argc)
764                   d->optarg = argv[d->optind++];
765                 else
766                   {
767                     if (print_errors)
768                       {
769
770                         fprintf (stderr,
771                                  "%s: option '%s' requires an argument\n",
772                                  argv[0], argv[d->optind - 1]);
773                       }
774                     d->__nextchar += strlen (d->__nextchar);
775                     return optstring[0] == ':' ? ':' : '?';
776                   }
777               }
778             d->__nextchar += strlen (d->__nextchar);
779             if (longind != NULL)
780               *longind = option_index;
781             if (pfound->flag)
782               {
783                 *(pfound->flag) = pfound->val;
784                 return 0;
785               }
786             return pfound->val;
787           }
788           d->__nextchar = NULL;
789           return 'W';   /* Let the application handle it.   */
790       }
791     if (temp[1] == ':')
792       {
793         if (temp[2] == ':')
794           {
795             /* This is an option that accepts an argument optionally.  */
796             if (*d->__nextchar != '\0')
797               {
798                 d->optarg = d->__nextchar;
799                 d->optind++;
800               }
801             else
802               d->optarg = NULL;
803             d->__nextchar = NULL;
804           }
805         else
806           {
807             /* This is an option that requires an argument.  */
808             if (*d->__nextchar != '\0')
809               {
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.  */
813                 d->optind++;
814               }
815             else if (d->optind == argc)
816               {
817                 if (print_errors)
818                   {
819                     fprintf (stderr,
820                              "%s: option requires an argument -- '%c'\n",
821                              argv[0], c);
822                   }
823                 d->optopt = c;
824                 if (optstring[0] == ':')
825                   c = ':';
826                 else
827                   c = '?';
828               }
829             else
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;
834           }
835       }
836     return c;
837   }
838 }
839
840 int
841 _getopt_internal (int argc, char *const *argv, const char *optstring,
842                   const struct option *longopts, int *longind, int long_only)
843 {
844   int result;
845
846   getopt_data.optind = optind;
847   getopt_data.opterr = opterr;
848
849   result = _getopt_internal_r (argc, argv, optstring, longopts,
850                                longind, long_only, &getopt_data);
851
852   optind = getopt_data.optind;
853   optarg = getopt_data.optarg;
854   optopt = getopt_data.optopt;
855
856   return result;
857 }
858
859 int
860 getopt_long (int argc, char *const *argv, const char *options,
861              const struct option *long_options, int *opt_index)
862 {
863   return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
864 }
865
866 #define TOLOWER(c) tolower(c)
867 typedef unsigned chartype;
868
869 char *
870 strcasestr (phaystack, pneedle)
871      const char *phaystack;
872      const char *pneedle;
873 {
874   register const unsigned char *haystack, *needle;
875   register chartype b, c;
876
877
878   haystack = (const unsigned char *) phaystack;
879   needle = (const unsigned char *) pneedle;
880
881   b = TOLOWER (*needle);
882   if (b != '\0')
883     {
884       haystack--;                               /* possible ANSI violation */
885       do
886         {
887           c = *++haystack;
888           if (c == '\0')
889             goto ret0;
890         }
891       while (TOLOWER (c) != (int) b);
892
893       c = TOLOWER (*++needle);
894       if (c == '\0')
895         goto foundneedle;
896       ++needle;
897       goto jin;
898
899       for (;;)
900         {
901           register chartype a;
902           register const unsigned char *rhaystack, *rneedle;
903
904           do
905             {
906               a = *++haystack;
907               if (a == '\0')
908                 goto ret0;
909               if (TOLOWER (a) == (int) b)
910                 break;
911               a = *++haystack;
912               if (a == '\0')
913                 goto ret0;
914 shloop:
915               ;
916             }
917           while (TOLOWER (a) != (int) b);
918
919 jin:      a = *++haystack;
920           if (a == '\0')
921             goto ret0;
922
923           if (TOLOWER (a) != (int) c)
924             goto shloop;
925
926           rhaystack = haystack-- + 1;
927           rneedle = needle;
928           a = TOLOWER (*rneedle);
929
930           if (TOLOWER (*rhaystack) == (int) a)
931             do
932               {
933                 if (a == '\0')
934                   goto foundneedle;
935                 ++rhaystack;
936                 a = TOLOWER (*++needle);
937                 if (TOLOWER (*rhaystack) != (int) a)
938                   break;
939                 if (a == '\0')
940                   goto foundneedle;
941                 ++rhaystack;
942                 a = TOLOWER (*++needle);
943               }
944             while (TOLOWER (*rhaystack) == (int) a);
945
946           needle = rneedle;             /* took the register-poor approach */
947
948           if (a == '\0')
949             break;
950         }
951     }
952 foundneedle:
953   return (char*) haystack;
954 ret0:
955   return 0;
956 }
957
958 int glob (const char * __pattern, int __flags,
959                  int (*__errfunc) (const char *, int),
960                  glob_t * __pglob) {
961
962     cfs_enter_debugger();
963     return 0;
964 }
965
966 void globfree(glob_t *__pglog)
967 {
968 }
969
970 int setenv(const char *envname, const char *envval, int overwrite)
971 {
972     int rc = 0;
973
974     if (GetEnvironmentVariable(envname, NULL, 0) == 0) {
975         overwrite = TRUE;
976     }
977
978     if (overwrite) {
979         rc = SetEnvironmentVariable(envname, envval);
980         rc = rc > 0 ? 0 : -1;
981     } else {
982         rc = -1;
983     }
984     return rc;
985 }
986
987 int uname(struct utsname *uts)
988 {
989     OSVERSIONINFO   OsVerInfo;
990
991     /* query computer name */
992     memset(uts, 0, sizeof(struct utsname));
993     strcpy(uts->sysname, "winnt");
994     strcpy(uts->release, "winnt");
995
996     /* query system version */
997     OsVerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
998     GetVersionEx(&OsVerInfo);
999
1000     if (OsVerInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) {
1001         if (OsVerInfo.dwMajorVersion == 6 &&
1002             OsVerInfo.dwBuildNumber > 3790) {
1003             strcpy(uts->release, "Vista");
1004         }
1005     } else {
1006         /* we got errors */
1007         return -1;
1008     }
1009
1010     sprintf(uts->version, "%d.%d", OsVerInfo.dwMajorVersion,
1011             OsVerInfo.dwMinorVersion);
1012     return 0;
1013 }
1014
1015 struct passwd * getpwuid(uid_t uid)
1016 {
1017     static struct passwd generic_passwd = {0, "root"};
1018     return &generic_passwd;
1019 }
1020
1021 void* pgalloc(size_t factor)
1022 {
1023     LPVOID page;
1024
1025     page = VirtualAlloc(NULL, CFS_PAGE_SIZE << factor,
1026                         MEM_COMMIT, PAGE_READWRITE);
1027     return page;
1028 }
1029
1030 void  pgfree(void * page)
1031 {
1032     _ASSERT(page != NULL);
1033     VirtualFree(page, 0, MEM_RELEASE);
1034 }
1035
1036 #endif /* !__KERNEL__ */