Porting to Linux

Porting to Linux

Post by Donovan Rebbec » Wed, 20 Sep 2000 04:00:00




Quote:>I am having trouble porting the following code to Linux.

What kind of trouble are you having ? Could you post the error
messages you're getting ?

--
Donovan

 
 
 

Porting to Linux

Post by Kaz Kylhe » Wed, 20 Sep 2000 04:00:00



Quote:>I am having trouble porting the following code to Linux.

>All help greatly appreciated.

>#include <stdio.h>
>#include <stdlib.h>
>//#include <conio.h>

// comments are a C99 feature. Most compiler still speak C89.

Quote:>/* Features to add            */
>/* 1. Add password protection */
>/* 2. Have multiple data files*/
>/* 3. Save/Delete data files. */
>/* 4. Create usage log of time of use*/

>struct Vehicle
> {
> unsigned int Vehicle_Number;
> unsigned int Vehicle_Purchase_Cost;
> unsigned int Vehicle_Sale_Price;
> };

>/* BEGIN FUNCTIONS DECLARATIONS*/
>void Vehicle_With_Highest_Sale_Price();
>void Average_Cost_Of_Vehicles_In_Stock();
>void Gross_Profit_If_All_Cars_Are_Sold();

You should use prototype declarations, e.g.:

        void Vehicle_With_Highest_Sale_Price(void);

Not specifying the parameter types takes you back to a style of C programming
that has been obsolete for well over a decade.

(Unless this program is intended to be C++: in C++ the lack of a parameter
list prototypes a function with no parameters; the explicit void keyword
is not needed).

Quote:>unsigned int ReadDataFromFile(); /* Function will return current index
>number*/
>void SaveDataToFile2();
>/* END FUNCTIONS DECLARATIONS*/

>#define MAX_ARRAY_SIZE 10
>//const int MAX_ARRAY_SIZE=10;
>int MAX_HAS_BEEN_REACHED=0;
>Vehicle VehicleArray[MAX_ARRAY_SIZE];

 ^^^^^^^

You have not declared the typedef name Vehicle. Your earlier ``struct Vehicle''
declartion only introduces the struct tag name ``Vehicle'' which is in a
separate namespace that can only be accessed using the struct keyword, e.g.

        struct Vehicle VehicleArray[MAX_ARRAY_SIZE];

Again, I'm suspecting that your program may be intended to be C++, since in
C++, a struct declaration introduces a type name into the ordinary name space.

If you want the GNU compiler to translate your program as C++, you must save
your program with a .cc or .cpp suffix.

Quote:> printf("\n\nDo You Wish To Enter Another Car?(Y/N)");
> fflush(stdin);

Calling fflush() on an input stream, or on an update stream whose last
operation was input, invokes undefined behavior.  Undefined behavior
is a serious programming error, because anything may happen: the program
may stop with an error message, crash or behave unpredictably.

The purpose of fflush() is to cause output data that is buffered in the stream
to be forwarded to the environment. It has absolutely nothing to do with input.

Quote:> scanf("%c",&Play_Again);
> }while ((Play_Again == 'y') || (Play_Again == 'Y'));

Never use scanf() for interactive input. Use fgets() to read a whole line,
and process that line. For obtaining a yes or no answer, you can use getchar()
to read successive characters in a loop until you encounter EOF or
newline ('\n').  In this loop, simply record whether the first non-blank
character encountered was 'Y' or 'y'.

- Show quoted text -

Quote:

>void Vehicle_With_Highest_Sale_Price()
>{
> for(n=1; n<=Index; n++)
> {
>  if (VehicleArray[n].Vehicle_Sale_Price > HighestPrice)
>  {
>   HighestPrice = VehicleArray[n].Vehicle_Sale_Price;
>   HighestCarNumber =  n;
>  }
> }
>};

>void Average_Cost_Of_Vehicles_In_Stock()
>{
> unsigned int Cost=0;
> for(n=0; n<=Index; n++)
>  Cost += VehicleArray[n].Vehicle_Purchase_Cost;
> //if(Index==0)Index++;/* Stop a divide by zero error*/
> AverageCarCost = Cost / (Index? Index: 1);
>};

>void Gross_Profit_If_All_Cars_Are_Sold()
>{
> unsigned int TotalOfVehiclePurchases=0;
> unsigned int TotalOfVehicleSales=0;

> for(n=1; n<=Index; n++)
>  TotalOfVehiclePurchases += VehicleArray[n].Vehicle_Purchase_Cost;

> for(n=1; n<=Index; n++)
>  TotalOfVehicleSales += VehicleArray[n].Vehicle_Sale_Price;

> printf("\n Sales: %u Purchases: %u", TotalOfVehicleSales,
>TotalOfVehiclePurchases);
> GrossProfit = TotalOfVehicleSales - TotalOfVehiclePurchases;
>};

>unsigned int ReadDataFromFile()
>{
> FILE *CarDataFile;

> printf("Reading Car Data File from disk\nPatience would be
>appreciated...");

> if((CarDataFile = fopen("CAR_DATA.DAT","r"))==NULL)
> {
>  printf("\nERROR: File probably does not exist in directory.");

>  /* Attempt to create the data file*/
>  if((CarDataFile = fopen("CAR_DATA.DAT","a"))==NULL)
>  {
>   printf("\nWe cannot create file.\n Program will now end.");
>   exit(EXIT_FAILURE);

Why does a reading function need to create the data file? This operation
can be deferred until it's time to actually save data.

Quote:>  Index=1; /* Sets array up from zero for compatibility with the rest of
>program*/

How does starting at 1 set up the array from zero? This comment is lying.

Moreover, in some parts of your program you loop from 1, and in others
you loop from 0.

Arrays start from index 0 in C. Don't bother trying to pretend otherwise,
unless you know what you are doing and have some excellent reason for doing so.
(Continuing to program Pascal in C is not good reason).

Quote:>  while (!feof(CarDataFile))

This style of writing an stream processing loop is incorrect. It is known as
Pascal Programmer Syndrome.  The problem is that it's senseless to test for end
of input before actually trying to read input.

Quote:>  {
>   fscanf(CarDataFile, "%u %u %u \n",
>    &VehicleArray[Index].Vehicle_Number,

Suppose the end of file occurs in the middle of the above fscanf().  Because
you don't check the return value of fscanf(), the rest of the loop body will
execute, and will erroneously add a nonexistent item to the array, reusing data
values from the previous iteration.

Read the comp.lang.c FAQ at http://www.eskimo.com/~scs/C-faq/top.html.

One of the questions answered goes something like this ``why does my program
process the last line of the input file twice?''.

The correct loop looks like this:

        for (Numitems = 0; Numitems < MAX_ARRAY_SIZE; Numitems++) {
                if (fscanf(..., "%u%u%u") != 3)       /* did not scan three items */
                        break;

                /* okay, got three unsigned integers, process them! */

                /* ... add to array ... */
        }
        /* Numitems now represents number of items read */

Quote:>    &VehicleArray[Index].Vehicle_Purchase_Cost,
>    &VehicleArray[Index].Vehicle_Sale_Price);
>   Index++;
>   if(Index==MAX_ARRAY_SIZE)
>   {
>    printf("\nERROR: Maximum number of values have been read from file.\n");

Note that if Index is equal to MAX_ARRAY_SIZE, it is incorrect to then
execute loops like

        for (i = 1; i <= Index; i++) { /* process array element */ }

Because on the last iteration of the loop, you will access the array
out of bounds.

--
Any hyperlinks appearing in this article were inserted by the unscrupulous
operators of a Usenet-to-web gateway, without obtaining the proper permission
of the author, who does not endorse any of the linked-to products or services.

 
 
 

Porting to Linux

Post by Nate Eldredg » Wed, 20 Sep 2000 04:00:00



Quote:> I am having trouble porting the following code to Linux.

What sort of trouble?

--

Nate Eldredge

 
 
 

Porting to Linux

Post by Please enter your nam » Thu, 21 Sep 2000 00:22:35


I am having trouble porting the following code to Linux.

All help greatly appreciated.

#include <stdio.h>
#include <stdlib.h>
//#include <conio.h>

/* Features to add            */
/* 1. Add password protection */
/* 2. Have multiple data files*/
/* 3. Save/Delete data files. */
/* 4. Create usage log of time of use*/

struct Vehicle
 {
 unsigned int Vehicle_Number;
 unsigned int Vehicle_Purchase_Cost;
 unsigned int Vehicle_Sale_Price;
 };

/* BEGIN FUNCTIONS DECLARATIONS*/
void Vehicle_With_Highest_Sale_Price();
void Average_Cost_Of_Vehicles_In_Stock();
void Gross_Profit_If_All_Cars_Are_Sold();

unsigned int ReadDataFromFile(); /* Function will return current index
number*/
void SaveDataToFile2();
/* END FUNCTIONS DECLARATIONS*/

#define MAX_ARRAY_SIZE 10
//const int MAX_ARRAY_SIZE=10;
int MAX_HAS_BEEN_REACHED=0;
Vehicle VehicleArray[MAX_ARRAY_SIZE];

/* BEGIN VARIABLE DECLARATIONS*/
int NEW_DATA_FILE=0;
unsigned int Index=0;
unsigned int n;
char Play_Again;

unsigned int AverageCarCost;     /* Holds average cost of all cars
*/
unsigned int GrossProfit;        /* Holds gross profit of all cars
*/
unsigned int HighestCarNumber;   /* Holds car number with highest sale price
*/
unsigned int HighestPrice = 0;   /* Holds highest price of all cars
*/
/* END VARIABLE DECLARATIONS*/

int main(void)
{
 Index = ReadDataFromFile();
 printf("Index returned from function = %d\n",Index);
 if((Index==0)&&(NEW_DATA_FILE==1))Index=1;/* File just created, so set
Index to 1*/

 /* BEGIN MAIN LOOP*/
 do
 {

  printf("Car Number: %u\n", Index);

  if(MAX_HAS_BEEN_REACHED != 1)
  {
   do
   {
    fflush(stdout);
    printf("\nCar Purchase Price: ");
    scanf("%u", &VehicleArray[Index].Vehicle_Purchase_Cost);

    printf("\nCar Sale Price: ");
    scanf("%u", &VehicleArray[Index].Vehicle_Sale_Price);

    if (VehicleArray[Index].Vehicle_Purchase_Cost >
VehicleArray[Index].Vehicle_Sale_Price)
    {
     printf("\nThe selling cost of $%u must be greater than the purchase
cost of $%u",
      VehicleArray[Index].Vehicle_Sale_Price,
      VehicleArray[Index].Vehicle_Purchase_Cost);
    }
   }while (VehicleArray[Index].Vehicle_Purchase_Cost >
VehicleArray[Index].Vehicle_Sale_Price);

   printf("Car Number \t Car Cost \t Car Sell\n");
   printf("------------------------------------------------\n");
  }/* END MAX REACHED IF*/

 for(n=1; n<=Index; n++)
 {
  printf("%u \t\t $%u \t\t $%u \n",
  n, VehicleArray[n].Vehicle_Purchase_Cost,
VehicleArray[n].Vehicle_Sale_Price);
 };

 Vehicle_With_Highest_Sale_Price();
 Average_Cost_Of_Vehicles_In_Stock();
 Gross_Profit_If_All_Cars_Are_Sold();

 printf("\n\n Highest Selling Price:  %u   $%u \n",
  HighestCarNumber, HighestPrice);
 printf(" Average cost of all cars:   $%u \n", AverageCarCost);
 printf(" Gross profit from all cars: $%u \n", GrossProfit);

 /* Call our save file function*/
 SaveDataToFile2();

 Index++;

 printf("\n\nDo You Wish To Enter Another Car?(Y/N)");
 fflush(stdin);
 scanf("%c",&Play_Again);
 }while ((Play_Again == 'y') || (Play_Again == 'Y'));

 return 0;

Quote:};

/* END MAIN LOOP*/

void Vehicle_With_Highest_Sale_Price()
{
 for(n=1; n<=Index; n++)
 {
  if (VehicleArray[n].Vehicle_Sale_Price > HighestPrice)
  {
   HighestPrice = VehicleArray[n].Vehicle_Sale_Price;
   HighestCarNumber =  n;
  }
 }

Quote:};

void Average_Cost_Of_Vehicles_In_Stock()
{
 unsigned int Cost=0;
 for(n=0; n<=Index; n++)
  Cost += VehicleArray[n].Vehicle_Purchase_Cost;
 //if(Index==0)Index++;/* Stop a divide by zero error*/
 AverageCarCost = Cost / (Index? Index: 1);

Quote:};

void Gross_Profit_If_All_Cars_Are_Sold()
{
 unsigned int TotalOfVehiclePurchases=0;
 unsigned int TotalOfVehicleSales=0;

 for(n=1; n<=Index; n++)
  TotalOfVehiclePurchases += VehicleArray[n].Vehicle_Purchase_Cost;

 for(n=1; n<=Index; n++)
  TotalOfVehicleSales += VehicleArray[n].Vehicle_Sale_Price;

 printf("\n Sales: %u Purchases: %u", TotalOfVehicleSales,
TotalOfVehiclePurchases);
 GrossProfit = TotalOfVehicleSales - TotalOfVehiclePurchases;

Quote:};

unsigned int ReadDataFromFile()
{
 FILE *CarDataFile;

 printf("Reading Car Data File from disk\nPatience would be
appreciated...");

 if((CarDataFile = fopen("CAR_DATA.DAT","r"))==NULL)
 {
  printf("\nERROR: File probably does not exist in directory.");

  /* Attempt to create the data file*/
  if((CarDataFile = fopen("CAR_DATA.DAT","a"))==NULL)
  {
   printf("\nWe cannot create file.\n Program will now end.");
   exit(EXIT_FAILURE);
  }
  else
  {
   printf("\nData file has been created.");
   NEW_DATA_FILE=1;
   fclose(CarDataFile);
  }
  }
else /* Data file has been successfully opened*/
{
 printf("\n\nFile opened for reading...\n\n");

 if(NEW_DATA_FILE != 1)/* File was NOT just created, so we can read from
it*/
 {
  Index=1; /* Sets array up from zero for compatibility with the rest of
program*/
  while (!feof(CarDataFile))
  {
   fscanf(CarDataFile, "%u %u %u \n",
    &VehicleArray[Index].Vehicle_Number,
    &VehicleArray[Index].Vehicle_Purchase_Cost,
    &VehicleArray[Index].Vehicle_Sale_Price);
   Index++;
   if(Index==MAX_ARRAY_SIZE)
   {
    printf("\nERROR: Maximum number of values have been read from file.\n");
    MAX_HAS_BEEN_REACHED=1;
    break;/* Will jump out of while loop as to NOT read any more entries */
   }
  }
 }
fclose(CarDataFile);

Quote:}
return Index;
};/* End Function*/

void SaveDataToFile2()
{
 FILE *CarDataFile;

 if ((CarDataFile = fopen("CAR_DATA.DAT","w+"))==NULL)
  printf("\nSeems we can't open the data file for writing.");
 else
 {
  for (n=1; n<=Index;n++)
  {
   fprintf(CarDataFile, "%u %u %u\n",
    VehicleArray[n].Vehicle_Number,
    VehicleArray[n].Vehicle_Purchase_Cost,
    VehicleArray[n].Vehicle_Sale_Price);
  }
 }
fclose(CarDataFile);

Quote:};

 
 
 

Porting to Linux

Post by Warren Youn » Thu, 21 Sep 2000 04:00:00



Quote:

> //#include <conio.h>

There is a uconio library out now.  See freshmeat.net.

Quote:> #define MAX_ARRAY_SIZE 10
> //const int MAX_ARRAY_SIZE=10;

If you compile with C++ rather than C, the "const int" version will do
what you want.  const in C is weak -- C++ fixed it.

And as for the rest of it, I agree with the other poster in this thread:
you just posted your entire program, with no info on what's going wrong
with it.  That's basically saying, "This is broken... fix it for me!"
--
= Warren -- See the *ix pages at http://www.*port.com/~tangent/ix/
=
= ICBM Address: 36.8274040 N, 108.0204086 W, alt. 1714m

 
 
 

Porting to Linux

Post by Brian Dotzou » Fri, 22 Sep 2000 04:00:00


Change the declaration

Vehicle VehicleArray[MAX_ARRAY_SIZE];

to be

struct Vehicle VehicleArray[MAX_ARRAY_SIZE];


Quote:> I am having trouble porting the following code to Linux.

> All help greatly appreciated.

> #include <stdio.h>
> #include <stdlib.h>
> //#include <conio.h>

> /* Features to add            */
> /* 1. Add password protection */
> /* 2. Have multiple data files*/
> /* 3. Save/Delete data files. */
> /* 4. Create usage log of time of use*/

> struct Vehicle
>  {
>  unsigned int Vehicle_Number;
>  unsigned int Vehicle_Purchase_Cost;
>  unsigned int Vehicle_Sale_Price;
>  };

> /* BEGIN FUNCTIONS DECLARATIONS*/
> void Vehicle_With_Highest_Sale_Price();
> void Average_Cost_Of_Vehicles_In_Stock();
> void Gross_Profit_If_All_Cars_Are_Sold();

> unsigned int ReadDataFromFile(); /* Function will return current index
> number*/
> void SaveDataToFile2();
> /* END FUNCTIONS DECLARATIONS*/

> #define MAX_ARRAY_SIZE 10
> //const int MAX_ARRAY_SIZE=10;
> int MAX_HAS_BEEN_REACHED=0;
> Vehicle VehicleArray[MAX_ARRAY_SIZE];

> /* BEGIN VARIABLE DECLARATIONS*/
> int NEW_DATA_FILE=0;
> unsigned int Index=0;
> unsigned int n;
> char Play_Again;

> unsigned int AverageCarCost;     /* Holds average cost of all cars
> */
> unsigned int GrossProfit;        /* Holds gross profit of all cars
> */
> unsigned int HighestCarNumber;   /* Holds car number with highest sale price
> */
> unsigned int HighestPrice = 0;   /* Holds highest price of all cars
> */
> /* END VARIABLE DECLARATIONS*/

> int main(void)
> {
>  Index = ReadDataFromFile();
>  printf("Index returned from function = %d\n",Index);
>  if((Index==0)&&(NEW_DATA_FILE==1))Index=1;/* File just created, so set
> Index to 1*/

>  /* BEGIN MAIN LOOP*/
>  do
>  {

>   printf("Car Number: %u\n", Index);

>   if(MAX_HAS_BEEN_REACHED != 1)
>   {
>    do
>    {
>     fflush(stdout);
>     printf("\nCar Purchase Price: ");
>     scanf("%u", &VehicleArray[Index].Vehicle_Purchase_Cost);

>     printf("\nCar Sale Price: ");
>     scanf("%u", &VehicleArray[Index].Vehicle_Sale_Price);

>     if (VehicleArray[Index].Vehicle_Purchase_Cost >
> VehicleArray[Index].Vehicle_Sale_Price)
>     {
>      printf("\nThe selling cost of $%u must be greater than the purchase
> cost of $%u",
>       VehicleArray[Index].Vehicle_Sale_Price,
>       VehicleArray[Index].Vehicle_Purchase_Cost);
>     }
>    }while (VehicleArray[Index].Vehicle_Purchase_Cost >
> VehicleArray[Index].Vehicle_Sale_Price);

>    printf("Car Number \t Car Cost \t Car Sell\n");
>    printf("------------------------------------------------\n");
>   }/* END MAX REACHED IF*/

>  for(n=1; n<=Index; n++)
>  {
>   printf("%u \t\t $%u \t\t $%u \n",
>   n, VehicleArray[n].Vehicle_Purchase_Cost,
> VehicleArray[n].Vehicle_Sale_Price);
>  };

>  Vehicle_With_Highest_Sale_Price();
>  Average_Cost_Of_Vehicles_In_Stock();
>  Gross_Profit_If_All_Cars_Are_Sold();

>  printf("\n\n Highest Selling Price:  %u   $%u \n",
>   HighestCarNumber, HighestPrice);
>  printf(" Average cost of all cars:   $%u \n", AverageCarCost);
>  printf(" Gross profit from all cars: $%u \n", GrossProfit);

>  /* Call our save file function*/
>  SaveDataToFile2();

>  Index++;

>  printf("\n\nDo You Wish To Enter Another Car?(Y/N)");
>  fflush(stdin);
>  scanf("%c",&Play_Again);
>  }while ((Play_Again == 'y') || (Play_Again == 'Y'));

>  return 0;
> };
> /* END MAIN LOOP*/

> void Vehicle_With_Highest_Sale_Price()
> {
>  for(n=1; n<=Index; n++)
>  {
>   if (VehicleArray[n].Vehicle_Sale_Price > HighestPrice)
>   {
>    HighestPrice = VehicleArray[n].Vehicle_Sale_Price;
>    HighestCarNumber =  n;
>   }
>  }
> };

> void Average_Cost_Of_Vehicles_In_Stock()
> {
>  unsigned int Cost=0;
>  for(n=0; n<=Index; n++)
>   Cost += VehicleArray[n].Vehicle_Purchase_Cost;
>  //if(Index==0)Index++;/* Stop a divide by zero error*/
>  AverageCarCost = Cost / (Index? Index: 1);
> };

> void Gross_Profit_If_All_Cars_Are_Sold()
> {
>  unsigned int TotalOfVehiclePurchases=0;
>  unsigned int TotalOfVehicleSales=0;

>  for(n=1; n<=Index; n++)
>   TotalOfVehiclePurchases += VehicleArray[n].Vehicle_Purchase_Cost;

>  for(n=1; n<=Index; n++)
>   TotalOfVehicleSales += VehicleArray[n].Vehicle_Sale_Price;

>  printf("\n Sales: %u Purchases: %u", TotalOfVehicleSales,
> TotalOfVehiclePurchases);
>  GrossProfit = TotalOfVehicleSales - TotalOfVehiclePurchases;
> };

> unsigned int ReadDataFromFile()
> {
>  FILE *CarDataFile;

>  printf("Reading Car Data File from disk\nPatience would be
> appreciated...");

>  if((CarDataFile = fopen("CAR_DATA.DAT","r"))==NULL)
>  {
>   printf("\nERROR: File probably does not exist in directory.");

>   /* Attempt to create the data file*/
>   if((CarDataFile = fopen("CAR_DATA.DAT","a"))==NULL)
>   {
>    printf("\nWe cannot create file.\n Program will now end.");
>    exit(EXIT_FAILURE);
>   }
>   else
>   {
>    printf("\nData file has been created.");
>    NEW_DATA_FILE=1;
>    fclose(CarDataFile);
>   }
>   }
> else /* Data file has been successfully opened*/
> {
>  printf("\n\nFile opened for reading...\n\n");

>  if(NEW_DATA_FILE != 1)/* File was NOT just created, so we can read from
> it*/
>  {
>   Index=1; /* Sets array up from zero for compatibility with the rest of
> program*/
>   while (!feof(CarDataFile))
>   {
>    fscanf(CarDataFile, "%u %u %u \n",
>     &VehicleArray[Index].Vehicle_Number,
>     &VehicleArray[Index].Vehicle_Purchase_Cost,
>     &VehicleArray[Index].Vehicle_Sale_Price);
>    Index++;
>    if(Index==MAX_ARRAY_SIZE)
>    {
>     printf("\nERROR: Maximum number of values have been read from file.\n");
>     MAX_HAS_BEEN_REACHED=1;
>     break;/* Will jump out of while loop as to NOT read any more entries */
>    }
>   }
>  }
> fclose(CarDataFile);
> }
> return Index;
> };/* End Function*/

> void SaveDataToFile2()
> {
>  FILE *CarDataFile;

>  if ((CarDataFile = fopen("CAR_DATA.DAT","w+"))==NULL)
>   printf("\nSeems we can't open the data file for writing.");
>  else
>  {
>   for (n=1; n<=Index;n++)
>   {
>    fprintf(CarDataFile, "%u %u %u\n",
>     VehicleArray[n].Vehicle_Number,
>     VehicleArray[n].Vehicle_Purchase_Cost,
>     VehicleArray[n].Vehicle_Sale_Price);
>   }
>  }
> fclose(CarDataFile);
> };

 
 
 

Porting to Linux

Post by root » Sat, 23 Sep 2000 04:00:00



Quote:> I am having trouble porting the following code to Linux.
> All help greatly appreciated.

If you are new to linux/unix I suggest you use perl
for prototyping until you get the hang of things.  Its
quite relevant to C anyway since it uses C library
functions to do most of the actual work...
 
 
 

Porting to Linux

Post by Adam The » Wed, 27 Sep 2000 04:00:00





> > //#include <conio.h>

> There is a uconio library out now.  See freshmeat.net.

i am working on a simple clock/alarm/timer program that i would like to
be very portable as is, accross win*, *nix, and hopefully other os. if
this cannot be done, i will stick with linux/unix.

i need to make the computer's speakers emit a sound. i have the working
code for this under dos, but it will not work for linux, since conio.h
and dos.h are needed by the code.

i see that uconio.h is out on freshmeat, (thanks!  :) but was wondering
if there is anyway to get both dos and *nix to use the same header file
so i can use the same program under *nix and win* machines.

or do i have to have *either* uconio.h or conio.h, and alienate one of
the os's?

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

Porting to Linux

Post by Adam The » Wed, 27 Sep 2000 04:00:00





> > //#include <conio.h>

> There is a uconio library out now.  See freshmeat.net.

i am working on a simple clock/alarm/timer program that i would like to
be very portable as is, accross win*, *nix, and hopefully other os. if
this cannot be done, i will stick with linux/unix.

i need to make the computer's speakers emit a sound. i have the working
code for this under dos, but it will not work for linux, since conio.h
and dos.h are needed by the code.

i see that uconio.h is out on freshmeat, (thanks!  :) but was wondering
if there is anyway to get both dos and *nix to use the same header file
so i can use the same program under *nix and win* machines.

or do i have to have *either* uconio.h or conio.h, and alienate one of
the os's?

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

Porting to Linux

Post by Paul D. Boy » Fri, 29 Sep 2000 04:00:00


: i am working on a simple clock/alarm/timer program that i would like to
: be very portable as is, accross win*, *nix, and hopefully other os. if
: this cannot be done, i will stick with linux/unix.

: i need to make the computer's speakers emit a sound. i have the working
: code for this under dos, but it will not work for linux, since conio.h
: and dos.h are needed by the code.

: i see that uconio.h is out on freshmeat, (thanks!  :) but was wondering
: if there is anyway to get both dos and *nix to use the same header file
: so i can use the same program under *nix and win* machines.

You can use the same program on different platforms if you use conditional
compilation directives.  For example, you could do something like this
with regard to your header files:

#ifdef DOS
#include <conio.h>
#endif

#ifdef UNIX
#include <unistd.h>
#include <termios.h>
#endif

When compiling your program  for DOS  you would have a command line
something like:
cc -DDOS  *.c
whereas on a Unix platform you would have something like:
cc -DUNIX *.c

Read about conditional compilation directives in a decent C programming
book.  In addition, when designing your program it is often better to
put all of the platform dependent code in one source file where in the
code you would also use conditional compilation directives:

#ifdef DOS
      if( kbhit() ) {
          /* some lame polling under DOS */

      }
#elif UNIX
      /* some UNIX way of waiting for input using select(2) */
#endif

Conditional compilations can get the code looking pretty ugly, but
some time they are necessary for cross platform work.

Paul

--
Paul D. Boyle

North Carolina State University
http://laue.chem.ncsu.edu/web/xray.welcome.html

 
 
 

1. Windows NT|2000 Linux 2.2 port (was Linux apps in win2000 port news!)

For about two months, I've been busy working away at the possibility of a
native NT microkernel version of a Linux server. It's definitely doable. I'm
collecting information at the moment. I was sort of hoping until I had some
code (typical bizarre project style), but since Interix have beat the OSS
community to the punch, I may as well announce it now.

I'm looking at making the linux server (subsystem) at least passingly
similar to the current 2.2 tree, so that changes could be merged in there
closer . The goal obviously is to run native Linux x86 and Alpha binaries
(depending on platform) without recompilation.

My current idea is for a %windir%\linux\... tree that contains a vmlinux.exe
and the standard linux file system standard and enough stuff to launch bash.
Once launched, the server launches linux images as normal. A
modification/complete re-write of umsdos would provide the file system (or
maybe just use ext2fs until this is a bit more of a reality). By making a
directX fbconsole layer, we could re-use the frame buffer console code as it
exists today. X would come (much) later, but again via the use of the XAA
layer in XFree86 4.0 (I know it doesn't yet exist, but I helped write the
early 3.9 stuff, so I know it does exist :-)

The native NT api is exceptionally well hidden and undocumented, but
NT/W2k's microkernel is as least as good as Mach in all respects (and memory
and thread management, it's significantly better), so a mklinux server port
wouldn't take more than a year with dedicated people.

 If anyone's interested please drop me a line. You'd need to have a compiler
and a passing knowledge of Windows NT/2000 to be useful at this stage. I'm
going to use Visual C++ until I can prove to myself that cygnus's (or other)
gcc under WIn32 can do the job. The reason for this is the header files,
which are vital to Win32 development.

Andrew

2. Cron Job

3. Virtual windows COM port to linux serial port?

4. SysV.4 Streams and Modules programming problem: 1

5. DSP/DIP packages for Linux + Sigview port to Linux...

6. Home networking: Trouble masquerading

7. PowerPC 601 port of linux ?

8. List datastreams and printer types

9. Reading IO ports in Linux

10. Has NETREK been ported to Linux?

11. How do I access IO port under linux?

12. porting to LINUX

13. Win 3.11 port to linux Would you use it?