LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-26-2007, 05:12 AM   #1
Santoshkb
Member
 
Registered: Sep 2007
Posts: 56

Rep: Reputation: 15
strtok corrupt the passed data?


Hi all,
Calling the function is_macid_valid(char * macid), modifies the
macid pointer hence unavailiable for further, i think problem lyis
using strtok, can i have suggestion , so
as to retain the macid value after calling is_macid_valid()..
Thanks in advance



int IS_MACID_VALID(char * macid)
{ int i = 0, len = 0; char *ptr;
len = strlen(macid);
ptr = strtok(macid, ":");
while(ptr) {
ptr = strtok(NULL, ":");
i++;
}
return (((len == 17) && (i==6))? 0 : -1);
}
//return((i==6)? 0 : 1);\

//if(i==6) printf("valid = %d\n", i);\
//else printf("Invalid=%d\n", i); \

main()
{
char san[24]="00:11:22:33:44:55";
printf("Min strlen = %d\n", strlen(san));
printf("Macid Before = %s\n", san);
printf("GG=%d\n",IS_MACID_VALID(san));
printf("After Macid = %s\n", san);
return 0;

Last edited by Santoshkb; 09-26-2007 at 05:16 AM. Reason: 1
 
Old 09-26-2007, 10:33 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
From the man page
Quote:
Bugs
Avoid using these functions. If you do use them, note that:
These functions modify their first argument.
These functions cannot be used on constant strings.
The identity of the delimiting character is lost.
The strtok() function uses a static buffer while parsing, so it's not thread safe. Use strtok_r() if this matters to you.
So either save a copy of the original or write your own function; I use 2 pointers and restore the delimiter on successive calls.

PS There is a dedicated programming section at LQ.
 
Old 09-26-2007, 08:03 PM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using C API; first query passed passed by program to MySQL is successful, second quer weibullguy Programming 2 06-12-2007 10:45 PM
FTP in Konsole gives corrupt data benny_j Linux - Newbie 2 03-10-2007 04:08 AM
Is there a variable that represents data passed in a pipe HGeneAnthony Linux - Newbie 1 02-27-2007 11:01 PM
Recover corrupt data from SD Memory Card new2lin Linux - Desktop 1 12-28-2006 01:06 PM
Corrupt/missing data fat32 need recovered cormack Linux - General 3 04-12-2006 05:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration