help with debuging system call

This is a discussion on help with debuging system call within the C Programming forums, part of the General Programming Boards category; Hi could you please help me with debugging my code. I've define :char write_buffer[]="COLORTERM=gnome-terminal"; the out file is:environment Q - ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    58

    Red face help with debuging system call

    Hi

    could you please help me with debugging my code.
    I've define :char write_buffer[]="COLORTERM=gnome-terminal";
    the out file is:environment

    Q - why the environment file content is:
    COLORTERM=gnome-terminalCCOLORTERM=gnome-terminalOLORTERM=gnome-terminalCOLORTERCOLORTERM=gnome-terminal$

    I'll expect it to be:COLORTERM=gnome-terminal

    thanks in advanced
    Code:
    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif
    
    
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <stdlib.h>  //used for exit()
    #include <string.h>
    
    #include <fcntl.h>
    #include <unistd.h>
    #include <assert.h>
    #include <error.h>
    
    
    //prototypes
    
    
    /*******************************************************************************/
    int main()
    {
        #define WorkPATH "//My_solution//environment"
        #define file "environment"
        int fd,length=0;
        int size;
        char read_buffer[30];
        char write_buffer[]="COLORTERM=gnome-terminal";
        
        
        //open the file, if it doesn't exist create it with the following permission access
        fd = open (WorkPATH,/* O_RDONLY */ O_RDWR  |O_CREAT,0775);
        if (fd == -1) 
        {
            /* The open failed. Print an error message and exit. */
            printf("error opening file: \n");
            exit(-1);
        }
        /* Read the size of the data in the  file. */
        //Use the systems calls in order to read from $path via read only permissions
        length = read (fd, read_buffer, sizeof (write_buffer));
        
        //Try to write to it ,with the same read only permissions
        length = write (fd, write_buffer, strlen (write_buffer));
        
            printf("The file Content is: \n");
               system("cat //My_solution//environment");    
        // Close the file descriptor, 
        close (fd);
             
         
    
        
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > length = read (fd, read_buffer, sizeof (write_buffer));
    Why are you using the size of the write buffer here?

    Are you confusing O_TRUNC with O_CREAT ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    Quote Originally Posted by ilans11il View Post
    Q - why the environment file content is:
    COLORTERM=gnome-terminalCCOLORTERM=gnome-terminalOLORTERM=gnome-terminalCOLORTERCOLORTERM=gnome-terminal$

    I'll expect it to be:COLORTERM=gnome-terminal
    Two problems: 1. you are reading and then writing, so your read/write pointer will start at file index 30 because you just read 30 bytes with the call to read. 2. After you do the write, you are going to leave whatever was already leftover in the file.

    As a solution, get rid of the `read' call and then call write like this:
    Code:
    length = write (fd, write_buffer, strlen (write_buffer));
    ftruncate(fd, length);
    Then the file referred to by filedescriptor fd will be truncated to length bytes after the call to write.

    EDIT: One more thing... the above is not guaranteed to write the entire write_buffer. For example, say write_buffer is 10000 bytes long. The purpose of the return value is to tell you how many bytes were actually written so that you can continue writing in a loop. The system is only guaranteed to write at least one byte (assuming this is possible).
    Last edited by c99tutorial; 02-10-2013 at 09:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C: system call
    By Dedalus in forum C Programming
    Replies: 6
    Last Post: 06-22-2009, 09:45 AM
  2. system call
    By kastrup_carioca in forum C Programming
    Replies: 7
    Last Post: 01-30-2006, 02:05 PM
  3. Bad System Call
    By osal in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2005, 04:21 PM
  4. System call
    By the_head in forum C Programming
    Replies: 1
    Last Post: 10-04-2003, 06:32 AM
  5. C system call and library call
    By Coconut in forum C Programming
    Replies: 6
    Last Post: 08-22-2002, 11:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21