Exemple #1
0
/* add a gid to the list of gids */
void add_gid(gid_t gid)
{
	struct idlist *list = gidlist;
	char *name;

	if (numeric_ids) return;

	/* don't map root */
	if (gid==0) return;

	if (!list) {
		if (!(name = gid_to_name(gid))) return;
		gidlist = add_list((int)gid, name);
		return;
	}

	while (list->next) {
		if (list->id == (int)gid) return;
		list = list->next;
	}

	if (list->id == (int)gid) return;

	if (!(name = gid_to_name(gid))) return;

	list->next = add_list((int)gid, name);
}
Exemple #2
0
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();
#ifdef TAGGED_STYLE

	printf("%s:\n", filename );			/* print name	 */

	printf("\t  mode: %s\n", filemode(info_p->st_mode) );
	printf("\t links: %d\n", (int) info_p->st_nlink);	/* links */
	printf("\t owner: %s\n", uid_to_name(info_p->st_uid) );
	printf("\t group: %s\n", gid_to_name(info_p->st_gid) );
	printf("\t  size: %ld\n",(long)info_p->st_size);	/* size  */
	printf("\t   mod: %s",   ctime(&info_p->st_mtime));
	printf("\taccess: %s",   ctime(&info_p->st_atime));
#endif
#ifdef	LS_STYLE
	printf( "%s"    , filemode(info_p->st_mode) );
	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 );
#endif

}
Exemple #3
0
void display_file_detail(struct FileList *file_list, int count)
/*
 * display for -l option.
 */
{
        char *uid_to_name(), *ctime(), *gid_to_name(), *filemode();
        char modestr[11];
        struct stat *info_p;
        int i;

        for(i = 0; i < count; ++i) {
                info_p = &file_list[i].info;

                file_mode_to_string(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));

                if(S_ISDIR(file_list[i].info.st_mode)) /* dir   show in blue  */
                        printf("\e[34m%s", file_list[i].name);
                else                                   /* other show in green */
                        printf("\e[92m%s", file_list[i].name);
                printf("\e[39m\n"); /* default white */
        }
}
Exemple #4
0
void show_file_info(char *filename, struct stat *info_p)

{

  char modestr[11];

 

  mode_to_letters(info_p->st_mode, modestr);//st_mode为文件的类型和存储的权限

 

  printf("%s"  , modestr);

  //st_nlink连接该文件的硬链接数,刚建立的文件值为1

  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);

}
Exemple #5
0
int search_acl_groups(char*** dst, const char* path, bool* belong) {
        acl_t acl;

        assert(path);
        assert(belong);

        acl = acl_get_file(path, ACL_TYPE_DEFAULT);
        if (acl) {
                acl_entry_t entry;
                int r;

                r = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
                while (r > 0) {
                        acl_tag_t tag;
                        gid_t *gid;
                        char *name;

                        r = acl_get_tag_type(entry, &tag);
                        if (r < 0)
                                break;

                        if (tag != ACL_GROUP)
                                goto next;

                        gid = acl_get_qualifier(entry);
                        if (!gid)
                                break;

                        if (in_gid(*gid) > 0) {
                                *belong = true;
                                break;
                        }

                        name = gid_to_name(*gid);
                        if (!name) {
                                acl_free(acl);
                                return log_oom();
                        }

                        r = strv_consume(dst, name);
                        if (r < 0) {
                                acl_free(acl);
                                return log_oom();
                        }

                next:
                        r = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
                }

                acl_free(acl);
        }

        return 0;
}
Exemple #6
0
static void test_gid_to_name_one(gid_t gid, const char *name) {
        _cleanup_free_ char *t = NULL;

        log_info("/* %s("GID_FMT", \"%s\") */", __func__, gid, name);

        assert_se(t = gid_to_name(gid));
        if (!synthesize_nobody() && streq(name, NOBODY_GROUP_NAME)) {
                log_info("(skipping detailed tests because nobody is not synthesized)");
                return;
        }
        assert_se(streq_ptr(t, name));
}
Exemple #7
0
void show_stat_info(char *fname, struct stat *buf) {
    static char mode[255];
    mode_to_letters(buf->st_mode, mode);
    printf("    mode: %s\n", mode); /* type + mode */
    printf("   links: %d\n", buf->st_nlink); /* # links */
    printf("    user: %s\n", uid_to_name(buf->st_uid)); /* user name */
    printf("   group: %s\n", gid_to_name(buf->st_gid)); /* group name */
    printf("    size: %s\n", bytes_to_human(buf->st_size)); /* file size */
    printf("accessed: %s\n", show_time((int) buf->st_atime)); /* accessed */
    printf("modified: %s\n", show_time((int) buf->st_mtime)); /* modified */
    printf(" changed: %s\n", show_time((int) buf->st_ctime)); /* changed */
}
Exemple #8
0
/* Print the file info */
void show_file_info(char *filename,struct stat *info_p)
{
    char modestr[11]; /* String of the mode */
    mode_to_letters(info_p->st_mode,modestr);
    fprintf(stdout, "%s",modestr);
    fprintf(stdout, "%2d ",(int)info_p->st_nlink);
    fprintf(stdout, "%-7s",uid_to_name(info_p->st_uid));
    fprintf(stdout, "%-7s",gid_to_name(info_p->st_gid));
    fprintf(stdout, "%5ld",(long)info_p->st_size);
    fprintf(stdout, " %.12s",4+ctime(&(info_p->st_mtime)));
    fprintf(stdout, " %s\n",filename);
}
Exemple #9
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(" %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);
}
Exemple #10
0
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);
}
Exemple #11
0
Fichier : ls.c Projet : iceout/uup
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);
}
Exemple #12
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));
}
Exemple #13
0
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);
}
Exemple #14
0
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);
}
Exemple #15
0
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	 */
}
Exemple #16
0
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);
}
Exemple #18
0
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);	

	}

}
Exemple #20
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;
}
Exemple #21
0
int acl_search_groups(const char *path, char ***ret_groups) {
        _cleanup_strv_free_ char **g = NULL;
        _cleanup_(acl_freep) acl_t acl = NULL;
        bool ret = false;
        acl_entry_t entry;
        int r;

        assert(path);

        acl = acl_get_file(path, ACL_TYPE_DEFAULT);
        if (!acl)
                return -errno;

        r = acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
        for (;;) {
                _cleanup_(acl_free_gid_tpp) gid_t *gid = NULL;
                acl_tag_t tag;

                if (r < 0)
                        return -errno;
                if (r == 0)
                        break;

                if (acl_get_tag_type(entry, &tag) < 0)
                        return -errno;

                if (tag != ACL_GROUP)
                        goto next;

                gid = acl_get_qualifier(entry);
                if (!gid)
                        return -errno;

                if (in_gid(*gid) > 0) {
                        if (!ret_groups)
                                return true;

                        ret = true;
                }

                if (ret_groups) {
                        char *name;

                        name = gid_to_name(*gid);
                        if (!name)
                                return -ENOMEM;

                        r = strv_consume(&g, name);
                        if (r < 0)
                                return r;
                }

        next:
                r = acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
        }

        if (ret_groups)
                *ret_groups = TAKE_PTR(g);

        return ret;
}
Exemple #22
0
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);
}
Exemple #23
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;
}
Exemple #24
0
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;
}