DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Last post Author Topic: Tiny 'Touch' 32 bits program in C (3 kb)  (Read 28,311 times)

Gerome

  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 154
Tiny 'Touch' 32 bits program in C (3 kb)
« on: September 29, 2006, 07:58 PM »
Hello,

For people who are aware of having an Unix like 'Touch' program, here are the sources + into the zipped attachement, a BAT to compile the source, the compiled source (a 3Kb one) :)

Code: (c)
// ********************************
// Author : Gerome GUILLEMIN
// Coded in pure C using LCC Win32
// Date : 29 th of September 2006
// ********************************
#include <windows.h>
#include <stdio.h>

BOOL SetFileToCurrentTime(HANDLE);

int main(int argc, char **argv)
{
	HANDLE lngHandle = NULL;
	char szFileName[MAX_PATH+1];
	if ( argc == 2 ) {
		strcpy( szFileName, argv[1] );
	    lngHandle = CreateFile(szFileName, GENERIC_WRITE,
					FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
		if (lngHandle && lngHandle != INVALID_HANDLE_VALUE) {
			SetFileToCurrentTime( lngHandle );
			CloseHandle( lngHandle );
			return 1; // => 1 will be the OK return
		}
	}
return 0; // => 0 will be the KO return
}

BOOL SetFileToCurrentTime(HANDLE hFile)
{
  FILETIME ft;
  SYSTEMTIME st;

  GetSystemTime(&st);                 // gets current time
  SystemTimeToFileTime(&st, &ft);     // converts to file time format
  return SetFileTime(hFile,           // sets last-write time for file
         (LPFILETIME)NULL, (LPFILETIME)NULL, &ft);
}
Yours,
(¯`·._.·[Gerome GUILLEMIN]·._.·´¯)
http://www.fbsl.net [FBSL Author]
http://gedd123.free.fr/FBSLv3.zip [FBSL Help file]
(¯`·._.·[If you need help... just ask]·._.·´¯)
« Last Edit: October 01, 2006, 04:28 PM by Gerome »

f0dder

  • [Well, THAT escalated quickly!]
  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
    • Participated in NANY Challenge Once
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #1 on: September 29, 2006, 09:50 PM »
You really should return '0' since that's the standard returncode for success...
- carpe noctem

Gerome

  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 154
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #2 on: September 29, 2006, 11:57 PM »
Hello,

You really should return '0' since that's the standard returncode for success...

Now you got the sources, you can do it :)
Personally I prefer telling 1 that is equal to True when successfull and 0 that is equal to 'False' when failed... It's just another point of view!
Yours,
(¯`·._.·[Gerome GUILLEMIN]·._.·´¯)
http://www.fbsl.net [FBSL Author]
http://gedd123.free.fr/FBSLv3.zip [FBSL Help file]
(¯`·._.·[If you need help... just ask]·._.·´¯)

f0dder

  • [Well, THAT escalated quickly!]
  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
    • Participated in NANY Challenge Once
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #3 on: September 30, 2006, 12:50 AM »
Now you got the sources, you can do it :)
Personally I prefer telling 1 that is equal to True when successfull and 0 that is equal to 'False' when failed... It's just another point of view!
Not if you ever have to deal with makefiles or the like.
(emphasis is mine).
- carpe noctem

Jibz

  • Cold Warrior
  • Developer
  • Joined in 2005
  • ***
  • Posts: 1,187
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Participated in NANY Challenge Four Times
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #4 on: September 30, 2006, 08:48 AM »
The idea in returning 0 on success and a non-zero value in case of failure is of course that if the program succeeds we generally aren't interested in why, whereas with a failure it's nice to know why.

So a program can return 1 for running out of memory, 2 for pointer error, 3 for .. etc. That way you can check if there is an error by looking at if the value is zero or non-zero, and if you need to know more about what caused the error you can look at the actual value returned :Thmbsup:.

The C standard defines two macros, EXIT_SUCCESS and EXIT_FAILURE, which can be used as appropriate return values to the operating system. They are respectively 0 and 1.
"A problem, properly stated, is a problem on it's way to being solved" -Buckminster Fuller
"Multithreading is just one damn thing after, before, or simultaneous with another" -Andrei Alexandrescu
« Last Edit: September 30, 2006, 09:58 AM by Jibz »

Gerome

  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 154
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #5 on: September 30, 2006, 03:04 PM »
Hi,

...
The C standard defines two macros, EXIT_SUCCESS and EXIT_FAILURE, which can be used as appropriate return values to the operating system. They are respectively 0 and 1.

