PHP Code:
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#include <st***.h>
#include <string.h>
/*
* _ _
* (_) (_)
* __ ____ _ _ _____ ___ ___ ___ ___ _ __ ___
* \ \/ / _` | |/ _ \ \ / / |/ _ \ / __/ _ \| '_ ` _ \
* > < (_| | | (_) \ V /| | (_) | (_| (_) | | | | | |
* /_/\_\__, |_|\___/ \_/ |_|\___(_)___\___/|_| |_| |_|
* __/ |
* |___/
*
* List files in a directory with relative inode number
* Usage: linode <directory>
* v1.0
*
*/
typedef struct contents {
char name[255];
int node_number;
struct contents * next;
} contents_struct;
contents_struct * getdata (char * name, int number) {
contents_struct * punt;
punt = (contents_struct *) malloc ( sizeof(contents_struct));
if ( punt == NULL) {
fprintf(stderr, "Errore di allocazione\n");
exit(-3);
}
strcpy (punt->name, name);
punt->node_number = number;
punt->next = NULL;
return punt;
}
contents_struct * add_to_list (contents_struct * list, contents_struct * t_list){
if (list==NULL){
list = t_list;
return list;
} else {
if ( strcmp (list->name, t_list->name ) > 1 ){
t_list->next = list;
list=t_list;
return list;
} else{
list->next = add_to_list ( list->next, t_list);
return list;
}
}
}
int main ( int n_entry , char** name_entry) {
if ( n_entry < 2){return 0;};
struct stat file_info;
DIR * directory;
struct dirent * dir_entry;
int err,i;
char array [1000];
contents_struct * list;
contents_struct * t_list;
list = NULL;
directory = opendir (*(name_entry + 1));
if ( directory == NULL){ fprintf(stderr, "Impossibile aprire directory\n"); exit(-1);}
for(dir_entry = readdir(directory); \
dir_entry != NULL; \
dir_entry = readdir(directory)){
strcpy ( array, *(name_entry + 1));
for(i=0;*(array+i)!=0;i++){};
if (*(array+i-1)!='/'){
strcat ( array, "/");
}
strcat ( array, dir_entry->d_name);
err = stat (array, &file_info );
if ( err == -1){ fprintf(stderr,"Errore lettura file\n"); exit(-2);}
t_list=getdata(dir_entry->d_name, (int)(file_info.st_ino));
list = add_to_list(list, t_list);
}
for(t_list=list;t_list!=NULL;t_list=t_list->next){
fprintf(stdout,"%s - %d\n",t_list->name,t_list->node_number);
}
return 0;
}