LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 11-13-2018, 01:09 PM   #1
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 490
Blog Entries: 4

Rep: Reputation: 238Reputation: 238Reputation: 238
close() generates compiler warning


In a C program, i want to read and write to a data file that is set to the owner's exclusive permissions (octal 0600). If the file does not exist, the program creates the empty file with these permissions. My code snippet looks like this:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <signal.h>
#include <sys/stat.h>
.
.
.
FILE *fh1;
int fh2;
struct stat st_buf;
int stat_rc;

stat_rc = stat(filename,&st_buf); // check if file exists
if (stat_rc  == -1) {
 if ((fh2 = open(filename, O_CREAT, 0600)) < 0) {	// create empty file w/ 0600 permissions
	 printf("\n%sCouldn't create '%s'.<br/>",ErrMsg,filename);
	 return(1);
	 }
	close(fh2); // close newly created file (GENERATES COMPILER WARNING)
}
// normal fopen to add to file, whether empty or existing
if (!(fh1 = fopen(filename,"ab"))) {
.
.
.
This code works fine. It compiles with no errors on my older home (Linux) machine, but on the newer FreeBSD server the compiler generates a warning "implicit declaration of function close(); did you mean pclose()?" I don't honestly know what the diference is between close() and pclose(), nor why the latter would be required for a file opened/created with open().

It is just a compiler warning, and the generated binary executes perfectly. So am currently living with the compiler warning, but it bugs me, and makes me think i'm doing something wrong. It also bugs me not knowing whether the close() is actually closing the file that open() created.

If i knew how to create a file with specific permissions via fopen(), that would serve even better, but i don't know how to do that, nor would i want to employ chmod() or fchmod() when (in most cases) the file already exists, with the correct permissions.
 
Old 11-13-2018, 01:27 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
What does it say when you enter the command: man close
 
1 members found this post helpful.
Old 11-13-2018, 02:45 PM   #3
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 490

Original Poster
Blog Entries: 4

Rep: Reputation: 238Reputation: 238Reputation: 238
Quote:
Originally Posted by smallpond View Post
What does it say when you enter the command: man close
Good tip. "man close" on the FreeBSD server mentioned the include file <unistd.h>. When this is included, the code compiles without warning on both systems. Still don't understand why open() didn't cause the warning.
 
Old 11-14-2018, 01:36 PM   #4
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018
open() is in fcntl.h

This posits some suggestions as to why they're in different headers and seem reasonable, but personally, I tend to put this kind of thing down to the fact that much of this stuff dates back to the 70s and that everyone involved was simply stoned while designing it!
 
1 members found this post helpful.
  


Reply

Tags
close(), open(), pclose()



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
close terminal mean close background process? Niceman2005 Linux - General 7 05-27-2010 02:58 PM
how to close port 1050 please help how to close it. mr norm Linux - General 1 09-12-2008 07:57 AM
mplayer 'close all' doesn't close anything allelopath Linux - Software 2 12-08-2005 09:40 AM
lid close generates no acpi event mannyzen76 Linux - Laptop and Netbook 0 02-21-2004 11:37 PM

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

All times are GMT -5. The time now is 03:01 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