M
Maxx
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]);
while ((numRead = read(STDIN_FILENO, buf, BUF_SIZE)) > 0)
len += numRead;
if (!tempBuf)
tempBuf = malloc(len);
if (tempBuf == NULL) {
errExit("cannot allocate");
memcpy(tempBuf, buf, numRead);
} else {
tempBuf = realloc(tempBuf, len);
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.
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]);
while ((numRead = read(STDIN_FILENO, buf, BUF_SIZE)) > 0)
len += numRead;
if (!tempBuf)
tempBuf = malloc(len);
if (tempBuf == NULL) {
errExit("cannot allocate");
memcpy(tempBuf, buf, numRead);
} else {
tempBuf = realloc(tempBuf, len);
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.