int main( int argc, char **argv ) { int c; while( ( c = getopt_long( argc, argv, shortopts, longopts, NULL ) ) != -1 ) { switch( c ) { case 'p': runProg( optarg ); case 'h': usage(); return 0; case 'v': prog_version(); return 0; default: runProg( PORT_NUMBER ); } } return 0; }
int main(){ /* */ /* BEGIN INITIALIZE */ /* */ bool loop = true; cout << "MAGIC SQUARE CONSTRUCTION PROGRAM \nBUILD VERSION " << LATEST_VERSION << "\nLAST REVISED " << REVISION_DATE << "\nCOPYRIGHT " << AUTHOR << "\n\n\n"; /* END INITIALIZE */ while(loop){ cout << "Enter command (or type 'help' for command list): "; string str; getline(cin, str); for(unsigned int z = 0; z < str.length(); z++) str[z] = tolower(str[z]); cout << endl; /* */ /* BEGIN CHECK USER INPUT */ /* */ if(str == "help") showCommands(); else if(str == "exit" || str == "quit") loop = false; else if(str == "generate squares"){ int remaining[NSQ]; STDMatrix builder; for(int i = 0; i < NSQ; i++) remaining[i] = 0; cout << "Generating Order-" << N << " squares. . . \n\n"; generateOrder4(remaining, builder, 1); cout << "All squares built."; } else if(str.find("output square ") != -1){ if(magics.size() == 0) throwException(1); else{ unsigned int a = atoi(str.substr(14).c_str()) - 1; if(a <= magics.size() - 1 && a >= 0) magics[a].print(); else throwException(2); } } else if(str.find("output squares ") != -1){ if(magics.size() == 0) throwException(1); else{ int pos = str.find("to") - 1; unsigned int a = atoi(str.substr(15, pos-2).c_str()) - 1; unsigned int b = atoi(str.substr(pos+3).c_str()) - 1; while(a <= b){ if(a <= magics.size() - 1 && a >= 0){ magics[a].print(); if(a != b) cout << "\n\n\n"; a++; } else{ throwException(2); break; } } } } else if(str.find("output type ") != -1){ if(magics.size() == 0) throwException(1); else{ unsigned int a = atoi(str.substr(12).c_str()); for(unsigned int i = 0; i < magics.size(); i++){ if(magics[i].getType() == a){ magics[i].print(); cout << "\n\n\n"; } } } } else if(str == "permute upper triangular"){ if(magics.size() == 0) throwException(1); else{ cout << "Permuting squares as upper triangular. . .\n\n"; permuteUT(); cout << "Permutation complete."; } } else if(str == "check square equality"){ if(magics.size() == 0) throwException(1); else{ cout << "Finding unique squares after tests for equality. . .\n\n"; findUnique_Comparison(); cout << "Unique squares found."; } } else if(str == "classify squares"){ if(magics.size() == 0) throwException(1); else{ cout << "Classifying squares as Dudeney Types. . .\n\n"; classify4(); cout << "Classification complete." << endl << "Remember to reclassify squares after any operation!"; } } else if(str == "sort squares"){ if(magics.size() == 0) throwException(1); else{ cout << "Sorting squares from least to greatest. . .\n\n"; sortLinearCompare(); cout << "Sorting complete."; } } else if(str == "find unique squares"){ if(magics.size() == 0) throwException(1); else{ cout << "Finding unique squares after tests for permutations. . .\n\n"; findUnique_Permute(); cout << "\nUnique squares found."; } } else if(str == "analysis"){ if(magics.size() == 0){ cout << "There are currently 0 squares created." << endl << "Generate squares for further details."; } else{ cout << "There are currently " << magics.size() << " squares created." << endl; if(magics[0].getType() == -1) cout << "Classify squares for further details."; else classList(); } } else if(str == "run program") runProg(); else cout << "Unknown Command '" << str << "'"; /* END CHECK USER INPUT */ cout << endl << endl; } return 0; }
void runshell(){ int flag, inp_flag, out_flag, ifd, ofd, stdin_copy, stdout_copy; char pth[BUFFER]; char fileIn[BUFFER], fileOut[BUFFER]; strcpy(pth,""); while(1){ printf("myshell@%s> ", getcwd(line,BUFFER)); line[0] = '\0'; fgets(line, BUFFER, stdin); clearTabs(line); removeLastEnter(line); // printf("\nLINE = {%s}\n",line); int pipeCount = isPipingNeeded(line); // printf("\npipeCount = %d\n",pipeCount); if(pipeCount>0){ piping(line,pipeCount); continue; } /************ NO PIPING IS REQUIRED ***********/ inp_flag = extractWord(line, '<' ,fileIn); out_flag = extractWord(line,'>', fileOut); if(inp_flag != -1 && out_flag != -1){ if(inp_flag == 1){ stdin_copy = dup(0); close(0); ifd = open(fileIn, O_RDONLY); if (ifd < 0) { fprintf(stderr, "Unable to open input file in read mode...\n"); continue; } } if(out_flag == 1){ stdout_copy = dup(1); close(1); ofd = open(fileOut, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (ofd < 0) { fprintf(stderr, "Unable to open output file in write mode...\n"); continue; } } } if(feof(stdin)){ printf("\n"); exit(0); } separateWord(line); //printf("%s",words[0]); if(!strcmp(words[0],"cd")){ strcpy(pth,""); strcat(pth,getcwd(line,BUFFER)); //if(words[1][0] != '/') strcat(pth,"/"); strcat(pth,words[1]); flag = chdir(pth); if(flag == -1){ flag = chdir(words[1]); if(flag == -1) printf("\nNo Such file or directory !!\n"); // runProg(words[0],"",words[1]); } } else if(!strcmp(words[0],"pwd")){ printf("%s\n",getcwd(line,BUFFER)); } else if(!strcmp(words[0],"mkdir")){ strcpy(pth,""); strcat(pth,getcwd(line,BUFFER)); strcat(pth,"/"); strcat(pth,words[1]); flag = mkdir(pth,0777); if(flag == -1){ flag = mkdir(words[1],0777); if(flag == -1) runProg(words[0],"",words[1],0); } } else if(!strcmp(words[0],"rmdir")){ strcpy(pth,""); strcat(pth,getcwd(line,BUFFER)); strcat(pth,"/"); strcat(pth,words[1]); flag = rmdir(pth); if(flag == -1){ flag = rmdir(words[1]); if(flag == -1) runProg(words[0],"",words[1],0); } } else if(!strcmp(words[0],"ls") && !(words[1][0] == '-' && words[1][1] == 'l')){ DIR *dp; struct dirent *ep; dp = opendir ("./"); if (dp != NULL) { while (ep = readdir (dp)) puts (ep->d_name); (void) closedir (dp); } else puts ("Couldn't open the directory."); } else if(!strcmp(words[0],"ls") && words[1][0] == '-' && words[1][1] == 'l'){ DIR *d; struct dirent *de; struct stat buf; int i,j; char P[10]="rwxrwxrwx",AP[10]=" "; struct passwd *p; struct group *g; struct tm *t; char time[26]; d=opendir("."); readdir(d); readdir(d); while((de=readdir(d))!=NULL) { stat(de->d_name,&buf); // File Type if(S_ISDIR(buf.st_mode)) printf("d"); else if(S_ISREG(buf.st_mode)) printf("-"); else if(S_ISCHR(buf.st_mode)) printf("c"); else if(S_ISBLK(buf.st_mode)) printf("b"); else if(S_ISLNK(buf.st_mode)) printf("l"); else if(S_ISFIFO(buf.st_mode)) printf("p"); else if(S_ISSOCK(buf.st_mode)) printf("s"); //File Permissions P-Full Permissions AP-Actual Permissions for(i=0,j=(1<<8);i<9;i++,j>>=1) AP[i]= (buf.st_mode & j ) ? P[i] : '-' ; printf("%s",AP); //No. of Hard Links printf("%lu",buf.st_nlink); //User Name p=getpwuid(buf.st_uid); printf(" %.8s",p->pw_name); //Group Name g=getgrgid(buf.st_gid); printf(" %-8.8s",g->gr_name); //File Size printf(" %lu",buf.st_size); //Date and Time of modification t=localtime(&buf.st_mtime); strftime(time,sizeof(time),"%b %d %H:%M",t); printf(" %s",time); //File Name printf(" %s\n",de->d_name); } } else if(!strcmp(words[0],"exit")){
int main(int args, char *argv[]){ parseArgs(args, argv); runProg(); return EXIT_SUCCESS; }