int main( int argc, char *argv[]) { int size, index, flagALRT = 0; //Variables to check integers struct fileInfo *table; //Table struct to store everything const char *flag = {"alrt"}; //Available flags char c; //Checks for the certain flag //While loop that checks though for all possible flags while ((c = getopt(argc, argv, flag)) != -1) { //Switch statement that checks for the type of flag. switch(c) { case 'a': flagALRT += AFLAG; break; case 'l': flagALRT += LFLAG; break; case 'r': flagALRT += RFLAG; break; case 't': flagALRT += TFLAG; break; case '?': //Checks if there is an unknown flag (void)fprintf(stderr, "Usage: %s [-alrt] [file ...]\n", argv[0]); exit(EXIT_FAILURE); } } //Checks if it's the current directory input if( argc == optind ) { size = buildFileInfoTable( "." , &table ); //Builds table in current displayFileInfo(table, size, flagALRT); //Displays all the file info free(table); //Frees up the table } //Checks of specific file or files for(index = optind; index < argc; index++) { size = buildFileInfoTable(argv[index], &table); //Checks multiple files displayFileInfo(table, size, flagALRT); //Display info about file free(table); //Frees up the table } return ( 0 ); /* Returns 0 if sucessfully executed */ }
int main( int argc, char *argv[] ){ unsigned int displayMode; int entries; struct fileInfo ** table_ptr =\ (struct fileInfo**) malloc(sizeof(struct fileInfo**)); /* Check if allocation is successful*/ if (table_ptr == NULL) { perror(STR_ALLOC_ERR); exit(1); } *table_ptr = (struct fileInfo*) malloc(sizeof(struct fileInfo*)); /* Check if allocation is successful*/ if (*table_ptr == NULL) { perror(STR_ALLOC_ERR); exit(1); } // Set display mode displayMode = setDisplayMode(argc, argv); /* Check for error flag of display mode*/ if (displayMode & ERR_FLAG) { (void) fprintf(stderr, STR_USAGE, argv[0]); exit(1); } if (argv[optind] == NULL) { // No files specified in command line // Build the file info table and the number of entries entries = buildFileInfoTable(".", table_ptr); // Display the file info displayFileInfo(*table_ptr, entries, displayMode); free(*table_ptr); } else { // files specified in command line while (argv[optind] != NULL) { // Keeps reading multiple files entries = buildFileInfoTable(argv[optind++], table_ptr); displayFileInfo(*table_ptr, entries, displayMode); free(*table_ptr); } } free(table_ptr); return 0; }
void judgmentArguments(char argument[]) { int amark; if(!strcmp(argument, "-l")) { //以长清单的形式从第三个文件开始显示 amark = NO_A_ARGUMENT; displayFileInfoLong(amark); } else if(!strcmp(argument, "-a")) { //从第一个文件开始只显示文件名 amark = HAVE_A_ARGUMENT; displayFileInfo(amark); } else if(!strcmp(argument, "-al") || !strcmp(argument, "-la")) { //以长清单的形式从第一个文件开始显示 amark = HAVE_A_ARGUMENT; displayFileInfoLong(amark); } }
// Main function in LSB Steg // Parameters are used to indicate the input file and available options int main(int argc, char *argv[]){ int x; char *pp; int *srcsize; if(argc < 3 || argc > 4) { printHelp(); return; } // initialize gray code conversion table buildGrayCode(); int i, j; char binary[9]; binary[8] = '\0'; for(i=0;i<256;i++){ for(j=0;j<8;j++){ binary[j] = getBit(toCGC[i], j)+48; } printf("%s\n", binary); } return 0; // comment this out if you want to test further // this is just to test the CGC builder // get the number of bits to use for data hiding or data extracting // if not specified, default to one if(argc == 4) { // the range for gNumLSB is 1 - 7; if gNumLSB == 0, then the mask would be 0xFF and the // shift value would be 8, leaving the target unmodified during embedding or extracting // if gNumLSB == 8, then the source would completely replace the target gNumLSB = *(argv[3]) - 48; if(gNumLSB < 1 || gNumLSB > 7) { gNumLSB = 1; printf("The number specified for LSB was invalid, using the default value of '1'.\n\n"); } gMask = 256 - (int) pow(2.0, gNumLSB); gShift = 8 - gNumLSB; } // read the source file pSrcFile = readFile(argv[1], &srcFileSize); if(pSrcFile == NULL) return; // Set up pointers to various parts of the source file pSrcFileHdr = (BITMAPFILEHEADER *) pSrcFile; pSrcInfoHdr = (BITMAPINFOHEADER *) (pSrcFile + sizeof(BITMAPFILEHEADER)); // pointer to first RGB color palette, follows file header and bitmap header pSrcColorTable = (RGBQUAD *) (pSrcFile + sizeof(BITMAPFILEHEADER) + pSrcInfoHdr->biSize); // file header indicates where image data begins pSrcData = pSrcFile + pSrcFileHdr->bfOffBits; pp = (char *) &(pSrcFileHdr->bfType); printf("File Type: %c%c\n", *pp, *(pp+1) ); srcsize = (int *) &(pSrcFileHdr->bfSize); printf("File Size: %d\n", *srcsize); // for debugging purposes, show file info on the screen displayFileInfo(argv[1], pSrcFileHdr, pSrcInfoHdr, pSrcColorTable, pSrcData); // scramble the color table if(strcmp(argv[2], "sc") == 0) { char nm[128]; unsigned char *pFile; FILE *ptrFile; strcpy(nm, "sc_"); strcat(nm, argv[1]); // scramble the entire color table pFile = scramble(pSrcFile, pSrcColorTable, srcFileSize); if(pFile == NULL) return; // open the new file, MUST set binary format (text format will add line feed characters) ptrFile = fopen(nm, "wb+"); if(ptrFile == NULL) { printf("Error opening new file for writing.\n\n"); return; } // write the file x = (int) fwrite(pFile, sizeof(unsigned char), srcFileSize, ptrFile); // check for success if(x != srcFileSize) { printf("Error writing file %s.\n\n", nm); return; } fclose(ptrFile); // close file return; } // read the target file pTgtFile = readFile(argv[2], &tgtFileSize); if(pTgtFile == NULL) return; // Set up pointers to various parts of file pTgtFileHdr = (BITMAPFILEHEADER *) pTgtFile; pTgtInfoHdr = (BITMAPINFOHEADER *) (pTgtFile + sizeof(BITMAPFILEHEADER)); pTgtColorTable = (RGBQUAD *) (pTgtFile + sizeof(BITMAPFILEHEADER) + pTgtInfoHdr->biSize); pTgtData = pTgtFile + pTgtFileHdr->bfOffBits; // for debugging purposes, show file info on the screen displayFileInfo(argv[2], pTgtFileHdr, pTgtInfoHdr, pTgtColorTable, pTgtData); // write the file to disk x = writeFile(pTgtFile, pTgtFileHdr->bfSize, argv[2]); return 0; } // main
void ls_main(int argc, char *argv[]) { char *dirname; char *argument; int filecount; int nummark; if(argc > 3) { printf("please input:my_ls [argument] [directory]\n"); exit(1); } else if(argc == 1) { //获取当前目录 dirname = 当前目录 dirname = NULL; nummark = NO_A_ARGUMENT; dirname = getcwd(NULL, 0); //令dirname = 当前目录 getDirMaxlenAndCount(dirname); getDirFileInfo(dirname); displayFileInfo(nummark); // free(dirname); //并显示除了. .. 之外的文件名称 } else if(argc == 2) { //令一个字符串tmp=argv[1] argument = argv[1]; if(argv[1][0] == '-') { //判断tmp的三种情况 dirname = getcwd(NULL, 0); getDirMaxlenAndCount(dirname); getDirFileInfo(dirname); judgmentArguments(argument); } else { //获取argv[1]的目录 dirname = 指定目录 nummark = NO_A_ARGUMENT; dirname = argv[1]; getDirMaxlenAndCount(dirname); getDirFileInfo(dirname); displayFileInfo(nummark); } } else if(argc == 3) { //先获取argv[2]的目录内容 dirname = argv[2]; getDirMaxlenAndCount(dirname); getDirFileInfo(dirname); //再令tmp = argv[1] argument = argv[1]; //判断tmp的三种情况 judgmentArguments(argument); } }