scandir help


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users scandir help
# 1  
Old 03-24-2010
Question scandir help

Hi everyone;

I am trying to compile and execute a test program I wrote that calls scandir and it seems to not like my prototype. The Documentation on SUN for this function is clear, but I am missed something..... and I can't figure it out;

I am using
Code:
cc -gv -osaveas  saveas.c

on Sun Solaris 8

===============code==========================
Code:
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <sys/stat.h>                                                                    
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <signal.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <pwd.h>
#include <limits.h>
#include <ctype.h>
#include <dirent.h>

#define TRUE  1
#define FALSE 0
#define HEX_0a '\x0a'
#define HEX_00 '\x00'

extern int scandir(char *val, struct dirent ***namelist, int (*select) (), int (*compar) () ); 
extern int errno;

int main ( )
{
    int num_entries, iresults, fh1, isize;
    long i, ii;
    int isize_beg, isize_end, start_brw_number, isize_num_start_pos;
    long reportno;
    unsigned int bytes, base;
    int FIRST_NUMERIC_FLAG;
    char debugLine[2000];
    char *numStrFld;
    char *val1, *p;
    char report_num[256], report_stripped[256], reportfile[256];
    char smisc_text[10], file_record[256], error_msg[200];
    char char_brw_number[10], cProcessDate[10], cProcessTime[10];
    struct dirent  **namelist, **list;
    struct stat buf;
    struct tm *tm;
    time_t timestamp;
    mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;

       val1=NULL;
       val1 = getenv("dd_RPTDIR");

       if (val1 == NULL)
          val1 = getenv("dd_MIFILES");


       if (( num_entries = scandir(val1, &list,  NULL, NULL)) < 0)
       {
           printf ( "\nBad scandir call");
           return 1;
       }

       fh1 = open ("dirlist.txt", O_TRUNC | O_CREAT | O_WRONLY, mode );
       if ( fh1 == -1 )
       {
           snprintf ( smisc_text, 10,"%d", errno );
           memset (error_msg, 0, sizeof(error_msg) );
           strcat (error_msg, smisc_text );
           printf ("\n%s", error_msg);
           free(namelist);
           return 1;
       }

//********************* header record which contains the report directory path
       memset (file_record, ' ', sizeof(file_record));
       strncpy(file_record, val1, (strlen(val1) ) );
       isize = strlen(val1);
       file_record[isize] = HEX_0a; isize++;
       file_record[isize] = HEX_00;
       isize = strlen(file_record);
       bytes = write (fh1, file_record, isize);
       if ( bytes == -1 )
       {
         snprintf ( smisc_text, 10,"%d", errno );
         close ( fh1 );
         memset (error_msg, 0, sizeof(error_msg) );
         strcpy (error_msg, "Funds System Error 705 - Unable to write to report header in dirlist.txt - ");
         strcat (error_msg, smisc_text );
         printf ("\n%s", error_msg);
         return 1;
        }

//*************************************************************

    if (num_entries)
	{
		for (i=0, list=namelist; i<num_entries; i++)
		{
		    memset (file_record, ' ', sizeof(file_record));
	            iresults = stat( ((*list)->d_name), &buf);
		    if ( ((*list)->d_name[0]) != '.' )
		    {
		       if ((strstr(((*list)->d_name), ".BRW") != NULL) || (strstr(((*list)->d_name), ".brw") != NULL))
		       {
	//	            isize = ((*list)->d_namlen);
		            isize-=4;
 		            strncpy(file_record, ((*list)->d_name), isize );
 		            if ( isize < 8 )
 		            {
 		            	do
 		            	{  file_record[isize] = ' ';
 		            	   isize++;
                                }  while ( isize < 8 );
                     }
                     file_record[isize] = HEX_0a; isize++;
                     file_record[isize] = HEX_00;
                     isize = strlen(file_record);
                     bytes = write (fh1, file_record, isize);
                     if ( bytes == -1 )
                     {
                        free(namelist);
                        snprintf ( smisc_text, 10,"%d", errno );
                        close ( fh1 );
                        memset (error_msg, 0, sizeof(error_msg) );
                        strcpy (error_msg, "Funds System Error 702 - Unable to write to report directory listing file, dirlist.txt - ");
                        strcat (error_msg, smisc_text );
                        printf ("\n%s", error_msg);
                        return 1;
                     }
               }
            }
            free (*list);
            list++;
          }
	}
      	free(namelist);
       	return 0;
}




when I compile the program,scandir is an undefines symbol....

Undefined first referenced
symbol in file
scandir saveas.o
ld: fatal: Symbol referencing errors. No output written to saveas

Last edited by vbe; 03-24-2010 at 02:04 PM.. Reason: code tags please
# 2  
Old 03-25-2010
It works fine on solaris 10 out of the box. Not on 8 or 9, I don't have anything else.

On solaris 9 however, the manpage scandir indicates that it is part of the berkeley (ucb) compatability library. This is for BSD. With Sun cc you compile with the berkeley compatibility wrapper /usr/ucb/cc instead of /opt/SUNWspro/bin/cc. But you have to have these compilers installed.

are you using gcc? alias cc=gcc You best solution is to call:
opendir()
readdir()
closedir()
in that order.

There is an oddball BSD library: /usr/4lib/libc.so.[whatever] In a quick test, I cannot get gcc to resolve scandir from it. nm shows that scandir is a symbol in there.
alphasort also has the same problem. If no gcc try /usr/ucb/cc instead of cc.

The only problem with BSD compatibility is that it affects the semantics of other system calls as well.
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. Programming

scandir() and threads

I couldn't find anywhere informations about it. Is scandir() thread-safe? (4 Replies)
Discussion started by: dawwin
4 Replies

2. Programming

scandir() + windows equivalent

Currently, Im trying to redesign some Unix-based C code to work on the Windows operating system and one problem I ran into when compiling was that the compiler failed to recognise scandir() (from my original Linux code) Im aware that scandir() is a Unix-based function, so is there an equivalent... (1 Reply)
Discussion started by: JamesGoh
1 Replies

3. Programming

scandir() for Solaris

I'd like to use scandir() on solaris, but it doesn't find sys/dir.h What do you think I should do? thanks (2 Replies)
Discussion started by: nadiamihu
2 Replies

4. Programming

Need help with scandir / stat

I'm writing a file manager program using FC3 and C, and I'm having a problem displaying the stat info of subdirectories. #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> int main() { char *dirname = "mydirectory"; struct dirent **namelist; ... (2 Replies)
Discussion started by: Bertsura
2 Replies
Login or Register to Ask a Question