/** * @author Ivan Gualandri * @param char* path percorso del file da aprire * @return DIR* puntatore alla cartella aperta se esiste, NULL altrimenti * * Dato un path contenente una cartella viene aperta se presente, e si torna il puntatore a DIR* */ DIR *opendir(const char *path){ int mpoint_id = 0; //char tmp_path[CURPATH_LEN]; char* rel_path; DIR* pdir; #ifdef DEBUG int error = get_abs_path((char*)path); printf("AbsPath in opendir: %s len: %d\nError code: %d\n", path, strlen(path), error); #else get_abs_path((char*)path); #endif //printf("%s\n", path); mpoint_id = get_mountpoint_id((char*)path); rel_path = get_rel_path(mpoint_id, path); //printf("Rel Path len%d - %s\n", strlen(rel_path), rel_path); if(mountpoint_list[mpoint_id].dir_op.opendir_f!=NULL) { pdir = mountpoint_list[mpoint_id].dir_op.opendir_f(rel_path); pdir->handle = mpoint_id; return pdir; } else { //printf("Could not open_dir no function found\n"); return NULL; } return NULL; }
void cmd_line_cd(const char *arg_line) { if(strcmp(arg_line, "--HELP") == 0) { console_printf("Change the current working directory\nusage: cd <directory path>\n"); return; } char line_copy[strlen(arg_line) + 1]; strcpy(line_copy, arg_line); char *first_word = strtok(line_copy, " "); // If you give no arguments to cd or the argument is "/", you are sent to root if (strlen(first_word) == 0 || strcmp(first_word, ROOT) == 0) { set_cur_path(ROOT); return; } char abs_path[WORKING_DIRECTORY_PATH_BUFFER_SIZE]; get_abs_path(first_word, abs_path); if (!abs_path[0]) { console_printf("cd: root has no parent\n"); return; } struct iso_dir *dir = iso_dopen(abs_path, 3); if (!dir) { console_printf("cd: no such path %s\n", abs_path); } else { set_cur_path(abs_path); } iso_dclose(dir); return; }
void resolve_command(char **cmd_ptr) { if (check_slash(*cmd_ptr)) { get_abs_path(cmd_ptr); } else { search_path(cmd_ptr); } }
/** * @author Ivan Gualandri * @param char* path percorso del file da aprire * @param int oflags parametri di apertura * * Dato un path viene aperto se presente, e si torna il numero di descrittore che lo contiene * @todo Inserire gestione flags */ int open(const char *path, int oflags, ...){ int prova; int mpid; int ret_fd; int error = 0; char *newpath; va_list ap; va_start(ap, oflags); ret_fd = 0; //printf("Path: %s\n", path); prova = va_arg(ap, int); newpath = kmalloc(CURPATH_LEN * sizeof(char)); memset(newpath, '\0', CURPATH_LEN); cur_fd=0; if(cur_fd == _SC_OPEN_MAX) cur_fd = 0; while(fd_list[cur_fd].mountpoint_id != -1 && cur_fd < _SC_OPEN_MAX){ //printf("%d %d\n", cur_fd, fd_list[cur_fd].mountpoint_id); cur_fd++; } if(cur_fd == _SC_OPEN_MAX) { printf("No more file descriptors available\n"); return -1; } strcpy(newpath, path); error = get_abs_path((char*) newpath); //printf("After get_abs: %s %s\n", newpath, current_user.cur_path); mpid = get_mountpoint_id((char*) newpath); //printf("Cur_fd: %d\n",cur_fd); if(mpid >-1) { fd_list[cur_fd].mountpoint_id = mpid; newpath = get_rel_path(mpid, (char *)newpath); } else { printf("That path doesn't exist\n"); va_end(ap); return -1; } if( mpid > -1 && mountpoint_list[fd_list[cur_fd].mountpoint_id].operations.open != NULL){ fd_list[cur_fd].fs_spec_id = (int) mountpoint_list[fd_list[cur_fd].mountpoint_id].operations.open(newpath, oflags); if(fd_list[cur_fd].fs_spec_id == -1){ printf("No file's Found\n"); va_end(ap); return -1; } } else { if(mpid>-1) printf("No OPEN services found here\n"); va_end(ap); return -1; } va_end(ap) fd_list[cur_fd].offset = 0; fd_list[cur_fd].flags_mask = oflags; ret_fd = cur_fd; cur_fd++; free(newpath); return ret_fd; }
/*generates statistic results*/ char *report(F_set *File_head, char *query, char *fmt, char *prefix, char *start_timestamp, double prob, int threads) { char *abs_query_path = NULL, *abs_filter_path = NULL; static char buffer[800] = {0}; static char timestamp[40] = {0}; abs_query_path = get_abs_path(query); abs_filter_path = get_abs_path(File_head->filename); float _contamination_rate = (float) (File_head->reads_contam) / (float) (File_head->reads_num); double p_value = cdf(File_head->hits,get_mu(File_head->all_k,prob),get_sigma(File_head->all_k,prob)); if(!fmt) { fprintf(stderr, "Output format not specified\n"); exit(EXIT_FAILURE); } else if(!strcmp(fmt, "json")) { isodate(timestamp); snprintf(buffer, sizeof(buffer), "{\"begin_timestamp\": \"%s\"," "\"end_timestamp\": \"%s\"," "\"sample\": \"%s\"," "\"bloom_filter\": \"%s\"," "\"total_read_count\": %lld," "\"contaminated_reads\": %lld," "\"total_hits\": %lld," "\"contamination_rate\": %f," "\"p_value\": %e," "\"threads\": %d" "}", start_timestamp, timestamp,abs_query_path, abs_filter_path, File_head->reads_num, File_head->reads_contam, File_head->hits, _contamination_rate, p_value, threads); // TSV output format } else if (!strcmp(fmt, "tsv")) { sprintf(buffer, "sample\tbloom_filter\ttotal_read_count\t_contaminated_reads\t_contamination_rate\n" "%s\t%s\t%lld\t%lld\t%f\t%e\n", abs_query_path , abs_filter_path, File_head->reads_num, File_head->reads_contam, _contamination_rate,p_value); } return buffer; }
int get_hotkey(){ WCHAR fbuffer[MAX_PATH]; WCHAR buffer[MAX_PATH]; DWORD size=MAX_PATH; if(GetUserName(fbuffer, &size)){ wcscat_s(fbuffer,MAX_PATH,L".ini"); get_abs_path(fbuffer,buffer); return GetPrivateProfileInt(L"customize",L"hotkey",DEFAULT_HOT_KEY,buffer); }else{ return DEFAULT_HOT_KEY;//VK_F9 } }
static char *resolve_name(const char *kfilename, int must_exist, int flags) { char *tmp; char *newfilename; tmp = getname(kfilename); if(IS_ERR(tmp)) return tmp; if((tmp[0] != '/' && cwd_virtual()) || strchr(tmp, AVFS_MAGIC_CHAR)) { DEB((KERN_INFO "resolve_name: %s (%i/%s)\n", tmp, current->pid, (current->flags & PF_AVFS) ? "on" : "off")); if(strcmp(tmp, "/#avfs-on") == 0) { printk(KERN_INFO "AVFS ON (pid: %i)\n", current->pid); current->flags |= PF_AVFS; newfilename = ERR_PTR(-EEXIST); } else if(!(current->flags & PF_AVFS)) newfilename = NULL; else if(strcmp(tmp, "/#avfs-off") == 0) { printk(KERN_INFO "AVFS OFF (pid: %i)\n", current->pid); current->flags &= ~PF_AVFS; newfilename = ERR_PTR(-EEXIST); } else { if(tmp[0] == '/') { newfilename = resolv_virt(tmp, must_exist, flags); } else { char *abspath; abspath = get_abs_path(tmp); if(abspath) { newfilename = resolv_virt(abspath, must_exist, flags); kfree(abspath); } else newfilename = NULL; } } } else newfilename = NULL; putname(tmp); return newfilename; }
BOOL set_hotkey(int key){ WCHAR hotkey[2]; WCHAR fbuffer[MAX_PATH]; WCHAR buffer[MAX_PATH]; DWORD size=MAX_PATH; if(GetUserName(fbuffer, &size)){ hotkey[0]=key+L'0'; hotkey[1]=L'\0'; wcscat_s(fbuffer,MAX_PATH,L".ini"); get_abs_path(fbuffer,buffer); return WritePrivateProfileString(L"customize",L"hotkey",hotkey,buffer); }else{ return 0; } }
/** * @brief Prints contents of a single file * @param fname Name of file to be printed */ void cat_file(const char *fname) { char abs_path[WORKING_DIRECTORY_PATH_BUFFER_SIZE]; char c; get_abs_path(fname, abs_path); struct iso_file *file = iso_fopen(abs_path, 3); if (!file) { console_printf("cat: %s does not exist\n", fname); iso_fclose(file); return; } while (!file->at_EOF) { iso_fread(&c, 1, 1, file); console_putchar(c); } iso_fclose(file); }
/** * @brief Lists contents of a single directory * @param dname Name of directory to list */ void ls_dir(const char *dname) { char abs_path[WORKING_DIRECTORY_PATH_BUFFER_SIZE]; get_abs_path(dname, abs_path); struct iso_dir *dir = iso_dopen(abs_path, 3); if (dir) { struct directory_record *dr = iso_dread(dir); while (dr) { if (is_dir(dr->file_flags[0])) { console_set_fgcolor(100,100,255); } else { console_set_fgcolor(0,255,0); } console_printf("%s\n", dr->file_identifier); dr = iso_dread(dir); } console_set_fgcolor(255,255,255); iso_dclose(dir); } else { console_printf("ls: no directory %s\n", dname); } iso_dclose(dir); }
void sql_write(const char* path,const char* op) { memset(&logg,0,sizeof(logg)); ++id; logg.logg_id=id; get_timestamp(0,logg.time); strncpy(logg.operation,op,32); // REMEMBER: path my be non existing // char *absolute_path; char *relative_path; absolute_path=get_abs_path(path); relative_path=get_rel_path(path); strncpy(logg.file,absolute_path,1024); struct stat stbuf; if(lstat(relative_path,&stbuf)==0) { logg.protection=(unsigned int)stbuf.st_mode; uid_t uid; uid=stbuf.st_uid; size_t buflen; buflen=sysconf(_SC_GETPW_R_SIZE_MAX); if(buflen==-1) exit(EXIT_FAILURE); char *buf; buf=malloc(buflen); if(!buf) exit(EXIT_FAILURE); struct passwd pwd; struct passwd *pwd_result; if(getpwuid_r(uid,&pwd,buf,buflen,&pwd_result)==0) { if(pwd_result) strncpy(logg.owner,pwd.pw_name,256); } free(buf); gid_t gid; gid=stbuf.st_gid; buflen=sysconf(_SC_GETGR_R_SIZE_MAX); if(buflen==-1) exit(EXIT_FAILURE); buf=malloc(buflen); if(!buf) exit(EXIT_FAILURE); struct group grp; struct group *grp_result; if(getgrgid_r(gid,&grp,buf,buflen,&grp_result)==0) { if(grp_result) strncpy(logg.group,grp.gr_name,256); } free(buf); } free(absolute_path); free(relative_path); strncpy(logg.host,conf.hostname,256); strncpy(logg.tag,conf.tagname,64); logg.uid=(unsigned int)fuse_get_context()->uid; logg.gid=(unsigned int)fuse_get_context()->gid; logg.pid=(unsigned int)fuse_get_context()->pid; if(conf.log_username) { uid_t uid; uid=fuse_get_context()->uid; size_t buflen; buflen=sysconf(_SC_GETPW_R_SIZE_MAX); if(buflen==-1) exit(EXIT_FAILURE); char *buf; buf=malloc(buflen); if(!buf) exit(EXIT_FAILURE); struct passwd pwd; struct passwd *result; if(getpwuid_r(uid,&pwd,buf,buflen,&result)==0) { if(result) strncpy(logg.username,pwd.pw_name,256); } free(buf); if(is_str(logg.username)) { if(conf.log_tty||conf.log_remote_host||conf.log_login_time) { setutxent(); struct utmpx *utx; while((utx=getutxent())) { if(!strcmp(logg.username,utx->ut_user)) { if(conf.log_tty) strncpy(logg.tty,utx->ut_line,16); if(conf.log_remote_host) strncpy(logg.remote_host,utx->ut_host,256); if(conf.log_login_time) get_timestamp(utx->ut_tv.tv_sec,logg.login_time); } } endutxent(); } } } if(conf.log_groupname) { gid_t gid; gid=fuse_get_context()->gid; size_t buflen; buflen=sysconf(_SC_GETGR_R_SIZE_MAX); if(buflen==-1) exit(EXIT_FAILURE); char *buf; buf=malloc(buflen); if(!buf) exit(EXIT_FAILURE); struct group grp; struct group *result; if(getgrgid_r(gid,&grp,buf,buflen,&result)==0) { if(result) strncpy(logg.groupname,grp.gr_name,256); } free(buf); } if(conf.log_cmd) { size_t size; size=strlen("/proc//cmdline")+10+1; char *buf; buf=malloc(size); if(!buf) exit(EXIT_FAILURE); if(snprintf(buf,size,"/proc/%i/cmdline",logg.pid)) { FILE *fp; fp=fopen(buf,"r"); free(buf); if(!fp) return; buf=malloc(1024); if(!buf) exit(EXIT_FAILURE); size_t bytes_r; bytes_r=fread(buf,1,1024,fp); fclose(fp); if(bytes_r==0) { free(buf); return; } strncpy(logg.cmd,buf,1024); if(conf.log_args) { int i=0; while(++i<bytes_r-1) if(buf[i]==0x00) buf[i]=0x20; int str_off=strlen(logg.cmd); strncpy(logg.args,&buf[str_off],1024); free(buf); } } } if(conf.log_ppid) { size_t size; size=strlen("/proc//status")+10+1; char *buf; buf=malloc(size); if(!buf) exit(EXIT_FAILURE); if(snprintf(buf,size,"/proc/%i/status",logg.pid)) { FILE *fp; fp=fopen(buf,"r"); free(buf); if(!fp) return; char *line=NULL; size_t len=0; while(getline(&line,&len,fp)!=-1) { if(!strncmp(line,"PPid:",5)) { if((strlen(line)-6)<20) sscanf(line,"PPid:\t%i",&logg.ppid); break; } } fclose(fp); free(line); } if(conf.log_ppid_cmd) { size_t size; size=strlen("/proc//cmdline")+10+1; char *buf; buf=malloc(size); if(!buf) exit(EXIT_FAILURE); if(snprintf(buf,size,"/proc/%i/cmdline",logg.ppid)) { FILE *fp; fp=fopen(buf,"r"); free(buf); if(!fp) return; buf=malloc(1024); if(!buf) exit(EXIT_FAILURE); size_t bytes_r; bytes_r=fread(buf,1,1024,fp); fclose(fp); if(bytes_r==0) { free(buf); return; } strncpy(logg.p_cmd,buf,1024); free(buf); } } } size_t size; size=strlen(INS) +strlen(conf.db_database) +strlen(conf.db_table) +20 +strlen(logg.time) +strlen(logg.host) +strlen(logg.tag) +strlen(logg.operation) +10 +10 +strlen(logg.username) +strlen(logg.groupname) +strlen(logg.tty) +strlen(logg.login_time) +strlen(logg.remote_host) +strlen(logg.cmd) +strlen(logg.args) +10 +10 +strlen(logg.p_cmd) +strlen(logg.file) +10 +strlen(logg.owner) +strlen(logg.group); char *buf; buf=malloc(size); if(!buf) exit(EXIT_FAILURE); if((size=snprintf(buf,size, INS, conf.db_database, conf.db_table, logg.logg_id, logg.time, logg.host, logg.tag, logg.operation, logg.uid, logg.gid, logg.username, logg.groupname, logg.tty, logg.login_time, logg.remote_host, logg.cmd, logg.args, logg.pid, logg.ppid, logg.p_cmd, logg.file, logg.protection, logg.owner, logg.group))) { if(mysql_real_query(conn,buf,size)) sql_err(conn,buf); } free(buf); }
int main(int argc,char *argv[]) { int error = -1; int i; int flc; int index; int free_FLC; struct fileinfo info; int new_flc; int new_index; int status; int num; char *last; char buffer[260]; char buffer1[260]; char file_name[10]; char name[10]; char ext[4]; char* etmp; /*Checks number of arguments*/ if(argc != 2) { printf("Please specify one directory.\n"); return error; } /*Right number of arguments*/ else { init_util(); status = parse_path(argv[1],&flc,&index); /*Checks if directory exists*/ if(status != E_NOTEXIST) { printf("Directory already exists.\n"); return error; } /*Directory does not exist, make a new one!*/ else { last = rindex(argv[1],'/'); /*Check if path is relative*/ if(last == 0) { get_abs_path(buffer1,argv[1]); last = rindex(buffer1,'/'); num = (int)(last-buffer1+1); strncpy(buffer,buffer1,num); buffer[num] = '\0'; //printf("Buffer is:%s\n",buffer); strcpy(name,argv[1]); //printf("I am here and name is:%s\n",name); } /*Path may still be relative, but there is at least one '/'*/ else { /*Check if there is an ending '/'*/ if(strlen(last) == 1) { //Checks if the last character is "/" get_abs_path(buffer, argv[1]); //printf("Buffer is:%s\n",buffer); strncpy(buffer1,buffer,(strlen(buffer)-1)); //printf("Buffer1 is:%s\n",buffer1); buffer1[strlen(buffer)-1]='\0'; last = rindex(buffer1,'/'); if(last == 0) { buffer[0] = '\0'; num = 1; } else { num = (int)(last-buffer1+1); strncpy(buffer,buffer1,num); buffer[num] = '\0'; //printf("Buffer is:%s\n",buffer); last = rindex(buffer1,'/'); if(last == 0) { buffer[0] = '\0'; } } } /*No ending '/', continue*/ else { //printf("Last is:%d\n",strlen(last)); num = (int)(last-argv[1]+1); strncpy(buffer,argv[1],num); strcpy(buffer1,argv[1]); //printf("Buffer is:%s\n",buffer); buffer[num] = '\0'; } /*Check if the path is really relative*/ if(argv[1][0] != '/' && last != 0) { last = rindex(buffer1,'/'); num = (int)(last-buffer1+1); //printf("%d\n",num); strcpy(name,&buffer1[num]); } else if(argv[1][0] != '/' && last == 0) { strcpy(name,buffer1); } /*Path is not relative, continue*/ else { strcpy(file_name,&buffer1[num]); //Keeps track of the new directory name //printf("%d\n",strlen(buffer1)-1); file_name[strlen(buffer1)-1]='\0'; //printf("File name is:%s\n",file_name); if(file_name[0] == '/') { //Checks if the first character is the "/" for(i=0;i<(strlen(buffer1)-1);i++) { name[i] = file_name[i+1]; } name[strlen(buffer1)-1]= '\0'; } else { strcpy(name,file_name); } } } //printf("num: %d buffer: %s\n", num, buffer); status = parse_path(buffer,&flc,&index); free_FLC = get_free_block(); info = get_file_info(flc,index); info.attrib = '\x10'; etmp = (char*)strtok(name, "."); strcpy(info.filename,etmp); etmp = (char*)strtok(NULL,"."); strcpy(info.ext, ((etmp == NULL) ? " " : etmp)); allocate_block(free_FLC,info,info.FLC,&new_flc,&new_index); strcpy(info.filename,".."); strcpy(info.ext," "); allocate_block(0,info,free_FLC,&new_flc,&new_index); strcpy(info.filename,"."); strcpy(info.ext," "); info.FLC = free_FLC; allocate_block(0,info,free_FLC,&new_flc,&new_index); return 1; } } kill_util(); }