Welcome Guest.   Make a donation to an author on the site August 30, 2014, 01:23:12 PM  *

Please login or register.
Or did you miss your validation email?


Login with username and password (forgot your password?)
Why not become a lifetime supporting member of the site with a one-time donation of any amount? Your donation entitles you to a ton of additional benefits, including access to exclusive discounts and downloads, the ability to enter monthly free software drawings, and a single non-expiring license key for all of our programs.


You must sign up here before you can post and access some areas of the site. Registration is totally free and confidential.
 
The N.A.N.Y. Challenge 2011! Download 30+ custom programs!
   
   Forum Home   Thread Marks Chat! Downloads Search Login Register  
Pages: [1] 2 Next   Go Down
  Reply  |  New Topic  |  Print  
Author Topic: Tiny 'Touch' 32 bits program in C (3 kb)  (Read 10373 times)
Gerome
Charter Honorary Member
***
Posts: 154


View Profile WWW Give some DonationCredits to this forum member
« 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) smiley

Formatted for C with the GeSHI Syntax Highlighter [copy or print]
  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. }

* Touch.zip (2.13 KB - downloaded 218 times.)
« Last Edit: October 01, 2006, 11:28:49 AM by Gerome » Logged

Yours,
(¯`·._.FBSL Help file]
(¯`·._.
f0dder
Charter Honorary Member
***
Posts: 8,774



[Well, THAT escalated quickly!]

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #1 on: September 29, 2006, 04:50:38 PM »

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

- carpe noctem
Gerome
Charter Honorary Member
***
Posts: 154


View Profile WWW Give some DonationCredits to this forum member
« 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 smiley
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!
Logged

Yours,
(¯`·._.FBSL Help file]
(¯`·._.
f0dder
Charter Honorary Member
***
Posts: 8,774



[Well, THAT escalated quickly!]

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #3 on: September 29, 2006, 07:50:12 PM »

Now you got the sources, you can do it smiley
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).
Logged

- carpe noctem
Jibz
Developer
***
Posts: 918



Cold Warrior

View Profile WWW Give some DonationCredits to this forum member
« 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 » Logged

"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
***
Posts: 154


View Profile WWW Give some DonationCredits to this forum member
« 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, ... )
Logged

Yours,
(¯`·._.FBSL Help file]
(¯`·._.
Jibz
Developer
***
Posts: 918



Cold Warrior

View Profile WWW Give some DonationCredits to this forum member
« 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.
Logged

"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
***
Posts: 154


View Profile WWW Give some DonationCredits to this forum member
« 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 ?
Logged

Yours,
(¯`·._.FBSL Help file]
(¯`·._.
Jibz
Developer
***
Posts: 918



Cold Warrior

View Profile WWW Give some DonationCredits to this forum member
« Reply #8 on: October 01, 2006, 06:40:12 AM »

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

"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
***
Posts: 154


View Profile WWW Give some DonationCredits to this forum member
« 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 smiley
Logged

Yours,
(¯`·._.FBSL Help file]
(¯`·._.
Jibz
Developer
***
Posts: 918



Cold Warrior

View Profile WWW Give some DonationCredits to this forum member
« Reply #10 on: October 01, 2006, 07:29:58 AM »

And you don't like following standards? cheesy
Logged

"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
***
Posts: 154


View Profile WWW Give some DonationCredits to this forum member
« Reply #11 on: October 01, 2006, 07:35:05 AM »

And you don't like following standards? cheesy

You definately NOT have read my previous posts, you're incredibly blind!
Logged

Yours,
(¯`·._.FBSL Help file]
(¯`·._.
Jibz
Developer
***
Posts: 918



Cold Warrior

View Profile WWW Give some DonationCredits to this forum member
« 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.
Logged

"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
***
Posts: 154


View Profile WWW Give some DonationCredits to this forum member
« 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.
Logged

Yours,
(¯`·._.FBSL Help file]
(¯`·._.
mouser
First Author
Administrator
*****
Posts: 33,287



see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #14 on: October 01, 2006, 07:52:22 AM »

gerome please try to be more reasonable!  Cool

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 smiley
Logged
Gerome
Charter Honorary Member
***
Posts: 154


View Profile WWW Give some DonationCredits to this forum member
« Reply #15 on: October 01, 2006, 07:56:25 AM »

Hello mouser
gerome please try to be more reasonable!  Cool

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 smiley

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.
Logged

Yours,
(¯`·._.FBSL Help file]
(¯`·._.
f0dder
Charter Honorary Member
***
Posts: 8,774



[Well, THAT escalated quickly!]

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« Reply #16 on: October 01, 2006, 09:10:24 AM »

Yay for people who don't care about standards. Professional? *cough*
« Last Edit: October 01, 2006, 09:18:59 AM by f0dder » Logged

- carpe noctem
Jibz
Developer
***
Posts: 918



Cold Warrior

View Profile WWW Give some DonationCredits to this forum member
« 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.
Logged

"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
Developer
***
Posts: 918



Cold Warrior

View Profile WWW Give some DonationCredits to this forum member
« 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 » Logged

"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
***
Posts: 154


View Profile WWW Give some DonationCredits to this forum member
« 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 smiley
So this is a fix :
if (lngHandle && lngHandle != INVALID_HANDLE_VALUE)
Logged

Yours,
(¯`·._.FBSL Help file]
(¯`·._.
hwtan
Charter Member
***
Posts: 72



see users location on a map View Profile Give some DonationCredits to this forum member
« 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.

Formatted for ASM with the GeSHI Syntax Highlighter [copy or print]
  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

* touch.exe (1.5 KB - downloaded 194 times.)
Logged
Gerome
Charter Honorary Member
***
Posts: 154


View Profile WWW Give some DonationCredits to this forum member
« Reply #21 on: October 01, 2006, 01:39:53 PM »

Hello,

Nicer smiley
BTW, you can also compile it up to 1024 bytes using PellesC, and still in plain C smiley
Logged

Yours,
(¯`·._.FBSL Help file]
(¯`·._.
hwtan
Charter Member
***
Posts: 72



see users location on a map View Profile Give some DonationCredits to this forum member
« Reply #22 on: October 01, 2006, 02:17:27 PM »

Interesting... AFAIK, win32 PE header is already 1KB in side...

Logged
Gerome
Charter Honorary Member
***
Posts: 154


View Profile WWW Give some DonationCredits to this forum member
« 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 smiley
Below it's impossible, but 512 bytes for MSVC++ 6 is amazing smiley
Logged

Yours,
(¯`·._.FBSL Help file]
(¯`·._.
f0dder
Charter Honorary Member
***
Posts: 8,774



[Well, THAT escalated quickly!]

see users location on a map View Profile WWW Read user's biography. Give some DonationCredits to this forum member
« 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 smiley

* touch.zip (1.4 KB - downloaded 191 times.)
Logged

- carpe noctem
Pages: [1] 2 Next   Go Up
  Reply  |  New Topic  |  Print  
 
Jump to:  
   Forum Home   Thread Marks Chat! Downloads Search Login Register  

DonationCoder.com | About Us
DonationCoder.com Forum | Powered by SMF
[ Page time: 0.082s | Server load: 0.16 ]