working on a program from the last chapter of k+r book.
here are the error messages i am getting when i tried to compile the program:
here is my source file which i called fsize1.c:
one thing i will mention, is that i compiled a version of the program in which i took out most if not all of the functions that were causing most of the error messages. For example the error message that dealt with the function from the book called readdir i took out, my logic is that it is contained in one of the include files and is causing the error message:
i will include my updated source file which i called fsize.c
i got the program to compile but when i went to run it i got the following from terminal:
when no dir is given as an argument it is supposed to use the current directory.
i don't know maybe this is how the program is supposed to work. count the number of bytes in a directory? any help?
why does terminal say (null)? why doesn't computer go to newline after (null)?
does 374 sound reasonable for a directory with like ten ordinary c files?
i ran the program a couple of more times.
when i just type ./fsize.out without any arguments i got the following:
and then when i wrote ./fsize.out cpcee.c
i got the following;
i did a get info and the number 878 matched the get info's
i tried other tests like writing a couple of files and they matched the get info's i looked at. One question i have is when i used no arguments i got
any help? do you think this program is behaving how the book intended?
here are the error messages i am getting when i tried to compile the program:
Code:
james-collinss-macbook-pro:chap8 jamescollins$ gcc fsize1.c -o fsize1.out
fsize1.c:21: error: conflicting types for stat
/usr/include/sys/stat.h:430: error: previous declaration of stat was here
fsize1.c: In function opendir:
fsize1.c:77: warning: incompatible implicit declaration of built-in function malloc
In file included from /usr/include/sys/dir.h:72,
from fsize1.c:92:
/usr/include/dirent.h: At top level:
/usr/include/dirent.h:82: error: conflicting types for DIR
dirent.h:12: error: previous declaration of DIR was here
/usr/include/dirent.h:106: error: conflicting types for closedir
fsize1.c:85: error: previous definition of closedir was here
/usr/include/dirent.h:110: error: conflicting types for opendir
fsize1.c:69: error: previous definition of opendir was here
/usr/include/dirent.h:114: error: conflicting types for readdir
dirent.h:15: error: previous declaration of readdir was here
fsize1.c:96: error: conflicting types for readdir
dirent.h:15: error: previous declaration of readdir was here
fsize1.c: In function readdir:
fsize1.c:100: error: struct <anonymous> has no member named fd
fsize1.c:105: error: DIRSIZ undeclared (first use in this function)
fsize1.c:105: error: (Each undeclared identifier is reported only once
fsize1.c:105: error: for each function it appears in.)
here is my source file which i called fsize1.c:
Code:
#include <string.h>
#include "syscalls.h"
#include <sys/file.h> /* flags for read and write */
#include <sys/types.h> /* typedefs */
#include <sys/stat.h> /* structure returned by stat */
#include "dirent.h"
void fsize(char *);
/* print file sizes */
main (int argc, char **argv)
{
if (argc == 1) /* default: current directory */
fsize(".");
else
while (--argc > 0)
fsize(*++argv);
return 0;
}
int stat(char *, struct stat *);
void dirwalk(char *, void (*fcn)(char *));
/* fsize: print size of file "name" */
void fsize(char *name)
{
struct stat stbuf;
if (stat(name, &stbuf) == -1) {
fprintf(stderr, "fsize: can't access %s\n", name);
return;
}
if ((stbuf.st_mode & S_IFMT) == S_IFDIR)
dirwalk(name, fsize);
printf("%8ld %s", stbuf.st_size, name);
}
#define MAX_PATH 1024
/* dirwalk: apply fcn to all files in dir */
void dirwalk(char *dir, void (*fcn)(char *))
{
char name[MAX_PATH];
Dirent *dp;
DIR *dfd;
if ((dfd = opendir(dir)) == NULL) {
fprintf(stderr, "dirwalk: can't open %s\n", dir);
return;
}
while ((dp = readdir(dfd)) != NULL) {
if (strcmp(dp->name, ".") == 0
|| strcmp(dp->name, "..") == 0)
continue; /* skip self and parent */
if (strlen(dir)+strlen(dp->name)+2 > sizeof(name))
fprintf(stderr, "dirwalk: name %s/%s too long\n",
dir, dp->name);
else {
sprintf(name, "%s/%s", dir, dp->name);
(*fcn)(name);
}
}
closedir(dfd);
}
int fstat(int fd, struct stat *);
/* opendir: open a directory for readdir calls */
DIR *opendir(char *dirname)
{
int fd;
struct stat stbuf;
DIR *dp;
if ((fd = open(dirname, O_RDONLY, 0)) == -1
|| fstat(fd, &stbuf) == -1
|| (stbuf.st_mode & S_IFMT) != S_IFDIR
|| (dp = (DIR *) malloc(sizeof(DIR))) == NULL)
return NULL;
dp->fd = fd;
return dp;
}
/* closedir: close directory opened by opendir */
void closedir(DIR *dp)
{
if (dp) {
close(dp->fd);
free(dp);
}
}
#include <sys/dir.h> /* local directory structures */
/* readdir: read directory entries in sequence */
Dirent *readdir(DIR *dp)
{
struct direct dirbuf; /* local directory structure */
static Dirent d; /* return: portable structure */
while (read(dp->fd, (char *) &dirbuf, sizeof(dirbuf))
== sizeof(dirbuf)) {
if (dirbuf.d_ino == 0) /* slot not in use */
continue;
d.ino = dirbuf.d_ino;
strncpy(d.name, dirbuf.d_name, DIRSIZ);
d.name[DIRSIZ] = '\0'; /* ensure termination */
return &d;
}
return NULL;
}
one thing i will mention, is that i compiled a version of the program in which i took out most if not all of the functions that were causing most of the error messages. For example the error message that dealt with the function from the book called readdir i took out, my logic is that it is contained in one of the include files and is causing the error message:
Code:
dirent.h:15: error: previous declaration of readdir was here
i will include my updated source file which i called fsize.c
Code:
#include <string.h>
#include "syscalls.h"
#include <sys/file.h> /* flags for read and write */
#include <sys/types.h> /* typedefs */
#include <sys/stat.h> /* structure returned by stat */
#include "dirent.h"
void fsize(char *);
/* print file sizes */
main (int argc, char **argv)
{
if (argc == 1) /* default: current directory */
fsize(".");
else
while (--argc > 0)
fsize(*++argv);
return 0;
}
/* fsize: print size of file "name" */
void fsize(char *name)
{
struct stat stbuf;
if (stat(name, &stbuf) == -1) {
fprintf(stderr, "fsize: can't access %s\n", name);
return;
}
if ((stbuf.st_mode & S_IFMT) == S_IFDIR)
dirwalk(name, fsize);
printf("%8ld %s", stbuf.st_size, name);
}
#define MAX_PATH 1024
/* dirwalk: apply fcn to all files in dir */
void dirwalk(char *dir, void (*fcn)(char *))
{
char name[MAX_PATH];
Dirent *dp;
DIR *dfd;
if ((dfd = opendir(dir)) == NULL) {
fprintf(stderr, "dirwalk: can't open %s\n", dir);
return;
}
while ((dp = readdir(dfd)) != NULL) {
if (strcmp(dp->name, ".") == 0
|| strcmp(dp->name, "..") == 0)
continue; /* skip self and parent */
if (strlen(dir)+strlen(dp->name)+2 > sizeof(name))
fprintf(stderr, "dirwalk: name %s/%s too long\n",
dir, dp->name);
else {
sprintf(name, "%s/%s", dir, dp->name);
(*fcn)(name);
}
}
closedir(dfd);
}
int fstat(int fd, struct stat *);
/* opendir: open a directory for readdir calls */
DIR *opendir(char *dirname)
{
int fd;
struct stat stbuf;
DIR *dp;
if ((fd = open(dirname, O_RDONLY, 0)) == -1
|| fstat(fd, &stbuf) == -1
|| (stbuf.st_mode & S_IFMT) != S_IFDIR
|| (dp = (DIR *) malloc(sizeof(DIR))) == NULL)
return NULL;
dp->fd = fd;
return dp;
}
/* closedir: close directory opened by opendir */
void closedir(DIR *dp)
{
if (dp) {
close(dp->fd);
free(dp);
}
}
i got the program to compile but when i went to run it i got the following from terminal:
Code:
james-collinss-macbook-pro:chap8 jamescollins$ ./fsize.out
374 (null)james-collinss-macbook-pro:chap8 jamescollins$
when no dir is given as an argument it is supposed to use the current directory.
i don't know maybe this is how the program is supposed to work. count the number of bytes in a directory? any help?
why does terminal say (null)? why doesn't computer go to newline after (null)?
does 374 sound reasonable for a directory with like ten ordinary c files?
i ran the program a couple of more times.
when i just type ./fsize.out without any arguments i got the following:
Code:
james-collinss-macbook-pro:chap8 jamescollins$ ./fsize.out
442 (null)james-collinss-macbook-pro:chap8 jamescollins$
and then when i wrote ./fsize.out cpcee.c
i got the following;
Code:
/fsize.out cpcee.c
878 (null)james-collinss-macbook-pro:chap8 jamescollins$
i did a get info and the number 878 matched the get info's
i tried other tests like writing a couple of files and they matched the get info's i looked at. One question i have is when i used no arguments i got
Code:
james-collinss-macbook-pro:chap8 jamescollins$ ./fsize.out
442 (null)james-collinss-macbook-pro:chap8 jamescollins$
any help? do you think this program is behaving how the book intended?