int main(int argc, char* argv[]) { const char* directory = argc > 1 ? argv[1] : "."; if (argc == 1){ if (printDirectoryStats(directory) != success) { printDirectoryError(directory); return failure; } } else{ if (isDirectory(directory) == success) { chdir(directory); if (printDirectoryStats(directory) != success) { printDirectoryError(directory); return failure; } } else if (printFileInfo(directory) != success){ printf("Error, file stats could not be printed"); return failure; } } return success; }
int main(int argc, char *argv[]) { if (argc < 2) { printf("Too few arguments\nUsage: %s filename\n",argv[0]); return 1; } struct Fileinfo fi; strcpy(fi.name,argv[1]); struct stat st; stat(fi.name, &st); if (access(fi.name, F_OK) != -1) // the file exists { fi.exists = true; if(S_ISREG(st.st_mode)) fi.type = "Regular"; else if(S_ISDIR(st.st_mode)) fi.type = "Directory"; else if(S_ISCHR(st.st_mode)) fi.type = "Character device"; else if(S_ISBLK(st.st_mode)) fi.type = "Block device"; else if(S_ISFIFO(st.st_mode)) fi.type = "FIFO"; else if(S_ISLNK(st.st_mode)) fi.type = "Symbolic link"; else if(S_ISSOCK(st.st_mode)) fi.type = "Socket"; else fi.type = "Unknown"; fi.size = st.st_size; struct passwd *pwd; pwd = getpwuid(st.st_uid); fi.owner = pwd->pw_name; struct group *g; g = getgrgid(st.st_gid); fi.group= g->gr_name; char cwd[1024]; if (getcwd(cwd, sizeof(cwd)) != NULL) fi.path = cwd; else fi.path = "Couldn't determine path"; printFileInfo(fi); } else // file doesn't exist, notify user and exit { fi.exists = false; printf("File %s does not exist\n",fi.name); } return 0; }
int main(void) { int rvalue; FILE *fp; char *buffer; int fd; fileInfo_t finfo; char athlete[ATHLETE_SIZE]; int offset; int blockNumInFile1; int blockNumInHeader1; int blockNumInFile2; int blockNumInHeader2; int firstValid; int secondValid; // ---------------------- HDF epipedo ---------------------- // Elegxos an yparxei to fileName printf("\nChecking if file %s exists...\n\n", fileName); if (HDF_ExistsFile(fileName) == TRUE) { printf("Checkpoint 1:\n"); printf("HDF_ExistsFile returned TRUE!\n\n"); } // Dimiourgia tou arxeiou fileName printf("Creating file %s...\n\n", fileName); if ( (rvalue=HDF_CreateFile(fileName)) != BFE_OK) { printf("Checkpoint 2:\n"); printf("HDF_CreateFile() returned %d!\n\n", rvalue); } // Anoigma tou arxeiou fileName printf("Opening file %s...\n\n", fileName); if ((rvalue = HDF_OpenFile(fileName,&fp)) != BFE_OK) { printf("Checkpoint 3:\n"); printf("HDF_OpenFile() returned %d, should have returned 0!\n\n",rvalue); } // Dimiourgia epikefalidas tou arxeiou sti mnimi buffer = malloc(sizeof(char) * BF_BLOCK_SIZE); memset(buffer, 0, BF_BLOCK_SIZE); // Antigrafi epikefalidas sto block 0 tou arxeiou printf("Writing file header to disk...\n\n"); if ((rvalue = HDF_WriteBlock(0, buffer, fp)) != BFE_OK) { printf("Checkpoint 4:"); printf("HDF_WriteBlock() returned %d!\n\n",rvalue); } // Kleisimo tou arxeiou fileName printf("Closing file %s\n\n", fileName); if ((rvalue = HDF_CloseFile(fp)) != BFE_OK) { printf("Checkpoint 5:\n"); printf("HDF_CloseFile() returned %d!\n\n", rvalue); } // ---------------------- FMM epipedo ---------------------- FMM_Init(); // Anoigma tou arxeiou printf("Opening file %s...\n\n", fileName); if ((fd = FMM_Open(fileName)) < 0) { printf("Checkpoint 6:\n"); printf("FMM_Open() returned %d!\n\n", fd); } // Elegxos an i thesi fd tou pinaka anoixtwn arxeiwn einai pleon egkyri kataxwrisi arxeiou printf("Checking for validity of position %d of open files array...\n\n", fd); if (FMM_IsValid(fd) != TRUE) { printf("Checkpoint 7:\n"); printf("FMM_IsValid() returned FALSE!\n\n"); } // Anaktisi pliroforias gia to arxeio me vasi to onoma strcpy(finfo.filename, fileName); printf("Retrieving file information by name...\n\n"); if ((rvalue = FMM_GetFileInfoByName(&finfo)) != BFE_OK) { printf("Checkpoint 8:\n"); printf("FMM_GetFileInfoByName() returned %d!\n\n", rvalue); } printFileInfo(&finfo); // Anaktisi pliroforias gia to arxeio me vasi to fd finfo.fd = fd; printf("Retrieving file information by file descriptor...\n\n"); if ((rvalue = FMM_GetFileInfoByFD(&finfo)) != BFE_OK) { printf("Checkpoint 9:\n"); printf("FMM_GetFileInfoByFD() returned %d!\n\n", rvalue); } printFileInfo(&finfo); // Dimiourgia enos neou block sti mnimi memset(buffer, 0, BF_BLOCK_SIZE); offset = 0; strcpy(athlete, "Periklis Iakovakis"); memcpy(buffer + offset, athlete, ATHLETE_SIZE); offset += ATHLETE_SIZE; strcpy(athlete, "Pyrros Dimas"); memcpy(buffer + offset, athlete, ATHLETE_SIZE); offset += ATHLETE_SIZE; strcpy(athlete, "Helena Isinbayeva"); memcpy(buffer + offset, athlete, ATHLETE_SIZE); offset += ATHLETE_SIZE; strcpy(athlete, "Blanka Vlasic"); memcpy(buffer + offset, athlete, ATHLETE_SIZE); // Grapsimo aytou tou block sto arxeio sti thesi blockNumInFile fp = finfo.fp; blockNumInFile1 = 5; printf("Writing block to disk...\n\n"); if ((rvalue = HDF_WriteBlock(blockNumInFile1, buffer, fp)) != BFE_OK) { printf("Checkpoint 10:"); printf("HDF_WriteBlock() returned %d!\n\n",rvalue); } // Enimerwsi tis epikefalidas tou arxeiou oti to block ayto einai pleon egkyro blockNumInHeader1 = blockNumInFile1 - 1; // PROSOXH SE AYTO!!! printf("Updating header...\n\n"); if ((rvalue = FMM_HEAD_Set(fd, blockNumInHeader1, TRUE)) != BFE_OK) { printf("Checkpoint 11:"); printf("FMM_HEAD_Set() returned %d!\n\n",rvalue); } // Elegxos an enimerwthike i epikefalida printf("Checking header...\n\n"); if (FMM_HEAD_IsValid(fd, blockNumInHeader1) == FALSE) { printf("Checkpoint 12:"); printf("FMM_HEAD_isValid() returned %d!\n\n",rvalue); } // Anaktisi tou prwtou egkyrou block tou arxeiou printf("Retrieving first valid block...\n\n"); if ((firstValid = FMM_HEAD_GetFirstBlock(fd)) == BFE_EOF) { printf("Checkpoint 13:"); printf("FMM_HEAD_GetFirstBlock returned %d!\n\n",rvalue); } printf("firstValid = %d, it should be equal to %d\n\n", firstValid, blockNumInHeader1); // Dimiourgia enos 2ou neou block sti mnimi memset(buffer, 0, BF_BLOCK_SIZE); offset = 0; strcpy(athlete, "Vasilis Spanoulis"); memcpy(buffer + offset, athlete, ATHLETE_SIZE); offset += ATHLETE_SIZE; strcpy(athlete, "Sofoklis Sxortsianitis"); memcpy(buffer + offset, athlete, ATHLETE_SIZE); // Grapsimo aytou tou block sto arxeio sti thesi blockNumInFile blockNumInFile2 = 3; printf("Writing block to disk...\n\n"); if ((rvalue = HDF_WriteBlock(blockNumInFile2, buffer, fp)) != BFE_OK) { printf("Checkpoint 14:"); printf("HDF_WriteBlock() returned %d!\n\n",rvalue); } // Enimerwsi tis epikefalidas tou arxeiou oti to block ayto einai pleon egkyro blockNumInHeader2 = blockNumInFile2 - 1; // PROSOXH SE AYTO!!! printf("Updating header...\n\n"); if ((rvalue = FMM_HEAD_Set(fd, blockNumInHeader2, TRUE)) != BFE_OK) { printf("Checkpoint 15:"); printf("FMM_HEAD_Set() returned %d!\n\n",rvalue); } // Anaktisi tou prwtou egkyrou block tou arxeiou printf("Retrieving first valid block...\n\n"); if ((firstValid = FMM_HEAD_GetFirstBlock(fd)) == BFE_EOF) { printf("Checkpoint 16:"); printf("FMM_HEAD_GetFirstBlock returned %d!\n\n",rvalue); } printf("firstValid = %d, it should be equal to %d\n\n", firstValid, blockNumInHeader2); // Anaktisi tou prwtou egkyrou block tou arxeiou meta apo to firstValid printf("Retrieving first valid block after block %d...\n\n", firstValid); if ((secondValid = FMM_HEAD_GetNextBlock(fd, firstValid)) == BFE_EOF) { printf("Checkpoint 17:"); printf("FMM_HEAD_GetFirstBlock returned %d!\n\n",rvalue); } printf("secondValid = %d, it should be equal to %d\n\n", secondValid, blockNumInHeader1); // Kleisimo arxeiou printf("Closing file %s...\n\n", fileName); if ((rvalue = FMM_Close(fd)) != BFE_OK) { printf("Checkpoint 19:\n"); printf("FMM_Close() returned %d!\n\n", rvalue); } free(buffer); return 0; }
int writeIconGif(char *fn, VirtualFile *src) { uint8 hdr[13]={0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x20, 0x00, 0x20, 0x00, 0xB3, 0x00, 0x00}; uint8 ani[19]={0x21, 0xF9, 0x04, 0x00, 0x33, 0x00, 0x07, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x20, 0x00, 0x00, 0x04}; uint8 web[19]={0x21, 0xFF, 0x0B, 0x4E, 0x45, 0x54, 0x53, 0x43, 0x41, 0x50, 0x45, 0x32, 0x2E, 0x30, 0x03, 0x01, 0x00, 0x00, 0x00}; char fname[17]; uint8 x, y, z; uint8 tmp[1024]; uint8 *icon; uint16 *pal; file_t f; GifSave *gif; DreamcastFile *df; printf(" [+] Reading %s ... ", src->getFileName()); if (src->readData() != 0) { printf(" [-] Source file %s could not be readed.\n", src->getFileName()); return(-1); } printf("OK!\n"); df=src->getDCFile(); printFileInfo(df); if (fn == NULL) { sprintf(fname, "%s.GIF", df->getName()); } else { strcpy(fname, fn); } printf(" [+] Writing %s ... ", fname); if (!(f=fs_open(fname, "wb"))) { printf("ERROR: Can't open %s!\n", fname); printf(" [-] Destination file %s could not be written.\n", fname); return(-2); } fs_write(f, hdr, 13); pal=df->getIconPalette(); for (x=0; x<16; x++) { tmp[x*3]=((pal[x] & 0x0f00) >> 8) *17; tmp[x*3 + 1]=((pal[x] & 0x00f0) >> 4) *17; tmp[x*3 + 2]=(pal[x] & 0x000f) *17; } fs_write(f, tmp, 48); fs_write(f, web, 19); ani[4]=df->getAnimationSpeed()*4; gif=new GifSave(); for (z=0; z<df->getIcons(); z++) { icon=df->getIconBitmap(z); fs_write(f, ani, 19); for (y=0; y<32; y++) { for (x=0; x<32; x+=2) { tmp[y*32 + x]=(icon[y*16 + x/2] & 0xf0) >> 4; tmp[y*32 + x+1]=(icon[y*16 + x/2] & 0x0f); } } gif->LZW_Compress(4, tmp, 1024, f); fs_write(f, "\0\0", 2); } delete gif; fs_write(f, ";", 1); fs_close(f); printf("OK!\n"); return(0); }
bool pushfile(const char *filename, unsigned int *sock) { char buffer[BLOCK], msg[MSGSIZE] = ""; int size = fileSize(filename); int nread, nwrite, tot=size; int file; char *sha = shaDigest(filename); int columns=0; struct timeval start; struct timeval stop; double difftime=0; if ( (file = open(filename, O_RDONLY)) < 0) { perrorf("open()",0); sendCommand(CMD_ABORT, NULL, sock); free(sha); return false; } printFileInfo(filename,size,sha); sprintf(msg,"filesize:%d sha:%s", size, sha); sendCommand(CMD_FILE_INFO, (char *)msg, sock); if (recvCommand(NULL, sock) != CMD_ACK) { if(sha) free(sha); return false; } if (size<=0) { if(sha) free(sha); return false; } gettimeofday(&start,NULL); while(1) { memset(buffer, 0, BLOCK); if ((nread=read(file, buffer, BLOCK)) < 0 ) { perrorf("read()",0); if (sha) free(sha); return false; } if ((nwrite=write(*sock, buffer, nread)) < 0) perrorf("write()",0); tot-=nwrite; if (columns > 0) stepProgressBar(size-tot,columns,size); if (tot <= 0) break; } gettimeofday(&stop,NULL); if (recvCommand(NULL, sock) != CMD_ACK) printf("* send of \"%s\" failed\n",filename); else { difftime=timeval_diff(&stop,&start); printf("* time: %f seconds, ",difftime); num2human((long double)(size/difftime)); printf("B/s\n"); printf("* result: sending \"%s\" completed successfully\n",filename); } if (sha) free(sha); close(file); return true; }
bool pullfile(const char *filename, unsigned int *sock) { char buffer[BLOCK]; int size, nread=0, nwrite=0, tot=0, file; mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; char *sha=NULL, recv_sha[41] = ""; int columns=0; struct timeval start; struct timeval stop; double difftime=0; if ((file=open(filename, O_WRONLY | O_CREAT, mode)) < 0) { makeSubDir((char*)filename); remove(filename); if ((file=open(filename, O_WRONLY | O_CREAT, mode))<0) { perrorf("open()",0); return false; } } if (recvCommand(buffer, sock) != CMD_FILE_INFO) { printf("* Can't get \"%s\". Permission or I/O error \n\n",filename); close(file); return false; } sscanf(buffer,"filesize:%d sha:%s",&size,recv_sha); printFileInfo(filename,size,recv_sha); sendCommand(CMD_ACK, NULL, sock); if (size==0) { close(file); return true; } if (size<0) { if (remove(filename) < 0) perrorf( "remove()",0); close(file); return false; } gettimeofday(&start,NULL); while(1) { memset(buffer, 0, BLOCK); if ((nread=read(*sock, buffer, BLOCK)) < 0) { perrorf("read()",0); return false; } if ((nwrite=write(file, buffer, nread)) < 0 ) perrorf(" write()",0); tot+=nread; if (columns > 0) stepProgressBar(tot,10,size); if (tot == size) break; } gettimeofday(&stop,NULL); difftime=timeval_diff(&stop,&start); printf("* time: %f seconds, ",difftime); num2human((long double)(tot/difftime)); printf("B/s\n"); sha = shaDigest(filename); if (strcmp(sha, recv_sha) == 0) { printf("* result: digest of %s is correct\n",filename); sendCommand(CMD_ACK, NULL, sock); } else { printf("* result: digest of %s is wrong\n",filename); sendCommand(CMD_ABORT, NULL, sock); } printf("\n"); if (sha) free(sha); close(file); return true; }