I don't sometimes care of what can say the standards...
Why ?
It's simply psychologic and it has a real explanation :
The common 'human' sense would recognize that a 'TRUE' value seems positive, and 'FALSE' the opposite.
So what ?
I've told to my program to execute this_task and it replied to me that there were NO errors singnaled, so the return is TRUE, else in case of failure it would return 0...
And this common human readable/understandable sense can be also found onto 80% of the APIs here and there, where 0 indicates a failure or a NULL return ( example : Findwindow, SleepEx, ... )
Yours,
(¯`·._.·[Gerome GUILLEMIN]·._.·´¯)
http://www.fbsl.net [FBSL Author]
http://gedd123.free.fr/FBSLv3.zip [FBSL Help file]
(¯`·._.·[If you need help... just ask]·._.·´¯)

Jibz

  • Cold Warrior
  • Developer
  • Joined in 2005
  • ***
  • Posts: 1,187
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Participated in NANY Challenge Four Times
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #6 on: October 01, 2006, 09:17 AM »
The problem with not following the generally accepted standards and conventions is that your program will behave unexpectedly.

If somebody uses your program in a script or makefile they will automatically assume that the return value will be zero on success, so they will be scratching their heads for a while wondering why your program keeps failing until they look at the source and see it.
"A problem, properly stated, is a problem on it's way to being solved" -Buckminster Fuller
"Multithreading is just one damn thing after, before, or simultaneous with another" -Andrei Alexandrescu

Gerome

  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 154
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #7 on: October 01, 2006, 11:00 AM »
Hello,

The problem with not following the generally accepted standards and conventions is that your program will behave unexpectedly.

At first, it seems you did NOT read explanations i gave into my previous post.

Quote
If somebody uses your program in a script or makefile they will automatically assume that the return value will be zero on success, so they will be scratching their heads for a while wondering why your program keeps failing until they look at the source and see it.

Secondly I gave souces also, and if you don't agree with that, feel free to make your own executable.
At last, there is no way my code lead into crash or alike, the only two return code are 1 in case of success and 0 in case of failure (generally encountered into API calls)
So WTF ?
Yours,
(¯`·._.·[Gerome GUILLEMIN]·._.·´¯)
http://www.fbsl.net [FBSL Author]
http://gedd123.free.fr/FBSLv3.zip [FBSL Help file]
(¯`·._.·[If you need help... just ask]·._.·´¯)

Jibz

  • Cold Warrior
  • Developer
  • Joined in 2005
  • ***
  • Posts: 1,187
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Participated in NANY Challenge Four Times
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #8 on: October 01, 2006, 11:40 AM »
I am just trying to help and explain, no reason to get angry :Thmbsup:.
"A problem, properly stated, is a problem on it's way to being solved" -Buckminster Fuller
"Multithreading is just one damn thing after, before, or simultaneous with another" -Andrei Alexandrescu

Gerome

  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 154
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #9 on: October 01, 2006, 11:48 AM »
Hello,

I am just trying to help and explain, no reason to get angry :Thmbsup:.

Please i don't need help onto that part.
FYI, i'm a professional system C/C++, ASM, .NET, PLSQL developper, and i love APIs :)
Yours,
(¯`·._.·[Gerome GUILLEMIN]·._.·´¯)
http://www.fbsl.net [FBSL Author]
http://gedd123.free.fr/FBSLv3.zip [FBSL Help file]
(¯`·._.·[If you need help... just ask]·._.·´¯)

Jibz

  • Cold Warrior
  • Developer
  • Joined in 2005
  • ***
  • Posts: 1,187
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Participated in NANY Challenge Four Times
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #10 on: October 01, 2006, 12:29 PM »
And you don't like following standards? :D
"A problem, properly stated, is a problem on it's way to being solved" -Buckminster Fuller
"Multithreading is just one damn thing after, before, or simultaneous with another" -Andrei Alexandrescu

Gerome

  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 154
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #11 on: October 01, 2006, 12:35 PM »
And you don't like following standards? :D

You definately NOT have read my previous posts, you're incredibly blind!
Yours,
(¯`·._.·[Gerome GUILLEMIN]·._.·´¯)
http://www.fbsl.net [FBSL Author]
http://gedd123.free.fr/FBSLv3.zip [FBSL Help file]
(¯`·._.·[If you need help... just ask]·._.·´¯)

