Use strchr() and strrchr().

It seems strchr() and strrchr() are used more often than index() and
rindex(). Therefore, simply migrate all kernel code to use it.

For the XFS code, remove an empty line to make the code identical to
the code in the Linux kernel.
This commit is contained in:
Ed Schouten 2012-01-02 12:12:10 +00:00
parent 762ad1d614
commit dc15eac046
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=229272
20 changed files with 47 additions and 49 deletions

View File

@ -253,7 +253,7 @@ db_inputchar(c)
db_putnchars(BACKUP, db_lc - db_lbuf_start); db_putnchars(BACKUP, db_lc - db_lbuf_start);
db_putnchars(BLANK, db_le - db_lbuf_start); db_putnchars(BLANK, db_le - db_lbuf_start);
db_putnchars(BACKUP, db_le - db_lbuf_start); db_putnchars(BACKUP, db_le - db_lbuf_start);
db_le = index(db_lbuf_start, '\0'); db_le = strchr(db_lbuf_start, '\0');
if (db_le[-1] == '\r' || db_le[-1] == '\n') if (db_le[-1] == '\r' || db_le[-1] == '\n')
*--db_le = '\0'; *--db_le = '\0';
db_lc = db_le; db_lc = db_le;

View File

@ -532,8 +532,8 @@ ad_describe(device_t dev)
u_int8_t *marker, vendor[64], product[64]; u_int8_t *marker, vendor[64], product[64];
/* try to separate the ATA model string into vendor and model parts */ /* try to separate the ATA model string into vendor and model parts */
if ((marker = index(atadev->param.model, ' ')) || if ((marker = strchr(atadev->param.model, ' ')) ||
(marker = index(atadev->param.model, '-'))) { (marker = strchr(atadev->param.model, '-'))) {
int len = (marker - atadev->param.model); int len = (marker - atadev->param.model);
strncpy(vendor, atadev->param.model, len); strncpy(vendor, atadev->param.model, len);

View File

@ -2827,7 +2827,7 @@ mxge_media_init(mxge_softc_t *sc)
} }
for (i = 0; i < 3; i++, ptr++) { for (i = 0; i < 3; i++, ptr++) {
ptr = index(ptr, '-'); ptr = strchr(ptr, '-');
if (ptr == NULL) { if (ptr == NULL) {
device_printf(sc->dev, device_printf(sc->dev,
"only %d dashes in PC?!?\n", i); "only %d dashes in PC?!?\n", i);

View File

@ -71,7 +71,7 @@ uart_cpu_channel(char *dev)
if ((aliases = OF_finddevice("/aliases")) != -1) if ((aliases = OF_finddevice("/aliases")) != -1)
(void)OF_getprop(aliases, dev, alias, sizeof(alias)); (void)OF_getprop(aliases, dev, alias, sizeof(alias));
len = strlen(alias); len = strlen(alias);
if ((p = rindex(alias, ':')) == NULL) if ((p = strrchr(alias, ':')) == NULL)
return (0); return (0);
p++; p++;
if (p - alias == len - 1 && (*p == 'a' || *p == 'b')) if (p - alias == len - 1 && (*p == 'a' || *p == 'b'))

View File

@ -712,7 +712,7 @@ MALLOC_DECLARE(M_NEWNFSDROLLBACK);
/* /*
* Set this macro to index() or strchr(), whichever is supported. * Set this macro to index() or strchr(), whichever is supported.
*/ */
#define STRCHR(s, c) index((s), (c)) #define STRCHR(s, c) strchr((s), (c))
/* /*
* Set the n_time in the client write rpc, as required. * Set the n_time in the client write rpc, as required.

View File

@ -206,10 +206,10 @@ static int nwfs_mount(struct mount *mp)
pe = pc+sizeof(mp->mnt_stat.f_mntfromname); pe = pc+sizeof(mp->mnt_stat.f_mntfromname);
bzero(pc, MNAMELEN); bzero(pc, MNAMELEN);
*(pc++) = '/'; *(pc++) = '/';
pc = index(strncpy(pc, conn->li.server, pe-pc-2),0); pc = strchr(strncpy(pc, conn->li.server, pe - pc - 2), 0);
if (pc < pe-1) { if (pc < pe-1) {
*(pc++) = ':'; *(pc++) = ':';
pc=index(strncpy(pc, conn->li.user, pe-pc-2),0); pc = strchr(strncpy(pc, conn->li.user, pe - pc - 2), 0);
if (pc < pe-1) { if (pc < pe-1) {
*(pc++) = '/'; *(pc++) = '/';
strncpy(pc, nmp->m.mounted_vol, pe-pc-2); strncpy(pc, nmp->m.mounted_vol, pe-pc-2);

View File

@ -234,10 +234,10 @@ smbfs_mount(struct mount *mp)
bzero(pc, MNAMELEN); bzero(pc, MNAMELEN);
*pc++ = '/'; *pc++ = '/';
*pc++ = '/'; *pc++ = '/';
pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0); pc = strchr(strncpy(pc, vcp->vc_username, pe - pc - 2), 0);
if (pc < pe-1) { if (pc < pe-1) {
*(pc++) = '@'; *(pc++) = '@';
pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0); pc = strchr(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0);
if (pc < pe - 1) { if (pc < pe - 1) {
*(pc++) = '/'; *(pc++) = '/';
strncpy(pc, ssp->ss_name, pe - pc - 2); strncpy(pc, ssp->ss_name, pe - pc - 2);

View File

@ -1039,7 +1039,7 @@ smbfs_pathcheck(struct smbmount *smp, const char *name, int nmlen, int nameiop)
* Backslash characters, being a path delimiter, are prohibited * Backslash characters, being a path delimiter, are prohibited
* within a path component even for LOOKUP operations. * within a path component even for LOOKUP operations.
*/ */
if (index(name, '\\') != NULL) if (strchr(name, '\\') != NULL)
return ENOENT; return ENOENT;
if (nameiop == LOOKUP) if (nameiop == LOOKUP)
@ -1051,20 +1051,20 @@ smbfs_pathcheck(struct smbmount *smp, const char *name, int nmlen, int nameiop)
*/ */
if (nmlen > 12) if (nmlen > 12)
return ENAMETOOLONG; return ENAMETOOLONG;
cp = index(name, '.'); cp = strchr(name, '.');
if (cp == NULL) if (cp == NULL)
return error; return error;
if (cp == name || (cp - name) > 8) if (cp == name || (cp - name) > 8)
return error; return error;
cp = index(cp + 1, '.'); cp = strchr(cp + 1, '.');
if (cp != NULL) if (cp != NULL)
return error; return error;
for (cp = name, i = 0; i < nmlen; i++, cp++) for (cp = name, i = 0; i < nmlen; i++, cp++)
if (index(badchars83, *cp) != NULL) if (strchr(badchars83, *cp) != NULL)
return error; return error;
} }
for (cp = name, i = 0; i < nmlen; i++, cp++) for (cp = name, i = 0; i < nmlen; i++, cp++)
if (index(badchars, *cp) != NULL) if (strchr(badchars, *cp) != NULL)
return error; return error;
return 0; return 0;
} }

