-2

Using bash, I can find count the count of specific extensions of a file like jpg, mp4 etc. How can I achieve this using C language system calls? Any help to achieve this using C language system calls?

ls -lR /path/to/dir/*.jpg | wc -l
Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
  • 2
    What do you mean by "C language system calls"? – EOF Dec 05 '16 at 18:10
  • I mean to achieve this using C language with any system library functions available. With bash or any regex its possible but I wanna solve using C language – Sravan Likith Dec 05 '16 at 18:13
  • Possible duplicate of [C: Run a System Command and Get Output?](http://stackoverflow.com/questions/646241/c-run-a-system-command-and-get-output) – Jack Deeth Dec 05 '16 at 18:13
  • 3
    It depends a bit on your definition of 'system call', but you might want any or all of [`scandir()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/scandir.html) and friends, [`readdir()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/readdir.html) and friends, [`nftw()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/nftw.html), [`fnmatch()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fnmatch.html). – Jonathan Leffler Dec 05 '16 at 18:16
  • @JonathanLeffler none of which are actually `c`, but rather POSIX... – EOF Dec 05 '16 at 18:16
  • 1
    @EOF: Yes, but then there are very few system calls in Standard C; there are only library functions, and at least some of those I mentioned are more library function than system call. In fact, only the `readdir()` collection are really close to system calls in my understanding of the terminology. The rest are library functions built atop other system calls (in particular, atop `readdir()` et al). – Jonathan Leffler Dec 05 '16 at 18:18
  • @JonathanLeffler: Well, on Linux `readdir()` is actually using the `getdents()` syscall AFAIK. Either way, it's unclear whether the OP wants to use `system()` or the BSD extension `syscall()` or whatever. – EOF Dec 05 '16 at 18:20

3 Answers3

1

Use popen like this.

#include <stdio.h>

int main(void){
    FILE *fp = popen("ls -lR /path/to/dir/*.jpg | wc -l", "r");
    int count;

    fscanf(fp, "%d", &count);
    pclose(fp);

    printf("%d\n", count);

    return 0;
}
BLUEPIXY
  • 38,201
  • 6
  • 29
  • 68
1

I code for general use. You can manipulate d_name on your own.

#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>

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

    if (argc != 2)
        /* redo input */
    if ((dp = opendir(argv[1])) == NULL)
        /* error opening dir */
    printf("%8s %8s %8s %8s %8s\n",
            "d_name", "d_fileno",
            "d_reclen", "d_type",
            "d_namlen");
    while ((dirp = readdir(dp)) != NULL)
        printf("%8s %8d %8d %8d %8d\n",
                dirp->d_name, dirp->d_fileno,
                dirp->d_reclen, dirp->d_type,
                dirp->d_namlen);
    closedir(dp);
    return 0;
}
郭春林
  • 130
  • 8
0

Here is an example that shows how to using the readdir() function. You should be able to easily modify this to do what you want.

Chimera
  • 5,552
  • 6
  • 41
  • 77
  • Does that include the possibility this person is trying to avoid learning something and *still* pass an exam? – Jongware Dec 05 '16 at 18:33
  • 1
    Well, I gave this person an example they would have to modify to do what they needed. So they would have to learn or understand what the code is doing. – Chimera Dec 05 '16 at 18:35
  • @RadLexus: Yes, like Chimara said, my given code is only the guideline. He must modify my code to get his work done. – 郭春林 Dec 06 '16 at 06:49