Advertisement
Guest User

Untitled

a guest
May 19th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.79 KB | None | 0 0
  1. /*Esercizio 1: Linguaggio C (obbligatorio): (20 punti)
  2. Scrivere un programma listexe che fornisca in output l'elenco dei processi attivi nel sistema mettendo in output per ogni
  3. processo il pid e il path dell'eseguibile.
  4. L'informazione puo' essere trovata scandendo la directory proc, infatti ad ogni processo attivo corrisponde una directory in
  5. /proc che ha come nome il numero del processo (ad esempio al processo 9801 corrisponde la directory /proc/9801) e all'interno
  6. di queste directory il file exe e' un link simbolico all'eseguibile.
  7. Esempio:
  8. $ ls -l /proc/9801/exe
  9. lrwxrwxrwx 1 renzo renzo 0 Jan 22 18:26 /proc/9801/exe -> /bin/bash
  10. l'output del programma listexe dovrebbe essere:
  11. $ listexe
  12. …..
  13. 9801 /bin/bash
  14. 9933 /usr/bin/vim
  15. …..
  16. (alcuni link simbolici possono essere non leggibili per sicurezza, verranno omessi).*/
  17.  
  18. #include <stdio.h>  
  19. #include <dirent.h>
  20. #include <stdlib.h>
  21. #include <ctype.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25.  
  26. #define n_array sizeof(proc_array)/sizeof(const char *)
  27.  
  28. char *creaPath(char * d_name, char * new_path){
  29.     strcpy (new_path, "/proc/");
  30.     strncat(new_path, d_name, strlen(d_name));
  31.     strncat(new_path, "/exe", strlen("/exe"));
  32.     return new_path;
  33. }
  34.  
  35. static int compare (const void * a, const void * b)
  36. {
  37.     /* The pointers point to offsets into "array", so we need to
  38.        dereference them to get at the strings. */
  39.  
  40.     return strcmp (*(const char **) a, *(const char **) b);
  41. }
  42.  
  43.    
  44.  
  45. int isNumeric (const char * s)
  46. {
  47.     if (s == NULL || *s == '\0' || isspace(*s))
  48.       return 0;
  49.     char * p;
  50.     strtod (s, &p);
  51.     return *p == '\0';
  52. }
  53.  
  54. int filter(const struct dirent * dire){
  55.  
  56.     /* Discard . and .. */
  57.     if( strncmp(dire->d_name, ".", 2) == 0
  58.         || strncmp(dire->d_name, "..", 3) == 0 )
  59.         return 0;
  60.  
  61.     /* Check whether it is a DIR or not.
  62.     * Some FS doesn't handle d_type, so we check UNKNOWN as well */
  63.     if( dire->d_type != DT_UNKNOWN
  64.             && dire->d_type != DT_DIR )
  65.         return 0;
  66.  
  67.     if( !(isNumeric(dire->d_name)) )
  68.         return 0;
  69.  
  70.     /* We've nothing against it. Accept */
  71.     return 1;
  72. }
  73.  
  74.  
  75. int main(int arcg, char ** argv){
  76.     int i, num_entry;
  77.     char linkname[255];
  78.     char proc_array[1024][1024];
  79.     char *file_list;
  80.     FILE * file_temp;
  81.  
  82.     struct entry {
  83.         char path[255];
  84.         char pid[10];
  85.     };
  86.  
  87.     file_temp = fopen("file.txt", "w+");
  88.  
  89.     int nchars;
  90.     struct dirent ** filelist = NULL;
  91.     int ndirs = scandir(argv[1], &filelist, filter, alphasort);
  92.  
  93.     if( ndirs < 0 )  /* Check errors */
  94.         return 1;
  95.  
  96.     for(; i < ndirs; ++i){
  97.         char new_path[255];
  98.        
  99.         creaPath(filelist[i]->d_name, new_path);
  100.         if ((nchars = readlink(new_path, linkname, 255)) > 0){
  101.             fprintf(file_temp, "%s %s\n", linkname, filelist[i]->d_name);  
  102.             num_entry++;
  103.         }
  104.  
  105.         memset(new_path, 0, 255);
  106.         memset(linkname, 0, 255);
  107.         nchars = 0;
  108.  
  109.     }
  110.  
  111.     fclose(file_temp);
  112.     file_temp = fopen("file.txt", "r");
  113.    
  114.     printf("%d\n", num_entry);
  115.  
  116.     char * mList[num_entry];
  117.     char buffer[1024];
  118.     int row=0;
  119.  
  120.    
  121.     while( (fgets(buffer, sizeof buffer, file_temp)) != NULL){
  122.        
  123.         fpos_t pos;
  124.         fgetpos(file_temp, &pos);
  125.        
  126.         /*struct entry* mEntry = (struct entry*)malloc(sizeof(struct entry));
  127.         fscanf(file_temp, "%s %s", mEntry->path, mEntry->pid);
  128.  
  129.         printf("%s %s\n", mEntry->path, mEntry->pid);*/
  130.  
  131.         fsetpos(file_temp, &pos);
  132.  
  133.         size_t len = strlen(buffer);
  134.         mList[row] = malloc(len);
  135.        
  136.         strncpy(mList[row], buffer, len-1);
  137.         mList[row][strlen(buffer)-1]='\0';
  138.  
  139.         row++;
  140.         memset(buffer, 0, 1024);
  141.         }
  142.  
  143.     qsort(mList, num_entry, sizeof(char *), compare);
  144.  
  145.     int y=0;
  146.     for (;y<num_entry;y++)
  147.         printf("%s\n", mList[y]);
  148.  
  149.     if( filelist != NULL ) {
  150.         for(i = 0; i < ndirs; ++i)
  151.             free(filelist[i]);
  152.         free(filelist);
  153.     }
  154.  
  155.     return 0;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement