LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 01-31-2012, 07:32 PM   #1
nickjomaffa
LQ Newbie
 
Registered: Jan 2012
Posts: 1

Rep: Reputation: Disabled
Unhappy Search for file in path and print blocks of free disk space in C?


Hi all,

I am working on a C program in Unix that basically is supposed to have the same functionality as a shell. I have most commands up and running. I cannot find solutions to two of the commands I have to implement though. They are:

'where filename' - does a search for the file called filename in each directory listed in the PATH and says the path it's located in if found

'disk' - prints amount of available disk space in system

Any answers will be appreciated.
 
Old 01-31-2012, 10:26 PM   #2
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
Just as an aside, AFAIK, the which command is often an external command, that is not built into the shell.

But if you wish to write a C language program that does what the which command does, you might wish to take a look at information on the string related functions, such as:

stpcpy, strcasecmp, strcat, strchr, strcmp, strcoll, strcpy, strcspn, strdup, strfry, strlen, strncat, strncmp, strncpy, strncasecmp, strpbrk, strrchr, strsep, strspn, strstr, strtok, strxfrm, index, rindex
 
Old 01-31-2012, 10:57 PM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
You need to first obtain the path list, e.g. using getenv("PATH"). Allocate a temporary string long enough to contain both the longest directory (delimited by colons) in the path list, a slash, the file name, and a trailing EOS (zero byte) to mark the end of the string.

Write a loop that tries each directory specified in the path list in the order they're listed. Remember to skip over empty ones, sometimes PATH contains multiple successive colons (like ::). Append a slash and the file name to the directory name, followed by an end of string mark '\0', then check using e.g access(pathname,X_OK) to see if that one is executable. If it is, this is the result; don't check any further. If none of them match, then most which implementations return nothing.

Most of this is string manipulation. Personally, I would only need strlen(), strcspn() and strspn() (the latter two to find where the next colon is, and to skip over the colon -- these can be written using a trivial loop too, of course).

Usually most people tend to write interfaces that save the pathname to a buffer provided by the caller. I disagree, strongly. Compare the GNU/POSIX.1-2008 getline() interface to old fgets(). As the example at the end of the getline man page shows, a trivial program can accept any input without problems, as long as there is enough resources available. A corresponding fgets() program is either wasteful (using a lot of memory for a buffer that is only partially used), or will get confused with long lines.

I shudder to think what kind of security problems could be caused by a part of a shell silently truncating the PATH string! Especially if it truncated to one length at one place, and to another length at another place. No, please! Don't do it! Noo.......

Therefore, I'd also use malloc() to allocate the pathname buffer (also used to return the string to the caller). Hint: malloc(strlen(pathlist)+strlen(filename)+2) is guaranteed to be long enough, and is not wasteful as the path list is always quite short. One extra char is for the slash, the other for the end-of-string '\0'. Just remember that when a problem occurs, the function should remember to free() the temporary/result buffer, and that all callers should remember to free() the pointer they receive after they've done with it.

Last edited by Nominal Animal; 02-01-2012 at 06:21 PM. Reason: typo: ':' is colon, not semicolon!
 
1 members found this post helpful.
  


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
partition space and disk space usage and free don't match nsic Linux - Newbie 3 02-22-2013 12:57 PM
File system question - free blocks RohanRNS Linux - General 1 04-15-2010 03:12 PM
How t get it known free disk space? ukrainet Linux - General 3 05-06-2008 10:52 PM
Red Hat 6.2 disk space error - "Freeing blocks not in datazone" richfink Linux - Software 2 08-26-2006 04:08 PM
free disk space mp4-10 Linux - Software 1 04-06-2005 03:32 AM

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

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