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
CREAT(3P)                                                    POSIX Programmer's Manual                                                   CREAT(3P)

PROLOG
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the correspond- ing Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux. NAME
creat - create a new file or rewrite an existing one SYNOPSIS
#include <sys/stat.h> #include <fcntl.h> int creat(const char *path, mode_t mode); DESCRIPTION
The function call: creat(path, mode) shall be equivalent to: open(path, O_WRONLY|O_CREAT|O_TRUNC, mode) RETURN VALUE
Refer to open(). ERRORS
Refer to open(). The following sections are informative. EXAMPLES
Creating a File The following example creates the file /tmp/file with read and write permissions for the file owner and read permission for group and oth- ers. The resulting file descriptor is assigned to the fd variable. #include <fcntl.h> ... int fd; mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; char *filename = "/tmp/file"; ... fd = creat(filename, mode); ... APPLICATION USAGE
None. RATIONALE
The creat() function is redundant. Its services are also provided by the open() function. It has been included primarily for historical purposes since many existing applications depend on it. It is best considered a part of the C binding rather than a function that should be provided in other languages. FUTURE DIRECTIONS
None. SEE ALSO
open(), the Base Definitions volume of IEEE Std 1003.1-2001, <fcntl.h>, <sys/stat.h>, <sys/types.h> COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technol- ogy -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html . IEEE
/The Open Group 2003 CREAT(3P)
All times are GMT -4. The time now is 08:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy