The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Trying to compile/run a program i wrote on a UNIX machine, on a Linux machine.
Discuss Trying to compile/run a program i wrote on a UNIX machine, on a Linux machine. in the C Programming forum on Dev Shed. Trying to compile/run a program i wrote on a UNIX machine, on a Linux machine. C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

April 11th, 2002, 05:46 PM
|
|
Junior Member
|
|
Join Date: Apr 2002
Location: london
Posts: 0
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
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);
}
|

April 11th, 2002, 06:12 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
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.
|

April 11th, 2002, 06:49 PM
|
|
Junior Member
|
|
Join Date: Apr 2002
Location: london
Posts: 0
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
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?
|

April 11th, 2002, 07:05 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
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.
|

April 11th, 2002, 07:19 PM
|
|
Junior Member
|
|
Join Date: Apr 2002
Location: london
Posts: 0
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Thanks, I'll have a look through it and see what I can pick up 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|