🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.programmer
Article #280

Re: How to determine device major number?

#280
From: Brendan Kehoe
Date: Mon, 03 Aug 1992 15:51
40 lines
1652 bytes
gram@aim1.aztec.co.za wrote:
>What is the easiest way to determine the major number of a device file from
>within a C program, given the pathname of the file?

Use the stat(2) system call; the st_rdev field is a word containing the
major and minor device numbers stored in its high and low bits
(respectively).  The header <sys/sysmacros.h> should contain two
macros, minor() and major(), that you can use to easily extract the
values.  On SVR4-based systems, there are functions major(3C) and minor(3C).

	#include <sys/types.h>
	#include <sys/stat.h>
	#ifdef SVR4
	# include <sys/mkdev.h>
	#else
	# include <sys/sysmacros.h>
	#endif
	
	int
	major_dev (file)
	     char *file;
	{
	  struct stat sb;

	  if (! file)
	    return -1;
	  if (stat (file, &sb) < 0)
	    {
	      perror ("stat");
	      exit (1);
	    }
	
	  return (int) major (sb.st_rdev);
	}

-- 
Brendan Kehoe, Sun Network Manager                      brendan@cs.widener.edu
Widener University                                                 Chester, PA
 "But Billy! A *small* allowance prepares you for a lifetime of small salaries
  and for your Social Security payments."                     -- Family Circus
Technical Details
Message-ID: <15jkmlINNcde@maggie.cs.widener.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!cs.utexas.edu!asuvax!ukma!widener!nobody
References: <gram.712833707@aim1>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!cs.utexas.edu!asuvax!ukma!widener!nobody
From: brendan@cs.widener.edu (Brendan Kehoe)
Newsgroups: comp.unix.programmer
Subject: Re: How to determine device major number?
Date: 3 Aug 1992 11:51:49 -0400
Organization: Widener University Computer Science Dept, Chester PA
Lines: 40
Message-ID: <15jkmlINNcde@maggie.cs.widener.edu>
References: <gram.712833707@aim1>
NNTP-Posting-Host: maggie.cs.widener.edu