void show_file_info(char *filename, struct stat *info_p){ char *uid_to_name(), *ctime(), *gid_to_name(), *filemode(); void mode_to_letters(); char modestr[11]; mode_to_letters(info_p->st_mode, modestr); printf("%s", modestr); printf(" %4d", (int)info_p->st_nlink); printf(" %-8s", uid_to_name(info_p->st_uid)); printf(" %-8s", gid_to_name(info_p->st_gid)); printf(" %8ld", (long)info_p->st_size); printf(" %.12s", ctime(&info_p->st_mtime)+4); printf(" %s\n", filename); }
static int print_inhibitors(sd_bus *bus, sd_bus_error *error) { _cleanup_bus_message_unref_ sd_bus_message *reply = NULL; const char *what, *who, *why, *mode; unsigned int uid, pid; unsigned n = 0; int r; r = sd_bus_call_method( bus, "org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", "ListInhibitors", error, &reply, ""); if (r < 0) return r; r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)"); if (r < 0) return bus_log_parse_error(r); while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) { _cleanup_free_ char *comm = NULL, *u = NULL; get_process_comm(pid, &comm); u = uid_to_name(uid); printf(" Who: %s (UID "UID_FMT"/%s, PID "PID_FMT"/%s)\n" " What: %s\n" " Why: %s\n" " Mode: %s\n\n", who, uid, strna(u), pid, strna(comm), what, why, mode); n++; } if (r < 0) return bus_log_parse_error(r); r = sd_bus_message_exit_container(reply); if (r < 0) return bus_log_parse_error(r); printf("%u inhibitors listed.\n", n); return 0; }
void show_file_info(char *filename,struct stat *info_p) { char *uid_to_name(),*ctime(),*gid_to_name(),*filemode(); // void mode_to_letters(); char modestr[11]; mode_to_letters(info_p->st_mode,modestr); printf("%s",modestr); printf("%3d ",(int)info_p->st_nlink); printf("%-5s",uid_to_name(info_p->st_uid)); printf("%-5s",gid_to_name(info_p->st_gid)); printf("%10ld ",(long)info_p->st_size); printf("%.14s",4+ctime(&info_p->st_mtime)); printf(" %s\n",basename(filename)); }
void show_file_info(char *fname, struct stat *info) { char modestr[11]; mode_to_letters(info->st_mode, modestr); printf("%s", modestr); printf(" %4d", (int)info->st_nlink); printf(" %-6s" , uid_to_name(info->st_uid) ); printf(" %-6s" , gid_to_name(info->st_gid) ); printf(" %8ld" , (long)info->st_size); printf(" %.12s", 4+ctime(&info->st_mtime)); printf(" %s\n", fname); }
void show_file_info(const char *filename, struct stat *info_p) { char modestr[11]; mode_to_letters(info_p->st_mode, modestr); printf("%s", modestr); printf(" %2d", (int)info_p->st_nlink); printf(" %-5s", uid_to_name(info_p->st_uid)); printf(" %-5s", gid_to_name(info_p->st_gid)); printf(" %6ld", (long)info_p->st_size); printf(" %.12s", 4+ctime(&info_p->st_mtime)); printf(" %s\n", filename); }
void show_file_info(char * filename, struct stat * statp) { char *filemode(), *uid_to_name(), *gid_to_name(), *ctime(); char modestr[11]; mode_to_letter(statp->st_mode, modestr); printf("%s ", modestr); printf("%4d ", (int)statp->st_nlink); printf("%-8s ", uid_to_name(statp->st_uid)); printf("%-8s ", gid_to_name(statp->st_gid)); printf("%8ld ", (long)statp->st_size); printf("%.12s ", 4+ctime(&statp->st_mtime)); printf("%s\n", filename); }
void show_file_info(char *filename, struct stat *info_p) { char modestr[11]; mode_to_letters(info_p->st_mode, modestr); printf("%s\t", modestr); printf("%4d\t", (int)info_p->st_nlink); printf("%-8s\t", uid_to_name(info_p->st_uid)); printf("%-8s\t", gid_to_name(info_p->st_gid)); printf("%8ld\t", (long)info_p->st_size); char * tim = ctime(&(info_p->st_mtim)); printf("%.12s\t", 4 + ctime(&(info_p->st_mtim))); printf("%s\n", filename); }
void show_file_info(char *filename,struct stat *info_p,int maxlink,int maxsize) /* * display the info about 'filename'. The info is stored in struct at *info_p */ { char *uid_to_name(short uid),*gid_to_name(short gid),*filemode(int mode); struct tm *mtime = gmtime(&info_p->st_mtime); printf("%s ",filemode(info_p->st_mode)); printf("%*d ",maxlink,(int)info_p->st_nlink); /* links */ printf("%s ",uid_to_name(info_p->st_uid)); printf("%s ",gid_to_name(info_p->st_gid)); printf("%*ld ",maxsize,(long)info_p->st_size); /* size */ printf("%d-%02d-%02d ",(mtime->tm_year) + 1900,(mtime->tm_mon) + 1,mtime->tm_mday); printf("%02d:%02d ",mtime->tm_hour + 8,mtime->tm_min); printf("%s \n",filename); /* print name */ }
static void extract_l_info(t_l_info *info, t_filedir *filedir) { unsigned int temp; info->block_mem_total += filedir->stats->st_blocks; temp = digitc(filedir->stats->st_nlink); if (info->link_spacing < temp) info->link_spacing = temp; temp = ft_strlen(uid_to_name(filedir->stats->st_uid)); if (info->user_spacing < temp) info->user_spacing = temp; temp = ft_strlen(gid_to_name(filedir->stats->st_gid)); if (info->group_spacing < temp) info->group_spacing = temp; temp = ls_size_size(info, filedir->stats); if (info->size_spacing < temp) info->size_spacing = temp; }
void show_file_info(char *filename, struct stat *info_p) /* * display the info about filename. The info is stored in struct at *info_p * */ { char *uid_to_name(), *ctime(), *gid_to_name(), *filemode(); void mode_to_letters(); char modestr[11]; mode_to_letters(info_p->st_mode, modestr); printf("%s", modestr); printf("%4d", (int)info_p->st_nlink); printf(" %-8s", uid_to_name(info_p->st_uid)); printf(" %-8s", gid_to_name(info_p->st_gid)); printf(" %8ld", (long)info_p->st_size); printf(" %.12s", 4+ctime(&info_p->st_mtime)); printf(" %s\n", filename); }
extern void show_stat_info(char *filename, struct stat *stat_buf_p) { char letters[10]; mode_to_letters(stat_buf_p->st_mode, letters); printf("%s", letters); printf("%ld ", stat_buf_p->st_nlink); char *uid_name = uid_to_name(stat_buf_p->st_uid); printf("%-8s ", uid_name); char *gid_name = gid_to_name(stat_buf_p->st_gid); printf("%-8s ", gid_name); printf("%ld ", stat_buf_p->st_size); char *time_str = ctime(&(stat_buf_p->st_mtime)) + 4; printf("%.12s ", time_str); printf("%s ", filename); printf("\n"); }
void show_file_info(char* filename, struct stat * info_p) { char *uid_to_name(), *ctime(), *gid_to_name(), *filemode(); void mode_to_letters(); char modestr[11]; char newFilename[20] = "./" ; // printf("run show file info\n"); mode_to_letters( info_p->st_mode, modestr ); if( (strncmp(".", filename ,1) == 0 ) || ( strncmp("..", filename, 2) == 0 )) { return ; } printf(" %s " , modestr ); printf(" %4d ", (int)info_p->st_nlink); printf(" %-8s ", uid_to_name(info_p->st_uid) ); printf(" %-8s ", gid_to_name(info_p->st_gid) ); printf(" %8ld ", (long)info_p->st_size ); printf(" %.12s ", 4 + ctime(&info_p->st_mtime)); printf(" %s\n", filename); if ((strncmp ("d", modestr,1) == 0) && (flag == OPTION) ) { printf("\n ++++++++++ Directory %s ++++++++++ \n", filename); // strcat(filename,"/."); strcat(newFilename ,filename); printf("%s\n",newFilename); chdir(newFilename); do_ls("."); chdir("../"); printf("\n ++++++++++ Directory %s End +++++++++ \n", filename); } }
static int rename_service(sd_bus *a, sd_bus *b) { _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL; _cleanup_free_ char *p = NULL, *name = NULL; const char *comm; char **cmdline; uid_t uid; pid_t pid; int r; assert(a); assert(b); r = sd_bus_get_owner_creds(b, SD_BUS_CREDS_EUID|SD_BUS_CREDS_PID|SD_BUS_CREDS_CMDLINE|SD_BUS_CREDS_COMM|SD_BUS_CREDS_AUGMENT, &creds); if (r < 0) return r; r = sd_bus_creds_get_euid(creds, &uid); if (r < 0) return r; r = sd_bus_creds_get_pid(creds, &pid); if (r < 0) return r; r = sd_bus_creds_get_cmdline(creds, &cmdline); if (r < 0) return r; r = sd_bus_creds_get_comm(creds, &comm); if (r < 0) return r; name = uid_to_name(uid); if (!name) return -ENOMEM; p = strv_join(cmdline, " "); if (!p) return -ENOMEM; /* The status string gets the full command line ... */ sd_notifyf(false, "STATUS=Processing requests from client PID "PID_FMT" (%s); UID "UID_FMT" (%s)", pid, p, uid, name); /* ... and the argv line only the short comm */ if (arg_command_line_buffer) { size_t m, w; m = strlen(arg_command_line_buffer); w = snprintf(arg_command_line_buffer, m, "[PID "PID_FMT"/%s; UID "UID_FMT"/%s]", pid, comm, uid, name); if (m > w) memzero(arg_command_line_buffer + w, m - w); } log_debug("Running on behalf of PID "PID_FMT" (%s), UID "UID_FMT" (%s), %s", pid, p, uid, name, a->unique_name); return 0; }
int main(int argc, char *argv[]) { int i; gid_t user_gid; uid_t user_uid; int is_only_group = 0; int is_only_group_all = 0; int is_name = 0; int is_only_user = 0; int is_real = 0; //bug!: myid -Gn - err char *opts="rgGnu"; int opt, opt_c = 0; while((opt=getopt(argc,argv,opts))!=-1){ switch(opt){ case 'a': break; case 'g': is_only_group = 1; break; case 'G': is_only_group_all = 1; break; case 'n': is_name = 1; break; case 'r': is_real = 1; break; case 'u': is_only_user = 1; break; } opt_c++; } //without username if(argc == 1 || (opt_c + 1) == argc){ if( is_real){ user_gid = getgid(); user_uid = getuid(); } else{ user_gid = getegid(); user_uid = geteuid(); } } //with username in command line else{ user_gid = name_to_uid(argv[1+opt_c]); user_uid = name_to_gid(argv[1+opt_c]); } // -g flag. if( is_only_group){ if( is_name) printf("%s\n",gid_to_name(user_gid)); else printf("%d\n",user_gid); return 0; } // -G flag. if( is_only_group_all){ int list_size = 0; gid_t * user_groups = get_groups(gid_to_name(user_gid),&list_size); for( i=0;i<list_size;i++) if( is_name) printf("%s ",gid_to_name(user_groups[i])); else printf("%d ",user_groups[i]); printf("\n"); return 0; } // -u flag. if( is_only_user){ if( is_name) printf("%s\n",uid_to_name(user_uid)); else printf("%d\n",user_uid); return 0; } //wrong flags if( is_name && is_real){ fprintf(stderr,"id: cannot print only names or real IDs in default format\n"); return EARG; } //common case: printf("uid=%d(%s) ",user_uid,uid_to_name(user_uid)); printf("gid=%d(%s) ",user_gid,gid_to_name(user_gid)); int list_size = 0; gid_t * user_groups = get_groups(gid_to_name(user_gid),&list_size); printf("groups="); printf("%s(%d)",gid_to_name(user_gid),user_gid); for( i=0;i<list_size;i++) printf("%d(%s) ", user_groups[i], gid_to_name(user_groups[i])); printf("\n"); return 0; }
static void test_install_printf(void) { char name[] = "name.service", path[] = "/run/systemd/system/name.service"; UnitFileInstallInfo i = { .name = name, .path = path, }; UnitFileInstallInfo i2 = { .name= name, .path = path, }; char name3[] = "*****@*****.**", path3[] = "/run/systemd/system/name.service"; UnitFileInstallInfo i3 = { .name = name3, .path = path3, }; UnitFileInstallInfo i4 = { .name = name3, .path = path3, }; _cleanup_free_ char *mid = NULL, *bid = NULL, *host = NULL, *uid = NULL, *user = NULL; assert_se(specifier_machine_id('m', NULL, NULL, &mid) >= 0 && mid); assert_se(specifier_boot_id('b', NULL, NULL, &bid) >= 0 && bid); assert_se((host = gethostname_malloc())); assert_se((user = uid_to_name(getuid()))); assert_se(asprintf(&uid, UID_FMT, getuid()) >= 0); #define expect(src, pattern, result) \ do { \ _cleanup_free_ char *t = NULL; \ _cleanup_free_ char \ *d1 = strdup(i.name), \ *d2 = strdup(i.path); \ assert_se(install_full_printf(&src, pattern, &t) >= 0 || !result); \ memzero(i.name, strlen(i.name)); \ memzero(i.path, strlen(i.path)); \ assert_se(d1 && d2); \ if (result) { \ printf("%s\n", t); \ assert_se(streq(t, result)); \ } else assert_se(t == NULL); \ strcpy(i.name, d1); \ strcpy(i.path, d2); \ } while (false) expect(i, "%n", "name.service"); expect(i, "%N", "name"); expect(i, "%p", "name"); expect(i, "%i", ""); expect(i, "%u", user); expect(i, "%U", uid); expect(i, "%m", mid); expect(i, "%b", bid); expect(i, "%H", host); expect(i2, "%u", user); expect(i2, "%U", uid); expect(i3, "%n", "*****@*****.**"); expect(i3, "%N", "name@inst"); expect(i3, "%p", "name"); expect(i3, "%u", user); expect(i3, "%U", uid); expect(i3, "%m", mid); expect(i3, "%b", bid); expect(i3, "%H", host); expect(i4, "%u", user); expect(i4, "%U", uid); }
/* * print_report() - output a record for the report request. */ static void print_report(int rec, struct acctent *ac) { char buf[160]; char *start_time; char *cp; char *cp1; if (once) { if (uptime_start) { start_time = ctime(&uptime_start); } else { start_time = "Unknown\n"; } fprintf(stdout, "\n\n"); fprintf(stdout, "SYSTEM BOOT TIME STARTING AT %s", start_time); fprintf(stdout, "For %s %s %s %s %s\n", cfg_sn->sysname, cfg_sn->nodename, cfg_sn->release, cfg_sn->version, cfg_sn->machine); fprintf(stdout, "Preserved Accounting Jobs (Jobs which are " "continued).\n"); fprintf(stdout, "==========================================" "==========\n\n"); fprintf(stdout, " JOB ID USERS PROJECT ID " " STARTED\n"); fprintf(stdout, "------------------ ----- ------------- " "------------------------\n"); once = FALSE; } switch (rec) { case REC_EOP: /* * Report about this preserved process. */ if (ac->csa != NULL ) { jobid = ac->csa->ac_jid; sprintf(buf, "%#18llx ", jobid); user = ac->csa->ac_uid; cp = uid_to_name(user); if (cp[0] == '?') { sprintf(&buf[19], "%8d ", user); } else { sprintf(&buf[19], "%8s ", cp); } project = ac->csa->ac_prid; if ((cp = prid_to_name(project)) == NULL) { cp="Unknown"; } sprintf(&buf[28], "%15s ", cp); if (ac->csa->ac_btime < btime) { btime = ac->csa->ac_btime; } fprintf(stdout, "%s", buf); } if (btime < MAXTIME) { fprintf(stdout, " %24s", ctime(&btime)); } else fprintf(stdout, "\n"); break; case REC_JOB: { struct acctsoj *soj; struct accteoj *eoj; soj = ac->soj; eoj = ac->eoj; if (soj != NULL) { user = soj->ac_uid; cp = uid_to_name(user); if (cp[0] == '?') { sprintf(buf, "%8d ", user); } else { sprintf(buf, "%8s ", cp); } if (soj->ac_type == AC_SOJ) { fprintf(stdout, "%#18llx %8s SOJ record %s", soj->ac_jid, buf, ctime(&soj->ac_btime)); } else { fprintf(stdout, "%#18llx %8s ROJ record %s", soj->ac_jid, buf, ctime(&soj->ac_rstime)); } } else if (eoj != NULL) { user = eoj->ac_uid; cp = uid_to_name(user); if (cp[0] == '?') { sprintf(buf, "%8d ", user); } else { sprintf(buf, "%8s ", cp); } fprintf(stdout, "%#18llx %8s EOJ record %s", eoj->ac_jid, buf, ctime(&eoj->ac_btime)); } /* CFG records should only be associated with jid 0 */ if ((ac->cfg != NULL) && (ac->csa != NULL)) { fprintf(stdout, "CFG rec_job with jid 0x%llx \n", ac->csa->ac_jid); } } break; case REC_WKMG: { struct wkmgmtbs *wm; wm = ac->wmbs; user = wm->uid; cp = uid_to_name(user); if (cp[0] == '?') { sprintf(buf, "%8d ", user); } else { sprintf(buf, "%8s ", cp); } if (wm->jid == WM_NO_JID) { fprintf(stdout, "No job id %8s ", buf); fprintf(stdout, "%s record %s", wm->serv_provider, ctime(&wm->time)); } else { fprintf(stdout, "%#18llx %8s %s record %s", wm->jid, buf, wm->serv_provider, ctime(&wm->time)); } } break; default: acct_err(ACCT_ABORT, _("An unknown record type (%4o) was found in the '%s' routine."), rec, "print_report()"); } /* end of switch(rec) */ return; }
void show_stat_info_oneline(char *fname, struct stat *buf) { static char mode[255]; mode_to_letters(buf->st_mode, mode); printf("%s %d %s %s %s\t%s %s\n", mode, buf->st_nlink, uid_to_name(buf->st_uid), gid_to_name(buf->st_gid), bytes_to_human(buf->st_size), show_time((int) buf->st_mtime), fname); }
/* * print_wm_rec - print raw workload management requests. */ static void print_wm_rec(struct wkmgmtbs *wbuf) { time_t stime; Ndebug(" WKMG_REC: type(%d), uid(%d-%s), jid(0x%llx).\n", wbuf->type, wbuf->uid, uid_to_name(wbuf->uid), wbuf->jid); Ndebug(" WKMG_REC: ash(0x%llx), prid(%lld-%s).\n", wbuf->ash, wbuf->prid, prid_to_name(wbuf->prid)); stime = wbuf->time; Ndebug(" WKMG_REC: reqid(%lld), arrayid(%d), time: %s", wbuf->reqid, wbuf->arrayid, ctime(&stime)); switch(wbuf->type) { case WM_INFO: { int subtype = wbuf->subtype < 0 ? 0 : wbuf->subtype; Ndebug(" WM_INFO: subtype(%d - %s).\n", subtype, Info_statname[subtype]); } break; case WM_RECV: { int subtype = wbuf->subtype < 0 ? 0 : wbuf->subtype; Ndebug(" WM_RECV: subtype(%d - %s).\n", subtype, Recv_statname[subtype]); Ndebug(" WM_RECV: machname(%s), reqname(%s), quename(%s), " "qtype(%d)\n", wbuf->machname, wbuf->reqname, wbuf->quename, wbuf->qtype); } break; case WM_INIT: { int subtype = wbuf->subtype < 0 ? 0 : wbuf->subtype; Ndebug(" WM_INIT: subtype(%d - %s).\n", subtype, Init_statname[subtype]); Ndebug(" WM_INIT: machname(%s), reqname(%s), quename(%s), " "qtype(%d)\n", wbuf->machname, wbuf->reqname, wbuf->quename, wbuf->qtype); } break; case WM_TERM: { int subtype = wbuf->subtype < 0 ? 0 : wbuf->subtype; Ndebug(" WM_TERM: subtype(%d - %s).\n", subtype, Term_statname[subtype]); Ndebug(" WM_TERM: machname(%s), reqname(%s), quename(%s), " "qtype(%d)\n", wbuf->machname, wbuf->reqname, wbuf->quename, wbuf->qtype); Ndebug(" WM_TERM: code(%lld), utime(%lld), stime(%lld)\n", wbuf->code, wbuf->utime, wbuf->stime); } break; case WM_SPOOL: { int subtype = wbuf->subtype < 0 ? 0 : wbuf->subtype; if (subtype == WM_SPOOL_INIT) { Ndebug(" WM_SPOOL: subtype(%d - %s).\n", subtype, Init_statname[subtype]); } else { Ndebug(" WM_SPOOL: subtype(%d - %s).\n", subtype, Term_statname[subtype]); } Ndebug(" WM_SPOOL: machname(%s), reqname(%s), quename(%s), " "qtype(%d)\n", wbuf->machname, wbuf->reqname, wbuf->quename, wbuf->qtype); } break; case WM_CON: { int subtype; Ndebug(" WM_CON: machname(%s), reqname(%s), quename(%s), " "qtype(%d)\n", wbuf->machname, wbuf->reqname, wbuf->quename, wbuf->qtype); Ndebug(" WM_CON: code(%lld), utime(%lld), stime(%lld)\n", wbuf->code, wbuf->utime, wbuf->stime); subtype = wbuf->subtype < 0 ? 0 : wbuf->subtype; Ndebug(" WM_CON: init_type(%d - %s).\n", wbuf->subtype, Init_statname[subtype]); subtype = wbuf->term_subtype < 0 ? 0 : wbuf->term_subtype; Ndebug(" WM_CON: term_type(%d - %s).\n", wbuf->term_subtype, Term_statname[subtype]); Ndebug(" WM_CON: qwtime(%lld)\n", wbuf->qwtime); } break; default: Ndebug(" Unknown workload management request type(%d)\n", wbuf->type); } /* end of switch(type) */ return; }
static int print_inhibitors(DBusConnection *bus, DBusError *error) { _cleanup_dbus_message_unref_ DBusMessage *reply = NULL; unsigned n = 0; DBusMessageIter iter, sub, sub2; int r; r = bus_method_call_with_reply( bus, "org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", "ListInhibitors", &reply, NULL, DBUS_TYPE_INVALID); if (r < 0) return r; if (!dbus_message_iter_init(reply, &iter)) return -ENOMEM; if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) return -EIO; dbus_message_iter_recurse(&iter, &sub); while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) { const char *what, *who, *why, *mode; _cleanup_free_ char *comm = NULL, *u = NULL; dbus_uint32_t uid, pid; if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRUCT) return -EIO; dbus_message_iter_recurse(&sub, &sub2); if (bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &what, true) < 0 || bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &who, true) < 0 || bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &why, true) < 0 || bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &mode, true) < 0 || bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &uid, true) < 0 || bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, false) < 0) return -EIO; get_process_comm(pid, &comm); u = uid_to_name(uid); printf(" Who: %s (UID %lu/%s, PID %lu/%s)\n" " What: %s\n" " Why: %s\n" " Mode: %s\n\n", who, (unsigned long) uid, strna(u), (unsigned long) pid, strna(comm), what, why, mode); dbus_message_iter_next(&sub); n++; } printf("%u inhibitors listed.\n", n); return 0; }
/* Change the owner and/or group of the file specified by FTS and ENT to UID and/or GID as appropriate. If REQUIRED_UID is not -1, then skip files with any other user ID. If REQUIRED_GID is not -1, then skip files with any other group ID. CHOPT specifies additional options. Return true if successful. */ static bool change_file_owner (FTS *fts, FTSENT *ent, uid_t uid, gid_t gid, uid_t required_uid, gid_t required_gid, struct Chown_option const *chopt) { char const *file_full_name = ent->fts_path; char const *file = ent->fts_accpath; struct stat const *file_stats; struct stat stat_buf; bool ok = true; bool do_chown; bool symlink_changed = true; switch (ent->fts_info) { case FTS_D: if (chopt->recurse) { if (ROOT_DEV_INO_CHECK (chopt->root_dev_ino, ent->fts_statp)) { /* This happens e.g., with "chown -R --preserve-root 0 /" and with "chown -RH --preserve-root 0 symlink-to-root". */ ROOT_DEV_INO_WARN (file_full_name); /* Tell fts not to traverse into this hierarchy. */ fts_set (fts, ent, FTS_SKIP); /* Ensure that we do not process "/" on the second visit. */ ignore_value (fts_read (fts)); return false; } return true; } break; case FTS_DP: if (! chopt->recurse) return true; break; case FTS_NS: /* For a top-level file or directory, this FTS_NS (stat failed) indicator is determined at the time of the initial fts_open call. With programs like chmod, chown, and chgrp, that modify permissions, it is possible that the file in question is accessible when control reaches this point. So, if this is the first time we've seen the FTS_NS for this file, tell fts_read to stat it "again". */ if (ent->fts_level == 0 && ent->fts_number == 0) { ent->fts_number = 1; fts_set (fts, ent, FTS_AGAIN); return true; } if (! chopt->force_silent) error (0, ent->fts_errno, _("cannot access %s"), quote (file_full_name)); ok = false; break; case FTS_ERR: if (! chopt->force_silent) error (0, ent->fts_errno, "%s", quote (file_full_name)); ok = false; break; case FTS_DNR: if (! chopt->force_silent) error (0, ent->fts_errno, _("cannot read directory %s"), quote (file_full_name)); ok = false; break; case FTS_DC: /* directory that causes cycles */ if (cycle_warning_required (fts, ent)) { emit_cycle_warning (file_full_name); return false; } break; default: break; } if (!ok) { do_chown = false; file_stats = NULL; } else if (required_uid == (uid_t) -1 && required_gid == (gid_t) -1 && chopt->verbosity == V_off && ! chopt->root_dev_ino && ! chopt->affect_symlink_referent) { do_chown = true; file_stats = ent->fts_statp; } else { file_stats = ent->fts_statp; /* If this is a symlink and we're dereferencing them, stat it to get info on the referent. */ if (chopt->affect_symlink_referent && S_ISLNK (file_stats->st_mode)) { if (fstatat (fts->fts_cwd_fd, file, &stat_buf, 0) != 0) { if (! chopt->force_silent) error (0, errno, _("cannot dereference %s"), quote (file_full_name)); ok = false; } file_stats = &stat_buf; } do_chown = (ok && (required_uid == (uid_t) -1 || required_uid == file_stats->st_uid) && (required_gid == (gid_t) -1 || required_gid == file_stats->st_gid)); } /* This happens when chown -LR --preserve-root encounters a symlink-to-/. */ if (ok && FTSENT_IS_DIRECTORY (ent) && ROOT_DEV_INO_CHECK (chopt->root_dev_ino, file_stats)) { ROOT_DEV_INO_WARN (file_full_name); return false; } if (do_chown) { if ( ! chopt->affect_symlink_referent) { ok = (lchownat (fts->fts_cwd_fd, file, uid, gid) == 0); /* Ignore any error due to lack of support; POSIX requires this behavior for top-level symbolic links with -h, and implies that it's required for all symbolic links. */ if (!ok && errno == EOPNOTSUPP) { ok = true; symlink_changed = false; } } else { /* If possible, avoid a race condition with --from=O:G and without the (-h) --no-dereference option. If fts's stat call determined that the uid/gid of FILE matched the --from=O:G-selected owner and group IDs, blindly using chown(2) here could lead chown(1) or chgrp(1) mistakenly to dereference a *symlink* to an arbitrary file that an attacker had moved into the place of FILE during the window between the stat and chown(2) calls. If FILE is a regular file or a directory that can be opened, this race condition can be avoided safely. */ enum RCH_status err = restricted_chown (fts->fts_cwd_fd, file, file_stats, uid, gid, required_uid, required_gid); switch (err) { case RC_ok: break; case RC_do_ordinary_chown: ok = (chownat (fts->fts_cwd_fd, file, uid, gid) == 0); break; case RC_error: ok = false; break; case RC_inode_changed: /* FIXME: give a diagnostic in this case? */ case RC_excluded: do_chown = false; ok = false; break; default: abort (); } } /* On some systems (e.g., GNU/Linux 2.4.x), the chown function resets the `special' permission bits. Do *not* restore those bits; doing so would open a window in which a malicious user, M, could subvert a chown command run by some other user and operating on files in a directory where M has write access. */ if (do_chown && !ok && ! chopt->force_silent) error (0, errno, (uid != (uid_t) -1 ? _("changing ownership of %s") : _("changing group of %s")), quote (file_full_name)); } if (chopt->verbosity != V_off) { bool changed = ((do_chown && ok && symlink_changed) && ! ((uid == (uid_t) -1 || uid == file_stats->st_uid) && (gid == (gid_t) -1 || gid == file_stats->st_gid))); if (changed || chopt->verbosity == V_high) { enum Change_status ch_status = (!ok ? CH_FAILED : !symlink_changed ? CH_NOT_APPLIED : !changed ? CH_NO_CHANGE_REQUESTED : CH_SUCCEEDED); char *old_usr = file_stats ? uid_to_name (file_stats->st_uid) : NULL; char *old_grp = file_stats ? gid_to_name (file_stats->st_gid) : NULL; describe_change (file_full_name, ch_status, old_usr, old_grp, chopt->user_name, chopt->group_name); free (old_usr); free (old_grp); } } if ( ! chopt->recurse) fts_set (fts, ent, FTS_SKIP); return ok; }
int main (int argc, char *argv[]) { if( argc != 2 && argc != 1){ printf("usage: %s dir\n", argv[0]); return 1; } DIR * dir; if(argc == 1){ if (! (dir = opendir( ".")) ){ perror("can't open dir\n"); return 2; } } else{ if (! (dir = opendir( argv[1])) ){ perror("can't open dir\n"); return 2; } } struct dirent *ds; struct stat ss; char path[BUF]; char cur_path[BUF]; char stime[BUF]; char link_path[BUF]; if( argc == 1) strcpy ( path, ""); if( argc == 2) strcpy ( path, argv[1]); //printf("path = %s\n", path); while(1) { ds = readdir( dir); if( !ds ) break; int is_link = 0; if( ds->d_type == D_TYPE_LINK ) is_link = 1; strcpy ( cur_path, path); strcat ( cur_path, ds->d_name ); //printf("cur_path = %s\n", cur_path); if (stat(cur_path, &ss) == -1) { perror("stat"); return 1; } if( !is_link ){ switch (ss.st_mode & S_IFMT) { case S_IFBLK: printf("b"); break; case S_IFCHR: printf("c"); break; case S_IFDIR: printf("d"); break; case S_IFIFO: printf("p"); break; case S_IFLNK: printf("l"); is_link = 1; break; case S_IFREG: printf("-"); break; case S_IFSOCK: printf("s"); break; default: printf("?"); break; } } else{ printf("l"); } //printf ( "mode = %o \n", ss.st_mode ); int i, m, s; //here we print rights =) for( m = 0700, s = 06; s >= 0; m >>= 3, s -= 3 ){ i = ss.st_mode & m; i >>= s; if(i & 04) printf("r"); else printf("-"); if(i & 02) printf("w"); else printf("-"); if(i & 01) printf("x"); else printf("-"); } printf(" %3d", (int)ss.st_nlink); printf(" %10s %10s", uid_to_name(ss.st_uid), gid_to_name(ss.st_gid)); printf(" %8d", (int)ss.st_size); strcpy ( stime, ctime(&ss.st_atime)); stime[strlen(stime)-2] = 0; // delete \n printf(" %s",stime); printf (" %s", ds->d_name); if( is_link ) { int readln = readlink ( cur_path, link_path, BUF); link_path[readln] = 0; printf(" -> %s", link_path); } printf("\n"); } return 0; }