View File

@ -1743,8 +1743,7 @@ xfs_parseargs(
while ((this_char = strsep(&options, ",")) != NULL) { while ((this_char = strsep(&options, ",")) != NULL) {
if (!*this_char) if (!*this_char)
continue; continue;
if ((value = strchr(this_char, '=')) != NULL)
if ((value = index(this_char, '=')) != NULL)
*value++ = 0; *value++ = 0;
if (!strcmp(this_char, MNTOPT_LOGBUFS)) { if (!strcmp(this_char, MNTOPT_LOGBUFS)) {

View File

@ -152,7 +152,7 @@ ibcs2_getipdomainname(td, uap)
/* Get the domain name. */ /* Get the domain name. */
getcredhostname(td->td_ucred, hname, sizeof(hname)); getcredhostname(td->td_ucred, hname, sizeof(hname));
dptr = index(hname, '.'); dptr = strchr(hname, '.');
if ( dptr ) if ( dptr )
dptr++; dptr++;
else else
@ -182,7 +182,7 @@ ibcs2_setipdomainname(td, uap)
return EINVAL; return EINVAL;
/* Get the host's unqualified name (strip off the domain) */ /* Get the host's unqualified name (strip off the domain) */
ptr = index(hname, '.'); ptr = strchr(hname, '.');
if ( ptr != NULL ) { if ( ptr != NULL ) {
ptr++; ptr++;
*ptr = '\0'; *ptr = '\0';

View File

@ -212,7 +212,7 @@ ibcs2_utssys(td, uap)
IBCS2_UNAME_VERSION, sizeof(sut.version) - 1); IBCS2_UNAME_VERSION, sizeof(sut.version) - 1);
getcredhostname(td->td_ucred, machine_name, getcredhostname(td->td_ucred, machine_name,
sizeof(machine_name) - 1); sizeof(machine_name) - 1);
p = index(machine_name, '.'); p = strchr(machine_name, '.');
if ( p ) if ( p )
*p = '\0'; *p = '\0';
strncpy(sut.nodename, machine_name, sizeof(sut.nodename) - 1); strncpy(sut.nodename, machine_name, sizeof(sut.nodename) - 1);

View File

@ -693,9 +693,9 @@ intr_event_describe_handler(struct intr_event *ie, void *cookie,
* description at that point. If one is not found, find the * description at that point. If one is not found, find the
* end of the name to use as the insertion point. * end of the name to use as the insertion point.
*/ */
start = index(ih->ih_name, ':'); start = strchr(ih->ih_name, ':');
if (start == NULL) if (start == NULL)
start = index(ih->ih_name, 0); start = strchr(ih->ih_name, 0);
/* /*
* See if there is enough remaining room in the string for the * See if there is enough remaining room in the string for the
@ -1832,8 +1832,8 @@ DB_SHOW_COMMAND(intr, db_show_intr)
struct intr_event *ie; struct intr_event *ie;
int all, verbose; int all, verbose;
verbose = index(modif, 'v') != NULL; verbose = strchr(modif, 'v') != NULL;
all = index(modif, 'a') != NULL; all = strchr(modif, 'a') != NULL;
TAILQ_FOREACH(ie, &event_list, ie_list) { TAILQ_FOREACH(ie, &event_list, ie_list) {
if (!all && TAILQ_EMPTY(&ie->ie_handlers)) if (!all && TAILQ_EMPTY(&ie->ie_handlers))
continue; continue;

View File

@ -341,9 +341,9 @@ DB_SHOW_COMMAND(ktr, db_ktr_all)
tstate.cur = (ktr_idx - 1) & (KTR_ENTRIES - 1); tstate.cur = (ktr_idx - 1) & (KTR_ENTRIES - 1);
tstate.first = -1; tstate.first = -1;
db_ktr_verbose = 0; db_ktr_verbose = 0;
db_ktr_verbose |= (index(modif, 'v') != NULL) ? 2 : 0; db_ktr_verbose |= (strchr(modif, 'v') != NULL) ? 2 : 0;
db_ktr_verbose |= (index(modif, 'V') != NULL) ? 1 : 0; /* just timestap please */ db_ktr_verbose |= (strchr(modif, 'V') != NULL) ? 1 : 0; /* just timestap please */
if (index(modif, 'a') != NULL) { if (strchr(modif, 'a') != NULL) {
db_disable_pager(); db_disable_pager();
while (cncheckc() != -1) while (cncheckc() != -1)
if (db_mach_vtrace() == 0) if (db_mach_vtrace() == 0)

View File

@ -1013,7 +1013,7 @@ kern_kldload(struct thread *td, const char *file, int *fileid)
* (kldname.ko, or kldname.ver.ko) treat it as an interface * (kldname.ko, or kldname.ver.ko) treat it as an interface
* name. * name.
*/ */
if (index(file, '/') || index(file, '.')) { if (strchr(file, '/') || strchr(file, '.')) {
kldname = file; kldname = file;
modname = NULL; modname = NULL;
} else { } else {
@ -1906,7 +1906,7 @@ linker_search_kld(const char *name)
int len; int len;
/* qualified at all? */ /* qualified at all? */
if (index(name, '/')) if (strchr(name, '/'))
return (linker_strdup(name)); return (linker_strdup(name));
/* traverse the linker path */ /* traverse the linker path */
@ -1927,7 +1927,7 @@ linker_basename(const char *path)
{ {
const char *filename; const char *filename;
filename = rindex(path, '/'); filename = strrchr(path, '/');
if (filename == NULL) if (filename == NULL)
return path; return path;
if (filename[1]) if (filename[1])

View File

@ -133,8 +133,7 @@ res_find(int *line, int *startln,
r_name, &r_unit, r_resname, r_value); r_name, &r_unit, r_resname, r_value);
if (hit && n != 4) { if (hit && n != 4) {
printf("CONFIG: invalid hint '%s'\n", cp); printf("CONFIG: invalid hint '%s'\n", cp);
/* XXX: abuse bogus index() declaration */ p = strchr(cp, 'h');
p = index(cp, 'h');
*p = 'H'; *p = 'H';
hit = 0; hit = 0;
} }
@ -172,18 +171,18 @@ res_find(int *line, int *startln,
s = cp; s = cp;
/* This is a bit of a hack, but at least is reentrant */ /* This is a bit of a hack, but at least is reentrant */
/* Note that it returns some !unterminated! strings. */ /* Note that it returns some !unterminated! strings. */
s = index(s, '.') + 1; /* start of device */ s = strchr(s, '.') + 1; /* start of device */
if (ret_name) if (ret_name)
*ret_name = s; *ret_name = s;
s = index(s, '.') + 1; /* start of unit */ s = strchr(s, '.') + 1; /* start of unit */
if (ret_namelen && ret_name) if (ret_namelen && ret_name)
*ret_namelen = s - *ret_name - 1; /* device length */ *ret_namelen = s - *ret_name - 1; /* device length */
if (ret_unit) if (ret_unit)
*ret_unit = r_unit; *ret_unit = r_unit;
s = index(s, '.') + 1; /* start of resname */ s = strchr(s, '.') + 1; /* start of resname */
if (ret_resname) if (ret_resname)
*ret_resname = s; *ret_resname = s;
s = index(s, '=') + 1; /* start of value */ s = strchr(s, '=') + 1; /* start of value */
if (ret_resnamelen && ret_resname) if (ret_resnamelen && ret_resname)
*ret_resnamelen = s - *ret_resname - 1; /* value len */ *ret_resnamelen = s - *ret_resname - 1; /* value len */
if (ret_value) if (ret_value)

View File

@ -355,7 +355,7 @@ ttyinq_findchar(struct ttyinq *ti, const char *breakc, size_t maxlen,
return (0); return (0);
while (boff < bend) { while (boff < bend) {
if (index(breakc, tib->tib_data[boff]) && !GETBIT(tib, boff)) { if (strchr(breakc, tib->tib_data[boff]) && !GETBIT(tib, boff)) {
*lastc = tib->tib_data[boff]; *lastc = tib->tib_data[boff];
return (boff - ti->ti_begin + 1); return (boff - ti->ti_begin + 1);
} }

View File

@ -1974,7 +1974,7 @@ kern_kmq_open(struct thread *td, const char *upath, int flags, mode_t mode,
* characters. * characters.
*/ */
len = strlen(path); len = strlen(path);
if (len < 2 || path[0] != '/' || index(path + 1, '/') != NULL) if (len < 2 || path[0] != '/' || strchr(path + 1, '/') != NULL)
return (EINVAL); return (EINVAL);
error = falloc(td, &fp, &fd, 0); error = falloc(td, &fp, &fd, 0);
@ -2077,7 +2077,7 @@ sys_kmq_unlink(struct thread *td, struct kmq_unlink_args *uap)
return (error); return (error);
len = strlen(path); len = strlen(path);
if (len < 2 || path[0] != '/' || index(path + 1, '/') != NULL) if (len < 2 || path[0] != '/' || strchr(path + 1, '/') != NULL)
return (EINVAL); return (EINVAL);
sx_xlock(&mqfs_data.mi_lock); sx_xlock(&mqfs_data.mi_lock);

View File

@ -89,12 +89,12 @@ fnmatch(const char *pattern, const char *string, int flags)
if (c == EOS) if (c == EOS)
if (flags & FNM_PATHNAME) if (flags & FNM_PATHNAME)
return ((flags & FNM_LEADING_DIR) || return ((flags & FNM_LEADING_DIR) ||
index(string, '/') == NULL ? strchr(string, '/') == NULL ?
0 : FNM_NOMATCH); 0 : FNM_NOMATCH);
else else
return (0); return (0);
else if (c == '/' && flags & FNM_PATHNAME) { else if (c == '/' && flags & FNM_PATHNAME) {
if ((string = index(string, '/')) == NULL) if ((string = strchr(string, '/')) == NULL)
return (FNM_NOMATCH); return (FNM_NOMATCH);
break; break;
} }

View File

@ -223,7 +223,7 @@ ng_ksocket_sockaddr_parse(const struct ng_parse_type *type,
/* Get socket address family followed by a slash */ /* Get socket address family followed by a slash */
while (isspace(s[*off])) while (isspace(s[*off]))
(*off)++; (*off)++;
if ((t = index(s + *off, '/')) == NULL) if ((t = strchr(s + *off, '/')) == NULL)
return (EINVAL); return (EINVAL);
if ((len = t - (s + *off)) > sizeof(fambuf) - 1) if ((len = t - (s + *off)) > sizeof(fambuf) - 1)
return (EINVAL); return (EINVAL);
@ -565,14 +565,14 @@ ng_ksocket_newhook(node_p node, hook_p hook, const char *name0)
/* Extract family, type, and protocol from hook name */ /* Extract family, type, and protocol from hook name */
snprintf(name, sizeof(name), "%s", name0); snprintf(name, sizeof(name), "%s", name0);
s1 = name; s1 = name;
if ((s2 = index(s1, '/')) == NULL) if ((s2 = strchr(s1, '/')) == NULL)
return (EINVAL); return (EINVAL);
*s2++ = '\0'; *s2++ = '\0';
family = ng_ksocket_parse(ng_ksocket_families, s1, 0); family = ng_ksocket_parse(ng_ksocket_families, s1, 0);
if (family == -1) if (family == -1)
return (EINVAL); return (EINVAL);
s1 = s2; s1 = s2;
if ((s2 = index(s1, '/')) == NULL) if ((s2 = strchr(s1, '/')) == NULL)
return (EINVAL); return (EINVAL);
*s2++ = '\0'; *s2++ = '\0';
type = ng_ksocket_parse(ng_ksocket_types, s1, 0); type = ng_ksocket_parse(ng_ksocket_types, s1, 0);

View File

@ -762,10 +762,10 @@ lomac_parse(struct mac_lomac *ml, char *string)
/* Do we have a range? */ /* Do we have a range? */
single = string; single = string;
range = index(string, '('); range = strchr(string, '(');
if (range == single) if (range == single)
single = NULL; single = NULL;
auxsingle = index(string, '['); auxsingle = strchr(string, '[');
if (auxsingle == single) if (auxsingle == single)
single = NULL; single = NULL;
if (range != NULL && auxsingle != NULL) if (range != NULL && auxsingle != NULL)
@ -776,13 +776,13 @@ lomac_parse(struct mac_lomac *ml, char *string)
*range = '\0'; *range = '\0';
range++; range++;
rangelow = range; rangelow = range;
rangehigh = index(rangelow, '-'); rangehigh = strchr(rangelow, '-');
if (rangehigh == NULL) if (rangehigh == NULL)
return (EINVAL); return (EINVAL);
rangehigh++; rangehigh++;
if (*rangelow == '\0' || *rangehigh == '\0') if (*rangelow == '\0' || *rangehigh == '\0')
return (EINVAL); return (EINVAL);
rangeend = index(rangehigh, ')'); rangeend = strchr(rangehigh, ')');
if (rangeend == NULL) if (rangeend == NULL)
return (EINVAL); return (EINVAL);
if (*(rangeend + 1) != '\0') if (*(rangeend + 1) != '\0')
@ -798,7 +798,7 @@ lomac_parse(struct mac_lomac *ml, char *string)
/* Nul terminate the end of the single string. */ /* Nul terminate the end of the single string. */
*auxsingle = '\0'; *auxsingle = '\0';
auxsingle++; auxsingle++;
auxsingleend = index(auxsingle, ']'); auxsingleend = strchr(auxsingle, ']');
if (auxsingleend == NULL) if (auxsingleend == NULL)
return (EINVAL); return (EINVAL);
if (*(auxsingleend + 1) != '\0') if (*(auxsingleend + 1) != '\0')