Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. char* run_lstat(int sockfd){
  2.     char line[42] = "Type the file you wish to get info about\n\0";
  3.     char* all_files = run_ls();
  4.     char* token = strtok(all_files, "\t");
  5.     char* msg = malloc(512); // i had to use this since i
  6.     memset(msg, '\0', 512);
  7.     int offset = 0;
  8.  
  9.  
  10.     do{
  11.         if(token[0] != '\n'){
  12.                 // realloc(msg, strlen(token)+2);
  13.             msg = strcat(msg, token);
  14.             msg = strcat(msg, "\n");
  15.             offset += (strlen(token)+1);
  16.         }
  17.         fprintf(stderr, "token = %s\n", token);
  18.         fprintf(stderr, "msg = %s\n", msg);
  19.     }while((token = strtok(NULL, "\t")) != NULL);
  20.  
  21.     fprintf(stderr, "msg = |%s|\n", msg);
  22.     msg = strcat(msg, line);
  23.  
  24.     if(write_on_roids(sockfd, msg) == -1){
  25.         perror("OOPS! something went wrong: ");
  26.         exit(EXIT_FAILURE);
  27.     }
  28.     memset(msg, '\0', 512);
  29.     free(all_files);
  30.     state = read_state;
  31.     msg = read_on_roids(sockfd);
  32.     msg[strlen(msg)-1] = '\0'; // remove \n
  33.     state = write_state;
  34.  
  35.     all_files = run_ls();
  36.     token = strtok(all_files, "\t");
  37.     do{
  38.         if(!strcmp(msg, token)) break;
  39.     }while((token = strtok(NULL, "\t")) != NULL);
  40.     if(token == NULL) return msg = strcpy(msg, "could not find file, try again");
  41.     // free(msg);
  42.     struct stat buf;
  43.     memset(msg, '\0', 512);
  44.     strcat(msg, currentDir);
  45.     strcat(msg, "/");
  46.     strcat(msg, token);
  47.  
  48.     lstat(msg, &buf);
  49.  
  50.     if(S_ISREG(buf.st_mode) != 0){
  51.         return msg = strcpy(msg, "is a regular file\n\0");
  52.     } else if(S_ISDIR(buf.st_mode) != 0){
  53.         return msg = strcpy(msg, "is a directory\n\0");
  54.     } else if(S_ISCHR(buf.st_mode) != 0){
  55.         return msg = strcpy(msg, "is a character device\n\0");
  56.     } else if(S_ISBLK(buf.st_mode) != 0){
  57.         return msg = strcpy(msg, "is a block device\n\0");
  58.     } else if(S_ISFIFO(buf.st_mode) != 0){
  59.         return msg = strcpy(msg, "is a named pipe\n\0");
  60.     } else {
  61.         return msg = strcpy(msg, "didnt recognice file\n\0");
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement