Back to index

glibc  2.9
strrchr.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 1995, 1996, 1997, 2003 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <string.h>
00020 
00021 #undef strrchr
00022 
00023 /* Find the last occurrence of C in S.  */
00024 char *
00025 strrchr (const char *s, int c)
00026 {
00027   register const char *found, *p;
00028 
00029   c = (unsigned char) c;
00030 
00031   /* Since strchr is fast, we use it rather than the obvious loop.  */
00032 
00033   if (c == '\0')
00034     return strchr (s, '\0');
00035 
00036   found = NULL;
00037   while ((p = strchr (s, c)) != NULL)
00038     {
00039       found = p;
00040       s = p + 1;
00041     }
00042 
00043   return (char *) found;
00044 }
00045 
00046 #ifdef weak_alias
00047 #undef rindex
00048 weak_alias (strrchr, rindex)
00049 #endif
00050 libc_hidden_builtin_def (strrchr)