Thread: Programm in Freedos and djgpp chashes when using commandline paramters

  1. #1
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    59

    Programm in Freedos and djgpp chashes when using commandline paramters

    Hallo guys!

    I have tried just for fun to work with FreeDOS 1.2 and djgpp to write some
    programs in c.
    But when i try to access arguments argv[] from (example)

    Code:
    int main(it argc, char *argv[]){
    .......
    
    result = strcmp(argv[1], "/?");
    to try
    Code:
    int main(it argc, char **argv){
    has the same results.

    the source will compile and link without errors, but if i call the exe the
    Programm will crash with SIGSEGV as an error.
    By other compilers the same code works fine.

    Where lies the rabbit in the pepper?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,553
    I hope "it argc" is a typo, it should be "int argc".

    How exactly are you calling the program?

    What are your actual command line arguments?

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    739
    Quote Originally Posted by rusyoldguy View Post
    Hallo guys!

    I have tried just for fun to work with FreeDOS 1.2 and djgpp to write some
    programs in c.
    But when i try to access arguments argv[] from (example)

    Code:
    int main(it argc, char *argv[]){
    .......
    
    result = strcmp(argv[1], "/?");
    to try
    Code:
    int main(it argc, char **argv){
    has the same results.

    the source will compile and link without errors, but if i call the exe the
    Programm will crash with SIGSEGV as an error.
    By other compilers the same code works fine.

    Where lies the rabbit in the pepper?
    If the program is called with no arguments to the program, then accessing argv[1] or higher would be illegal.

    Us the following to display the command line arguments:
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
       int x = 0;
    
      printf("argc == %d\n", argc);
    
      for(x = 0; x < argc; x++)
       {
          printf("argv[%d] == %s\n", x, argv[x]);
       }
    
       return 0;
    }
    Run this with no command line args, and with various number of args to see the results.
    Last edited by rstanley; 06-09-2020 at 07:41 AM.

  4. #4
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    59
    sorry, my fault.#

    In the original code i used

    int argc

    What i don't understand is, when i call the programm without any argument it crashes in FreeDOS.
    The same EXE works fine if i call it with a given argument, like "/?" "help" or anything.
    I wrote it 1995 with qc25 compiler of MSDOS.

    The original MSDOS had no Program to rename a directory:
    Code:
    /* Programm rendir.c benennt Verzeichnisse um */
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h>
    
    void Autor(void)
    {
     printf("Test-Version\n");
    /* Name of Author */
    
    
    }
    
    
    int main(int argc, char *argv[])
    {
     int frage;
     char altername[300], neuername[300];
    
    if(((strcmp(argv[1], "/?")) == 0) || ((strcmp(argv[1], "/h")) == 0) || \
            ((strcmp(argv[1], "help")) == 0))
           {
            AUTOR();
            printf("\nRENDIR.EXE .... Ein Programm zum Umbenennen von Verzeichnissen");
            printf("\ngeschrieben fuer MS-DOS Betriebssysteme");
            printf("\nSyntax: rendir alter_Verzeichnissname neuer_Verzeichnissname");
            getch();
            return(0);
           }
     if(argc != 3)
      goto keinargument;
     
     strcpy(altername, argv[1]);
     strcpy(neuername, argv[2]);
     goto request;
    
     keinargument: printf("\nRENDIR.EXE .... Ein Programm zum Umbenennen von Verzeichnissen");
                   printf("\ngeschrieben fuer MS-DOS Betriebssysteme");
                   printf("\nSyntax: rendir alter_Verzeichnissname neuer_Verzeichnissname");
                   printf("\nEnde........e oder E");
                   printf("\nInfo........i oder I");
                   printf("\nWeiter......beliebige Tate");
     
            frage = getche();
            if ((frage == 'e') || (frage == 'E')) 
             return(0);
            if ((frage == 'i') || (frage == 'I')) 
             {
             AUTOR();
             return(0);
             }
            
            printf("\nAlter Verzeichniss-Name: ");
            scanf("%s", altername);
            printf("\nNeuer Verzeichniss-Name: ");
            scanf("%s", neuername);
     request:
     printf("\nAlter Name:%s\nNeuerName :%s\n", altername, neuername);
     printf("\nWollen Sie wirklich diese Verzeichniss umbenennen(j/n)");
    
     frage = getche();
     if ((frage == 'n') || (frage == 'N'))
      return(0);
    
     frage = rename( altername, neuername );
     if (frage != 0)
      {
       printf("\nUmbenennung fehlgeschlagen\n");
       printf("\nBitte geben Sie beim alten und beim");
       printf("\nneuen Verzeichnissnamen den kompletten Pfad an\n");
      }
     
    
    return(0);
    }
    I work with an emulator qemu-kvm on SUSE Linux tumbleweed. At the same emulated Freedos i have an old
    QC25 compiler, with it i have no Problems. Why does progams where compiled for DOS with DJGPP don't like
    a call without arguments?Programm in Freedos and djgpp chashes when using commandline paramters-freedos-djgpp-failure-jpg

    Same results i've got with compile and link with RHIDE.EXE.
    Also i compiled and linked on with gcc himself. No other results.
    Last edited by rusyoldguy; 06-09-2020 at 08:24 AM.

  5. #5
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    174
    The code is incorrect, that's why. You need to ensure that the index is less than `argc`. Period.
    The power of null.

  6. #6
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    59
    Here a call with given argument:
    Programm in Freedos and djgpp chashes when using commandline paramters-freedos-djgpp-failure_2-jpgProgramm in Freedos and djgpp chashes when using commandline paramters-freedos-djgpp-failure_2-jpg

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    739
    See my previous response!

    If the program is called with NO COMMAND LINE ARGUMENTS, then attempting to access argv[1], argc[2], argc[3], ... is ILLEGAL!!! You can ONLY access argv[0] through argv[argc - 1]!

    You should check argc first. If you program requires command line arguments, then print an error message, and end the program if argc is too low!

    I would also recommend losing all the gotos and create functions.

  8. #8
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    59
    Thank You!

    Now it works!

    I think i had Tomatoes on the eyes!
    He who makes no mistakes makes nothing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a commandline
    By Sentral in forum C++ Programming
    Replies: 4
    Last Post: 10-01-2006, 09:09 PM
  2. commandline on delay or something:P
    By boozy in forum C++ Programming
    Replies: 4
    Last Post: 11-03-2004, 01:40 PM
  3. my own commandline parser function
    By scrappy in forum C Programming
    Replies: 7
    Last Post: 08-22-2003, 02:57 AM
  4. about commandline arguments
    By Abdi in forum C Programming
    Replies: 2
    Last Post: 05-26-2002, 01:20 AM
  5. visual c++ question (paramters)
    By rxg00u in forum Windows Programming
    Replies: 0
    Last Post: 04-11-2002, 07:50 AM

Tags for this Thread