LinuxQuestions.org
Review your favorite Linux distribution.
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 03-10-2006, 02:22 PM   #1
superm1a3
LQ Newbie
 
Registered: Feb 2006
Posts: 3

Rep: Reputation: 0
Linking error when using system call: getdents


Whenever I use this function: getdents(), there will always be a linking error: (My OS is RedHat 9)

/tmp/ccGcMA98.o(.text+0xd4): In function `processDirectory':
: undefined reference to `getdents'
collect2: ld returned 1 exit status

I searched through google, there were not many related records. Some said that it was because the gcc version. Some said that this function was ok under UNIX. Does anybody know how to solve this problem? Thanks in advance.
 
Old 03-10-2006, 02:27 PM   #2
superm1a3
LQ Newbie
 
Registered: Feb 2006
Posts: 3

Original Poster
Rep: Reputation: 0
Code

Code:
#include <stdio.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/types.h>
#include <linux/dirent.h>
#include <linux/unistd.h>

long processFile ();
long processDirectory ();

main (argc, argv)
     int argc;
     char* argv[];
{
  long count;
  count = processFile (argv[1]);
  printf ("Total number of non-directory files is %ld\n", count);
  return (0);
}

long processFile (name)
     char* name;
{
  struct stat statBuf; /* To hold the return data from stat () */
  mode_t mode;
  int result;
  result = stat (name, &statBuf); /* Stat the specified file */
  if (result == -1) return (0); /* Error */
  mode = statBuf.st_mode; /* Look at the file's mode */
  if (S_ISDIR (mode)) /* Directory */
    return (processDirectory (name));
  else
    return (1); /* A nondirectory file was processed */
}

long processDirectory (dirName)
     char* dirName;
{
  int fd, children, i, charsRead, childPid, status;
  long count, totalCount;
  char fileName[100];
  struct dirent dirEntry;
  fd = open (dirName, O_RDONLY); /* Open directory for reading */
  children = 0; /* Initialize child process count */
  while (1) /* Scan directory */
    {
      charsRead = getdents (fd, &dirEntry, sizeof (struct dirent));
      if (charsRead == 0) break; /* End of directory */
      if (strcmp (dirEntry.d_name, ".") != 0  &&
	  strcmp (dirEntry.d_name, "..") != 0)
	{
	  if (fork () == 0) /* Create a child to process dir. entry */
	    {
	      sprintf (fileName, "%s%s", dirName, dirEntry.d_name);
	      count = processFile (fileName);
	      exit (count);
	    }
	  else
	    ++children; /* Increment count of child processes */
	}
      lseek (fd, dirEntry.d_off, SEEK_SET); /* Jump to next dir. entry */
    }
  close (fd); /* Close directory */
  totalCount = 0; /* Initialize file count */
  for (i = 1; i <= children; i++) /* Wait for children to terminate */
    {
      childPid = wait (&status); /* Accept child's termination code */
      totalCount += (status >> 8); /* Update file count */
    }
  return (totalCount); /* Return number of files in directory */
}
 
Old 03-10-2006, 10:48 PM   #3
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
Use either getdirentries() or readdir()... getdents() is a BSD call that is sometimes implemented as a system call in Linux with no Glibc wrapper.
 
  


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
Linking is badly borked on your system acidjuice Slackware 19 05-13-2005 12:56 PM
Is it possible to use system() and get the return value from the system call newguy21 Programming 1 08-11-2004 01:37 PM
NETPERF: ERROR --> send_udp_stream: error on remote: Interrupted system call dravya Linux - General 1 05-29-2004 05:49 PM
Error when add a new system call in RH9.0. GreenStar Linux - Newbie 2 11-01-2003 01:52 PM
about system call alchen1999 Programming 10 09-01-2003 03:43 AM

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

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