int makedir (const QString& newdir) { QChar p; QString buffer; int len = newdir.length(); if (len <= 0) return 0; buffer = newdir; if (buffer.endsWith("/") || buffer.endsWith("\\")) buffer.left(len - 1); if (mymkdir(buffer) == 0) return 1; uint cIndex = 1; p = buffer[1]; while (1) { QString tmpBuf; while(!p.isNull() && p != '\\' && p != '/') p = buffer[cIndex++]; tmpBuf = buffer.left(cIndex - 1); if ((mymkdir(tmpBuf) == -1) && (errno == ENOENT)) return 0; if (p.isNull()) break; p = buffer[++cIndex]; } return 1; }
int makedir ( const char *newdir) { char *buffer ; char *p; int len = (int)strlen(newdir); if (len <= 0) return 0; buffer = (char*)malloc(len+1); if (buffer==NULL) { printf("Error allocating memory\n"); return UNZ_INTERNALERROR; } strcpy(buffer,newdir); if (buffer[len-1] == '/') { buffer[len-1] = '\0'; } if (mymkdir(buffer) == 0) { free(buffer); return 1; } p = buffer+1; while (1) { char hold; while(*p && *p != '\\' && *p != '/') p++; hold = *p; *p = 0; if ((mymkdir(buffer) == -1) && (errno == ENOENT)) { printf("couldn't create directory %s\n",buffer); free(buffer); return 0; } if (hold == 0) break; *p++ = hold; } free(buffer); return 1; }
static void sample_tile(texgz_tex_t* src, texgz_tex_t* dst, int month, int x, int y, double latT, double lonL, double latB, double lonR) { assert(src); assert(dst); int m; int n; for(m = 0; m < SUBTILE_SIZE; ++m) { for(n = 0; n < SUBTILE_SIZE; ++n) { sample_data(src, dst, x, y, m, n, latT, lonL, latB, lonR); } } // export the tile char fname[256]; snprintf(fname, 256, "png256/%i/9/%i/%i.png", month, x, y); fname[255] = '\0'; mymkdir(fname); texgz_png_export(dst, fname); }
/** The main(). It parses the command line, setup the parms, ask the scheduler for signal to proceed, and then starts skysim to do sky coverage. */ int main(int argc, const char *argv[]){ dirstart=mygetcwd(); char *scmd=argv2str(argc, argv, " "); ARG_S* arg=parse_args(argc,argv); /*In detach mode send to background and disable drawing*/ if(arg->detach){ daemonize(); }else{ redirect(); } info2("%s\n", scmd); info2("Output folder is '%s'. %d threads\n",arg->dirout, arg->nthread); skyc_version(); /*register signal handler */ register_signal_handler(skyc_signal_handler); /* Ask job scheduler for permission to proceed. If no CPUs are available, will block until ones are available. if arg->force==1, will run immediately. */ scheduler_start(scmd,arg->nthread,0,!arg->force); /*setting up parameters before asking scheduler to check for any errors. */ dirsetup=stradd("setup",NULL); PARMS_S * parms=setup_parms(arg); if(parms->skyc.dbg){ mymkdir("%s",dirsetup); } if(!arg->force){ info2("Waiting start signal from the scheduler ...\n"); /*Failed to wait. fall back to own checking.*/ int count=0; while(scheduler_wait()&& count<60){ warning_time("failed to get reply from scheduler. retry\n"); sleep(10); count++; scheduler_start(scmd,arg->nthread,0,!arg->force); } if(count>=60){ warning_time("fall back to own checker\n"); wait_cpu(arg->nthread); } } info2("Simulation started at %s in %s.\n",myasctime(),myhostname()); free(scmd); free(arg->dirout); free(arg); THREAD_POOL_INIT(parms->skyc.nthread); /*Loads the main software*/ OMPTASK_SINGLE skysim(parms); free_parms(parms); free(dirsetup); free(dirstart); rename_file(0); scheduler_finish(0); info2("End:\t%.2f MiB\n",get_job_mem()/1024.); info2("Simulation finished at %s in %s.\n",myasctime(),myhostname()); return 0; }
int makedir (const char *newdir, int (* printErr)(const char *errmsg, ...) ) { char *buffer ; char *p; int len = strlen(newdir); if (len <= 0) return 0; buffer = (char*)malloc(len+1); strcpy(buffer,newdir); if (buffer[len-1] == '/') { buffer[len-1] = '\0'; } if (mymkdir(buffer) == 0) { free(buffer); return 1; } p = buffer+1; while (1) { char hold; while(*p && *p != '\\' && *p != '/') p++; hold = *p; *p = 0; if ((mymkdir(buffer) == -1) && (errno == ENOENT)) { printErr("couldn't create directory %s\n",buffer); free(buffer); return 0; } if (hold == 0) break; *p++ = hold; } free(buffer); return 1; }
int makedir (char *newdir) { char *buffer ; char *p; int len = (int)strlen(newdir); if (len <= 0) return 0; buffer = (char*)malloc(len+1); strcpy(buffer,newdir); if (buffer[len-1] == '/') { buffer[len-1] = '\0'; } if (mymkdir(buffer) == 0) { free(buffer); return 1; } p = buffer+1; while (1) { char hold; while(*p && *p != '\\' && *p != '/') p++; hold = *p; *p = 0; if ((mymkdir(buffer) == -1) && (errno == ENOENT)) { free(buffer); return 0; } if (hold == 0) break; *p++ = hold; } free(buffer); return 1; }
static int makedir( const wchar_t *newdir ) { wchar_t *buffer ; wchar_t *p; int len = (int)wcslen(newdir); if (len <= 0) return 0; buffer = new wchar_t[len+1]; wcscpy(buffer, newdir); if (buffer[len-1] == '/') { buffer[len-1] = '\0'; } if (mymkdir(buffer) == 0) { delete[] buffer; return 1; } p = buffer+1; while (1) { char hold; while(*p && *p != '\\' && *p != '/') p++; hold = *p; *p = 0; if ((mymkdir(buffer) == -1) && !NSDirectory::Exists(buffer)) { delete[] buffer; return 0; } if (hold == 0) break; *p++ = hold; } delete[] buffer; return 1; }
int bb_mkdir(const char *path, mode_t mode) { int retstat = 0; log_msg("\nbb_mkdir(path=\"%s\", mode=0%3o)\n", path, mode); retstat = mymkdir(path); log_msg("mkdir returned: %d\n", retstat); return retstat; }
int makedir (const char *path, int (* printErr)(const char *errmsg, ...) ) { char *complete; char *temp; int len; complete = malloc(strlen(path) + 2); strcpy(complete, path); /* normalize directory separators */ temp = complete; while ((temp = strchr(temp, '\\')) != NULL) *temp = '/'; /* assume any path given is a directory so add '/' to end if missing */ len = strlen(complete); if (complete[len-1] != '/') { complete[len] = '/'; complete[len+1] = '\0'; } /* don't try to create root dir */ if (*complete == '/') temp = complete + 1; /* point to 1st char after root */ else if (*(complete+1) == ':') { if (*(complete+2) == '/') temp = complete + 3; /* point to after drive & root */ else temp = complete + 2; /* point to after drive (relative) */ } else temp = complete; /* point to start of relative path */ /* make each segment of path (slash '/' separated) */ while ((temp = strchr(temp, '/')) != NULL) { *temp = '\0'; if ((mymkdir(complete) == -1) && (errno == ENOENT)) { printErr("couldn't create directory %s\n",complete); free(complete); return 0; } *temp = '/'; temp++; } free(complete); return 1; }
static int mkdir_p (char *s) { int rc = 0; for (char *p = s; rc == 0 && p && *p; ) { p = strchr (p, '/'); if (p && *p) { rc = mymkdir (s, p); p++; } } if (rc != 0) { warn ("cannot create file %s due to mkdir failures\n", s); } else if (!s || !*s) { warn ("no file to write!\n"); rc = -1; } return rc; }
main(int argc, char *argv[ ]) { //*Lab 5 CODE char *tok; char cmdArgs[2][MAX] = {'\0'}; char cwd[128]; int cmdCount = 0; //LAB 5 CODE* char temp[MAX]; int n; char line[MAX], ans[MAX]; //*Lab 5 CODE getcwd(cwd, 128); // get CWD pathname // Lab 5 CODE* printf("cwd: %s\n", cwd); if (argc < 3){ printf("Usage : client ServerName ServerPort\n"); exit(1); } client_init(argv); // sock <---> server printf("******** processing loop *********\n"); while (1){ //printf("input two numbers to add : "); //bzero(line, MAX); // zero out line[ ] // *LAB 5 CODE cmdCount = 0; strcpy(cmdArgs[0],""); strcpy(cmdArgs[1],""); printf("Input a command: "); // LAB 5 CODE* fgets(line, MAX, stdin); // get a line (end with \n) from stdin //printf("line: %s\n", line); line[strlen(line)-1] = 0; // kill \n at end //printf("line[0]: %c\n", line[0]); //if(line[0]==0) // exit if NULL line //{exit(0);} // *Lab 5 CODE strcpy(temp, line); //printf("temp: %s\n", temp); tok = strtok(line, " "); cmdCount++; //printf("temp: %s\n", temp); //printf("cmdArgs[0] = %s\n", cmdArgs[0]); //printf("cmdCount: %d\n", cmdCount); while(tok != NULL) { strcpy(cmdArgs[cmdCount-1], tok); tok = strtok(NULL, " "); cmdCount++; //printf("cmdCount: %d\n", cmdCount); } //printf("cmdCount: %d\n", cmdCount); //printf("line: %s\n", line); //printf("cmdArgs[0] = %s\n", cmdArgs[0]); if(strcmp(cmdArgs[0], "lcat") == 0) { if(strcmp(cmdArgs[1],"") != 0) {mycat(cmdArgs[1]);} else {printf("ERROR: pathname not given");} } else if (strcmp(cmdArgs[0], "lpwd") == 0) { mypwd(cwd); } else if (strcmp(cmdArgs[0], "lls") == 0) { printf("args: %s\n", cmdArgs[1]); if(strcmp(cmdArgs[1],"") != 0) {myls(cmdArgs[1]);} else { printf("cwd: %s\n", cwd); myls(cwd); } } else if (strcmp(cmdArgs[0], "lcd") == 0) { if(strcmp(cmdArgs[1],"") != 0) {mycd(cmdArgs[1]);} else {mycd(cwd);} } else if (strcmp(cmdArgs[0], "lmkdir") == 0) { if(strcmp(cmdArgs[1],"") != 0) {mymkdir(cmdArgs[1]);} else {printf("ERROR: pathname not given");} } else if (strcmp(cmdArgs[0], "lrmdir") == 0) { if(strcmp(cmdArgs[1],"") != 0) {myrmdir(cmdArgs[1]);} else {printf("ERROR: pathname not given");} } else if (strcmp(cmdArgs[0], "lrm") == 0) { if(strcmp(cmdArgs[1],"") != 0) {myrm(cmdArgs[1]);} else {printf("ERROR: pathname not given");} } else if (strcmp(cmdArgs[0], "get") == 0) { if(strcmp(cmdArgs[1],"") != 0) {mycp(cmdArgs[1], cwd);} else {printf("ERROR: pathname not given");} } else if (strcmp(cmdArgs[0], "quit") == 0) { printf("Goodbye!\n"); return 0; } else // Not a local command, work with the sever { // Send command to server n = write(server_sock, temp, MAX); printf("client: wrote n=%d bytes; line=(%s)\n", n, temp); // Send command pathname //n = write(server_sock, cmdArgs[0], MAX); //printf("client: wrote n=%d bytes; line=(%s)\n", n, line); // Now read the Reply and Results in from the Server... //REPLY //n = read(server_sock, ans, MAX); //printf("client: read n=%d bytes; echo=(%s)\n",n, ans); //RESULTS //n = read(server_sock, ans, MAX); //printf("client: read n=%d bytes; echo=(%s)\n",n, ans); readData(ans); } // Lab 5 CODE* // // Send ENTIRE line to server //n = write(server_sock, line, MAX); //printf("client: wrote n=%d bytes; line=(%s)\n", n, line); // Read a line from sock and show it //n = read(server_sock, ans, MAX); //printf("client: read n=%d bytes; echo=(%s)\n",n, ans); } }
int do_extract_currentfile(unzFile uf, char * revisionRoot) { char filename_inzip[256]; char* filename_withoutpath; char* p; int err=UNZ_OK; FILE *fout=NULL; void* buf; uInt size_buf; unz_file_info64 file_info; err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0); if (err!=UNZ_OK) { printf("error %d with zipfile in unzGetCurrentFileInfo\n",err); return err; } size_buf = WRITEBUFFERSIZE; buf = (void*)malloc(size_buf); if (buf==NULL) { printf("Error allocating memory\n"); return UNZ_INTERNALERROR; } p = filename_withoutpath = filename_inzip; while ((*p) != '\0') { if (((*p)=='/') || ((*p)=='\\')) filename_withoutpath = p+1; p++; } if ((*filename_withoutpath)=='\0') { char * dir; dir = (char *)malloc(strlen(revisionRoot) + strlen(filename_inzip) + 2); strcpy(dir, revisionRoot); strcat(dir, "/"); strcat(dir, filename_inzip); mymkdir(dir); free(dir); } else { const char* write_filename; int skip=0; int length; char * fWFN; write_filename = filename_inzip; length = strlen(write_filename) + strlen(revisionRoot) + 2; fWFN = (char *)malloc(length); strcpy(fWFN, revisionRoot); strcat(fWFN, "/"); strcat(fWFN, write_filename); err = unzOpenCurrentFile(uf); if (err!=UNZ_OK) { printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err); } if ((skip==0) && (err==UNZ_OK)) { fout=fopen64(fWFN,"wb"); /* some zipfile don't contain directory alone before file */ if ((fout==NULL) && (filename_withoutpath!=(char*)filename_inzip)) { char * dir; int length; char c=*(filename_withoutpath-1); *(filename_withoutpath-1)='\0'; length = strlen(write_filename) + strlen(revisionRoot) + 2; dir = (char *)malloc(length); strcpy(dir, revisionRoot); strcat(dir, "/"); strcat(dir, write_filename); makedir(dir); *(filename_withoutpath-1)=c; free(dir); fout=fopen64(fWFN,"wb"); } if (fout==NULL) { printf("error opening %s\n",write_filename); } } if (fout!=NULL) { do { err = unzReadCurrentFile(uf,buf,size_buf); if (err<0) { printf("error %d with zipfile in unzReadCurrentFile\n",err); break; } if (err>0) if (fwrite(buf,err,1,fout)!=1) { printf("error in writing extracted file\n"); err=UNZ_ERRNO; break; } } while (err>0); if (fout) fclose(fout); if (err==0) change_file_date(fWFN,file_info.dosDate, file_info.tmu_date); } if (err==UNZ_OK) { err = unzCloseCurrentFile (uf); if (err!=UNZ_OK) { printf("error %d with zipfile in unzCloseCurrentFile\n",err); } } else unzCloseCurrentFile(uf); /* don't lose the error */ free(fWFN); } free(buf); return err; }
static int do_extract_currentfile(unzFile uf,const int* popt_extract_without_path,int* popt_overwrite,const char* password) { char filename_inzip[256]; char* filename_withoutpath; char* p; int err=UNZ_OK; int res=FR_NO_FILE; void* buf; uInt size_buf; FIL f; unz_file_info file_info; err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0); if (err!=UNZ_OK) { printf("error %d with zipfile in unzGetCurrentFileInfo\n",err); return err; } size_buf = WRITEBUFFERSIZE; buf = (void*)malloc(size_buf); if (buf==NULL) { printf("Error allocating memory\n"); return UNZ_INTERNALERROR; } p = filename_withoutpath = filename_inzip; while ((*p) != '\0') { if (((*p)=='/') || ((*p)=='\\')) filename_withoutpath = p+1; p++; } if ((*filename_withoutpath)=='\0') { if ((*popt_extract_without_path)==0) { printf("creating directory: %s\n",filename_inzip); mymkdir(filename_inzip); } } else { char* write_filename; int skip=0; if ((*popt_extract_without_path)==0) write_filename = filename_inzip; else write_filename = filename_withoutpath; err = unzOpenCurrentFilePassword(uf,password); if (err!=UNZ_OK) { printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err); } if ((skip==0) && (err==UNZ_OK)) { res = f_open_char(&f,write_filename,FA_WRITE|FA_CREATE_ALWAYS); /* some zipfile don't contain directory alone before file */ if ((res != FR_OK) && ((*popt_extract_without_path)==0) && (filename_withoutpath!=(char*)filename_inzip)) { char c=*(filename_withoutpath-1); *(filename_withoutpath-1)='\0'; makedir(write_filename); *(filename_withoutpath-1)=c; res = f_open_char(&f,write_filename,FA_WRITE|FA_CREATE_ALWAYS); } if (res != FR_OK) { printf("error opening %s\n",write_filename); } } if (res == FR_OK) { printf(" extracting: %s\n",write_filename); do { err = unzReadCurrentFile(uf,buf,size_buf); if (err<0) { printf("error %d with zipfile in unzReadCurrentFile\n",err); break; } if (err>0) { UINT wrote; if (f_write(&f,buf,err,&wrote)!=FR_OK) { printf("error in writing extracted file\n"); err=UNZ_ERRNO; break; } } } while (err>0); if (res == FR_OK) { f_close(&f); res = FR_NO_FILE; } } if (err==UNZ_OK) { err = unzCloseCurrentFile (uf); if (err!=UNZ_OK) { printf("error %d with zipfile in unzCloseCurrentFile\n",err); } } else unzCloseCurrentFile(uf); /* don't lose the error */ } free(buf); return err; }
static int do_extract_currentfile(unzFile uf,const int* popt_extract_without_path,int* popt_overwrite,const char* password) { char filename_inzip[256]; char* filename_withoutpath; char* p; int err=UNZ_OK; FILE *fout=NULL; void* buf; uInt size_buf; unz_file_info file_info; err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0); if (err!=UNZ_OK) { //printf("error %d with zipfile in unzGetCurrentFileInfo\n",err); return err; } size_buf = WRITEBUFFERSIZE; buf = (void*)malloc(size_buf); if (buf==NULL) { //printf("Error allocating memory\n"); return UNZ_INTERNALERROR; } p = filename_withoutpath = filename_inzip; while ((*p) != '\0') { if (((*p)=='/') || ((*p)=='\\')) filename_withoutpath = p+1; p++; } if ((*filename_withoutpath)=='\0') { if ((*popt_extract_without_path)==0) { //printf("creating directory: %s\n",filename_inzip); mymkdir(filename_inzip); } } else { char* write_filename; int skip=0; if ((*popt_extract_without_path)==0) write_filename = filename_inzip; else write_filename = filename_withoutpath; err = unzOpenCurrentFilePassword(uf,password); if (err!=UNZ_OK) { //printf("error %d with zipfile in unzOpenCurrentFilePassword\n",err); } if (((*popt_overwrite)==0) && (err==UNZ_OK)) { char rep=0; FILE* ftestexist; ftestexist = fopen(write_filename,"rb"); if (ftestexist!=NULL) { fclose(ftestexist); do { char answer[128]; int ret; printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename); ret = scanf("%1s",answer); if (ret != 1) { exit(EXIT_FAILURE); } rep = answer[0] ; if ((rep>='a') && (rep<='z')) rep -= 0x20; } while ((rep!='Y') && (rep!='N') && (rep!='A')); } if (rep == 'N') skip = 1; if (rep == 'A') *popt_overwrite=1; } if ((skip==0) && (err==UNZ_OK)) { fout=fopen(write_filename,"wb"); /* some zipfile don't contain directory alone before file */ if ((fout==NULL) && ((*popt_extract_without_path)==0) && (filename_withoutpath!=(char*)filename_inzip)) { char c=*(filename_withoutpath-1); *(filename_withoutpath-1)='\0'; makedir(write_filename); *(filename_withoutpath-1)=c; fout=fopen(write_filename,"wb"); } if (fout==NULL) { //printf("error opening %s\n",write_filename); } } if (fout!=NULL) { //printf(" extracting: %s\n",write_filename); do { err = unzReadCurrentFile(uf,buf,size_buf); if (err<0) { //printf("error %d with zipfile in unzReadCurrentFile\n",err); break; } if (err>0) if (fwrite(buf,err,1,fout)!=1) { //printf("error in writing extracted file\n"); err=UNZ_ERRNO; break; } total_unzipped += size_buf; } while (err>0); if (fout) fclose(fout); } if (err==UNZ_OK) { err = unzCloseCurrentFile (uf); if (err!=UNZ_OK) { //printf("error %d with zipfile in unzCloseCurrentFile\n",err); } } else unzCloseCurrentFile(uf); /* don't lose the error */ } free(buf); return err; }
int callInternal(command* C, char* homeDir) { if (C->argc == 0) return 0; else if (strcmp(C->argv[0],"exit") == 0) return 1; else if (strcmp(C->argv[0],"mycat") == 0) { if (C->argc == 2) mycat(C->argv[1]); else if (C->argc == 1) printf("Argument missing\n"); else if (C->argc > 2) printf("Incorrect usage of mycat\n"); } else if (strcmp(C->argv[0],"mycp") == 0) { if (C->argc == 3) mycp(C->argv[1],C->argv[2]); else if (C->argc == 2) printf("Destination needed\n"); else if (C->argc == 1) printf("Source & destination needed\n"); else printf("Incorrect usage of mycp\n"); } else if (strcmp(C->argv[0],"mypwd") == 0) { if (C->argc > 1) printf("Incorrect usage of mypwd\n"); else { char* pwd = mypwd(); printf("%s\n",pwd); free(pwd); } } else if (strcmp(C->argv[0],"mymkdir") == 0) { if (C->argc < 2) printf("missing argument\n"); else if (C->argc > 2) printf("Incorrect usage of mymkdir\n"); else mymkdir(C->argv[1]); } else if (strcmp(C->argv[0],"myrmdir") == 0) { if (C->argc < 2) printf("missing argument\n"); else if (C->argc > 2) printf("Incorrect usage of mymkdir\n"); else myrmdir(C->argv[1]); } else if (strcmp(C->argv[0],"mycd") == 0) { if (C->argc >2) printf("Incorrect usage of mycd\n"); else if (C->argc == 2) mycd(C->argv[1]); else mycd(homeDir); } else if (strcmp(C->argv[0],"myls") == 0) { if (C->argc == 1) myls(NULL,0); else if (C->argc == 2) if (strcmp(C->argv[1],"-l") == 0) myls(NULL,1); else myls(C->argv[1],0); else if (C->argc == 3) if (strcmp(C->argv[1],"-l") == 0) myls(C->argv[2],1); else printf("Incorrect usage of myls\n"); else printf("Incorrect usage of myls\n"); } else if (strcmp(C->argv[0],"myrm") == 0) { if (C->argc == 2) myrm(C->argv[1]); else if (C->argc == 1) printf("missing argument\n"); else printf("Incorrect usage of myrm\n"); } else printf("command not found: %s\n",C->argv[0]); return 0; }
int do_extract_currentfile(unzFile uf, const int* popt_extract_without_path, int*, const char* password) { char fn_inzip[256]; QString filename_inzip; QString filename_withoutpath; int err=UNZ_OK; FILE *fout=NULL; void* buf; uInt size_buf; unz_file_info file_info; //uLong ratio=0; err = unzGetCurrentFileInfo(uf,&file_info,fn_inzip,sizeof(fn_inzip),NULL,0,NULL,0); if (err!=UNZ_OK) return err; size_buf = WRITEBUFFERSIZE; buf = (void*)malloc(size_buf); if (buf==NULL) return UNZ_INTERNALERROR; QChar p; // uint cIndex = 0; filename_inzip = QString::fromLocal8Bit(fn_inzip); filename_withoutpath = filename_inzip; for (int i = 0; i < filename_inzip.length(); i++) { p = filename_inzip[i]; if( (p == '/') || (p =='\\')) filename_withoutpath = filename_inzip.mid(i+1); } if (filename_withoutpath.isEmpty()) { if ((*popt_extract_without_path)==0) mymkdir(filename_inzip); } else { QString write_filename; int skip=0; if ((*popt_extract_without_path)==0) write_filename = filename_inzip; else write_filename = filename_withoutpath; err = unzOpenCurrentFilePassword(uf,password); if ((skip==0) && (err==UNZ_OK)) { fout = openfile(write_filename, "wb"); /* some zipfile don't contain directory alone before file */ if ((fout==NULL) && ((*popt_extract_without_path)==0) && (filename_withoutpath != filename_inzip)) { uint len = filename_inzip.length() - filename_withoutpath.length() - 1; QString write_dir = filename_inzip.left(len); makedir(write_dir); fout = openfile(write_filename, "wb"); } } if (fout!=NULL) { do { err = unzReadCurrentFile(uf,buf,size_buf); if (err<0) break; if (err>0) if (fwrite(buf,err,1,fout)!=1) { err=UNZ_ERRNO; break; } } while (err>0); if (fout) fclose(fout); if (err==0) change_file_date(write_filename,file_info.dosDate, file_info.tmu_date); } if (err==UNZ_OK) err = unzCloseCurrentFile (uf); else unzCloseCurrentFile(uf); /* don't lose the error */ } free(buf); return err; }
int main(int argc, char** argv) { int count=0, i=0, cmp, j, valid=0; char key[MAX]; //run the shell till user inputs 'quit' or Ctrl-C while(strcmp(cmdline, "quit") != 0) { count = 0; //our shell's prompt printf("afreen@myShell $ "); //get command line input gets(cmdline); //tokenize the CL input delimited by " " using strtok() into args args[count++] = strtok(cmdline, " "); //tokenize the entire string till the next token encountered //does not have a NULL pointer while((args[count++] = strtok(NULL, " "))); count--; //no of arguments (argc) //if user wants to clear screen if(strcmp(args[0], "clear") == 0) { myclear(); } //checking for the input command strcpy(key, args[0]); for(i=0; i<18; i++) //look through our check array for command { if(strcmp(check[i], key) == 0) { valid = -1; switch(i) { case 0: mycat(count, args); break; case 1: mycd(count, args); break; case 2: mycp(count, args); break; case 3: mydate(count, args); break; case 4: myhead(count, args); break; case 5: myls(count, args); break; case 6: myll(count, args); break; case 7: myln(count, args); break; case 8: myld(count, args); break; case 9: mymkdir(count, args); break; case 10: mymv(count, args); break; case 11: mypwd(count, args); break; case 12: myrm(count, args); break; case 13: myrmdir(count, args); break; case 14: mytail(count, args); break; case 15: mytime(count, args); break; case 16: mytouch(count, args); break; case 17: myuname(count, args); break; default: printf("%s: Command not found\n", key); break; } break; } } } return 0; }
static int do_extract_currentfile( unzFile uf, const int* popt_extract_without_path, int* popt_overwrite, const char* password, ZnkStr ermsg, ZnkZipProgressFuncT progress_func, void* param, size_t num_of_entry, size_t idx_of_entry ) { char filename_inzip[256]; char* filename_withoutpath; /* tail */ char* p; int err=UNZ_OK; FILE* fout=NULL; void* buf; uInt size_buf; char rep_buf[ 512 ] = ""; unz_file_info file_info; err = unzGetCurrentFileInfo( uf, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, /* extraField */ NULL, 0 /* szComment */ ); if( err != UNZ_OK ){ ZnkStr_addf( ermsg, "ZnkZip : Error : %d with zipfile in unzGetCurrentFileInfo\n", err ); return err; } size_buf = WRITEBUFFERSIZE; buf = (void*)malloc(size_buf); if( buf == NULL ){ Znk_printf_e( "Error : allocating memory\n" ); return UNZ_INTERNALERROR; } p = filename_withoutpath = filename_inzip; while( (*p) != '\0' ){ if (((*p)=='/') || ((*p)=='\\')) filename_withoutpath = p+1; p++; } if( (*filename_withoutpath)=='\0' ){ /* tail is empty : thus, this is directory */ if( (*popt_extract_without_path)==0 ){ if( progress_func ){ Znk_snprintf( rep_buf, sizeof(rep_buf), "creating directory: %s", filename_inzip ); (*progress_func)( rep_buf, num_of_entry, idx_of_entry, param ); } mymkdir(filename_inzip); } } else { const char* write_filename; int skip=0; if( (*popt_extract_without_path)==0 ){ write_filename = filename_inzip; } else { write_filename = filename_withoutpath; } err = unzOpenCurrentFilePassword( uf, password ); if( err != UNZ_OK ){ ZnkStr_addf( ermsg, "ZnkZip : Error : %d with zipfile in unzOpenCurrentFilePassword.\n", err ); } #if 0 if( ((*popt_overwrite)==0) && (err==UNZ_OK) ){ char rep=0; FILE* ftestexist; ftestexist = fopen(write_filename,"rb"); if( ftestexist != NULL ){ fclose(ftestexist); do { char answer[128]; int ret; Znk_printf("The file %s exists. Overwrite ? [y]es, [n]o, [A]ll: ",write_filename); ret = scanf("%1s",answer); if (ret != 1) { exit(EXIT_FAILURE); } rep = answer[0] ; if ((rep>='a') && (rep<='z')) rep -= 0x20; } while ((rep!='Y') && (rep!='N') && (rep!='A')); } if (rep == 'N') skip = 1; if (rep == 'A') *popt_overwrite=1; } #else *popt_overwrite = 1; /* all-overwrite */ #endif if( (skip==0) && (err==UNZ_OK) ){ fout = fopen( write_filename, "wb" ); /* some zipfile don't contain directory alone before file */ if( ( fout==NULL ) && ( (*popt_extract_without_path)==0 ) && ( filename_withoutpath != (char*)filename_inzip ) ){ char c = *(filename_withoutpath-1); *(filename_withoutpath-1)='\0'; makedir( write_filename ); *(filename_withoutpath-1)=c; fout = fopen( write_filename, "wb" ); } if( fout == NULL ){ ZnkStr_addf( ermsg, "ZnkZip : Error : opening [%s].\n", write_filename ); } } if( fout != NULL ){ //Znk_printf(" extracting: %s\n",write_filename); if( progress_func ){ Znk_snprintf( rep_buf, sizeof(rep_buf), "extracting: %s", write_filename ); (*progress_func)( rep_buf, num_of_entry, idx_of_entry, param ); } do{ err = unzReadCurrentFile(uf,buf,size_buf); if( err < 0 ){ ZnkStr_addf( ermsg, "ZnkZip : Error : %d with zipfile in unzReadCurrentFile\n", err ); break; } else if( err > 0 ){ if( fwrite(buf,err,1,fout) != 1 ){ ZnkStr_addf( ermsg, "ZnkZip : Error : in writing extracted file\n" ); err=UNZ_ERRNO; break; } } } while( err>0 ); if (fout) fclose(fout); if (err==0){ change_file_date( write_filename, file_info.dosDate, file_info.tmu_date ); } } if( err==UNZ_OK ){ err = unzCloseCurrentFile (uf); if (err!=UNZ_OK){ ZnkStr_addf( ermsg, "ZnkZip : Error : %d with zipfile in unzCloseCurrentFile\n", err ); } } else { unzCloseCurrentFile(uf); /* don't lose the error */ } } free(buf); return err; }
static int do_extract_currentfile( unzFile uf, const int* popt_extract_without_path, int* popt_overwrite, const char* password ) { char filename_inzipA[256]; wchar_t filename_inzip[256]; wchar_t* filename_withoutpath; wchar_t* p; int err=UNZ_OK; NSFile::CFileBinary oFile; FILE *fout=NULL; void* buf; uInt size_buf; unz_file_info file_info; uLong ratio=0; err = unzGetCurrentFileInfo(uf,&file_info,filename_inzipA,sizeof(filename_inzipA),NULL,0,NULL,0); std::wstring filenameW = codepage_issue_fixFromOEM(filename_inzipA); wcscpy(filename_inzip , filenameW.c_str()); if (err!=UNZ_OK) { return err; } size_buf = WRITEBUFFERSIZE; buf = (void*)malloc(size_buf); if (buf==NULL) { return UNZ_INTERNALERROR; } p = filename_withoutpath = filename_inzip; while ((*p) != '\0') { if (((*p)=='/') || ((*p)=='\\')) filename_withoutpath = p+1; p++; } if ((*filename_withoutpath)=='\0') { if ((*popt_extract_without_path)==0) { mymkdir(filename_inzip); } } else { const wchar_t* write_filename; int skip=0; if ((*popt_extract_without_path)==0) write_filename = filename_inzip; else write_filename = filename_withoutpath; err = unzOpenCurrentFilePassword(uf,password); if (((*popt_overwrite)==0) && (err==UNZ_OK)) { char rep=0; NSFile::CFileBinary oFileTemp; if (oFileTemp.OpenFile(write_filename)) { oFileTemp.CloseFile(); } if (rep == 'N') skip = 1; if (rep == 'A') *popt_overwrite=1; } if ((skip==0) && (err==UNZ_OK)) { if(oFile.CreateFileW(write_filename)) fout = oFile.GetFileNative(); // some zipfile don't contain directory alone before file if ((fout==NULL) && ((*popt_extract_without_path)==0) && (filename_withoutpath!=(wchar_t*)filename_inzip)) { char c=*(filename_withoutpath-1); *(filename_withoutpath-1)='\0'; makedir(write_filename); *(filename_withoutpath-1)=c; if(oFile.CreateFileW(write_filename)) fout = oFile.GetFileNative(); } } if (fout!=NULL) { do { err = unzReadCurrentFile(uf, buf, size_buf); if (err<0) { break; } if (err>0) if (fwrite(buf,err,1,fout)!=1) { err=UNZ_ERRNO; break; } } while (err>0); //close вызовется в oFile //if (fout) // fclose(fout); if (err==0) change_file_date(write_filename,file_info.dosDate, file_info.tmu_date); } if (err==UNZ_OK) { err = unzCloseCurrentFile (uf); } else unzCloseCurrentFile(uf); // don't lose the error } free(buf); return err; }
/** Save NGS WFS and other information for later use in MAOS simulations.*/ void skysim_save(const SIM_S *simu, const ASTER_S *aster, const double *ipres, int selaster, int seldtrat, int isky){ const PARMS_S* parms=simu->parms; const int nwvl=parms->maos.nwvl; char path[PATH_MAX]; snprintf(path,PATH_MAX,"Res%d_%d_maos/sky%d",simu->seed_maos,parms->skyc.seed,isky); mymkdir("%s",path); for(int iwfs=0; iwfs<aster[selaster].nwfs; iwfs++){ dcell *sepsf=dcelldup(aster[selaster].wfs[iwfs].pistat->psf); for(int ic=0; ic<sepsf->nx*sepsf->ny; ic++){ dfftshift(sepsf->p[ic]);/*put peak in center. required by MAOS. */ } writebin(sepsf, "%s/pistat_wfs%d",path,iwfs+6); dcellfree(sepsf); writebin(aster->wfs[iwfs].pistat->sanea->p[seldtrat], "%s/nea_tot_wfs%d",path,iwfs+6); writebin(aster[selaster].wfs[iwfs].pistat->sanea->p[seldtrat], "%s/nea_wfs%d",path,iwfs+6); writebin(aster[selaster].wfs[iwfs].pistat->sanea, "%s/neafull_wfs%d",path,iwfs+6); } writebin(aster[selaster].gain->p[seldtrat], "%s/gain",path); writebin(simu->mres->p[isky], "%s/mres",path); writebin(simu->psd_tt,"%s/psd_tt",path); writebin(simu->psd_ps,"%s/psd_ps",path); char fnconf[PATH_MAX]; snprintf(fnconf,PATH_MAX,"%s/base.conf",path); FILE *fp=fopen(fnconf,"w"); fprintf(fp,"sim.seeds=[%d]\n",simu->seed_maos); fprintf(fp,"sim.end=%d\n", parms->maos.nstep); fprintf(fp,"sim.dt=%g\n", parms->maos.dt); fprintf(fp,"sim.zadeg=%g\n", parms->maos.zadeg); fprintf(fp,"sim.mffocus=%d\n", parms->maos.mffocus); fprintf(fp,"sim.ahstfocus=%d\n", parms->maos.ahstfocus); fprintf(fp,"tomo.ahst_wt=3\n"); fprintf(fp,"sim.servotype_lo=2\n");/*type II */ fprintf(fp,"sim.eplo='gain.bin'\n"); fprintf(fp,"powfs0_llt.fnrange='%s'\n", parms->maos.fnrange); fprintf(fp,"atm.r0z=%.4f\n", parms->maos.r0z); fprintf(fp,"atm.size=[128 128]\n"); if(parms->maos.wddeg){ fprintf(fp, "atm.wddeg=["); for(int ips=0; ips<parms->maos.nwddeg; ips++){ fprintf(fp, "%.2f ", parms->maos.wddeg[ips]); } fprintf(fp, "]\n"); } fprintf(fp,"wfs.thetax=[0 0 -33.287 -20.5725 20.5725 33.287"); for(int iwfs=0; iwfs<aster[selaster].nwfs; iwfs++){ fprintf(fp," %.4f", aster[selaster].wfs[iwfs].thetax*206265); } fprintf(fp,"]\n"); fprintf(fp,"wfs.thetay=[0 35 10.8156 -28.3156 -28.3156 10.8156"); for(int iwfs=0; iwfs<aster[selaster].nwfs; iwfs++){ fprintf(fp," %.4f", aster[selaster].wfs[iwfs].thetay*206265); } fprintf(fp,"]\n"); fprintf(fp,"wfs.siglev=[900 900 900 900 900 900"); for(int iwfs=0; iwfs<aster[selaster].nwfs; iwfs++){ fprintf(fp, " %.2f", aster[selaster].wfs[iwfs].siglevtot); } fprintf(fp,"]\n"); fprintf(fp,"wfs.wvlwts=[1 1 1 1 1 1"); for(int iwfs=0; iwfs<aster[selaster].nwfs; iwfs++){ for(int iwvl=0; iwvl<nwvl; iwvl++){ fprintf(fp," %.2f ", aster[selaster].wfs[iwfs].siglev->p[iwvl] /aster[selaster].wfs[iwfs].siglevtot); } } fprintf(fp,"]\n"); fprintf(fp,"wfs.powfs=[0 0 0 0 0 0"); for(int iwfs=0; iwfs<aster[selaster].nwfs; iwfs++){ fprintf(fp, " %d", aster[selaster].wfs[iwfs].ipowfs+1); } fprintf(fp,"]\n"); dmat* rnefs=parms->skyc.rnefs; double rne=IND(rnefs,seldtrat,0); double bkgrnd=aster[selaster].wfs[0].bkgrnd; if(parms->maos.npowfs==1){ fprintf(fp, "powfs.piinfile=[\"\" \"pistat\" ]\n"); fprintf(fp, "powfs.neareconfile=[\"\" \"nea_tot\"]\n"); fprintf(fp, "powfs.phyusenea=[0 1]\n"); fprintf(fp, "powfs.dtrat=[1 %d]\n", (int)parms->skyc.dtrats->p[seldtrat]); fprintf(fp, "powfs.bkgrnd=[0 %.2f]\n", bkgrnd); fprintf(fp, "powfs.rne=[3 %.2f]\n", rne); fprintf(fp, "powfs.phystep=[0 %ld]\n", 50+(long)parms->skyc.dtrats->p[seldtrat]*20); fprintf(fp, "powfs.noisy=[1 1 ]\n"); fprintf(fp, "powfs.pixtheta=[0.5/206265 %g/206265000]\n", parms->skyc.pixtheta[1]*206265000); fprintf(fp, "powfs.pixpsa=[6 %d]\n", parms->skyc.pixpsa[0]); fprintf(fp, "powfs.ncomp=[64 %d]\n", parms->maos.ncomp[0]); fprintf(fp, "powfs.nwvl=[1 %d]\n",nwvl); fprintf(fp, "powfs.wvl=[0.589e-6"); for(int ip=0; ip<1; ip++){ for(int iwvl=0; iwvl<nwvl; iwvl++){ fprintf(fp, " %.4g", parms->maos.wvl[iwvl]); } } fprintf(fp,"]\n"); }else if(parms->maos.npowfs==2){ fprintf(fp, "powfs.piinfile=[\"\" \"pistat\" \"pistat\"]\n"); fprintf(fp, "powfs.neareconfile=[\"\" \"nea_tot\" \"nea_tot\"]\n"); fprintf(fp, "powfs.phyusenea=[0 1 1]\n"); fprintf(fp, "powfs.dtrat=[1 %d %d]\n", (int)parms->skyc.dtrats->p[seldtrat], (int)parms->skyc.dtrats->p[seldtrat]); fprintf(fp, "powfs.bkgrnd=[0 %.2f %.2f]\n", bkgrnd, bkgrnd); fprintf(fp, "powfs.rne=[3 %.2f %.2f]\n", rne,rne); fprintf(fp, "powfs.phystep=[0 %ld %ld]\n", 50+(long)parms->skyc.dtrats->p[seldtrat]*20, 50+(long)parms->skyc.dtrats->p[seldtrat]*20); fprintf(fp, "powfs.noisy=[1 1 1]\n"); fprintf(fp, "powfs.pixtheta=[0.5/206265 %g/206265000 %g/206265000]\n", parms->skyc.pixtheta[0]*206265000, parms->skyc.pixtheta[1]*206265000); fprintf(fp, "powfs.pixpsa=[6 %d %d]\n", parms->skyc.pixpsa[0], parms->skyc.pixpsa[1]); fprintf(fp, "powfs.ncomp=[64 %d %d]\n", parms->maos.ncomp[0], parms->maos.ncomp[1]); fprintf(fp, "powfs.nwvl=[1 %d %d]\n",nwvl,nwvl); fprintf(fp, "powfs.wvl=[0.589e-6"); for(int ip=0; ip<2; ip++){ for(int iwvl=0; iwvl<nwvl; iwvl++){ fprintf(fp, " %.4g", parms->maos.wvl[iwvl]); } } fprintf(fp,"]\n"); fprintf(fp, "powfs.wvlwts=[]\n"); }else{ error("Fill this out please\n"); } fclose(fp); snprintf(fnconf,PATH_MAX,"%s/skyres.txt",path); fp=fopen(fnconf,"w"); fprintf(fp, "TotAll\tNGS\tTT\n"); fprintf(fp, "%g\t%g\t%g\n", sqrt(ipres[0])*1e9, sqrt(ipres[1])*1e9, sqrt(ipres[2])*1e9); fclose(fp); }