Jibz

  • Cold Warrior
  • Developer
  • Joined in 2005
  • ***
  • Posts: 1,187
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Participated in NANY Challenge Four Times
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #12 on: October 01, 2006, 12:41 PM »
Quote from: "Gerome"
I don't sometimes care of what can say the standards...

If I misunderstood that somehow, please enlighten me :huh:.
"A problem, properly stated, is a problem on it's way to being solved" -Buckminster Fuller
"Multithreading is just one damn thing after, before, or simultaneous with another" -Andrei Alexandrescu

Gerome

  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 154
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #13 on: October 01, 2006, 12:44 PM »
Quote from: "Gerome"
I don't sometimes care of what can say the standards...

If I misunderstood that somehow, please enlighten me :huh:.

Just open you eyes and your mind and all will be then clearer.
Yours,
(¯`·._.·[Gerome GUILLEMIN]·._.·´¯)
http://www.fbsl.net [FBSL Author]
http://gedd123.free.fr/FBSLv3.zip [FBSL Help file]
(¯`·._.·[If you need help... just ask]·._.·´¯)

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,931
    • Gathering Badge
    • Plugin Coding Medal
    • Participated in NANY Challenge Seventeen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #14 on: October 01, 2006, 12:52 PM »
gerome please try to be more reasonable!  8)

actually Jibz' explanation was a great one I thought (regarding why anything but 0 is used to represent different error codes).

but regardless, this is just one of those cases where it's silly to go against the standard.  return codes can actually be used by other programs and i don't see a good reason not to follow convention.  just follow in the good hacker tradition and write a curse-filled paragraph in the readme about how you disagree with the standard, and then implement the standard :)

Gerome

  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 154
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #15 on: October 01, 2006, 12:56 PM »
Hello mouser
gerome please try to be more reasonable!  8)

actually Jibz' explanation was a great one I thought (regarding why anything but 0 is used to represent different error codes).

but regardless, this is just one of those cases where it's silly to go against the standard.  return codes can actually be used by other programs and i don't see a good reason not to follow convention.  just follow in the good hacker tradition and write a curse-filled paragraph in the readme about how you disagree with the standard, and then implement the standard :)

WTF 'standard' ?
And when APIs you're using just return 0 as ERROR, how do you consider standard as standard ?
There are several programming schools, several point of view, and mine is optimistic + API sided, where 0 means ERROR and 1 or TRUE means OK, that's all folks.
Yours,
(¯`·._.·[Gerome GUILLEMIN]·._.·´¯)
http://www.fbsl.net [FBSL Author]
http://gedd123.free.fr/FBSLv3.zip [FBSL Help file]
(¯`·._.·[If you need help... just ask]·._.·´¯)

f0dder

  • [Well, THAT escalated quickly!]
  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
    • Participated in NANY Challenge Once
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #16 on: October 01, 2006, 02:10 PM »
Yay for people who don't care about standards. Professional? *cough*
- carpe noctem
« Last Edit: October 01, 2006, 02:18 PM by f0dder »

Jibz

  • Cold Warrior
  • Developer
  • Joined in 2005
  • ***
  • Posts: 1,187
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Participated in NANY Challenge Four Times
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #17 on: October 01, 2006, 02:21 PM »
Quote from: "Gerome"
WTF 'standard' ?

How about the ANSI/ISO C standard:

Quote
If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If the value of status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned.

Stroustrup (my K&R is in danish and I didn't want to post a translation .. it says basically the same thing though):

Quote
The int returned by main(), if any, is the program's return value to "the system." If no value is returned, the system will receive a value indicating successful completion. A nonzero value from main() indicates failure.

MSDN:

Quote
By convention, a return code of zero means that the program completed successfully.

For most programs it doesn't matter much what value you return, since nobody is ever going to inspect it anyway. But a program like touch is likely to be used in scripts and makefiles, and that's why I feel it's better to have it return the most standard values.
"A problem, properly stated, is a problem on it's way to being solved" -Buckminster Fuller
"Multithreading is just one damn thing after, before, or simultaneous with another" -Andrei Alexandrescu

Jibz

  • Cold Warrior
  • Developer
  • Joined in 2005
  • ***
  • Posts: 1,187
    • Programming School - Stage 1 Medal
    • Coding Snacks
    • Participated in NANY Challenge Four Times
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #18 on: October 01, 2006, 03:45 PM »
Btw, if I'm not mistaken, CreateFile returns INVALID_HANDLE_VALUE on failure, which is -1 .. so your program returns your success value of 1 even if the file does not exist.
"A problem, properly stated, is a problem on it's way to being solved" -Buckminster Fuller
"Multithreading is just one damn thing after, before, or simultaneous with another" -Andrei Alexandrescu
« Last Edit: October 01, 2006, 03:55 PM by Jibz »

Gerome

  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 154
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #19 on: October 01, 2006, 04:29 PM »

Btw, if I'm not mistaken, CreateFile returns INVALID_HANDLE_VALUE on failure, which is -1 .. so your program returns your success value of 1 even if the file does not exist.

Right :)
So this is a fix :
if (lngHandle && lngHandle != INVALID_HANDLE_VALUE)
Yours,
(¯`·._.·[Gerome GUILLEMIN]·._.·´¯)
http://www.fbsl.net [FBSL Author]
http://gedd123.free.fr/FBSLv3.zip [FBSL Help file]
(¯`·._.·[If you need help... just ask]·._.·´¯)

