On Dec 29 2011, 9:49*am, Barry Schwarz <schwa...@dqel.com> wrote:
> On Thu, 29 Dec 2011 07:28:16 -0800 (PST), Maxx
>
>
>
>
>
>
>
>
>
> <grungeddd.m...@gmail.com> wrote:
> >I'm writing this program which works like the linux command tee which
> >copies the standard input to standard output and to the file provided
> >in the command line argument. By default this program will truncate
> >any existing file to zero or if the option *"-a" is specified on the
> >command line it will append the output at the end of the file. *Now
> >this program is getting compiled error free but when i attempt to run
> >it i get a segmentation fault.
>
> >./exrcs401<ename.c.inc tfile
> >Segmentation fault.
>
> >exrcs401 is the name of the program
> >ename.c.inc is the input file
> >tfile is the output file.
>
> >here is the program:::
>
> >#include <sys/stat.h>
> >#include <fcntl.h>
> >#include <stdlib.h>
> >#include <string.h>
>
> >#ifndef BUF_SIZE
> >#define BUF_SIZE 1024
> >#endif
>
> >int
> >main(int argc, char *argv[])
> >{
> > * *int openFlags, outputFd;
> > * *mode_t filePerms;
> > * *ssize_t numRead, numWritten;
> > * *size_t len = 0;
> > * *char buf[BUF_SIZE], *tempBuf;
>
> > * *if (argc < 2 || strcmp(argv[1], "--help") == 0)
> > * * * *usageErr("%s input file or {-a:append}...\n",
> > * * * * * * * * *argv[0]);
>
> > * *filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
> > * * * * * * * *S_IROTH | S_IWOTH;
>
> > * *if (strcmp(argv[2], "-a") == 0)
> > * * * *openFlags = O_RDWR | O_APPEND;
>
> > * *else
> > * * * *openFlags = O_RDWR | O_CREAT | O_TRUNC;
>
> > * *outputFd = open(argv[1], openFlags, filePerms);
> > * *if (outputFd = -1)
> > * * * *errExit("opening file %s", argv[1]);
>
> Where is this function declared?
>
>
This custom error functions are all declared in "tlpi_hdr.h". and they
work fine i've tested them in other programs
>
> > * *while ((numRead = read(STDIN_FILENO, buf, BUF_SIZE)) > 0)
> > * * * *len += numRead;
> > * * * *if (!tempBuf)
>
> Where is tempBuf provided a value. *It is not initialized in the
> definition. *Its value is indeterminate and you have no idea how this
> if evaluates. *If it is not set to NULL by default in your compiler,
> this if evaluates to false and ...
>
>
Yeah i get it leaving tempbuf with an indeterminate value must have
been the source of the error.
I wasn't aware of this at all.
>
> > * * * * * *tempBuf = malloc(len);
> > * * * * * *if (tempBuf == NULL) {
> > * * * * * * * *errExit("cannot allocate");
>
> Does errExit actually return?
>
> > * * * * * * * *memcpy(tempBuf, buf, numRead);
>
> I think you have your braces mixed up. *This call to memcpy is
> executed only when tempBuf is NULL and you cannot copy into the area
> pointed to by NULL. *Dereferencing a NULL pointer is a frequent cause
> of seg faults.
>
>
>
> > * * * * * *} else {
> > * * * * * * * *tempBuf = realloc(tempBuf, len);
>
> ... you invoke undefined behavior by passing realloc a value that is
> neither NULL nor the result of a previous call to {m|c|re}alloc.
> Passing such a value to free or {c|re}alloc is also a frequent cause
> of seg faults.
>
>
>
>
>
>
>
>
>
> > * * * * * * * *memcpy(tempBuf, buf, numRead);
> > * * * * * *}
>
> > * * * *if (( write(STDOUT_FILENO, buf, numRead )) != numRead &&
> > * * * * * *( write(outputFd, tempBuf, numRead) != numRead))
> > * * * * * * * *fatal("couldn't write whole buffer");
>
> > * *if (numRead == -1)
> > * * * *errExit("read");
>
> > * *if (close(outputFd) == -1)
> > * * * *errExit("close output");
>
> > * *exit(EXIT_SUCCESS);
>
> >}
>
> >Please help i cannot figure out the source of the problem.
>
> Have you tried using a debugger?
>
No i haven't but i did used the printf() technique pointed out by the
others in this board. Anyways i think that tempbuf thing was the
source of the problem, i will fix it right away. Thanks a lot.
Thanks
Maxx
|