Yes, your code worked perfectly and much simpler. The only thing bugs me is the NULL for the second strtok_r()
From man strtok:
strtok() wants a NULL because somewhere inside, there is an if(string==NULL) { // Use the string we had last time and literally no other reason. It's just a weird old library call that insists you use it in a very particular way.
There is no point worrying what the contents of tmp1 are either. It might not even be the same in a different libc.
I think your confusion is related to the concept of re-entrancy. A re-entrant function, if you call it twice with the exact same parameters, would do the exact same thing. strtok() violates this, because it remembers what string you gave it last time you called it.
Imagine you're breaking up the string "a:b:c|d:e:f|g:h:i" with strtok upon "|". It gives you "a:b:c", and you call strtok() again with ":" to break it into "a", "b", "c". In doing so, strtok() will forget the original string!
This is perfectly okay with strtok_r since you can give them different tmp variables. Those variables, not the function itself, will remember where it was last, so there is no conflict.
Last edited by Corona688; 01-16-2014 at 05:23 PM..
Is it possible to make function variables local?
I mean for example, I have a script variable 'name' and in function I have declared variable 'name'
I need to have script's 'name' have the same value as it was before calling the function with the same declaration.
The way to preserve a... (5 Replies)
Hi,
I have a base class and derived a class from the base class, i want to print & read the data for the object created for the derived class,so i have overloaded both the << and >> operators and also have done the foward declaration.
Below is the code snippet,
#include <iostream>
class... (3 Replies)
I attached a README file that I will refer to.
I successfully completed everything in the README file until step 4.
# pwd
/gani/gani-2.4.4
# ls
COPYING Makefile.macros gem.c
Makefile Makefile.sparc_gcc gem.h
Makefile.amd64_gcc ... (1 Reply)
I have no idea what the following means. The teacher is too advanced for me to understand fully. We literally went from running a few commands over the last few months to starting shell scripting. I am not a programmer, I am more hardware oriented. I wish I knew what this question was asking... (3 Replies)
Hi,
While installation of apache on linux, we perform the below tasks.
1) Untar
2) configure
3) make
4) make install.
I wanted to understand the difference and working of configure/make/make install.
Can any one help me understanding this?
Thanks in advance. (1 Reply)
I wrote the following Makefile:
dirs := a b c d
files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
.PHONY: all
all:
touch $(files)
The first two lines are taken from GNU make tutorial, Section 8.5 The foreach Function. I would expect the recipe
touch $(files)
to be... (2 Replies)
Greetings,
I m wondering if it's possible do do the following :
I have a simple function called "FindMoveDelete" which does the following :
FindMoveDelete()
{
find . -iname "$FILENAME*.ext" -exec mv {} "$PATH/$VAR" \; &&
find . -maxdepth 1 -type d -iname "$FILENAME*" -exec rm -rf {}... (6 Replies)
Hi,
If I declare a function inside another function, it overwrites any previously declared function with the same name. This is NOT what I want.
Example:
#!/bin/bash
_test() { echo test; }
_myf() {
# I'm using the same name as the other function.
_test() { echo local test; }... (8 Replies)
Hello -
I wrote few scripts on bash shell script and grafana triggers those scripts and show on console .
I want to write the console output to a log file as well by using tee command and I am successful as well . I am wondering Instead of writing same logic on multiple scripts , why... (4 Replies)
Hi All,
Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition.
#Body
function1()
{
if
then
echo "exist"
else
echo "not exist"
}
#if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies
LEARN ABOUT NETBSD
strtok_r
STRTOK(3) BSD Library Functions Manual STRTOK(3)NAME
strtok, strtok_r -- string tokens
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include <string.h>
char *
strtok(char * restrict str, const char * restrict sep);
char *
strtok_r(char *str, const char *sep, char **lasts);
DESCRIPTION
The strtok() function is used to isolate sequential tokens in a nul-terminated string, str. These tokens are separated in the string by at
least one of the characters in sep. The first time that strtok() is called, str should be specified; subsequent calls, wishing to obtain
further tokens from the same string, should pass a null pointer instead. The separator string, sep, must be supplied each time, and may
change between calls.
The strtok() function returns a pointer to the beginning of each subsequent token in the string, after replacing the separator character
itself with a NUL character. Separator characters at the beginning of the string or at the continuation point are skipped so that zero
length tokens are not returned. When no more tokens remain, a null pointer is returned.
The strtok_r() function implements the functionality of strtok() but is passed an additional argument, lasts, which points to a user-provided
pointer which is used by strtok_r() to store state which needs to be kept between calls to scan the same string; unlike strtok(), it is not
necessary to limit tokenizing to a single string at a time when using strtok_r().
EXAMPLES
The following will construct an array of pointers to each individual word in the string s:
#define MAXTOKENS 128
char s[512], *p, *tokens[MAXTOKENS];
char *last;
int i = 0;
snprintf(s, sizeof(s), "cat dog horse cow");
for ((p = strtok_r(s, " ", &last)); p;
(p = strtok_r(NULL, " ", &last)), i++) {
if (i < MAXTOKENS - 1)
tokens[i] = p;
}
tokens[i] = NULL;
That is, tokens[0] will point to "cat", tokens[1] will point to "dog", tokens[2] will point to "horse", and tokens[3] will point to "cow".
SEE ALSO index(3), memchr(3), rindex(3), strchr(3), strcspn(3), strpbrk(3), strrchr(3), strsep(3), strspn(3), strstr(3)STANDARDS
The strtok() function conforms to ANSI X3.159-1989 (``ANSI C89''). The strtok_r() function conforms to IEEE Std 1003.1c-1995 (``POSIX.1'').
BUGS
The System V strtok(), if handed a string containing only delimiter characters, will not alter the next starting point, so that a call to
strtok() with a different (or empty) delimiter string may return a non-NULL value. Since this implementation always alters the next starting
point, such a sequence of calls would always return NULL.
BSD August 11, 2002 BSD