int main(int argc, char *argv[]) { int error = 0; switch(argc) { case 1: user_mode(); break; case 2: if(strcmp(argv[1],"-penc") == 0) { resolve(encode_character); } else if(strcmp(argv[1],"-pdec") == 0) { resolve(decode_character); } else if(strcmp(argv[1],"--help") == 0) { print_help(argv[0]); return 0; } else { error = 1; } break; case 3: if(strcmp(argv[1],"-enc") == 0) { file_mode(argv[2],ENCODE); } else if(strcmp(argv[1],"-dec") == 0) { file_mode(argv[2],DECODE); } else { error = 1; } break; default: error = 1; } if(error) { fprintf(stderr, "\n\tERROR"); fprintf(stderr, "%s\n", help_message(argv[0])); fflush(stderr); return 1; } return 0; }
int pfile_infos(t_files *node, char *fname, t_params opts) { struct stat stated; char *slh; char *tmp; slh = ft_strdup(epure_name(fname, opts)); node->details = (char **)malloc(sizeof(char *) * 8); node->details[1] = NULL; node->details[7] = NULL; if ((lstat(fname, &stated) == -1)) { node->details[0] = ft_strjoin(fname, ft_strjoin(": ", strerror(errno))); return (0); } node->fcount = S_ISDIR(stated.st_mode) && !node->fcount ? -1 : 0; node->stmp = stated.st_mtimespec; if (opts & ADD_FTYPE) { tmp = slh; ft_memmove((slh = ft_strnew(ft_strlen(slh) + 1)), tmp, ft_strlen(tmp)); slh[ft_strlen(tmp)] = file_mode(stated.st_mode, 1); free(tmp); } if (!(opts & 0x01)) node->details[0] = slh; return (opts & 0x01 ? s_pfileinfo(stated, node, slh) : stated.st_blocks); }
void show_file_detail () { int index = 0 ; struct stat stat_buf ; struct passwd *psd ; struct group * grp ; ///输出缓冲 char out_put_buf[256] ; char time_buf[40] ; printf ("current pos = %d \n" , current_pos) ; bzero (out_put_buf,sizeof (out_put_buf)) ; for ( ; index < current_pos ; ++ index ) { if ( stat (file_box[index] , &stat_buf) == -1) { perror ("stat error 4 ") ; exit (1) ; } out_put_buf[0] = file_type (&stat_buf) ; file_mode(&stat_buf , out_put_buf+1); psd = getpwuid(stat_buf.st_uid) ; grp = getgrgid (stat_buf.st_gid) ; strcpy (time_buf , ctime(&stat_buf.st_mtime)) ; time_buf[strlen(time_buf)-1] = 0 ; sprintf (out_put_buf + strlen (out_put_buf) , "%s %lu %s %s %8ld %s %s \n" , ". " , stat_buf.st_nlink , psd -> pw_name , grp ->gr_name ,stat_buf.st_size , time_buf , file_box[index]); printf ("%s" , out_put_buf ) ; bzero (out_put_buf , sizeof (out_put_buf)) ; } }
/* If flags == 1, fail unless PATH is an existing directory. * If flags == 0, fail unless PATH is an existing non-directory. * If flags == -1, fail unless PATH exists; the caller doesn't care whether * PATH is a file or a directory. */ static int _exists(const char *path, int flags) { mode_t mode; mode = file_mode(path); if (mode != 0) { switch (flags) { case 1: if (!S_ISDIR(mode)) { return FALSE; } break; case 0: if (S_ISDIR(mode)) { return FALSE; } break; default: break; } return TRUE; } return FALSE; }
int input(){ printf("> "); memset(inputbuf, 0, INPUT_BUFSIZE); setbuf(stdin, NULL); fgets(inputbuf, INPUT_BUFSIZE, stdin); setbuf(stdin, NULL); char *friend_name = strtok(inputbuf, ":"); char *message = strtok(NULL, "\n"); if (friend_name != NULL && message != NULL) { send_message(friend_name, message);//EOF 0x04 return TRUE; } if (!strcmp(friend_name, "exit\n")){ client_shutdown = 1; return TRUE; } if (!strcmp(friend_name, "file\n")){ file_mode(); return TRUE; } printf("%s", "usage\t<$name><:><$message>\n"); printf("%s", "\t<exit>\n"); printf("%s", "\t<file>\n"); return FALSE; }
END_TEST START_TEST (file_mode_test) { mode_t res; const char *path; res = file_mode(NULL); fail_unless(res == 0, "Failed to handle null argument"); fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL, strerror(errno), errno); path = "/"; res = file_mode(path); fail_unless(res != 0, "Failed to find mode for '%s': %s", path, strerror(errno)); }
extern int is_fifo( const_str pathname ) { unsigned long mode; if( (mode = file_mode( pathname )) != 0 ) { return S_ISFIFO( mode ); } return 0; }
/* Convenience funcitons */ extern int is_dir( const_str pathname ) { mode_t mode; if( (mode = file_mode( pathname )) != 0 ) { return (int) S_ISDIR( mode ); } return 0; }
void format_long(char *name, t_stat st, t_env *e) { ino_number(st, e); file_mode(st); number_of_link(st, e); owner_name(st, e); group_name(st, e); number_of_bytes(st, e); last_modif(e, st); path_name(name, st, e); }
/* TODO: preserve mod times */ static gboolean do_copy_file(const char *fromfile, const char *tofile) { FILE *fromfp, *tofp; mode_t mode; string_var todir; int n; char buf[1024]; todir = file_dirname(fromfile); if ((mode = file_mode(todir)) < 0) mode = 0755; todir = file_dirname(tofile); if (file_build_tree(todir, mode) < 0) return FALSE; if ((fromfp = fopen(fromfile, "r")) == 0) { log::perror(fromfile); return FALSE; } if ((mode = file_mode(fromfile)) == 0) mode = 0644; /* should never happen */ if ((tofp = file_open_mode(tofile, "w", mode)) == 0) { log::perror(tofile); fclose(fromfp); return FALSE; } while ((n = fread(buf, 1, sizeof(buf), fromfp)) > 0) fwrite(buf, 1, n, tofp); fclose(fromfp); fclose(tofp); return TRUE; }
VISIBLE QFile * Qfopen (FILE *file, const char *mode) { QFile *qfile; int flags = file_mode (mode, 0); if (!file) return 0; qfile = calloc (sizeof (*qfile), 1); if (!qfile) return 0; qfile->file = file; if (flags & QF_READ) qfile->size = check_file (fileno (file), -1, -1, 0); qfile->c = -1; return qfile; }
VISIBLE QFile * Qopen (const char *path, const char *mode) { QFile *file; char *m; int flags, reading, zip; int size = -1; m = alloca (strlen (mode) + 1); flags = file_mode (mode, m); zip = flags & QF_ZIP; reading = flags & QF_READ; if (reading) { int fd = open (path, O_RDONLY); if (fd != -1) { size = check_file (fd, -1, -1, &zip); close (fd); } } file = calloc (sizeof (*file), 1); if (!file) return 0; file->size = size; #ifdef HAVE_ZLIB if (zip) { file->gzfile = gzopen (path, m); if (!file->gzfile) { free (file); return 0; } } else #endif { file->file = fopen (path, m); if (!file->file) { free (file); return 0; } } file->c = -1; return file; }
/* If DIRP == 1, fail unless PATH is an existing directory. * If DIRP == 0, fail unless PATH is an existing non-directory. * If DIRP == -1, fail unless PATH exists; the caller doesn't care whether * PATH is a file or a directory. */ static int _exists(const char *path, int dirp) { mode_t fmode; fmode = file_mode(path); if (fmode != 0) { if (dirp == 1 && !S_ISDIR(fmode)) { return FALSE; } else if (dirp == 0 && S_ISDIR(fmode)) { return FALSE; } return TRUE; } return FALSE; }
static inline char *ft_print_fmode(mode_t details) { char *rights; rights = ft_strnew(10); rights[0] = file_mode(details, 0); rights[1] = details & S_IRUSR ? 'r' : '-'; rights[2] = details & S_IWUSR ? 'w' : '-'; rights[3] = details & S_IXUSR ? 'x' : '-'; if (details & S_ISUID) rights[3] = details & S_IXUSR ? 's' : 'S'; rights[4] = details & S_IRGRP ? 'r' : '-'; rights[5] = details & S_IWGRP ? 'w' : '-'; rights[6] = details & S_IXGRP ? 'x' : '-'; if (details & S_ISGID) rights[6] = details & S_IXGRP ? 's' : 'S'; rights[7] = details & S_IROTH ? 'r' : '-'; rights[8] = details & S_IWOTH ? 'w' : '-'; rights[9] = details & S_IXGRP ? 'x' : '-'; if (details & S_ISVTX) rights[9] = details & S_IXGRP ? 't' : 'T'; return (rights); }
// filtloc and filter operate on the filenames and determine whether they // may be selected: // ALL: don't filter, in this case, filter can be NULL // BASE: filter with cmp_base(filename, filter) - only show when part up to // last extension matches // EXT: filter with cmp_ext(filename, filter) - only show when (last) extension // matches // NAME: filter with strcasecmp(filename, filter) - only show when whole name // matches char * select_file(enum filter_spec filtloc, char *filter) { char *selected_file, str[256]; int npages, pagenum = 1; const int perpage = PERPAGE - 1; DECL_LINE(backbtn); DECL_LINE(prevpage); DECL_LINE(nextpage); strcpy(sbstr, "file browser"); selected_file = malloc(PATH_MAX * sizeof(char)); selected_file[0] = '\0'; while (selected_file[0] == '\0') { char *pwd = NULL; int n; int *by, *kept; int j, nkept; int match, keep; uint16_t mode; sel = -1; clear_screen(); START_LIST(8, 48, 448, 0xFF404040, 0xFF808080, 0xFFC0C0C0) update_dir_list(); n = file_count(); by = malloc(n * sizeof(int)); kept = malloc(n * sizeof(int)); npages = 1 + n / perpage; pwd = getcwd(pwd, PATH_MAX); text(pwd, 16, 24, 1, 1, 0xFF606060, 0xFF000000); DRAW_LINE(backbtn, "back", "return to previous menu without making a selection"); if (pagenum > 1) { DRAW_LINE(prevpage, "previous page", "not all files are shown"); } j = 0; for (int i = 0; i < n; ++i) { switch (filtloc) { case ANY: match = 1; break; case BASE: match = !cmp_base(file_name(i), filter); break; case EXT: match = !cmp_ext(file_name(i), filter); break; case NAME: match = !strcasecmp(file_name(i), filter); break; default: match = 0; } if (file_is_dir(i) || match) { kept[j] = i; ++j; } } nkept = j; for (int i = perpage * (pagenum - 1); i < nkept; ++i) { if (i > pagenum * perpage) break; snprintf(str, sizeof(str), "permissions = %o", file_mode(kept[i]) & 07777); USECOLR = !USECOLR; CURCOLR = USECOLR ? BG1COLR : BG2COLR; current_y += 48; by[i] = current_y; fill_rect(0, by[i], 1024, 44, CURCOLR); text(file_name(kept[i]), col1_x, by[i] + 8, 1, 1, 0xFFFFFFFF, CURCOLR); text(str, col2_x, by[i] + 8, 1, 1, 0xFFFFFFFF, CURCOLR); } if (pagenum < npages) { DRAW_LINE(nextpage, "next page", "not all files are shown"); } while (sel == -1) { update_screen(); input_read(); if (BTN_CLICKED(backbtn)) { free(by); free(kept); return NULL; } for (int i = perpage * (pagenum - 1); i <= nkept; ++i) if (was_clicked(0, by[i], 1024, 44)) sel = kept[i]; if (BTN_CLICKED(prevpage)) { --pagenum; break; } else if (BTN_CLICKED(nextpage)) { ++pagenum; break; } } if (sel != -1) { free(by); free(kept); pagenum = 1; if (file_is_dir(sel)) { if (chdir(file_name(sel)) == -1) perror("chdir"); } else { strcpy(selected_file, pwd); strcat(selected_file, "/"); strcat(selected_file, file_name(sel)); } } } return selected_file; }
void file_server::main(std::string file_name) { std::string path; if(!check_in_document_root(file_name,path)) { show404(); return; } int s=file_mode(path); if((s & S_IFDIR)) { std::string path2; int mode_2=0; bool have_index = check_in_document_root(file_name+"/" + index_file_ ,path2); if(have_index) { mode_2 = file_mode(path2); have_index = (mode_2 & S_IFREG) != 0; } if( !file_name.empty() && file_name[file_name.size()-1]!='/' // not ending with "/" as should && (have_index || list_directories_) ) { response().set_redirect_header(file_name + "/"); response().out()<<std::flush; return; } if(have_index) { path = path2; s=mode_2; } else { if(list_directories_) list_dir(file_name,path); else show404(); return; } } if(!(s & S_IFREG)) { show404(); return; } std::string ext; size_t pos = path.rfind('.'); if(pos != std::string::npos) ext=path.substr(pos); mime_type::const_iterator p=mime_.find(ext); if(p!=mime_.end()) response().content_type(p->second); else response().content_type("application/octet-stream"); if(!allow_deflate_) { response().io_mode(http::response::nogzip); } booster::nowide::ifstream file(path.c_str(),std::ios_base::binary); if(!file) { show404(); return; } response().out()<<file.rdbuf(); // write stream to stream }
static bool zwritefile(char *fname) { char bakname[PATHMAX + 1]; struct mark smark; struct stat sbuf; int nlink = 1, rc; bool bak = false; /* If the file existed, check to see if it has been modified. */ if (Curbuff->mtime && stat(fname, &sbuf) == 0) { if (sbuf.st_mtime > Curbuff->mtime) { strconcat(PawStr, PAWSTRLEN, "WARNING: ", lastpart(fname), " has been modified. Overwrite? ", NULL); if (ask(PawStr) != YES) return false; } #ifndef __QNXNTO__ nlink = sbuf.st_nlink; #endif } #if HUGE_FILES if (Bbuff->fd != -1) { strconcat(PawStr, PAWSTRLEN, "WARNING: huge file ", lastpart(fname), " not yet read. Are you sure? ", NULL); if (ask(PawStr) != YES) return false; } #endif /* check for links and handle backup file */ make_bakname(bakname, sizeof(bakname), fname); if (nlink > 1) { strconcat(PawStr, PAWSTRLEN, "WARNING: ", lastpart(fname), " is linked. Preserve? ", NULL); switch (ask(PawStr)) { case YES: if (VAR(VBACKUP)) bak = cp(fname, bakname); break; case NO: if (VAR(VBACKUP)) bak = rename(fname, bakname); else unlink(fname); /* break link */ break; case ABORT: return false; } } else if (VAR(VBACKUP)) bak = rename(fname, bakname); bmrktopnt(Bbuff, &smark); rc = bwritefile(Bbuff, fname, file_mode()); bpnttomrk(Bbuff, &smark); if (rc) { if (stat(fname, &sbuf) == 0) Curbuff->mtime = sbuf.st_mtime; else Curbuff->mtime = -1; clrpaw(); } else { if (errno == EACCES) error("File is read only."); else error("Unable to write file."); if (bak) { if (sbuf.st_nlink) { cp(bakname, fname); unlink(bakname); } else rename(bakname, fname); } } return rc; }
/* Returns true if the file exists in some shape or form; zero if it does not. */ extern int file_exists( const_str pathname ) { return file_mode( pathname ) != 0; }