Esempio n. 1
0
int
xml_print_dir(const char* dir)
{
   DIR *dirp;
   struct dirent *d;
   struct stat stats;
   int num_files = 0;

   if((dirp = opendir(dir)) == NULL)
      return(-1);

   chdir(dir);

   while((d = readdir(dirp)) != NULL)
   {
      xmlChar *xml_str;
      char atime[20];
      char mtime[20];
      int size_len;

      if ((d->d_name == NULL) || !strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
          continue;

      if(lstat(d->d_name, &stats) != 0)
      {
        fprintf(stderr, "couldn't stat: %s\n", d->d_name);
      }

#if defined (__MINGW32__)
      /* somehow atime is -1 on Windows XP when the atime is in future */
      if (stats.st_atime < 0) stats.st_atime = 0; 
      /* somehow mtime is -1 on Windows XP when the mtime is in future */
      if (stats.st_mtime < 0) stats.st_mtime = 0; 
#endif

      /* format time as per ISO 8601 */
      strftime(atime, sizeof atime, "%Y%m%dT%H%M%SZ", gmtime(&stats.st_atime));
      strftime(mtime, sizeof mtime, "%Y%m%dT%H%M%SZ", gmtime(&stats.st_mtime));

      xml_str = xml_C11NNormalizeAttr((const xmlChar *) d->d_name);
      printf("<%s p=\"%s\" a=\"%s\" m=\"%s\" s=\"",
          get_file_type(stats.st_mode), get_file_perms(stats.st_mode),
          atime, mtime);
      size_len = printf("%lu", (unsigned long) stats.st_size);
      printf("\"%.*s", 16-size_len, "                ");
      printf(" n=\"%s\"/>\n", xml_str);
      num_files++;
      xmlFree(xml_str);

   } /* end of for loop */

   closedir(dirp);
   return num_files;
}
Esempio n. 2
0
/*****************************************************************
 * Read directory helper function.
 ****************************************************************/
void ReadDirectory(intf_thread_t *p_intf, GtkListStore *p_list, char *psz_dir )
{
    GtkTreeIter    iter;
    struct dirent **pp_namelist;
    struct passwd *p_pw;
    struct group  *p_grp;
    struct stat    st;
    int n=-1, status=-1;

    msg_Dbg(p_intf, "Changing to dir %s", psz_dir);
    if (psz_dir)
    {
       status = chdir(psz_dir);
       if (status<0)
          msg_Dbg(p_intf, "permision denied" );
    }
    n = scandir(".", &pp_namelist, 0, alphasort);

    if (n<0)
        perror("scandir");
    else
    {
        int i;
        gchar *ppsz_text[4];

        if (lstat("..", &st)==0)
        {
            /* user, group  */
            p_pw  = getpwuid(st.st_uid);
            p_grp = getgrgid(st.st_gid);

            /* XXX : kludge temporaire pour yopy */
            ppsz_text[0] = "..";
            ppsz_text[1] = get_file_perms(st);
            ppsz_text[2] = p_pw->pw_name;
            ppsz_text[3] = p_grp->gr_name;

            /* Add a new row to the model */
            gtk_list_store_append (p_list, &iter);
            gtk_list_store_set (p_list, &iter,
                                0, ppsz_text[0],
                                1, ppsz_text[1],
                                2, st.st_size,
                                3, ppsz_text[2],
                                4, ppsz_text[3],
                                -1);

            if (ppsz_text[1]) free(ppsz_text[1]);
        }
            /* kludge */
        for (i=0; i<n; i++)
        {           
            if ((pp_namelist[i]->d_name[0] != '.') &&
                (lstat(pp_namelist[i]->d_name, &st)==0))
            {
                /* user, group  */
                p_pw  = getpwuid(st.st_uid);
                p_grp = getgrgid(st.st_gid);

                /* This is a list of strings. */
                ppsz_text[0] = pp_namelist[i]->d_name;
                ppsz_text[1] = get_file_perms(st);
                ppsz_text[2] = p_pw->pw_name;
                ppsz_text[3] = p_grp->gr_name;
#if 0
                msg_Dbg(p_intf, "(%d) file: %s permission: %s user: %s group: %s", i, ppsz_text[0], ppsz_text[1], ppsz_text[2], ppsz_text[3] );
#endif
                gtk_list_store_append (p_list, &iter);
                gtk_list_store_set (p_list, &iter,
                                    0, ppsz_text[0],
                                    1, ppsz_text[1],
                                    2, st.st_size,
                                    3, ppsz_text[2],
                                    4, ppsz_text[3],
                                    -1);

                if (ppsz_text[1]) free(ppsz_text[1]);
            }
        }
        free(pp_namelist);
    }
}