C Program for finding a file in the directory

C Program for finding a file in the directory


#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;

char *process[10] = {"neel", "peel", "feel"};

int i;
for (i=0; i< 3; i++)
{
    if ((dp = opendir("/home/cg/root")) == NULL)
    {
        printf("can't open %s", "/home/cg/root");
        exit(1);
    }

    while ((dirp = readdir(dp)) != NULL)
    {
         if(!strcmp(dirp->d_name, process[i]))
            printf("\n%s", dirp->d_name);
       
    }
    printf("\n");
   
    closedir(dp);
}

C program to find pid of a file

include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    int process_id;
    char process_id_from_file[10];
    FILE *pid_fd;
    char command[100];

    sprintf(command,"pidof %s", argv[1]);
    pid_fd = popen(command, "r");
    if (pid_fd == NULL)
    {
       // perror("Error in reading pgrep %s", process_name);
        perror("Error in reading pgrep ");
        return -1;
    }

    if (fgets(process_id_from_file, sizeof process_id_from_file , pid_fd) == NULL)
    {
        return -1;
    }
    pclose(pid_fd);

    process_id = atoi(process_id_from_file);

    if (process_id != 0)
    {
        printf("\nprocess id : %d \n", process_id);
    }
   return 0;
}

c program for strtok

c program for strtok 

#include <string.h>
#include <stdio.h>
  
int main()

{

   char str[80] = "I-am-captain-neel-jack-sparrow";

   const char s[2] = "-";

   char *token;
  
   /* get the first token */

   token = strtok(str, s);
  
   /* walk through other tokens */

   while( token != NULL )

   {

      printf( " %s\n", token );
  
      token = strtok(NULL, s);

   }
  
   return(0);

}

 output:

h-4.2$ main                                                                                                                                                                          

 I                                                                                                                                                                                            

 am                                                                                                                                                                                                

 captain                                                                                                                                                                                          

 neel                                                                                                                                                                                              

 jack                                                                                                                                                                                              

 sparrow  

C program to calculate the time difference between 2 time stamps

#include <time.h>
#include <stdio.h>

int main(void)
{
struct tm str_time;
time_t time_of_day;

struct tm str_time1;
time_t time_of_day1;
       
       
str_time.tm_year = 2012; //-1900;
str_time.tm_mon = 6;
str_time.tm_mday = 5;
str_time.tm_hour = 10;
str_time.tm_min = 3;
str_time.tm_sec = 5;
str_time.tm_isdst = 0;

str_time1.tm_year = 2017-1900;
str_time1.tm_mon = 1;
str_time1.tm_mday = 1;
str_time1.tm_hour = 1;
str_time1.tm_min = 1;
str_time1.tm_sec = 1;
str_time1.tm_isdst = 0;

time_of_day = mktime(&str_time);
printf(ctime(&time_of_day));

time_of_day1 = mktime(&str_time1);
printf(ctime(&time_of_day1));


int f = mktime(&str_time);
printf("\n mktime: %d \n", f);

int g = mktime(&str_time1);
printf("\n mktime: %d \n", g);

return 0;
}


**********************************************************************
output:
**********************************************************************
sh-4.2$ main                                                                                                                                                                                       
Thu Jul  5 10:03:05 2012                                                                                                                                                                           
Wed Feb  1 01:01:01 2017                                                                                                                                                                           
                                                                                                                                                                                                   
 mktime: 1341482585                                                                                                                                                                                
                                                                                                                                                                                                   
 mktime: 1485910861