LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 10-03-2009, 01:45 AM   #1
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,223

Rep: Reputation: 167Reputation: 167
how to make a messy sprintf function work


ok, i am writing my own kernel and
i am writing my own version of sprintf
but its not working quite right and i dont know why
i know its mess buy
Code:
int sprintf(char *out, const char *format, ...){
int x;
char val[32];
va_list va;
va_start(va, format);
int ticker = 0;
int ptr = 0;
while(format[ticker]){
if(format[ticker] != '%'){out[ptr] = format[ticker]; ticker++; ptr++;}
else {
ticker++;
out[ptr] = 0;
switch(format[ticker]){
case 'c':
x = va_arg(va, char);
out[ptr] = x;
ptr++;break;
case 's':
strcat(out, va_arg(va, char *));
ptr = strlen(out) + 1;
break;
case 'd':
case 'i':
case 'f':
case 'u':
x = va_arg(va, int);
itoa(x, val, 10);
strcat(out, val);
ptr = strlen(out)+1;
break;
case '%':
out[ptr] ='%';
ptr++;
break;
case 'X':
case 'x':
x = va_arg(va, int);
itoa(x, val, 16);
strcat(out, val);
ptr = strlen(out)+1;
break;
default:break;
}
ticker++;
}
}
out[ptr] = 0;
va_end(ap);
return ptr;
}
 
Old 10-03-2009, 01:47 AM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,223

Original Poster
Rep: Reputation: 167Reputation: 167
on an unrelated question can GCC make 16bit code?
 
Old 10-03-2009, 01:59 AM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Why do you set ptr to one past \0 for formats other than %c and %%?
Kevin Barry

Last edited by ta0kira; 10-03-2009 at 02:00 AM.
 
Old 10-03-2009, 02:19 AM   #4
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,223

Original Poster
Rep: Reputation: 167Reputation: 167
i am alittle confused could you point it out in the code?
 
Old 10-03-2009, 02:28 AM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by smeezekitty View Post
Code:
case 's':
strcat(out, va_arg(va, char *));
ptr = strlen(out) + 1;
break;
This is in three places. It seems like you should leave off +1: my guess is it causes text falling between two format operations to be overwritten by the second one.
Kevin barry
 
Old 10-03-2009, 02:49 AM   #6
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,223

Original Poster
Rep: Reputation: 167Reputation: 167
that did it thank you so so so much!
i have been tearing my hair off over this kernel
i promise to release it open source as soon as its done (could be between 2 weeks to 2 months)
 
Old 10-03-2009, 03:58 AM   #7
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by smeezekitty View Post
that did it thank you so so so much!
i have been tearing my hair off over this kernel
i promise to release it open source as soon as its done (could be between 2 weeks to 2 months)
This is a joke, right? You're writing the kernel to an operating system, but you don't know how to set pointers yet. Is this a project for a cyber-fiction class, and we're the guinea pigs? Or is this a massive put-on, and the entire episode will be posted to an ironically titled post-modern blog two weeks from now?

Normally a personal learns how to write computer programs, struggles past the usual stage of making dumb mistakes, writes a few major applications, becomes seasoned and efficient, stops trying to program by newsgroup (clearly not so), and then finally, maybe 15-20 years later, takes on OS writing. ... normally.
 
Old 10-03-2009, 11:49 AM   #8
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,223

Original Poster
Rep: Reputation: 167Reputation: 167
Quote:
Originally Posted by lutusp View Post
This is a joke, right? You're writing the kernel to an operating system, but you don't know how to set pointers yet. Is this a project for a cyber-fiction class, and we're the guinea pigs? Or is this a massive put-on, and the entire episode will be posted to an ironically titled post-modern blog two weeks from now?

Normally a personal learns how to write computer programs, struggles past the usual stage of making dumb mistakes, writes a few major applications, becomes seasoned and efficient, stops trying to program by newsgroup (clearly not so), and then finally, maybe 15-20 years later, takes on OS writing. ... normally.
take your attitude elsewhere
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
make Xconfig doesn't work, make menuconfig does work Debian/KDE bucovaina78 Linux - Kernel 1 10-15-2008 01:26 PM
Dynamic sprintf introuble Programming 6 06-25-2006 08:20 AM
sprintf() help needed!!! rino.caldelli Programming 9 03-07-2006 06:43 AM
sprintf to c++ conversion rino.caldelli Programming 2 03-03-2006 09:44 PM
sprintf value disappear syseeker Programming 8 02-17-2006 04:20 AM


All times are GMT -5. The time now is 11:41 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration