void ls_list(ARCHD *arcn, time_t now, FILE *fp) { struct stat *sbp; char f_mode[MODELEN]; char f_date[DATELEN]; const char *timefrmt; /* * if not verbose, just print the file name */ if (!vflag) { (void)fprintf(fp, "%s\n", arcn->name); (void)fflush(fp); return; } if (d_first < 0) d_first = 0; /* * user wants long mode */ sbp = &(arcn->sb); #if 0 strmode(sbp->st_mode, f_mode); #else strcpy(f_mode, ""); #endif /* * time format based on age compared to the time pax was started. */ if ((sbp->st_mtime + SIXMONTHS) <= now) timefrmt = d_first ? OLDFRMTD : OLDFRMTM; else timefrmt = d_first ? CURFRMTD : CURFRMTM; /* * print file mode, link count, uid, gid and time */ #if 0 if (strftime(f_date,DATELEN,timefrmt,localtime(&(sbp->st_mtime))) == 0) #endif f_date[0] = '\0'; (void)fprintf(fp, "%s%2u %-*s %-*s ", f_mode, sbp->st_nlink, UT_NAMESIZE, name_uid(sbp->st_uid, 1), UT_GRPSIZE, name_gid(sbp->st_gid, 1)); /* * print device id's for devices, or sizes for other nodes */ if ((arcn->type == PAX_CHR) || (arcn->type == PAX_BLK)) # ifdef NET2_STAT (void)fprintf(fp, "%4u,%4u ", major(sbp->st_rdev), minor(sbp->st_rdev)); # else (void)fprintf(fp, "%4lu,%4lu ", (unsigned long)major(sbp->st_rdev), (unsigned long)minor(sbp->st_rdev)); # endif else {
static int add_line_from_stat(struct libscols_table *tb, struct libscols_line *parent, int parent_fd, struct stat *st, const char *name) { struct libscols_line *ln; char modbuf[11], *p; mode_t mode = st->st_mode; int rc = 0; ln = scols_table_new_line(tb, parent); if (!ln) err(EXIT_FAILURE, "failed to create output line"); /* MODE; local buffer, use scols_line_set_data() that calls strdup() */ strmode(mode, modbuf); if (scols_line_set_data(ln, COL_MODE, modbuf)) goto fail; /* SIZE; already allocated string, use scols_line_refer_data() */ p = size_to_human_string(0, st->st_size); if (!p || scols_line_refer_data(ln, COL_SIZE, p)) goto fail; /* NAME */ if (scols_line_set_data(ln, COL_NAME, name)) goto fail; /* colors */ if (scols_table_colors_wanted(tb)) { struct libscols_cell *ce = scols_line_get_cell(ln, COL_NAME); if (S_ISDIR(mode)) scols_cell_set_color(ce, "blue"); else if (S_ISLNK(mode)) scols_cell_set_color(ce, "cyan"); else if (S_ISBLK(mode)) scols_cell_set_color(ce, "magenta"); else if ((mode & S_IXOTH) || (mode & S_IXGRP) || (mode & S_IXUSR)) scols_cell_set_color(ce, "green"); } if (S_ISDIR(st->st_mode)) { int fd; if (parent_fd >= 0) fd = openat(parent_fd, name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC); else fd = open(name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC); if (fd >= 0) { rc = add_children(tb, ln, fd); close(fd); } } return rc; fail: err(EXIT_FAILURE, "failed to create cell data"); return -1; }
/* * === FUNCTION ====================================================================== * Name: print_member_concise * Description: Prints a string representing the attributes of an archive * member. * * Name, inspiration, and string formatting ideas due to Dan Albert. * ===================================================================================== */ static void print_member_verbose(int arch, struct ar_hdr *hdr, char **files, int count, int temp_fd) { char fname[SARFNAME+1]; get_hdr_attr(hdr, name_attr, fname, SARFNAME+1); if(count > 0 && match_fnames(fname, files, count, strcmp) == -1) return; struct shadow_hdr shadow; fill_shadow_hdr(hdr, &shadow); char ftime[18]; struct tm *time = localtime(&shadow.ar_date); strftime(ftime, 18, "%b %e %H:%M %Y", time); char modestr[10]; modestr[9] = '\0'; strmode(shadow.ar_mode, modestr); printf("%-9s %d/%-6d %5lld %s %s\n", modestr, shadow.ar_uid, shadow.ar_gid, (long long)shadow.ar_size, ftime, shadow.ar_name ); }
static const char * fmt_filemode(mode_t mode) { static char buffer[12]; strmode(mode, buffer); return buffer; }
static int dashl_filler(void *buf, const char *name, const struct stat *sb, off_t off) { char mode[16]; sprintf(lsbuf[lsi++], "%s %s %lld %lld\n", name, strmode(mode, sb->st_mode), sb->st_size, sb->st_blocks); return 0; }
int main(int argc, char * argv[], char * envp[]) { char * path = NULL; int ch, fildes, count; struct dirent dbuf[10]; argv0 = argv[0]; while ((ch = getopt(argc, argv, "la")) != EOF) { switch (ch) { case 'l': flags.l = 1; break; case 'a': flags.a = 1; break; case '?': default: usage(); } } argc -= optind; argv += optind; if (*argv) path = *argv; if (path == NULL || !strcmp(path, "")) path = "./"; fildes = open(path, O_DIRECTORY | O_RDONLY | O_SEARCH); if (fildes < 0) { printf("Open failed\n"); return 1; } while ((count = getdents(fildes, (char *)dbuf, sizeof(dbuf))) > 0) { for (int i = 0; i < count; i++) { if (!flags.a && dbuf[i].d_name[0] == '.') continue; if (flags.l) { struct stat stat; char mode[12]; fstatat(fildes, dbuf[i].d_name, &stat, 0); strmode(stat.st_mode, mode); printf("% 7u %s %u:%u %s\n", (unsigned)dbuf[i].d_ino, mode, (unsigned)stat.st_uid, (unsigned)stat.st_gid, dbuf[i].d_name); } else { printf("%s ", dbuf[i].d_name); } } } printf("\n"); close(fildes); return 0; }
static int check(char *path, char *name, struct stat *sp) { int ch, first; char modep[15]; /* * If it's not a symbolic link and it's unwritable and we're * talking to a terminal, ask. Symbolic links are excluded * because their permissions are meaningless. Check stdin_ok * first because we may not have stat'ed the file. */ if (!stdin_ok || S_ISLNK(sp->st_mode) || !access(name, W_OK) || errno != EACCES) return (1); strmode(sp->st_mode, modep); (void)fprintf(stderr, "override %s%s%s/%s for %s? ", modep + 1, modep[9] == ' ' ? "" : " ", user_from_uid(sp->st_uid, 0), group_from_gid(sp->st_gid, 0), path); (void)fflush(stderr); first = ch = getchar(); while (ch != '\n' && ch != EOF) ch = getchar(); return (first == 'y' || first == 'Y'); }
void printlong(char *name, /* filename to print */ char *accpath, /* current valid path to filename */ struct stat *sb) /* stat buffer */ { char modep[15]; (void)printf("%7lu %6lld ", (u_long)sb->st_ino, (long long)sb->st_blocks); (void)strmode(sb->st_mode, modep); (void)printf("%s %3lu %-*s %-*s ", modep, (unsigned long)sb->st_nlink, LOGIN_NAME_MAX, user_from_uid(sb->st_uid, 0), LOGIN_NAME_MAX, group_from_gid(sb->st_gid, 0)); if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode)) (void)printf("%3llu,%5llu ", (unsigned long long)major(sb->st_rdev), (unsigned long long)minor(sb->st_rdev)); else (void)printf("%9lld ", (long long)sb->st_size); printtime(sb->st_mtime); (void)printf("%s", name); if (S_ISLNK(sb->st_mode)) printlink(accpath); (void)putchar('\n'); }
static int print_namei(struct namei *nm, char *path) { int i; if (path) printf("f: %s\n", path); for (; nm; nm = nm->next) { char md[11]; if (nm->noent) { int blanks = 1; if (flags & NAMEI_MODES) blanks += 9; if (flags & NAMEI_OWNERS) blanks += uwidth + gwidth + 2; if (!(flags & NAMEI_VERTICAL)) blanks += 1; blanks += nm->level * 2; printf("%*s ", blanks, ""); printf(_("%s - No such file or directory\n"), nm->name); return -1; } strmode(nm->st.st_mode, md); if (nm->mountpoint) md[0] = 'D'; if (!(flags & NAMEI_VERTICAL)) { for (i = 0; i < nm->level; i++) fputs(" ", stdout); fputc(' ', stdout); } if (flags & NAMEI_MODES) printf("%s", md); else printf("%c", md[0]); if (flags & NAMEI_OWNERS) { printf(" %-*s", uwidth, get_id(ucache, nm->st.st_uid)->name); printf(" %-*s", gwidth, get_id(gcache, nm->st.st_gid)->name); } if (flags & NAMEI_VERTICAL) for (i = 0; i < nm->level; i++) fputs(" ", stdout); if (S_ISLNK(nm->st.st_mode)) printf(" %s -> %s\n", nm->name, nm->abslink + nm->relstart); else printf(" %s\n", nm->name); } return 0; }
static void printmodestr(FTSENT *p) { char buf[MODE_BUFSZ]; mode_t m=p->fts_statp->st_mode; strmode(m, buf); printf("%s ", buf); }
void print_long(OBJ *obj) { struct stat *st; FTSENT *p; char mode[MODE_BUFF_SIZE]; p = obj->obj_list; if(!obj->obj_isroot) print_total(obj->obj_size_total); while (p != NULL) { if (p->fts_number == FTS_NO_PRINT) { p = p->fts_link; continue; } st = p->fts_statp; /* print inode */ if (f_ino) printf("%*"PRIuMAX" ", obj->obj_ino_max, (uintmax_t)st->st_ino); /* print number of blocks */ if (f_block) { print_block((int64_t)st->st_blocks * DEFAULT_BSIZE, obj->obj_block_max); } /* print mode */ strmode(st->st_mode, mode); printf("%11s ", mode); /* print number of links */ printf("%*"PRIuMAX" ", obj->obj_link_max, (uintmax_t)st->st_nlink); /* print owner and group */ print_user_and_group(st->st_uid, st->st_gid, obj->obj_user_max, obj->obj_group_max); /* print size */ if (S_ISCHR(st->st_mode) || S_ISBLK(st->st_mode)) print_device(st->st_rdev, obj->obj_major_max, obj->obj_minor_max); else print_size(st->st_size, obj->obj_size_max); print_date(st); print_name(p, WITH_LINK); putchar('\n'); p = p->fts_link; } }
/* * === FUNCTION ====================================================================== * Name: print_modestr * Description: Calls strmode to print the 9-character permission string. * ===================================================================================== */ static inline void print_modestr(mode_t mode) { char modestr[10]; modestr[9] = '\0'; strmode(mode, modestr); printf("mode: %s\n", modestr); }
void fxp_local_do_ls( QString args , QVector<QMap<char, QString> > & fileinfos) { int sz; QMap<char,QString> thefile; char file_size[32]; char file_date[64]; char file_type[32]; char fname[PATH_MAX+1] = {0}; //char the_path[PATH_MAX+1]; QString the_path ; struct tm *ltime; struct stat thestat ; DIR * dh = opendir( GlobalOption::instance()->locale_codec->fromUnicode(args) ) ; struct dirent * entry = NULL ; fileinfos.clear(); while ((entry = readdir(dh)) != NULL) { thefile.clear(); memset(&thestat, 0, sizeof(thestat)); //strcpy(the_path,args); //strcat(the_path,"/"); //strcat(the_path,entry->d_name); the_path = args + "/" + GlobalOption::instance()->locale_codec->toUnicode(entry->d_name); if (strcmp(entry->d_name, ".") == 0) goto out_point; if (strcmp(entry->d_name, "..") == 0) goto out_point; if (stat(GlobalOption::instance()->locale_codec->fromUnicode(the_path), &thestat) != 0) continue; ltime = localtime(&thestat.st_mtime); sprintf(file_size,"%llu", (long long)thestat.st_size); strmode(thestat.st_mode, file_type); if (ltime != NULL) { if (time(NULL) - thestat.st_mtime < (365*24*60*60)/2) sz = strftime(file_date, sizeof file_date, "%Y/%m/%d %H:%M:%S", ltime); else sz = strftime(file_date, sizeof file_date, "%Y/%m/%d %H:%M:%S", ltime); if (sz == 0) { } } strcpy(fname, entry->d_name); thefile.insert( 'N', GlobalOption::instance()->locale_codec->toUnicode(fname) ); thefile.insert( 'T', QString(file_type) ); thefile.insert( 'S', QString(file_size ) ); thefile.insert( 'D', QString( file_date ) ); fileinfos.push_back(thefile); out_point: continue ; } closedir(dh); }
void filemodestring (struct stat const *statp, char *str) { strmode (statp->st_mode, str); if (S_TYPEISSEM (statp)) str[0] = 'F'; else if (S_TYPEISMQ (statp)) str[0] = 'Q'; else if (S_TYPEISSHM (statp)) str[0] = 'S'; }
void printlong(DISPLAY *dp) { struct stat *sp; FTSENT *p; NAMES *np; char buf[20]; if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) (void)printf("total %llu\n", howmany(dp->btotal, blocksize)); for (p = dp->list; p; p = p->fts_link) { if (IS_NOPRINT(p)) continue; sp = p->fts_statp; if (f_inode) (void)printf("%*llu ", dp->s_inode, (unsigned long long)sp->st_ino); if (f_size) (void)printf("%*lld ", dp->s_block, howmany((long long)sp->st_blocks, blocksize)); (void)strmode(sp->st_mode, buf); np = p->fts_pointer; (void)printf("%s %*u ", buf, dp->s_nlink, sp->st_nlink); if (!f_grouponly) (void)printf("%-*s ", dp->s_user, np->user); (void)printf("%-*s ", dp->s_group, np->group); if (f_flags) (void)printf("%-*s ", dp->s_flags, np->flags); if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode)) (void)printf("%3d, %3d ", major(sp->st_rdev), minor(sp->st_rdev)); else if (dp->bcfile) (void)printf("%*s%*lld ", 8 - dp->s_size, "", dp->s_size, (long long)sp->st_size); else printsize(dp->s_size, sp->st_size); if (f_accesstime) printtime(sp->st_atime); else if (f_statustime) printtime(sp->st_ctime); else printtime(sp->st_mtime); (void)mbsprint(p->fts_name, 1); if (f_type || (f_typedir && S_ISDIR(sp->st_mode))) (void)printtype(sp->st_mode); if (S_ISLNK(sp->st_mode)) printlink(p); (void)putchar('\n'); } }
check(char *path, char *name, struct stat *sp) #endif { int ch, first; #ifdef __GNO__ static #endif char modep[15], flagsp[128]; /* Check -i first. */ if (iflag) (void)fprintf(stderr, "remove %s? ", path); else { /* * If it's not a symbolic link and it's unwritable and we're * talking to a terminal, ask. Symbolic links are excluded * because their permissions are meaningless. Check stdin_ok * first because we may not have stat'ed the file. */ #ifndef __GNO__ if (!stdin_ok || S_ISLNK(sp->st_mode) || !access(name, W_OK) && !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) && (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid)) return (1); strmode(sp->st_mode, modep); strcpy(flagsp, flags_to_string(sp->st_flags, NULL)); if (*flagsp) strcat(flagsp, " "); (void)fprintf(stderr, "override %s%s%s/%s %sfor %s? ", modep + 1, modep[9] == ' ' ? "" : " ", user_from_uid(sp->st_uid, 0), group_from_gid(sp->st_gid, 0), *flagsp ? flagsp : "", path); #else if (!stdin_ok || !access(name,W_OK)) return (1); (void)fprintf(stderr, "override protection for %s? ", path); #endif } (void)fflush(stderr); first = ch = getchar(); while (ch != '\n' && ch != EOF) ch = getchar(); return (first == 'y' || first == 'Y'); }
int prepareBuf(char **pCache, FTSENT* pChild, Length len) { char pEntryBuf[NAME_MAX + 1]; char *pTemp = NULL; struct stat *pStat = {0}; char pMode[12]; bzero(pEntryBuf, NAME_MAX + 1); pStat = pChild->fts_statp; bzero(pMode, 12); if(flg_display != in_C && flg_display != in_x) return 0; if(!flg_dot && pChild->fts_name[0] == '.') return 1; pCache[pCacheCount] = (char*) calloc(len.column + 1, sizeof(char *)); strmode(pStat->st_mode, pMode); // inode if(flg_i) { (void) snprintf(pEntryBuf, sizeof(pEntryBuf), "%*lu ",len.l_ino,pStat->st_ino); (void) strcat(pCache[pCacheCount], pEntryBuf); } // block if(flg_s) { pTemp = resetBlock(pStat->st_blocks); (void) snprintf(pEntryBuf, sizeof(pEntryBuf), "%*s ", len.l_blocks, pTemp); (void) strcat(pCache[pCacheCount], pEntryBuf); free(pTemp); } // name pTemp = displayName(pChild->fts_accpath); (void) snprintf(pEntryBuf, sizeof(pEntryBuf), "%-*s",len.l_name, pTemp); (void) strcat(pCache[pCacheCount], pEntryBuf); free(pTemp); // -F if(flg_F) { (void) snprintf(pEntryBuf, sizeof(pEntryBuf), "%c ", displayChar(pMode)); (void) strcat(pCache[pCacheCount], pEntryBuf); } // printf("pre: %s\n", pCache[pCacheCount]); pCacheCount++; return (1); }
/* * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh */ char * ls_file(const char *name, const struct stat *st, int remote, int si_units) { int ulen, glen, sz = 0; struct tm *ltime = localtime(&st->st_mtime); char *user, *group; char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1]; char sbuf[FMT_SCALED_STRSIZE]; time_t now; strmode(st->st_mode, mode); if (!remote) { user = user_from_uid(st->st_uid, 0); } else { snprintf(ubuf, sizeof ubuf, "%u", (u_int)st->st_uid); user = ubuf; } if (!remote) { group = group_from_gid(st->st_gid, 0); } else { snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid); group = gbuf; } if (ltime != NULL) { now = time(NULL); if (now - (365*24*60*60)/2 < st->st_mtime && now >= st->st_mtime) sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime); else sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime); } if (sz == 0) tbuf[0] = '\0'; ulen = MAX(strlen(user), 8); glen = MAX(strlen(group), 8); if (si_units) { fmt_scaled((long long)st->st_size, sbuf); snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8s %s %s", mode, (u_int)st->st_nlink, ulen, user, glen, group, sbuf, tbuf, name); } else { snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8llu %s %s", mode, (u_int)st->st_nlink, ulen, user, glen, group, (unsigned long long)st->st_size, tbuf, name); } return xstrdup(buf); }
void filemodestring (struct stat const *statp, char *str) { strmode (statp->st_mode, str); if (S_TYPEISSEM (statp)) str[0] = 'F'; else if (IS_MIGRATED_FILE (statp)) str[0] = 'M'; else if (S_TYPEISMQ (statp)) str[0] = 'Q'; else if (S_TYPEISSHM (statp)) str[0] = 'S'; else if (S_TYPEISTMO (statp)) str[0] = 'T'; }
/* Dumps given metadata */ void mentries_dump(struct metahash *mhash) { const struct metaentry *mentry; char mode[11 + 1] = ""; char date[12 + 2 + 2 + 2*1 + 1 + 2 + 2 + 2 + 2*1 + 1] = ""; char zone[5 + 1] = ""; struct tm cal; for (int key = 0; key < HASH_INDEXES; key++) { for (mentry = mhash->bucket[key]; mentry; mentry = mentry->next) { strmode(mentry->mode, mode); localtime_r(&mentry->mtime, &cal); strftime(date, sizeof(date), "%F %T", &cal); strftime(zone, sizeof(zone), "%z", &cal); printf("%s\t%s\t%s\t%s.%09ld %s\t%s%s\n", mode, mentry->owner, mentry->group, date, mentry->mtimensec, zone, mentry->path, S_ISDIR(mentry->mode) ? "/" : ""); for (int i = 0; i < mentry->xattrs; i++) { printf("\t\t\t\t%s%s\t%s=", mentry->path, S_ISDIR(mentry->mode) ? "/" : "", mentry->xattr_names[i]); ssize_t p = 0; for (; p < mentry->xattr_lvalues[i]; p++) { const char ch = mentry->xattr_values[i][p]; if ((unsigned)(ch - 32) > 126 - 32) { p = -1; break; } } if (p >= 0) printf("\"%.*s\"\n", (int)mentry->xattr_lvalues[i], mentry->xattr_values[i]); else { printf("0x"); for (p = 0; p < mentry->xattr_lvalues[i]; p++) printf("%02hhx", (char)mentry->xattr_values[i][p]); printf("\n"); } } } } }
static void print_namei(struct namei *nm, char *path) { struct namei *prev = NULL; int i; if (path) printf("f: %s\n", path); for (; nm; prev = nm, nm = nm->next) { char md[11]; strmode(nm->st.st_mode, md); if (nm->mountpoint) md[0] = 'D'; if (!(flags & NAMEI_VERTICAL)) { for (i = 0; i < nm->level; i++) fputs(" ", stdout); fputc(' ', stdout); } if (flags & NAMEI_MODES) printf("%s", md); else printf("%c", md[0]); if (flags & NAMEI_OWNERS) { printf(" %-*s", uwidth, get_id(ucache, nm->st.st_uid)->name); printf(" %-*s", gwidth, get_id(gcache, nm->st.st_gid)->name); } if (flags & NAMEI_VERTICAL) for (i = 0; i < nm->level; i++) fputs(" ", stdout); if (S_ISLNK(nm->st.st_mode)) printf(" %s -> %s\n", nm->name, nm->abslink + nm->relstart); else printf(" %s\n", nm->name); } }
static int check(const char *path, const char *name, struct stat *sp) { int ch, first; char modep[15], *flagsp; /* Check -i first. */ if (iflag) (void)fprintf(stderr, "remove %s? ", path); else { /* * If it's not a symbolic link and it's unwritable and we're * talking to a terminal, ask. Symbolic links are excluded * because their permissions are meaningless. Check stdin_ok * first because we may not have stat'ed the file. */ if (!stdin_ok || S_ISLNK(sp->st_mode) || (!access(name, W_OK) && !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) && (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid))) return (1); strmode(sp->st_mode, modep); if ((flagsp = fflagstostr(sp->st_flags)) == NULL) err(1, "fflagstostr"); if (Pflag) errx(1, "%s: -P was specified, but file is not writable", path); (void)fprintf(stderr, "override %s%s%s/%s %s%sfor %s? ", modep + 1, modep[9] == ' ' ? "" : " ", user_from_uid(sp->st_uid, 0), group_from_gid(sp->st_gid, 0), *flagsp ? flagsp : "", *flagsp ? " " : "", path); free(flagsp); } (void)fflush(stderr); first = ch = getchar(); while (ch != '\n' && ch != EOF) ch = getchar(); return (first == 'y' || first == 'Y'); }
char * ssl_load_file(const char *name, off_t *len, mode_t perm) { struct stat st; off_t size; char *buf = NULL; int fd, saved_errno; char mode[12]; if ((fd = open(name, O_RDONLY)) == -1) return (NULL); if (fstat(fd, &st) != 0) goto fail; if (st.st_uid != 0) { log_warnx("warn: %s: not owned by uid 0", name); errno = EACCES; goto fail; } if (st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO) & ~perm) { strmode(perm, mode); log_warnx("warn: %s: insecure permissions: must be at most %s", name, &mode[1]); errno = EACCES; goto fail; } size = st.st_size; if ((buf = calloc(1, size + 1)) == NULL) goto fail; if (read(fd, buf, size) != size) goto fail; close(fd); *len = size + 1; return (buf); fail: if (buf != NULL) free(buf); saved_errno = errno; close(fd); errno = saved_errno; return (NULL); }
void printlong(char *name, char *accpath, struct stat *sb) { char modep[15]; (void)printf("%6lu %8"PRId64" ", (u_long) sb->st_ino, sb->st_blocks); (void)strmode(sb->st_mode, modep); (void)printf("%s %3u %-*s %-*s ", modep, sb->st_nlink, MAXLOGNAME - 1, user_from_uid(sb->st_uid, 0), MAXLOGNAME - 1, group_from_gid(sb->st_gid, 0)); if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode)) (void)printf("%#8jx ", (uintmax_t)sb->st_rdev); else (void)printf("%8"PRId64" ", sb->st_size); printtime(sb->st_mtime); (void)printf("%s", name); if (S_ISLNK(sb->st_mode)) printlink(accpath); (void)putchar('\n'); }
void printlong(char *name, char *accpath, struct stat *sb) { char modep[15]; (void)printf("%6u %4lld ", sb->st_ino, (long long)sb->st_blocks); (void)strmode(sb->st_mode, modep); (void)printf("%s %3u %-*.*s %-*.*s ", modep, sb->st_nlink, NAME_WIDTH, UT_NAMESIZE, user_from_uid(sb->st_uid, 0), NAME_WIDTH, UT_NAMESIZE, group_from_gid(sb->st_gid, 0)); if (S_ISCHR(sb->st_mode) || S_ISBLK(sb->st_mode)) (void)printf("%3d, %3d ", major(sb->st_rdev), minor(sb->st_rdev)); else (void)printf("%8lld ", (long long)sb->st_size); printtime(sb->st_mtime); (void)printf("%s", name); if (S_ISLNK(sb->st_mode)) printlink(accpath); (void)putchar('\n'); }
void print_stat( const compiled_stats_t const * stats ) { if( !stats ) return; struct passwd* user_info = getpwuid( stats->uid ); struct group* group_info = getgrgid( stats->gid ); printf( " Relative Name ...... %s", stats->name ); // add '/' if it is a dir if( S_ISDIR( stats->mode ) ) printf( "/\n" ); else printf( "\n" ); printf( " Inode Number ....... %ld\n", stats->inode ); // add (self) if gid or uid is the currently // running user/group if( stats->uid == getuid() ) printf( " Owner .............. %s (self)\n", user_info->pw_name ); else printf( " Owner .............. %s\n", user_info->pw_name ); if( stats->gid == getgid() ) printf( " Group .............. %s (self)\n", group_info->gr_name ); else printf( " Group .............. %s\n", group_info->gr_name ); printf( " File Size (bytes) .. %ld\n", stats->size ); printf( " Last Modified ...... %s", ctime( &(stats->mtime) ) ); char perm[12]; strmode( stats->mode, perm ); printf( " Access Rights ...... %s\n", perm ); printf( "\n" ); }
/* * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh */ static char * ls_file(char *name, struct stat *st) { int ulen, glen, sz = 0; struct passwd *pw; struct group *gr; struct tm *ltime = localtime(&st->st_mtime); char *user, *group; char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1]; strmode(st->st_mode, mode); if ((pw = getpwuid(st->st_uid)) != NULL) { user = pw->pw_name; } else { snprintf(ubuf, sizeof ubuf, "%u", (u_int)st->st_uid); user = ubuf; } if ((gr = getgrgid(st->st_gid)) != NULL) { group = gr->gr_name; } else { snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid); group = gbuf; } if (ltime != NULL) { if (time(NULL) - st->st_mtime < (365*24*60*60)/2) sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime); else sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime); } if (sz == 0) tbuf[0] = '\0'; ulen = MAX(strlen(user), 8); glen = MAX(strlen(group), 8); snprintf(buf, sizeof buf, "%s %3d %-*s %-*s %8llu %s %s", mode, st->st_nlink, ulen, user, glen, group, (u_int64_t)st->st_size, tbuf, name); return xstrdup(buf); }
static int check(char *path, char *name, struct stat *sp) { int ch, first; char modep[15]; /* Check -i first. */ if (iflag) (void)fprintf(stderr, "remove '%s'? ", path); else { /* * If it's not a symbolic link and it's unwritable and we're * talking to a terminal, ask. Symbolic links are excluded * because their permissions are meaningless. Check stdin_ok * first because we may not have stat'ed the file. */ if (!stdin_ok || S_ISLNK(sp->st_mode) || !(access(name, W_OK) && (errno != ETXTBSY))) return (1); strmode(sp->st_mode, modep); if (Pflag) { warnx( "%s: -P was specified but file could not" " be overwritten", path); return 0; } (void)fprintf(stderr, "override %s%s%s:%s for '%s'? ", modep + 1, modep[9] == ' ' ? "" : " ", user_from_uid(sp->st_uid, 0), group_from_gid(sp->st_gid, 0), path); } (void)fflush(stderr); first = ch = getchar(); while (ch != '\n' && ch != EOF) ch = getchar(); return (first == 'y' || first == 'Y'); }
/* checkdir - check all components of a path for root access. If * st_dev!=0 then it's a local file system, and root always has * access; if it's 0 then it's NFS, so check access for 'other * users'. */ int checkdir(const char *path) { #ifndef __APPLE__ struct stat root, sb; int fd, tmp, mask = S_IXOTH; char name[1024], mode[32]; stat("/", &root); /* can't fail */ fd = open(path, O_RDONLY); if (fd < 0 || fstat(fd, &sb) < 0) { fprintf(stderr, "can't read %s: %s\n", path, strerror(errno)); return 0; } while (!(sb.st_dev == root.st_dev && sb.st_ino == root.st_ino)) { if (major(sb.st_dev) == 0 && !(sb.st_mode & mask)) { strmode(mode, sb.st_mode); printf("NFS permissions error on '%s' : \n\t%s\n", fd2path(fd, name, sizeof(name)), mode); close(fd); return 0; } tmp = fd; if ((fd = openat(tmp, "..", O_RDONLY)) < 0 || (fstat(fd, &sb) < 0)) { fprintf(stderr, "error reading %s/..: %s\n", fd2path(tmp, name, sizeof(name)), strerror(errno)); close(tmp); return 0; } close(tmp); } close(fd); #endif return 1; }
static void ktrstat(const struct stat *statp) { char mode[12]; struct passwd *pwd; struct group *grp; /* * note: ktrstruct() has already verified that statp points to a * buffer exactly sizeof(struct stat) bytes long. */ printf("struct stat { "); strmode(statp->st_mode, mode); printf("dev=%d, ino=%llu, mode=%s, nlink=%u, ", statp->st_dev, (unsigned long long)statp->st_ino, mode, statp->st_nlink); if (resolv == 0 || (pwd = getpwuid(statp->st_uid)) == NULL) printf("uid=%u, ", statp->st_uid); else printf("uid=\"%s\", ", pwd->pw_name); if (resolv == 0 || (grp = getgrgid(statp->st_gid)) == NULL) printf("gid=%u, ", statp->st_gid); else printf("gid=\"%s\", ", grp->gr_name); printf("rdev=%d, ", statp->st_rdev); printf("atime="); print_timespec(&statp->st_atim, 0); printf(", mtime="); print_timespec(&statp->st_mtim, 0); printf(", ctime="); print_timespec(&statp->st_ctim, 0); printf(", size=%lld, blocks=%lld, blksize=%u, flags=0x%x, gen=0x%x", statp->st_size, statp->st_blocks, statp->st_blksize, statp->st_flags, statp->st_gen); printf(" }\n"); }