LCC-win32: compile unidelay

MPG UniCenter

MPG UniCenter

lcc-win32 C compiler.

LCC how to compile

On the previous page I covered how to create a project folder in lcc-win32 this page looks at compiling with lcc-win32. If you followed the introduction we have a project folder named uniform_server_delay and an automatically generated source file for program unidelay. Before looking at the process in detail its worth exploring the need for this delay utility.

Background

When executing a batch file there sometimes is a requirement to pause execution before moving onto the next instruction. Unfortunately there is no inbuilt instruction to do this.

There are novel workarounds the two most common are:

Ping

PING 1.1.1.1 -n 1 -w 60000 >NUL

CHOICE

REM | CHOICE /C:AB /T:A,%1 > NUL

I favour ping because it is cross OS compatible however it fails if IP address is active.

"Choice" looks good however not all OS’es support it, in particular XP (note: It has been re-introduced in Vista)

Workarounds like this are far from ideal, a solution is to download one of the many utilities available. Can you trust them not to deliver something unexpected? With that dilemma the answer is to create your own with the added advantage it can be tailored specifically to meet your requirements.

Top

Requirements - spec

My requirements for this delay utility are to provide a default delay of one second for use in batch file loop delays. The second requirement is to pass a parameter extending the delay for use in something like portable cron. The utility shall run hidden.

Top

Code

The code by its nature is simple I have added no error checking and assumed the maximum delay of 9 hours would be more than adequate.

#include <stdheaders.h>
#include <windows.h>

int main(int argc,char *argv[])
{
  int delay;                          // Delay required
  int default_delay=1000;             // 1000ms gives 1s delay

  if (argc == 2){                     // If arg provided
    int seconds = (int)atoi(argv[1]); // Convert to int. Seconds required
    delay=seconds * default_delay;    // Calculate total delay
  }

  else {                              // Arg not provided
    delay=default_delay;              // Use default
  }

  sleep(delay);                       // Delay for specified time
  return 0;
}

The default delay of one second in computer terms is long while being short in human terms to prevent annoyances.

Top

Edit source file

  1. Start lcc Start > All programs > lcc-win32 > lcc-win32
    If the project file was the last one you had opened page unidelay.c will display.
  2. If the page is not displayed:
    You previously had another project open close it: project > close
    Open project: project > open in the pop-up window select unidelay.prj click Open
    page unidelay.c will display.
  3. Delete all the code (it was automatically generated when creating a project) and copy the above code into the file.
  4. Save the page file > save

Top

Make - Compile - Run

At this stage we are ready to create our program.

  1. Click Complier (A) a dropdown menu is displayed.
  2. Click Make (B) this creates all the necessary files.
    Any errors are reported at the bottom of the page correct these and run Make again.
  3. With all errors corrected run the program, Click on Execute unidelay.exe (E)
    blank console window is displayed. After a delay of one second “Press any key “ is displayed.
    Note it also displays how long the script took to execute.
  4. The above proves the program works. Create the executable by clicking on Compile unidelay.c (D)
  5. Look in folder E:\lcc\projects\uniform_server_delay\lcc check the size of unidelay.exe its big 14.4K for such a small program. This is because it contains debug information we will correct this latter.

 

With a working program it’s worth putting a few errors in the source code and run “make” to explore the errors reported. Only introduce a single error at a time otherwise it becomes confusing. Make sure the program works before moving onto the next step.

Top

Debugging

You have created a program it compiles and runs however produces unpredicted results (bugs). Ideally what you need is a way to step through the program and check what values the variables contain during run time.

Debugger

The debugger with all the options you can select is relatively complex. However I find the following steps quickly resolves most problems.

  1. Start debugger: click on Compiler (A) > click on Debugger (C)
    After a short time the debugger window is displayed.
    Note: The top menu link Compiler is replace with Debug (F) the new drop down is shown on the right.
  2. The default break point is highlighted and a red bullet point displayed to the left. The program has stopped at this point.
  3. Move your mouse to the left of a line of code and click the left mouse button to position the cursor.
  4. Click Run to cursor (G) or press key F7 this run the program to that point.
    Note: The variables shown at the bottom of the page are updated.
  5. Repeat steps 3 and 4 for each location in your program that you wish to check.
  6. When you have finished debugging return to your source code by clicking Stop debugging (H)


Note: If you need to perform more complex debugging read the manual.

 

Top

Passing parameters to a program

During development you want to test a program with parameters if that’s part of its function. The delay function has the option to accept a single parameter, which increases the delay. The following shows how to set this and test.

Configure the debugger

  1. Top menu click Project from the drop down select Configuration. Window opens shown on right
  2. Select Debugger tab as shown.
  3. In the Command line arguments to pass to program (A) enter the values you wish to pass in this example enter 10 (gives a ten second delay)
  4. Click OK (B)
  5. Compile > Make
  6. Compile > Execute unidelay.exe
    A blank console window is displayed. After a delay of ten seconds “Press any key “ is displayed.
    Confirms program is working correctly.
    Note it also displays how long the script took to execute.


Note 1: More than one parameter can be entered separate them with a space for example:

10 Fred 20 another_parameter 45

Note 2: Parameters are ignore when you Compile a program hence no need to change or remove them.

 

Top

Optimise and hide

This is the final part of generating any program; let the compiler optimise it and remove any debug information. I partially covered this in the introduction and will use the same images.

Enter Project > Configuration and set the following:

Compiler

  1. Check Optimize box (A) – Reduces code size
  2. Check Elminate unused assignments (B) – Reduces code size
  3. Uncheck Generate debug info (C) – Debug code will not be included again reduces code size.

Linker

  1. Compiler settings are reflected in the Linker make sure the Do not include the debug information box (C) has been checked.
  2. Click radio button Windows appl (D)
  3. Click OK (F)

 

 

The trick that hides the application is at step 5 however at step 6 it whinges about an error “Windows program but no resources defined”. Ignore this and continue with the following steps:

  1. Compiler > Make or press F9
  2. Compiler > Compile unidelay.c
  3. Check the size of unidelay.exe (E:\lcc\projects\uniform_server_delay\lcc)
    It was 14.4K after optimisation 3.03K

Top

Final Test

The finished program is used within batch files hence would be a good idea to test in this environment.

  • Inside folder: E:\lcc\projects\uniform_server_delay\lcc
  • Create a test batch file test.bat add the following code and save:
@echo off
pause
unidelay.exe
echo Starting 10s delay
unidelay.exe 10
echo End of delay
pause

Run the batch file, after pressing any key a short pause (1s) before "Starting 10s delay" is displayed. After 10 seconds "End of delay" is displayed.

Top

Summary

This and the previous page have covered a lot of ground resulting in a real program you can use. The intention was to provide a quick start guide to get you up and running more advanced features of the programming environments can be found in the manual.

Having the ability to compile small utilities opens up new areas it allows you to explore other possibilities. If a program does not meet you requirements perhaps a small utility will provide a solution. When running Drupal on a Windows machine it requires a cron-proress to be run, you can simulate this however it is not portable. The next example fills a gap with a small utility I have dubbed portable cron.

Top


  Ric