Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Esercizio 1: Linguaggio C (obbligatorio): (20 punti)
- Scrivere un programma listexe che fornisca in output l'elenco dei processi attivi nel sistema mettendo in output per ogni
- processo il pid e il path dell'eseguibile.
- L'informazione puo' essere trovata scandendo la directory proc, infatti ad ogni processo attivo corrisponde una directory in
- /proc che ha come nome il numero del processo (ad esempio al processo 9801 corrisponde la directory /proc/9801) e all'interno
- di queste directory il file exe e' un link simbolico all'eseguibile.
- Esempio:
- $ ls -l /proc/9801/exe
- lrwxrwxrwx 1 renzo renzo 0 Jan 22 18:26 /proc/9801/exe -> /bin/bash
- l'output del programma listexe dovrebbe essere:
- $ listexe
- …..
- 9801 /bin/bash
- 9933 /usr/bin/vim
- …..
- (alcuni link simbolici possono essere non leggibili per sicurezza, verranno omessi).*/
- #include <stdio.h>
- #include <dirent.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #define n_array sizeof(proc_array)/sizeof(const char *)
- char *creaPath(char * d_name, char * new_path){
- strcpy (new_path, "/proc/");
- strncat(new_path, d_name, strlen(d_name));
- strncat(new_path, "/exe", strlen("/exe"));
- return new_path;
- }
- static int compare (const void * a, const void * b)
- {
- /* The pointers point to offsets into "array", so we need to
- dereference them to get at the strings. */
- return strcmp (*(const char **) a, *(const char **) b);
- }
- int isNumeric (const char * s)
- {
- if (s == NULL || *s == '\0' || isspace(*s))
- return 0;
- char * p;
- strtod (s, &p);
- return *p == '\0';
- }
- int filter(const struct dirent * dire){
- /* Discard . and .. */
- if( strncmp(dire->d_name, ".", 2) == 0
- || strncmp(dire->d_name, "..", 3) == 0 )
- return 0;
- /* Check whether it is a DIR or not.
- * Some FS doesn't handle d_type, so we check UNKNOWN as well */
- if( dire->d_type != DT_UNKNOWN
- && dire->d_type != DT_DIR )
- return 0;
- if( !(isNumeric(dire->d_name)) )
- return 0;
- /* We've nothing against it. Accept */
- return 1;
- }
- int main(int arcg, char ** argv){
- int i, num_entry;
- char linkname[255];
- char proc_array[1024][1024];
- char *file_list;
- FILE * file_temp;
- struct entry {
- char path[255];
- char pid[10];
- };
- file_temp = fopen("file.txt", "w+");
- int nchars;
- struct dirent ** filelist = NULL;
- int ndirs = scandir(argv[1], &filelist, filter, alphasort);
- if( ndirs < 0 ) /* Check errors */
- return 1;
- for(; i < ndirs; ++i){
- char new_path[255];
- creaPath(filelist[i]->d_name, new_path);
- if ((nchars = readlink(new_path, linkname, 255)) > 0){
- fprintf(file_temp, "%s %s\n", linkname, filelist[i]->d_name);
- num_entry++;
- }
- memset(new_path, 0, 255);
- memset(linkname, 0, 255);
- nchars = 0;
- }
- fclose(file_temp);
- file_temp = fopen("file.txt", "r");
- printf("%d\n", num_entry);
- char * mList[num_entry];
- char buffer[1024];
- int row=0;
- while( (fgets(buffer, sizeof buffer, file_temp)) != NULL){
- fpos_t pos;
- fgetpos(file_temp, &pos);
- /*struct entry* mEntry = (struct entry*)malloc(sizeof(struct entry));
- fscanf(file_temp, "%s %s", mEntry->path, mEntry->pid);
- printf("%s %s\n", mEntry->path, mEntry->pid);*/
- fsetpos(file_temp, &pos);
- size_t len = strlen(buffer);
- mList[row] = malloc(len);
- strncpy(mList[row], buffer, len-1);
- mList[row][strlen(buffer)-1]='\0';
- row++;
- memset(buffer, 0, 1024);
- }
- qsort(mList, num_entry, sizeof(char *), compare);
- int y=0;
- for (;y<num_entry;y++)
- printf("%s\n", mList[y]);
- if( filelist != NULL ) {
- for(i = 0; i < ndirs; ++i)
- free(filelist[i]);
- free(filelist);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement