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