LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-16-2009, 06:18 AM   #1
wwuster
LQ Newbie
 
Registered: Jul 2009
Posts: 3

Rep: Reputation: 0
How to tell if a file is open in C?


I need a function in C that will tell me if a file is already open. I've been searching for such a thing but can't find one. This is for Ubuntu 9.04.

thanks,
William
 
Old 07-16-2009, 06:47 AM   #2
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
man lsof
 
Old 07-16-2009, 11:58 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
"lsof" is a program, not a C function call.

But there is no such function call. To make one, you would first need to get the full path of a given file ath. This can be done with the realpath (3) library function. Then scan all /proc/[0-9]*/fd/* and call readlink (2) on each symlink found and compare with the full path you got from calling realpath (3) in the first step. If there is a match the file is open.

This is (briefly) what the "lsof" and "fuser" programs do.
 
  


Reply



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
open lock file /var/lib/postfix/master.lock: cannot open file: Permission denied gabsik Linux - Server 6 08-30-2012 09:39 PM
[SOLVED] cdrecord: No such file or directory. Cannot open '/dev/pg0'. Cannot open SCSI driver. procfs Linux - Software 5 01-18-2008 10:06 AM
wxPython problem: /_core_.so: cannot open shared object file: No such file or directo aregmi Linux - Software 2 06-06-2007 08:53 PM
Geting error loop QFike:getch: File not open QFile:atEnd: this file is not open badgerbox76 Linux - Newbie 6 01-07-2006 05:30 PM
How to open a wps file in open office 1.1 oneiric Linux - Software 4 04-15-2004 04:07 AM

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

All times are GMT -5. The time now is 09:22 PM.

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