Advertisement
tsnaik

OS lab 4

Aug 3rd, 2015
809
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.78 KB | None | 0 0
  1. #include<stdio.h>
  2. #include <string.h>
  3. #include<stdlib.h>
  4. #include<unistd.h>
  5. #include<dirent.h>
  6. #include <errno.h>
  7. #include<sys/types.h>
  8. #include <sys/stat.h>
  9. #include<fcntl.h>
  10. #define BUFF 1024
  11.  
  12. void fun(char *name)
  13. {
  14.     /*struct stat path_stat;
  15.         stat(name, &path_stat);
  16.    
  17.     if(S_ISDIR(path_stat.st_mode))
  18.     {
  19.         chdir(name);
  20.     }
  21.     */
  22.  
  23.    
  24.    
  25.    
  26.        
  27.     char buf[BUFF];
  28.     char buff[BUFF], buf2[BUFF];
  29.    
  30.     //printf("\n%s",name);
  31.    
  32.     //printf(getcwd(buf2,sizeof(buf)));
  33.     strcpy(buf2,"/proc/");
  34.    
  35.     strcat(buf2,name);
  36.    
  37.    
  38.    
  39.     //printf("\n%s",buf2);
  40.     chdir(buf2);
  41.     //printf("\t%s",getcwd(buf,sizeof(buf)));
  42.    
  43.     int n = open("stat",O_RDONLY);
  44.    
  45.     //printf("\n%s",buf2);
  46.     //printf(getcwd(buf,sizeof(buf)));
  47.    
  48.     int no;
  49.    
  50.     //printf(getcwd(buf,sizeof(buf)));
  51.     while((no=read(n,buff,BUFF))>0)
  52.     {
  53.         char *token;
  54.         int count = 0;
  55.        
  56.         token = strtok(buff," ");
  57.         //printf("%s\n",token);
  58.        
  59.         while(token != NULL && count!=2 )
  60.         {
  61.             printf("%s\n",token);
  62.            
  63.             token = strtok(NULL," ");  
  64.             count++;
  65.         }
  66.     }
  67. }
  68.  
  69. int main()
  70. {
  71.     char buf[BUFF];
  72.         DIR *dirstream;
  73.         struct dirent *d;
  74.        
  75.         /*char *pwd;
  76.         pwd = getcwd(buf,sizeof(buf));
  77.         */
  78.        
  79.         chdir("/proc");
  80.         if(getcwd(buf,sizeof(buf))!=NULL)
  81.         {
  82.             if((dirstream=opendir(buf))!=NULL)
  83.             {
  84.                 while((d=readdir(dirstream))!=NULL)
  85.                 {
  86.                     if(isdigit((d->d_name)[0]))
  87.                        
  88.                     {
  89.                         fun(d->d_name);
  90.                        
  91.                    
  92.                     }
  93.                    
  94.                    
  95.                    
  96.                    
  97.                    
  98.                
  99.                 }
  100.            
  101.             }
  102.             else
  103.                     {
  104.                         printf("Error.");
  105.                         exit(1);
  106.                     }
  107.        
  108.         }
  109.  
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement