fstat is failing with invalid argument - c

fileDes= open(dbPathname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
}
printf("File %s opened successfully with handle %d\n", dbPathname, fileDes );
if(-1 == fileDes)
{
PERS_ERROR( "pers_db_open_default: File %s opened Failed %d \n", dbPathname, fileDes );
return NULL; //return PERS_COM_FAILURE;
}
*db = fileDes;
printf("file info: FD=[%d], &FD=[%d], fileDes=[%d] \n", (*db),(db), fileDes );
if( 0 != fstat(fileDes, &sb))
{;
perror("fileInfo() fstat-perror= \n");
printf("\nfstat error: [%s] and st.st_mode is [%d]\n",strerror(errno), sb.st_mode);
return NULL;
}
output:-
fileInfo() fstat-perror=
: Invalid argument
Segmentation fault (core dumped)

Related

permission denied with open()

The issue arises with fopen(), stating that permission is denied. I have tried to input all the modes however this was unsuccessful. Same when adding all the writing modes. I am attempting to open a file to with which i can write. Thanks to anyone who helps
if(filename != NULL && redirectArrow == true){
mode_t modes = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
int fd = open(filename, O_WRONLY | O_CREAT , S_IWUSR | S_IWGRP | S_IWOTH); //opening file in write only, and creating file if it does not exist
if(fd == -1){
perror("Failed to create and open the file\n");
exit(3);
}

C unistd.h write() command write extra character

I've been trying to do c programming implementation of cp command in unix/linux by using system calls (read(), write(), open(), close()).
But when I run my program through terminal by copying my source code of this program to the same directory with name change (the source code is about 300 lines)
and when I open that output file , it has more character than the original file.
The extra line is the same as 200ish line. Where does it came from?
here's the screenshot when compile
argp.c
123.c
This is the source code near the end of the original file(argp.c). You will see how I use the read write method.
while (count - ind > 1) {
strcpy(cdir, argv[argc-1]);
if (!isFile(cdir)) {
strcat(cdir, basename(arguments.argv[ind]));
}
if (arguments.update) {
stat(cdir,&stDest);
stat(arguments.argv[ind],&stSrc);
if (difftime(stDest.st_mtim.tv_sec, stSrc.st_mtim.tv_sec) > 0 ) {
printf("Destination file is newer\n");
exit(EXIT_FAILURE);
}
}
//open source file
src = open(arguments.argv[ind],O_RDONLY);
//if source file can't be opened
if (src == -1) {
printf("\nError opening file %s errno = %d\n",arguments.argv[ind],errno);
exit(EXIT_FAILURE);
}
//open target file
if (arguments.force) {
//with -f option(default)
tgt = open(cdir, O_WRONLY | O_CREAT | O_TRUNC , S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH);
} else {
//with -n option
tgt = open(cdir, O_WRONLY | O_CREAT | O_EXCL , S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH);
}
//if the target file cannot be read or already exist(with -n option)
if (tgt == -1) {
printf("File exist or it's not a file.\nCan't copy.\n");
exit(EXIT_FAILURE);
}
//read source file
//write target file
while ((pos = read(src, buffer, BUFFERSIZE)) > 0) {
if (write(tgt, buffer, BUFFERSIZE) != pos) {
exit(EXIT_FAILURE);
}
}
//if the source file cannot be read
if (pos == -1) {
printf("\nError in reading data from %s\n",arguments.argv[ind]);
}
//close source file
if (close(src) == -1) {
printf("\nError in closing file %s\n",arguments.argv[ind]);
}
//close target file
if (close(tgt) == -1) {
printf("\nError in closing file %s\n",cdir);
}
ind++;
}
if (arguments.verbose) {
printf("Copy successfully!\n");
}
exit(EXIT_SUCCESS);
}
This is the source code near the end of the copy file(123.c)
while (count - ind > 1) {
strcpy(cdir, argv[argc-1]);
if (!isFile(cdir)) {
strcat(cdir, basename(arguments.argv[ind]));
}
if (arguments.update) {
stat(cdir,&stDest);
stat(arguments.argv[ind],&stSrc);
if (difftime(stDest.st_mtim.tv_sec, stSrc.st_mtim.tv_sec) > 0 ) {
printf("Destination file is newer\n");
exit(EXIT_FAILURE);
}
}
//open source file
src = open(arguments.argv[ind],O_RDONLY);
//if source file can't be opened
if (src == -1) {
printf("\nError opening file %s errno = %d\n",arguments.argv[ind],errno);
exit(EXIT_FAILURE);
}
//open target file
if (arguments.force) {
//with -f option(default)
tgt = open(cdir, O_WRONLY | O_CREAT | O_TRUNC , S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH);
} else {
//with -n option
tgt = open(cdir, O_WRONLY | O_CREAT | O_EXCL , S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH);
}
//if the target file cannot be read or already exist(with -n option)
if (tgt == -1) {
printf("File exist or it's not a file.\nCan't copy.\n");
exit(EXIT_FAILURE);
}
//read source file
//write target file
while ((pos = read(src, buffer, BUFFERSIZE)) > 0) {
if (write(tgt, buffer, BUFFERSIZE) != pos) {
exit(EXIT_FAILURE);
}
}
//if the source file cannot be read
if (pos == -1) {
printf("\nError in reading data from %s\n",arguments.argv[ind]);
}
//close source file
if (close(src) == -1) {
printf("\nError in closing file %s\n",arguments.argv[ind]);
}
//close target file
if (close(tgt) == -1) {
printf("\nError in closing file %s\n",cdir);
}
ind++;
}
if (arguments.verbose) {
printf("Copy successfully!\n");
}
exit(EXIT_SUCCESS);
}
it(EXIT_FAILURE);
}
//read source file
//write target file
while ((pos = read(src, buffer, BUFFERSIZE)) > 0) {
if (write(tgt, buffer, BUFFERSIZE) != pos) {
exit(EXIT_FAILURE);
}
}
//if the source file cannot be read
if (pos == -1) {
printf("\nError in reading data from %s\n",arguments.argv[ind]);
}
//close source file
if (close(src) == -1) {
printf("\nError in closing file %s\n",arguments.argv[ind]);
}
//close target file
if (close(tgt) == -1) {
printf("\nError in closing file %s\n",cdir);
}
ind++;
}
if (arguments.verbose) {
printf("Copy successfully!\n");
}
exit(EXIT_SUCCESS);
}
You always try to read/write BUFFERSIZE bytes. But what happens if the file you want to copy has a size multiple of BUFFERSIZE? You write what have been read last time.
read return the number of bytes read, so each write should try to write this number of bytes:
Instead of:
pos = read(src, buffer, BUFFERSIZE);
//write target file
while (pos > 0) {
write(tgt, buffer, BUFFERSIZE);
pos = read(src, buffer, BUFFERSIZE);
}
Use:
pos = read(src, buffer, BUFFERSIZE);
//write target file
while (pos > 0) {
write(tgt, buffer, pos);
pos = read(src, buffer, BUFFERSIZE);
}
Same here, instead of:
//write target file
while ((pos = read(src, buffer, BUFFERSIZE)) > 0) {
if (write(tgt, buffer, BUFFERSIZE) != pos) {
exit(EXIT_FAILURE);
}
}
Use:
//write target file
while ((pos = read(src, buffer, BUFFERSIZE)) > 0) {
if (write(tgt, buffer, pos) != pos) {
exit(EXIT_FAILURE);
}
}
You unconditionally write a full buffer, regardless of how much was read:
//write target file
while ((pos = read(src, buffer, BUFFERSIZE)) > 0) {
if (write(tgt, buffer, BUFFERSIZE) != pos) {
exit(EXIT_FAILURE);
}
}
should be:
//write target file
while ((pos = read(src, buffer, BUFFERSIZE)) > 0) {
if (write(tgt, buffer, pos) != pos) {
exit(EXIT_FAILURE);
}
}

Open() for output not creating file

I have this function that utilizes open to set i/o redirection:
void setOutput(char * buffer){
int file = open(buffer, O_WRONLY || O_CREAT, S_IWUSR);
if(file < 0){ printf("error opening %s for output\n", buffer); }
if(dup2(file, 1) < 0){ printf("error with dup2 opening %s for output\n", buffer); }
}
When I run it, it works fine for files that are already defined but returns -1 when it receives a non-created file. Not sure why
You need to change the following
int file = open(buffer, O_WRONLY || O_CREAT, S_IWUSR);
To
int file = open(buffer, O_WRONLY | O_CREAT, S_IWUSR);
Format :
int open( char *filename, int access, int permission );
access : Should be provided as a bit wise OR operator, that means using | not || which is logical OR

C - write() not writing to file

My problem is simple, when i try to confirm if the function wrote on the file, nothing shows up, the file is there, but there's nothing inside the file, where should be 1024 times the string i wanted.
int escreve1x( const char* path , const char* cadeia )
int fd = open( path, O_CREAT, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH );
int i;
printf("%s\n", cadeia);
for ( i=0 ; i<=1024 ; i++ )
{
write( fd, cadeia, 10);
}
return 0 ;
1-Add "O_RDWR" flag for making fd writable.
2-Always debug the result of write syscall;
int fd = open( path, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH );
if(fd<0) {
printf("Open failed\n");
return 1;
}
int i;
printf("%s\n", cadeia);
for ( i=0 ; i<=1024 ; i++ ) {
int status=write( fd, cadeia, 10);
if(status<0) {
printf("Write failed");
}
}
return 0;
If it says write has failed then check errno.

Redirection doesn't work in shell:: ls: cannot access >: No such file or directory

I don't know why redirection doesn't work in the shell I have written. Here's my code"
int i;
for (i=1; !args[i];i++)
{
if (args[i]== ">")
{
printf("argv[i] %s %d \n", args[i], i);
int out;
// out = open("out", O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
out=open("out", O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU);
int fdl=dup2(out,1);
close(out);
execvp(args[0],args);
}
}
Also here's the error I receive :
mysh> ls
basic_shell basic_shell.c~ fork fork_2 fork_cp.c
basic_shell.c basic_shell_OK.c fork_1 fork.c
mysh> ls > file
ls: cannot access >: No such file or directory
ls: cannot access file: No such file or directory
Please let me know what's wrong?
If args is an array of char*, then this condition
if (args[i]== ">")
does not do what you think it does. It compares the pointers and not what they point to. To compare string you have to use strcmp.

Resources