Sponsored Content
Top Forums Programming Very simple Unix Prog in C, could use advice/help Post 16824 by Perderabo on Thursday 7th of March 2002 09:04:43 AM
Old 03-07-2002
The particular error that you are searching for is here:
if( fd = open( FILENAME, O_RDWR ) == -1 )
If the open system call succeeds, it will not return -1. Thus you will set fd=(open() == -1) to zero! You will not attempt the creat. Then you stat() the real file. Eventually you attempt a read from fd 0. You could just type something since you're reading from the terminal.

You gotta use:
if ((fd = open() ) == -1)
to get what you want. Don't feel too bad, this one still nails me.

You have several other bugs, but I'll let you find them for the most part. One bug is very subtle though. First you open() the file, if that fails then you creat() it. That is a race condition. Suppose the file is not there, so the open() fails. Just then, another process creates it. Then your creat() call must now fail too. Whenever you check to see if something is possible and then do it if looks possible, you are setting yourself up for this situation. It's ok to do a check first, but then you must check for success to see if it really worked.
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Advice on UNIX/Linux

I have 6 extra hard drives and want to install them in my boxes and run UNIX, LINUX andyes M$ Winblows. How do I make it so i have the choice of which HD to boot from? Also, which UNIX distribution should i run to get aquainted with it, and wheres the best place to get it? Thank you very much if... (5 Replies)
Discussion started by: r1sk
5 Replies

2. UNIX Desktop Questions & Answers

Learning Unix , Advice?

I am a long-time Windows NT, 2000 user at home and work. I want to learn Unix to broaden myself. What flavor do you recommend and how do you recommend practicing with it at home? how to get a copy of it to practice with? (1 Reply)
Discussion started by: robmaxfli
1 Replies

3. UNIX for Dummies Questions & Answers

Advice to build a cheap Unix machine?

Hello! In my perpetual quest to learn Unix, I want to build a cheap Unix server for my personal use. Do you guys have suggestions of web sites, vendors etc...where I could get info on things such as step-by-step instruction, 2nd hand hardware etc... I would like to go preferably with a HP... (2 Replies)
Discussion started by: alan
2 Replies

4. UNIX for Dummies Questions & Answers

Re-learning Unix - need some advice.

I learned Unix about 6 years ago, I think it was the System V version back then. My course taught me, Unix commands, shell scripting, Vi editor, and probably more stuff. I wanted to stick with Unix but life didn't allow me to, my college back then used Windows 95 and then my job as a... (8 Replies)
Discussion started by: thoughts
8 Replies

5. UNIX for Dummies Questions & Answers

UNIX Help & Advice

I originally sent this as a PM to Neo, but I was ignorant towards the rule of PMing someone with technical information. Below is a copy / paste of the questions I asked. If anyone can help, I'd appreciate it tons. Thank you! :D The first OS I ever encountered was DOS and Windows 3.1. Ever since... (1 Reply)
Discussion started by: ZeeSquared
1 Replies

6. UNIX for Dummies Questions & Answers

UNIX advice needed desperately :/

Firstly I do need to state that I am turning here as a last resort. Im in my first year of a Computer Networking and technical support course, and as a first year I must complete a Introductory Unix/Linux class. I have been doing well in the course so far however I have been stumped on the final... (1 Reply)
Discussion started by: setaylor5
1 Replies

7. Solaris

Advice to become a unix guru?

Hello, I have recently completed my training for Solaris 10 and have a good understanding of the bases now. I am playing around with my system but can't do much since my knowledge is limited. I am looking for some projects or some threads where I can learn more and expand my knowledge step by... (16 Replies)
Discussion started by: saudsos
16 Replies

8. UNIX for Dummies Questions & Answers

Advice needed regarding Unix doc/book

I am a newbie Oracle DBA. All our oracle servers are hosted in AIX and Solaris Environment. I have no knowledge about this Unix O/S. So, it will be great if anyone here can suggest me any good book which will teach me the basics. Any inputs/suggestions will be great for me. (1 Reply)
Discussion started by: sandip250382
1 Replies

9. Shell Programming and Scripting

Need advice for project UNIX to Linux migration

I am working on UNIX AIX to Linux migration. Does anybody know the good site for doing this? Thanks for contribution (4 Replies)
Discussion started by: digioleg54
4 Replies
explain_creat_or_die(3) 				     Library Functions Manual					   explain_creat_or_die(3)

NAME
explain_creat_or_die - create and open a file creat and report errors SYNOPSIS
#include <libexplain/creat.h> void explain_creat_or_die(const char *pathname, int mode); DESCRIPTION
The explain_creat_or_die function is used to call the creat(2) system call. On failure an explanation will be printed to stderr, obtained from explain_creat(3), and then the process terminates by calling exit(EXIT_FAILURE). This function is intended to be used in a fashion similar to the following example: explain_creat_or_die(pathname, mode); pathname The pathname, exactly as to be passed to the creat(2) system call. mode The mode, exactly as to be passed to the creat(2) system call. Returns: This function only returns on success. On failure, prints an explanation and exits. SEE ALSO
creat(2) open and possibly create a file or device explain_creat(3) explain creat(2) errors exit(2) terminate the calling process COPYRIGHT
libexplain version 0.52 Copyright (C) 2008 Peter Miller explain_creat_or_die(3)
All times are GMT -4. The time now is 03:23 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy