Almost -> Hello World!


 
Thread Tools Search this Thread
Top Forums Programming Almost -> Hello World!
# 1  
Old 09-04-2010
Hammer & Screwdriver Almost -> Hello World!

Hello! I know I must take the efforts of learning C..! I need to recompile a binary with the following at the beginning: test if a file exists, remove it and exit. All in "C". As simple as this in sh:

Code:
file=/tmp/filename
if [ -f $file ]; then
    rm -f $file
    exit 0
fi

Thanks!
# 2  
Old 09-04-2010
I think there are a lot of us here who could write that in C in a matter of a few minutes, but you'd not learn anything if any of us did. Thus, I'll offer a few pointers:
  • look at using argc and argv to get the file name from the command line
  • Use the stat() function to determine whether the file exists.
  • Use the unlink() funciton to remove the file if it exists

Use the man command to determine the calling parameters and how they work. Have fun -- C is a wonderful language.
# 3  
Old 09-04-2010
Hi, agama! Don't forsake me this time! I know I need to learn C, but what you said to me is like speaking in greek! I'm wrestling with a broken binary which is called by initramfs, I tried doing a gpkg-divert on the binary and calling it from a shell script. Something like:

Code:
#!/bin/sh

file=/tmp/filename
if [ -f $file ]; then
    rm -f $file
    exit 0
else
    $0.debian $@ # the name of the diverted binary
fi

Now, the binary is called in the beginning of boot, and it tells: /sbin/binary.debian: not found (while the binary is there...) The script works flawlessly, but fails when called by initramfs.

So I found that I should include this piece of code in the C source, but I don't understand a single piece of code in C and I'm in a big hurry.

I even feel like developing some tools in C (which is IMO the best language - powerful and lightweight), but it's too much for me in such a hurry.

Thanks!
# 4  
Old 09-04-2010
Your request sounded like a homework problem.

Here is a simple programme that unlinks the file named on the command line. You should be able to adapt it to your needs.

Code:
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>


int main( int argc, char **argv )
{
        struct stat buf;

        if( argc <= 1 )
                return;                 // no filename supplied on command line

        if( stat( argv[1], &buf ) >= 0 )        // assume success means the file is there
                unlink( argv[1] );

        return 0;
}

This User Gave Thanks to agama For This Post:
# 5  
Old 09-05-2010
Oh, thanks! I actually got it with:

Code:
FILE *fp = fopen("/tmp/filename","r");
if( fp ) {
    unlink("/tmp/filename");
    exit(EXIT_SUCCESS);
fclose(fp);
}

But unfortunately I thought I would just paste it in the beginning of some int main() and it would work, but is far beyond that... Too many source files and none of them worked. Thanks anyway!
# 6  
Old 09-05-2010
Quote:
Originally Posted by teresaejunior
Oh, thanks! I actually got it with:

Code:
FILE *fp = fopen("/tmp/filename","r");
if( fp ) {
    unlink("/tmp/filename");
    exit(EXIT_SUCCESS);
fclose(fp);
}

But unfortunately I thought I would just paste it in the beginning of some int main() and it would work, but is far beyond that... Too many source files and none of them worked. Thanks anyway!
Free the file descriptor back to the system and then unlink it.
This User Gave Thanks to matrixmadhan For This Post:
# 7  
Old 09-05-2010
Oh, I understand! Thanks! The problem now is that I don't know where to paste this piece of code. I don't know which source file generates the binary... There are too many source files, and none of them is named like the originated binary... From the Makefile is difficult to find it out.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. What is on Your Mind?

Mad World Remix of Moby Video (Are You Lost In The World Like Me)

This is an excellent video comment on modern society and the remix is good too: https://www.youtube.com/watch?v=5DU1B_XkyIk 5DU1B_XkyIk Watch the video above and post your comments. (3 Replies)
Discussion started by: Neo
3 Replies

2. What is on Your Mind?

Hello World!

Just wanna say "Hello World!" :) (6 Replies)
Discussion started by: ment0smintz
6 Replies

3. Programming

Exact meaning of the "world" in "hello world"

Hello! I have a question to native English-speaking people. In the popular program's "hello world" greeting, what meaning the "world" has: "all", "everybody", "people", "friends" or "whole world", "planet", "Earth", "Universe"? In other words, to whom this greeting is addressed: to the... (14 Replies)
Discussion started by: Eugene Muzychen
14 Replies

4. What is on Your Mind?

How Will the World End?

How will the world end (someday long into the future, we hope)? (68 Replies)
Discussion started by: Neo
68 Replies

5. UNIX for Dummies Questions & Answers

What in the world does $$ mean?

If I type "echo $$" in to the command line it outputs some random number but it outputs the same one every time. What is this? (7 Replies)
Discussion started by: weexpectedthis
7 Replies

6. What is on Your Mind?

What The World Needs Now...

What does the world need now.... ??? Feel free to suggest new items to the poll .... we might add them :) (25 Replies)
Discussion started by: Neo
25 Replies

7. Solaris

hello world

just wanted to give salutations to all in here. i hope to contribute as much as i take. happy "unix-ing" :b: (0 Replies)
Discussion started by: JeepResQ
0 Replies
Login or Register to Ask a Question