C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 11th, 2002, 05:46 PM
ryo ryo is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: london
Posts: 0 ryo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to ryo
Trying to compile/run a program i wrote on a UNIX machine, on a Linux machine.

Hi,
I'm writing a program to emulate the ls command, and have written a lot of it on a UNIX machine at college. I'm now trying to compile and run the program on my home machine (Debian Linux) using gcc, but am getting a non-terminating loop, and no useful results.
I get the result:
$У@?Ð
over and over again, instead of listing the files in the directory.
Is this a problem that's not easily fixed because of the different Operating Systems? Or is there something I can do to make it work on both?
Any info appreciated.

Source code:

/* emulator of the ls function */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

void printstats(char name[])
{

}

void listdir(char dirname[])
{
int fp,i=0,j,readfile;
char buffer[16];
if ((fp = (open(dirname,O_RDONLY))) != -1 )
{
printf("sucess\n");
while((readfile=read(fp,buffer,sizeof(buffer)))!=0)
{
for (j=2;j<15;j++)
{
printf("%c",buffer[j]);
}
printf("\n");
i++;
}
close(dirname);
}
else
{
printf("fail");
}

}

void main(int argc, char *argv[])
{
listdir(*++argv);
}

Reply With Quote
  #2  
Old April 11th, 2002, 06:12 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,385 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 30 m 25 sec
Reputation Power: 4080
I think you should be using opendir(), readdir() and closedir() instead of open(), read() and close()?? These functions are part of the standard C library and are designed to specifically read directories.

When you try to read the directory with the open() and read() commands, you're probably getting the directory contents along with the file attributes and everything else (which may or may not be stored in binary format), which is why you're seeing all the funny characters. You'd see the same behaviour, if you type:

cat /directoryname

Under FreeBSD, you'll see a lot of junk characters along with the file names. These are the additional information (such as permissions, inodes etc.) that are associated with the file names. The command will not work under Linux (at least RedHat 7.1 up) because cat will complain that it is a directory.

Anyway, opendir(), readdir() and closedir() are the way to go.

Reply With Quote
  #3  
Old April 11th, 2002, 06:49 PM
ryo ryo is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: london
Posts: 0 ryo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to ryo
Ok I tried that and at least it got rid of the non-terminating loop
Now I have a different problem though:

ryo@debian:~/c dev$ ./list .
sucess
4?¡ò@??
4?¡ò@??
4?¡ò@??
4?¡ò@??
Segmentation fault

I've kinda been dumped in the deep end with system calls in c, is there anywhere I can read up on it?
By the way the program will go on to read permissions, inode numbers, and more info, would using opendir(), readdir(), and closedir() hinder that?

Reply With Quote
  #4  
Old April 11th, 2002, 07:05 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,385 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 30 m 25 sec
Reputation Power: 4080
Here's some code demonstrating the usage of opendir(), closedir() and readdir(). I've tested this on both FreeBSD and Linux.

Code:
#include <dirent.h>
#include <stdio.h>

int dirlist(char *);

int main(int argc, char *argv[]) {
  if (argc > 1)
    dirlist(*++argv);

  return 0;
}

int dirlist(char *dirname) {
  DIR *dirp;
  struct dirent *direntry;

  fprintf(stderr, "Dir list for %s\n", dirname);

  /* Open a pointer to the directory structure */
  if ((dirp = opendir(dirname)) == NULL) {
    fprintf(stderr, "Could not open dir %s\n", dirname);
    return 1;
  }

  /* Read each file in sequence from the directory structure */
  while ((direntry = readdir(dirp)) != NULL) {
    fprintf(stdout, "%s\n", direntry->d_name);
  }

  /* Close the resource */
  closedir(dirp);

  return 0;
}


As for finding the rest of the information, the dirent structure contains a field called d_ino which is the inode number. For the rest of the information, check out the stat() family of functions. You can get the help for these functions under Linux by typing:

man -S2 stat

Incidentally, the opendir(), readdir(), stat() etc functions conform to POSIX standards, so they should be available on practically all modern *NIXES.

Reply With Quote
  #5  
Old April 11th, 2002, 07:19 PM
ryo ryo is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: london
Posts: 0 ryo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to ryo
Thanks, I'll have a look through it and see what I can pick up

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Trying to compile/run a program i wrote on a UNIX machine, on a Linux machine.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap