Advertisement
symdrome

main.c fixed

Sep 8th, 2023
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.64 KB | Source Code | 0 0
  1. #include <dirent.h>
  2. #include <stdbool.h>
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9.  
  10. typedef struct fileDir {
  11.   char *src;
  12.   char *dst;
  13.   char *op;
  14. } fileDir;
  15.  
  16. int main(int argc, char *argv[]) {
  17.   void help();
  18.   fileDir cli(int argc, char *argv[]);
  19.   void process(fileDir d);
  20.  
  21.   if (argc <= 2) {
  22.     fprintf(stderr, "Please type a name for csv file.\n");
  23.     help();
  24.   }
  25.  
  26.   fileDir dir = cli(argc, argv);
  27.  
  28.   process(dir);
  29.  
  30.   return 0;
  31. }
  32.  
  33. void process(fileDir d) {
  34.   bool isAudioFiles(char const *name);
  35.   char *getTime(const char *s, char *n);
  36.  
  37.   DIR *dir;
  38.   struct dirent *entry;
  39.  
  40.   FILE *file;
  41.   char *path = (char *)malloc(100 * sizeof(char));
  42.  
  43.   strcat(path, d.dst);
  44.   strcat(path, "/");
  45.   strcat(path, d.op);
  46.   file = fopen(path, "w+");
  47.  
  48.   if ((dir = opendir(d.src)) == NULL)
  49.     fprintf(stderr, "could't open %s\n", d.src), exit(1);
  50.   else {
  51.     fprintf(file, "NOME,DURAÇÃO\n");
  52.     while ((entry = readdir(dir)) != NULL) {
  53.       if (isAudioFiles(entry->d_name) == true) {
  54.         fprintf(file, "%s,%s", entry->d_name, getTime(d.src, entry->d_name));
  55.       }
  56.     }
  57.     fclose(file);
  58.     closedir(dir);
  59.   }
  60. }
  61.  
  62. char *getTime(const char *s, char *n) {
  63.   FILE *fp;
  64.  
  65.   char time[15];
  66.  
  67.   char *result = (char *)malloc(sizeof(size_t));
  68.  
  69.   char cmd[100] = "soxi -D";
  70.   strcat(cmd, " ");
  71.   strcat(cmd, s);
  72.   strcat(cmd, "/");
  73.   strcat(cmd, n);
  74.  
  75.   fp = popen(cmd, "r");
  76.  
  77.   if (fp == NULL)
  78.     fprintf(stderr, "soxi command failed\n");
  79.  
  80.   if (fgets(time, 15, fp) != NULL)
  81.     result = time;
  82.   else
  83.     fprintf(stderr, "fgets error\n");
  84.  
  85.   pclose(fp);
  86.   return result;
  87. }
  88.  
  89. bool isAudioFiles(char const *name) {
  90.   size_t len = strlen(name);
  91.   if (len > 4 &&
  92.       (strcmp(name + len - 4, ".mp3") && strcmp(name + len - 4, ".mp4") &&
  93.        strcmp(name + len - 4, ".wav")) == 0)
  94.     return true;
  95.   else
  96.     return false;
  97. }
  98.  
  99. fileDir cli(int argc, char *argv[]) {
  100.   void help();
  101.   int opt;
  102.   fileDir dir = {".", ".", ""};
  103.  
  104.   while ((opt = getopt(argc, argv, "s:d:n:")) != -1) {
  105.     switch (opt) {
  106.     case 's':
  107.       dir.src = optarg;
  108.       break;
  109.     case 'd':
  110.       dir.dst = optarg;
  111.       break;
  112.     case 'n':
  113.       dir.op = optarg;
  114.       break;
  115.     default:
  116.       printf("Unknown option: %c\n", optopt);
  117.       help();
  118.       exit(1);
  119.     }
  120.   }
  121.  
  122.   return dir;
  123. }
  124.  
  125. void help() {
  126.   fprintf(stderr, "\nUSAGE: audio_length [OPTIOMS] -n <FILENAME.csv>\n"
  127.                   "Options:\n"
  128.                   "       -s <path for source directory>\n"
  129.                   "       -d <path for destination file>\n\n");
  130. }
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement