LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 10-30-2009, 02:05 AM   #1
whatis
LQ Newbie
 
Registered: Oct 2009
Posts: 12

Rep: Reputation: 0
Traversing /dev


hi

I am new to the Linux platfom

How can I write a C program to traverse the /dev and print the major and minor numbers of devices.

Plz help
 
Old 10-30-2009, 02:43 AM   #2
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,893

Rep: Reputation: 68
I assume you already know how to use terminal (probably in a terminal emulator inside graphical environment). I also assume you have C programming experience. For many things in Linux you can get documentation using "man" command. So run the following commands:
Code:
man 3 opendir
man 3 readdir
man 2 stat
Numbers 2 and 3 specify manual page type (wrappers for kernel calls and library functions of the libc, respectively); they are optional, but "man stat" finds "man 1 stat" - documentation for a shell command. Printing can be done with "printf" just fine.

After you have read the documentation, you can ask more specific questions.
 
Old 10-30-2009, 06:03 AM   #3
whatis
LQ Newbie
 
Registered: Oct 2009
Posts: 12

Original Poster
Rep: Reputation: 0
Thanks alot!!

It was of great help.

I have another query, I want to use the functions, major (st_rdev) and minor (st_rdev) to get the major and minor numbers resp from the stat structure.
But I'm unable to locate the header file that should be included for these functions.
 
Old 10-30-2009, 12:48 PM   #4
whatis
LQ Newbie
 
Registered: Oct 2009
Posts: 12

Original Poster
Rep: Reputation: 0
I am using 'opendir' and 'readdir' functions to traverse the /dev directory. I am also populating the 'stat' structure for each file inside the directory. But the structure doesnt have a direct entry for major and minor numbers. Is there a way to retrieve this information, from the already available information in the 'stat' struct.

I am not too sure abt major(st_rdev) and minor(st_rev) usage.
Plz help!
 
Old 10-30-2009, 01:54 PM   #5
raskin
Senior Member
 
Registered: Sep 2005
Location: Russia
Distribution: NixOS (http://nixos.org)
Posts: 1,893

Rep: Reputation: 68
You are right about major() and minor().

Look at the "man 2 stat" closer. See the "major (3)" reference? This means it is a type-three entity (section three is system library entries). When you see "[name] ([number])" reference in man, you are supposed to look up "man [number] [name]" once you need details.

If "man 3 major" leaves some questions unanswered, you are welcome to ask them, of course.
 
Old 11-02-2009, 12:22 AM   #6
whatis
LQ Newbie
 
Registered: Oct 2009
Posts: 12

Original Poster
Rep: Reputation: 0
Smile

I am able to use the major(st_rev) and minor(st_rdev) functions and the header file <sys/types.h> for the same.
 
  


Reply

Tags
dev, minor, program


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Traversing /dev whatis Linux - Newbie 5 11-01-2009 07:45 PM
Traversing Subnets gankoji Linux - Networking 6 10-24-2009 02:14 AM
C Shell - Traversing a file lundq1kp Linux - Newbie 1 06-06-2008 07:41 PM
Recursively traversing sub-directories ark86 Linux - Newbie 2 01-29-2006 02:15 PM
Traversing from previous post c0rk3y *BSD 1 07-26-2002 10:15 PM


All times are GMT -5. The time now is 02:21 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration