DOS in C Language

Rajnesh 0 Tallied Votes 1K Views Share

/*This program works just like MS-DOS. It takes in the input, uses the C Library, uses the system files and function, executing and displaying the code on the same command prompt.
*/

/*************************************************
Author: Rajnesh Prasad
Program: Disk Operating System(DOS) from C Language
Date: 4th July, 2006
**************************************************/
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <dir.h>
#include <windows.h>

char *current_directory(char *path);
void welcome();
void menu();

void main()
{
	char command[50];
   char olddir[150];
   //Changing the console color(www.daniweb.com)
   HANDLE  hConsole;
   hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

   system("TITLE DOS --- Designed by Rajneshwar Prasad");
   cls:
   system("COLOR 0F");
   welcome();

   do{
   	aptech:
   	current_directory(olddir);
      SetConsoleTextAttribute(hConsole, 14);
	   printf(" %s  ",olddir);
      SetConsoleTextAttribute(hConsole, 12);
  	 	gets(command);
      SetConsoleTextAttribute(hConsole, 24);
      if(strcmpi(command,"1")==0){
      	menu();
         goto aptech;
      }
      else if(strcmpi(command,"2")==0){
      	system("calc.exe");
      }
      else if(strcmpi(command,"3")==0){
      	system("mspaint.exe");
      }
      else if(strcmpi(command,"cls")==0){
         system("cls");
      	goto cls;
      }
      else if(strcmpi(command,"cd..")==0){
      		chdir("\\");
      }
      else if(strcmpi(command,"c:")==0){
      		system("c:\\");
      }
      else{
      	SetConsoleTextAttribute(hConsole, 10);
      	printf("  ");
         SetConsoleTextAttribute(hConsole, 11);
			system(command);
      }
   }while(strcmp(command,"exit"));
}
char *current_directory(char *path)
{
  strcpy(path, "X:\\");
  path[0] = 'A' + getdisk();
  getcurdir(0, path+3);
  return(path);
}
void welcome(){
	printf("\t\t\t--------------------------------\n");
	printf("\t\t\tºAuthor :   Rajneshw…r Prasad  º\n");
   printf("\t\t\tºProgram:   DOS from C Languageº\n");
   printf("\t\t\tº             ¸2006            º\n");
   //Changing the console color(www.daniweb.com)
   HANDLE  hConsole;
   hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
   SetConsoleTextAttribute(hConsole, 5);
   printf("\t\t\t\n\n");
   SetConsoleTextAttribute(hConsole, 4);
   printf("\t\t\tEnter 1 to get other menus\n\n");
}
void menu(){
	HANDLE  hConsole;
   hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hConsole, 2);
	printf("--------------------------------\n");
	printf("ºCalculator     :             2º\n");
   printf("ºMicrosoft Paint:             3º\n");
   printf("º             ¸2006            º\n");

   SetConsoleTextAttribute(hConsole, 5);
   printf("\n\n");
}

Dani AI

Generated

The code posted by is a useful learning attempt at a DOS-style command loop, but it mixes obsolete DOS/Borland calls with Windows APIs and uses several unsafe patterns. 's comment that the program is "very buggy" is accurate. Below are the highest-priority issues visible in the snippet and pragmatic fixes that address stability, portability, and security.

  • Unsafe input: gets() is removed from modern C; it allows buffer overflow. Replace with fgets() and strip the trailing newline.
  • Dangerous command execution: calling system() on raw user input permits command injection and platform-dependent behavior. Implement builtins (cd, exit) and launch external programs with CreateProcess (Windows) or fork+exec (POSIX) after tokenizing the input.
  • Nonportable/obsolete APIs: getdisk, getcurdir, <dir.h>, <conio.h> are not standard. Use _getcwd / GetCurrentDirectory and _chdrive / SetCurrentDirectory instead.
  • Buffer and string handling: unbounded strcpy into path risks overflow. Use snprintf/strncat and always check sizes. Replace strcmpi with the platform-specific _stricmp or strcasecmp.
  • Program entry and error checking: use int main(void) and check return values (GetStdHandle, CreateProcess, file APIs).
  • Encoding and UI text: garbled menu characters come from an encoding mismatch. Save the source file in the compiler-expected encoding (or use UTF-8 consistently) and prefer plain ASCII for console text or use wide-char APIs.

Example safe input pattern:

char cmd[128];
if (fgets(cmd, sizeof cmd, stdin)) {
    cmd[strcspn(cmd, "\r\n")] = '\0';
}

Compiling with full warnings, running static analysis, and testing on a modern toolchain will reveal many remaining problems. Applying the items above will remove the most serious bugs and make the project maintainable.

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

Very buggy program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.