hwtan

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 73
    • Programming School - Stage 1 Medal
    • Programming School - Deluxe Bonus Medal
    • Programming School - Dabbler Medal
    • Coding Snacks
    • Linux User
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #20 on: October 01, 2006, 06:36 PM »
If you want it smaller, here's the program in assembly. Executable file has size of 1536 bytes.

Code: (asm)
    .486                                ; create 32 bit code
    .model flat, stdcall                ; 32 bit memory model
    option casemap :none                ; case sensitive
  
    include \masm32\include\windows.inc
    include \masm32\include\kernel32.inc
    include \masm32\include\user32.inc
    include \masm32\include\shell32.inc 

    includelib \masm32\lib\kernel32.lib
    includelib \masm32\lib\shell32.lib

    .code
start:

main proc
        local   commandLine:LPSTR
        local   numOfArgs:DWORD
        local   argsP:DWORD
        local   fileH:HANDLE
        local   currentSysTime:SYSTEMTIME
        local   newFileTime:FILETIME
    
        invoke  GetCommandLineW
        mov     commandLine, eax
        invoke  CommandLineToArgvW, commandLine, addr numOfArgs
        mov     argsP, eax
        cmp     numOfArgs, 2
        jl      failure
        mov     eax, argsP
        mov     ebx, [eax + 4]
        invoke  CreateFileW, ebx, FILE_READ_ATTRIBUTES+FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ+FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL
        mov     fileH, eax
        cmp     eax, INVALID_HANDLE_VALUE
        je      failure
        invoke  GetSystemTime, addr currentSysTime
        invoke  SystemTimeToFileTime, addr currentSysTime, addr newFileTime
        invoke  SetFileTime, fileH, NULL, NULL, addr newFileTime
        invoke  ExitProcess, 0
        
failure:         
        invoke  ExitProcess, -1 
main    endp
    
end     start

Gerome

  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 154
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #21 on: October 01, 2006, 06:39 PM »
Hello,

Nicer :)
BTW, you can also compile it up to 1024 bytes using PellesC, and still in plain C :)
Yours,
(¯`·._.·[Gerome GUILLEMIN]·._.·´¯)
http://www.fbsl.net [FBSL Author]
http://gedd123.free.fr/FBSLv3.zip [FBSL Help file]
(¯`·._.·[If you need help... just ask]·._.·´¯)

hwtan

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 73
    • Programming School - Stage 1 Medal
    • Programming School - Deluxe Bonus Medal
    • Programming School - Dabbler Medal
    • Coding Snacks
    • Linux User
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #22 on: October 01, 2006, 07:17 PM »
Interesting... AFAIK, win32 PE header is already 1KB in side...

Gerome

  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 154
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #23 on: October 01, 2006, 07:20 PM »
Hi,
Interesting... AFAIK, win32 PE header is already 1KB in side...


Yes, but there are also PE compilation options using MSVC++6.0 that allows you to make 512 bytes length 32 bits executables :)
Below it's impossible, but 512 bytes for MSVC++ 6 is amazing :)
Yours,
(¯`·._.·[Gerome GUILLEMIN]·._.·´¯)
http://www.fbsl.net [FBSL Author]
http://gedd123.free.fr/FBSLv3.zip [FBSL Help file]
(¯`·._.·[If you need help... just ask]·._.·´¯)

f0dder

  • [Well, THAT escalated quickly!]
  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
    • Participated in NANY Challenge Once
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #24 on: October 01, 2006, 07:21 PM »
Just for fun, here's a 1kb C (well, C++) version, that has proper system return code :)
- carpe noctem

Original address: https://www.donationcoder.com/forum/index.php?topic=5537.0