C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-20-2007, 03:05 AM   #1
Registered User
 
Join Date: Apr 2006
Location: Mumbai, India
Posts: 26
Change in sequence due to a '\n'

Hi!

I am trying to run the following code.

Code:
#include <stdio.h>
#include <errno.h>


int main()
{
        char s[10];
        int nread;
        FILE* fp;
        char c;

        fp = fopen("abc", "w+");
        printf("error: %d, error_string: %s", errno, strerror(errno));

        fprintf(fp, "Hello\n");
        fseek(fp, 0, SEEK_SET);

        while((c = fgetc(fp)) != EOF)
                write(1, &c, 1);

        fclose(fp);
        return 0;
}
It compiles fine.
(none):~/prog # gcc -o err err.c
(none):~/prog #

But when I try to run it I am getting -

(none):~/prog # ./err
Hello
error: 0, error_string: Success(none):~/prog #

If I put a newline after printf like below -

Code:
printf("error: %d, error_string: %s\n", errno, strerror(errno));
I am getting the following output -

(none):~/prog # ./err
error: 0, error_string: Success
Hello
(none):~/prog #

Why does this change in sequence happen?

Thanks and regards,

Arun
arunj is offline   Reply With Quote
Old 10-20-2007, 05:30 AM   #2
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,295
I hope you're not actually running / compiling it as root, that'd be silly.

Depending on how you've got output buffering set-up, it will flush at the end of every line or when the buffer fills up. Think of it as so,

Scenario 1.
out = "error: 0, error_string: Success(none):"
out ="hello\n" # flush

Scenario 2.
out = "error: 0, error_string: Success(none):\n" #flush
out ="hello\n" #flush

If you want to change buffering use setvbuf or call fflush().
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim
zacs7 is offline   Reply With Quote
Old 10-21-2007, 06:54 AM   #3
Registered User
 
Join Date: Apr 2006
Location: Mumbai, India
Posts: 26
Hi!

Thanks for the reply. What is silly if it is run as root?

Thanks,

Arun
arunj is offline   Reply With Quote
Old 10-21-2007, 07:39 AM   #4
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,710
> What is silly if it is run as root?
Because of the amount of extra damage you can do if you get it wrong.

Say for example you had a buggy 'rm' command and you tried to remove /
Running as root will hang you out to dry.
Running as a mortal will tell you to take a hike.

Likewise, reading email as root, surfing the web as root.
The root account is for necessary admin only, not a convenience for day to day work. It's not like it's that hard to leave another terminal window open if you need to do root work.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 10-21-2007, 07:45 AM   #5
Registered User
 
Join Date: Apr 2006
Location: Mumbai, India
Posts: 26
Hi!

Which method in this piece of code is comparable to # rm /?

Thanks,

Arun
arunj is offline   Reply With Quote
Old 10-21-2007, 07:46 AM   #6
Registered User
 
Join Date: Apr 2006
Location: Mumbai, India
Posts: 26
or # rm -rf /?
arunj is offline   Reply With Quote
Old 10-21-2007, 08:15 AM   #7
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,710
I was merely highlighting a possible future scenario.

Or are you suggesting that you'll do the right thing when it matters (because I won't believe you).

Or are you going to wait for someone to "own" your box because you've become overly complacent about running as root?
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 10-21-2007, 10:27 PM   #8
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,768
Quote:
Originally Posted by arunj View Post
Hi!

Which method in this piece of code is comparable to # rm /?
If you have bugs in your code, anything is possible.

Back in the days of DOS, where everything basically "ran as root," I completely blitzed my entire system MORE THAN ONCE by making simple programming errors. It can happen to you.
brewbuck is offline   Reply With Quote
Old 10-22-2007, 02:25 AM   #9
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by brewbuck View Post
If you have bugs in your code, anything is possible.

Back in the days of DOS, where everything basically "ran as root," I completely blitzed my entire system MORE THAN ONCE by making simple programming errors. It can happen to you.
I once type "delete *.*" on "the wrong disk" (meaning do delete a:*.*, but I ended up deleting c:*.* instead). Not a good idea.

If you are not developing system code, where you have to run as root to be able to run the code in the first place, I suggest you do "adduser yourname" and then use the new account for code development. You can always set up a "sudo" account for "yourname" and then do "sudo some-command" to perform commands as root.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 10-22-2007, 10:41 AM   #10
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,768
Quote:
Originally Posted by matsp View Post
I once type "delete *.*" on "the wrong disk" (meaning do delete a:*.*, but I ended up deleting c:*.* instead). Not a good idea.
One of my boo-boos was trying to access a structure member through a far NULL pointer. A far NULL pointer, of course, points to the interrupt vector table. So I ended up smashing the vector for one of DOS's disk write routines, and it happened inside a TSR, right in the middle of DOS trying to update something in the FAT. Imagine yanking the rug out from under somebody trying to carefully paint a china lamp.

The FAT was hosed, that was a reinstall on that one.
brewbuck is offline   Reply With Quote
Old 10-22-2007, 12:21 PM   #11
Jaxom's & Imriel's Dad
 
Kennedy's Avatar
 
Join Date: Aug 2006
Location: Alabama
Posts: 877
To be fair, though, it is sometimes needful to develop as root -- Like when you are attempting to control hardware without drivers. Cannot do it without being root, or at least chmod +S the file -- which is the same thing.

EDIT: Though, I will say that I did do the following command(s) and really screwed myself:
modprobe mtdcore
modprobe jffs2
modprobe mtdram total_size=63488 erasesize=12
modprobe mtdchar
modprobe mtdblock
dd if=fs.img of=/dev/mtd0
mount -t jffs2 /dev/mtdblock0 mnt
cd mnt/mnt/etc
cp * /etc

oops!!!!!! CRAP!!!!, I mean
cp * ../../etc
SNOT BUBBLES!!!!!!

Dumb dumb di dumb, Dumb di dumdum dumb di dumb. <-- meant to be sung not read.

Last edited by Kennedy; 10-22-2007 at 12:30 PM.
Kennedy is offline   Reply With Quote
Old 10-24-2007, 01:58 PM   #12
Registered User
 
Join Date: Apr 2006
Location: Mumbai, India
Posts: 26
Quote:
Originally Posted by Salem View Post
I was merely highlighting a possible future scenario.

Or are you suggesting that you'll do the right thing when it matters (because I won't believe you).

Or are you going to wait for someone to "own" your box because you've become overly complacent about running as root?
Hi!

It often happens that if I don't do the wrong thing and learn how to rectify it again I'll never be able to grab the correct concept.
And I think comments like "because I won't believe you" and "you've become overly complacent about running as root" etc. are a bit personal. Are these lines really necessary to answer a question like " What is the problem if I run it as a root"?

Thanks and reagrds,

Arun
arunj is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
c++builder6 change form names problem Leite33 C++ Programming 2 06-09-2008 08:20 AM
how to change static char* and not lose mem ? jabka C Programming 15 09-07-2007 05:33 PM
Change Value in an array beginner999 C Programming 3 01-18-2003 07:16 AM
How to change CString data to CString data(in Hex format)? (And change back) ooosawaddee3 C++ Programming 2 11-08-2002 03:22 AM
trying to make change from the price of the item and the amount given. please help!!! tobyman C++ Programming 2 09-04-2001 02:12 PM


All times are GMT -6. The time now is 11:13 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22