Fixed some issues found by winapi_check.
[wine] / dlls / ntdll / string.c
1 /*
2  * NTDLL string functions
3  *
4  * Copyright 2000 Alexandre Julliard
5  * Copyright 2000 Jon Griffiths
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23
24 #include <ctype.h>
25 #include <string.h>
26
27 #include "windef.h"
28
29 /*********************************************************************
30  *                  _memicmp   (NTDLL.@)
31  */
32 INT __cdecl NTDLL__memicmp( LPCSTR s1, LPCSTR s2, DWORD len )
33 {
34     int ret = 0;
35     while (len--)
36     {
37         if ((ret = tolower(*s1) - tolower(*s2))) break;
38         s1++;
39         s2++;
40     }
41     return ret;
42 }
43
44 /*********************************************************************
45  *                  _strupr   (NTDLL.@)
46  */
47 LPSTR __cdecl _strupr( LPSTR str )
48 {
49     LPSTR ret = str;
50     for ( ; *str; str++) *str = toupper(*str);
51     return ret;
52 }
53
54 /*********************************************************************
55  *                  _strlwr   (NTDLL.@)
56  *
57  * convert a string in place to lowercase
58  */
59 LPSTR __cdecl _strlwr( LPSTR str )
60 {
61     LPSTR ret = str;
62     for ( ; *str; str++) *str = tolower(*str);
63     return ret;
64 }
65
66
67 /*********************************************************************
68  *                  _ultoa   (NTDLL.@)
69  */
70 LPSTR  __cdecl _ultoa( unsigned long x, LPSTR buf, INT radix )
71 {
72     char buffer[32], *p;
73
74     p = buffer + sizeof(buffer);
75     *--p = 0;
76     do
77     {
78         int rem = x % radix;
79         *--p = (rem <= 9) ? rem + '0' : rem + 'a' - 10;
80         x /= radix;
81     } while (x);
82     strcpy( buf, p );
83     return buf;
84 }
85
86
87 /*********************************************************************
88  *                  _ltoa   (NTDLL.@)
89  */
90 LPSTR  __cdecl _ltoa( long x, LPSTR buf, INT radix )
91 {
92     LPSTR p = buf;
93     if (x < 0)
94     {
95         *p++ = '-';
96         x = -x;
97     }
98     _ultoa( x, p, radix );
99     return buf;
100 }
101
102
103 /*********************************************************************
104  *                  _itoa           (NTDLL.@)
105  */
106 LPSTR  __cdecl _itoa( int x, LPSTR buf, INT radix )
107 {
108     return _ltoa( x, buf, radix );
109 }
110
111
112 /*********************************************************************
113  *              _splitpath (NTDLL.@)
114  */
115 void __cdecl _splitpath(const char* inpath, char * drv, char * dir,
116                         char* fname, char * ext )
117 {
118   /* Modified PD code from 'snippets' collection. */
119   char ch, *ptr, *p;
120   char pathbuff[MAX_PATH], *path=pathbuff;
121
122   strcpy(pathbuff, inpath);
123
124   /* convert slashes to backslashes for searching */
125   for (ptr = (char*)path; *ptr; ++ptr)
126     if ('/' == *ptr)
127       *ptr = '\\';
128
129   /* look for drive spec */
130   if ('\0' != (ptr = strchr(path, ':')))
131   {
132     ++ptr;
133     if (drv)
134     {
135       strncpy(drv, path, ptr - path);
136       drv[ptr - path] = '\0';
137     }
138     path = ptr;
139   }
140   else if (drv)
141     *drv = '\0';
142
143   /* find rightmost backslash or leftmost colon */
144   if (NULL == (ptr = strrchr(path, '\\')))
145     ptr = (strchr(path, ':'));
146
147   if (!ptr)
148   {
149     ptr = (char *)path; /* no path */
150     if (dir)
151       *dir = '\0';
152   }
153   else
154   {
155     ++ptr; /* skip the delimiter */
156     if (dir)
157     {
158       ch = *ptr;
159       *ptr = '\0';
160       strcpy(dir, path);
161       *ptr = ch;
162     }
163   }
164
165   if (NULL == (p = strrchr(ptr, '.')))
166   {
167     if (fname)
168       strcpy(fname, ptr);
169     if (ext)
170       *ext = '\0';
171   }
172   else
173   {
174     *p = '\0';
175     if (fname)
176       strcpy(fname, ptr);
177     *p = '.';
178     if (ext)
179       strcpy(ext, p);
180   }
181
182   /* Fix pathological case - Win returns ':' as part of the
183    * directory when no drive letter is given.
184    */
185   if (drv && drv[0] == ':')
186   {
187     *drv = '\0';
188     if (dir)
189     {
190       pathbuff[0] = ':';
191       pathbuff[1] = '\0';
192       strcat(pathbuff,dir);
193       strcpy(dir,pathbuff);
194     }
195   }
196 }