LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Traversing /dev (https://www.linuxquestions.org/questions/programming-9/traversing-dev-765489/)

whatis 10-30-2009 01:05 AM

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

raskin 10-30-2009 01:43 AM

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.

whatis 10-30-2009 05:03 AM

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.

whatis 10-30-2009 11:48 AM

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!

raskin 10-30-2009 12:54 PM

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.

whatis 11-01-2009 11:22 PM

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


All times are GMT -5. The time now is 09:31 AM.