Website Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • August 30, 2015, 03:01:58 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

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

Gerome

  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 154
    • View Profile
    • Get my Freestyle Basic Script Language + compiler!
    • Donate to Member
Tiny 'Touch' 32 bits program in C (3 kb)
« on: September 29, 2006, 02:58:35 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
  1. // ********************************
  2. // Author : Gerome GUILLEMIN
  3. // Coded in pure C using LCC Win32
  4. // Date : 29 th of September 2006
  5. // ********************************
  6. #include <windows.h>
  7. #include <stdio.h>
  8.  
  9. BOOL SetFileToCurrentTime(HANDLE);
  10.  
  11. int main(int argc, char **argv)
  12. {
  13.         HANDLE lngHandle = NULL;
  14.         char szFileName[MAX_PATH+1];
  15.         if ( argc == 2 ) {
  16.                 strcpy( szFileName, argv[1] );
  17.             lngHandle = CreateFile(szFileName, GENERIC_WRITE,
  18.                                         FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
  19.                 if (lngHandle && lngHandle != INVALID_HANDLE_VALUE) {
  20.                         SetFileToCurrentTime( lngHandle );
  21.                         CloseHandle( lngHandle );
  22.                         return 1; // => 1 will be the OK return
  23.                 }
  24.         }
  25. return 0; // => 0 will be the KO return
  26. }
  27.  
  28. BOOL SetFileToCurrentTime(HANDLE hFile)
  29. {
  30.   FILETIME ft;
  31.   SYSTEMTIME st;
  32.  
  33.   GetSystemTime(&st);                 // gets current time
  34.   SystemTimeToFileTime(&st, &ft);     // converts to file time format
  35.   return SetFileTime(hFile,           // sets last-write time for file
  36.          (LPFILETIME)NULL, (LPFILETIME)NULL, &ft);
  37. }
  38.  
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, 11:28:49 AM by Gerome »

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 8,858
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #1 on: September 29, 2006, 04:50:38 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
    • View Profile
    • Get my Freestyle Basic Script Language + compiler!
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #2 on: September 29, 2006, 06:57:07 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

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 8,858
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #3 on: September 29, 2006, 07:50:12 PM »
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

  • Developer
  • Joined in 2005
  • ***
  • Posts: 1,026
    • View Profile
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #4 on: September 30, 2006, 03:48:37 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.
« Last Edit: September 30, 2006, 04:58:14 AM by Jibz »

Gerome

  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 154
    • View Profile
    • Get my Freestyle Basic Script Language + compiler!
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #5 on: September 30, 2006, 10:04:39 AM »
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

  • Developer
  • Joined in 2005
  • ***
  • Posts: 1,026
    • View Profile
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #6 on: October 01, 2006, 04:17:29 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.

Gerome

  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 154
    • View Profile
    • Get my Freestyle Basic Script Language + compiler!
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #7 on: October 01, 2006, 06:00:53 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

  • Developer
  • Joined in 2005
  • ***
  • Posts: 1,026
    • View Profile
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #8 on: October 01, 2006, 06:40:12 AM »
I am just trying to help and explain, no reason to get angry :Thmbsup:.

Gerome

  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 154
    • View Profile
    • Get my Freestyle Basic Script Language + compiler!
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #9 on: October 01, 2006, 06:48:31 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

  • Developer
  • Joined in 2005
  • ***
  • Posts: 1,026
    • View Profile
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #10 on: October 01, 2006, 07:29:58 AM »
And you don't like following standards? :D

Gerome

  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 154
    • View Profile
    • Get my Freestyle Basic Script Language + compiler!
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #11 on: October 01, 2006, 07:35:05 AM »
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

  • Developer
  • Joined in 2005
  • ***
  • Posts: 1,026
    • View Profile
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #12 on: October 01, 2006, 07:41:19 AM »
Quote from: Gerome
I don't sometimes care of what can say the standards...

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

Gerome

  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 154
    • View Profile
    • Get my Freestyle Basic Script Language + compiler!
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #13 on: October 01, 2006, 07:44:53 AM »
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: 34,926
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #14 on: October 01, 2006, 07:52:22 AM »
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
    • View Profile
    • Get my Freestyle Basic Script Language + compiler!
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #15 on: October 01, 2006, 07:56:25 AM »
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

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 8,858
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #16 on: October 01, 2006, 09:10:24 AM »
Yay for people who don't care about standards. Professional? *cough*
- carpe noctem
« Last Edit: October 01, 2006, 09:18:59 AM by f0dder »

Jibz

  • Developer
  • Joined in 2005
  • ***
  • Posts: 1,026
    • View Profile
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #17 on: October 01, 2006, 09:21:14 AM »
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.

Jibz

  • Developer
  • Joined in 2005
  • ***
  • Posts: 1,026
    • View Profile
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #18 on: October 01, 2006, 10:45:21 AM »
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.
« Last Edit: October 01, 2006, 10:55:24 AM by Jibz »

Gerome

  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 154
    • View Profile
    • Get my Freestyle Basic Script Language + compiler!
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #19 on: October 01, 2006, 11:29:46 AM »

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: 72
    • View Profile
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #20 on: October 01, 2006, 01:36:12 PM »
If you want it smaller, here's the program in assembly. Executable file has size of 1536 bytes.

Code: ASM
  1.     .486                                ; create 32 bit code
  2.     .model flat, stdcall                ; 32 bit memory model
  3.     option casemap :none                ; case sensitive
  4.  
  5.     include \masm32\include\windows.inc
  6.     include \masm32\include\kernel32.inc
  7.     include \masm32\include\user32.inc
  8.     include \masm32\include\shell32.inc
  9.  
  10.     includelib \masm32\lib\kernel32.lib
  11.     includelib \masm32\lib\shell32.lib
  12.  
  13.     .code
  14. start:
  15.  
  16. main proc
  17.         local   commandLine:LPSTR
  18.         local   numOfArgs:DWORD
  19.         local   argsP:DWORD
  20.         local   fileH:HANDLE
  21.         local   currentSysTime:SYSTEMTIME
  22.         local   newFileTime:FILETIME
  23.    
  24.         invoke  GetCommandLineW
  25.         mov     commandLine, eax
  26.         invoke  CommandLineToArgvW, commandLine, addr numOfArgs
  27.         mov     argsP, eax
  28.         cmp     numOfArgs, 2
  29.         jl      failure
  30.         mov     eax, argsP
  31.         mov     ebx, [eax + 4]
  32.         invoke  CreateFileW, ebx, FILE_READ_ATTRIBUTES+FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ+FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL
  33.         mov     fileH, eax
  34.         cmp     eax, INVALID_HANDLE_VALUE
  35.         je      failure
  36.         invoke  GetSystemTime, addr currentSysTime
  37.         invoke  SystemTimeToFileTime, addr currentSysTime, addr newFileTime
  38.         invoke  SetFileTime, fileH, NULL, NULL, addr newFileTime
  39.         invoke  ExitProcess, 0
  40.        
  41. failure:        
  42.         invoke  ExitProcess, -1
  43. main    endp
  44.    
  45. end     start
  46.  
  47.  

Gerome

  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 154
    • View Profile
    • Get my Freestyle Basic Script Language + compiler!
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #21 on: October 01, 2006, 01:39:53 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: 72
    • View Profile
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #22 on: October 01, 2006, 02:17:27 PM »
Interesting... AFAIK, win32 PE header is already 1KB in side...


Gerome

  • Charter Honorary Member
  • Joined in 2006
  • ***
  • Posts: 154
    • View Profile
    • Get my Freestyle Basic Script Language + compiler!
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #23 on: October 01, 2006, 02:20:38 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

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 8,858
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Donate to Member
Re: Tiny 'Touch' 32 bits program in C (3 kb)
« Reply #24 on: October 01, 2006, 02:21:28 PM »
Just for fun, here's a 1kb C (well, C++) version, that has proper system return code :)
- carpe noctem