Thread: How to 'delete' and 'echo' in Linux??

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    4

    Unhappy How to 'delete' and 'echo' in Linux??

    Dear all,
    this is my first time I post in the forum.

    I would like to ask you if you know how I can get my hostname and how I can use echo in linux using C.
    I dont want the system() or execl ways, I want to make the code!

    Can anyone help me?

    Thanks!

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Welcome to the forum!

    What exactly do you mean by "I want to make the code"? Give an example of how you would want to run this program of yours to clarify things.

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Look for unlink() for deleting a file...hostname is done via the BSD socket libs, gethostbyname() I think (gotta look it up, been a while since I used it). Echo is accomplished by basic C IO commands (some echo the input, others do not). Now if you meant by echo 'print stuff to screen), printf() will work fine, is basic C and not unique to Linux..
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by jeffcobb View Post
    Look for unlink() for deleting a file
    Why not remove() which is a standard C function, instead of using a platform specific function?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    Thanks!
    for example I have tried the code below to get the current directory:
    Code:
    #define FALSE 0
    #define TRUE !FALSE
    
    extern int alphasort();
    
    char pathname[MAXPATHLEN];
    
    main() {
        int count, i;
        struct direct **files;
        int file_select();
    
        getwd(pathname);
            
            
        printf("Current Working Directory = %s\n", pathname);
        count = scandir(pathname, &files, file_select, alphasort);
    
        /* If no files found, make a non-selectable menu item */
    
     }
    
    int file_select(struct direct *entry)
     {
        if ((strcmp(entry->d_name, ".") == 0) ||
                (strcmp(entry->d_name, "..") == 0))
            return (FALSE);
        else
            return (TRUE);
    }
    it works without any problem.
    So I'm looking to do something same with "echo" and "rm"...
    Any ideas??

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    echo this would be
    Code:
    #include <stdio.c>
    printf("%s","this");
    rm that.file would be
    Code:
    #include <unistd.h>
    unlink("that.file");
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Are you trying to write a C program that does what the "echo" utility does on *nix systems?

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    Thanks for your reply!

    rm works OK!!!
    but I have problem with "echo"
    I wrote this:
    Code:
    #include <stdio.h>
    main()
    {
    printf("%s","this");
    }
    then I wrote in terminal "./a.out blabla" and I didnt see my message :-(

    ....

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Lightbulb

    Ah -- you need to use the command line args:
    Code:
    int main(int argc, char *argv[]) {
        if (argc < 2) {
            puts("echo what?!");
            return -1;
        }
        printf("%s\n",argv[1]);
        return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Just curious but why are you reinventing the wheel with these functions?

  11. #11
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by cpjust View Post
    Why not remove() which is a standard C function, instead of using a platform specific function?
    Maybe because....the OP asked for a Linux way of doing it?
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by jeffcobb View Post
    Maybe because....the OP asked for a Linux way of doing it?
    remove() works on Linux just the same as it does on any other OS. I only use platform specific code if there isn't a portable function available to do the job.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  13. #13
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    Thanks for your reply guys!
    Sorry for the delay but I didnt have internet connection.
    OK so I have created 2 files...how I can write a C file and then call the mains of the other 2 files from my basic c file?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. shell problem
    By ArXi in forum Linux Programming
    Replies: 2
    Last Post: 06-04-2009, 02:30 AM
  2. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  3. delete line from text file
    By Waldo2k2 in forum Linux Programming
    Replies: 6
    Last Post: 05-07-2005, 06:03 PM