Directory listing with wildcards in C - c

Is there a ready-made function in C that can list the contents of a directory using wildcards to filter out file names, for example, the equivalent of:
echo [!b]????
which shows the names of directory entries that are four characters long and do not start with "b"?
I know I can use scandir, but then, I need to provide my own filter function:
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
int filter(const struct dirent *entry)
{
if (strlen(entry->d_name) == 4 && entry->d_name[0] != 'b') return 1;
else return 0;
}
void main(void)
{
struct dirent **list;
int count;
count=scandir(".", &list, filter, alphasort)-1;
if (count < 0)
puts("Cannot open directory");
else
for (; count >= 0; count--)
puts(list[count]->d_name);
free(list);
}
Honestly, I am seriously considering actually calling shell to do it for me:
#include <stdio.h>
#include <stdlib.h>
void main(void)
{
FILE *fp;
char buffer[1024];
fp=popen("echo [!b]???", "r");
if (fp == NULL)
puts("Failed to run command.");
else
while (fgets(buffer, sizeof(buffer), fp) != NULL)
puts(buffer);
pclose(fp);
}

As mentioned in the comments the glob() function would be pretty good for this:
#include <stdio.h>
#include <glob.h>
int
main (void)
{
int i=0;
glob_t globbuf;
if (!glob("[!b]????", 0, NULL, &globbuf)) {
for (i=0; i <globbuf.gl_pathc; i++) {
printf("%s ",globbuf.gl_pathv[i]);
}
printf("\n");
globfree(&globbuf);
} else
printf("Error: glob()\n");
}

Related

How to read line by line using system call in C

In my program, I can currently read char by char a file with given name "fichier1.txt", but what I'm looking for is to store a line(line char pointer here) and then display it that way :
-ligne 1 : content line 1
-line 2 : content line 2
-ect...
I've tried to store char by char but since it's a pointer and I'm yet that much familiar with pointers I'm not able to store a line and then reuse the pointer to store the char of the next line.
I have to say that it's part of a school projet and I have to use POSIX standard.
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include <pthread.h>
#include<string.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
int read_fd, write_fd;
off_t offset = 0;
char lu;
struct stat statFd;
char *fichier = "fichier1.txt";
read_fd = open(fichier,O_RDONLY);
stat(fichier, &statFd);
if(read_fd == -1){
perror("open");
exit(EXIT_FAILURE);
}
int i = 0;
char * line; // variable to store line
while(lseek(read_fd,offset, SEEK_SET) < statFd.st_size)
{
if(read(read_fd, &lu, 1) != -1)
{
printf("%c",lu);
offset++;
} else {
perror("READ\n");
close(read_fd);
close(write_fd);
exit(EXIT_FAILURE);
}
}
return 0;
}
I'd like to use open() function and not fopen()
Since you are able to read character after character from the file, the logic in while loop will be used to store an entire line (up to 199 characters, you can increase it though) at once in an array & then display it:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(void)
{
FILE *fptr=fopen( "fichier1.txt","r");
int i;
char arr[200]; //THIS ARRAY WILL HOLD THE CONTENTS OF A LINE TEMPORARILY UNTIL IT IS PRINTED
int temp_index=0,line_number=1;;
memset(arr,'\0',sizeof(arr));
while((i=getc(fptr))!=EOF)
{
if(i!='\n')
{
arr[temp_index++]=i;
}
if(i=='\n')
{
printf(line %d: %s\n",line_number++,arr);
temp_index=0;
memset(arr,'\0',sizeof(arr));
}
}
return 0;
}
Calling lseek at every iteration may be inefficient and may fail on devices which are incapable of seeking. I would write a program along these lines below if I don't need to store lines.
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int lc = 0; /* line count */
int c; /* character read */
FILE *fp = fopen("fichier1.txt", "r");
if (fp == NULL) {
perror("fopen");
return EXIT_FAILURE;
}
while ((c = fgetc(fp)) != EOF) {
printf("line %d: ", ++lc);
while (c != '\n' && c != EOF) {
putchar(c);
c = fgetc(fp);
}
putchar('\n');
}
return 0;
}
Or, a program using fgets to read a line at once:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
int main (void)
{
int lc = 0; /* line count */
char buf[4096]; /* buffer to store the line read */
bool newline = true;
FILE *fp = fopen("fichier1.txt", "r");
if (fp == NULL) {
perror("fopen");
return EXIT_FAILURE;
}
while (fgets(buf, sizeof buf, fp) != NULL) {
if (newline)
printf("line %d: ", ++lc);
printf("%s", buf);
newline = strchr(buf, '\n');
}
return 0;
}

Find Argv[i] in /bin and /sbin in C

I cannot locate argv[i] in /bin (or /sbin), package only checks the directory it was run from. How do I check for argv[i] in /bin and /sbin?
I am currently working on my own package manager and I am writing it in pure C. I am currently writing the check to see if the package(s) passed (using argv[]) are already installed. The issue I am having is that when I run for the check my code only checks in the directory it was run from and I need it to check /bin and /sbin (I am going to handle to checking of /sbin) and I am trying to get it to check /bin, but am having zero luck. I cannot seem to get this to work, each time they just check the current working directory and I need them to check /bin. I can't figure this out, has anyone by chance done this in pure C before? Thank you
These are all the methods I have tried so far, none of them work...
using stat()
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <dirent.h>
int main(int argc, char *argv[])
{
struct dirent *de = calloc(1, sizeof(struct dirent));
DIR *dr = opendir("/bin"); /* directory to open */
short i;
struct stat *program = calloc(1, sizeof(struct stat));
if (dr == NULL) {
printf("directory could not be opened");
return 0;
}
while ((de = readdir(dr)) != NULL) {
for (i = 1; i < argc; i++) {
if (stat(argv[i], program) == 0) {
printf("found\n");
closedir(dr);
}
else {
printf("not found\n");
closedir(dr);
}
}
}
}
using realpath
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
int main(int argc, char *argv[])
{
struct dirent *de = calloc(1, sizeof(struct dirent));
DIR *dr = opendir("/bin"); /* directory to open */
short i;
char *res = realpath(argv[i], NULL);
if (dr == NULL) {
printf("directory could not be opened");
return 0;
}
while ((de = readdir(dr)) != NULL) {
for (i = 1; i < argc; i++) {
if (res == NULL) {
printf("found\n");
closedir(dr);
}
else {
printf("not found\n");
closedir(dr);
}
}
}
}
using strcmp
#include <stdio.h>
#include <string.h>
#include <dirent.h>
int main(int argc, char *argv[])
{
struct dirent *de;
DIR *dr = opendir("/bin"); /* directory to open */
short i;
struct stat program;
if (dr == NULL) {
printf("directory could not be opened");
return 0;
}
while ((de = readdir(dr)) != NULL) {
for (i = 1; i < argc; i++) {
if (strcmp(de->d_name, argv[i]) == 0) {
printf("found\n");
closedir(dr);
}
else {
printf("not found\n");
closedir(dr);
}
}
}
}
I am expecting them all to work as follows:
check echo
// it would go to /bin and find echo and then print
found
but when I run them they only check the current working directory, so for example:
check connection.c
// finds connection.c in the same directory
found
Solution
Naha! I found a way to do it! So using the function chdir() I can run stat() in /bin like so:
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
short i;
struct stat *program = calloc(1, sizeof(struct stat));
for (i = 1; i < argc; i++) {
chdir("/bin"); /* move to /bin */
if (chdir("/bin") != 0)
return 1;
if (stat(argv[i], program) == 0)
return 0;
else
return 1;
}
}
I tried simplifying your code just to output the contents of the folder, I get the contents of the '/bin' folder each time.
As a general rule of thumb, I like to get my code to compile without warnings, test it does what I expect it to, then work on the next bit.
The 'struct stat program' was causing the code not to compile, my guess is you were running an old version of the code.
#include <stdio.h>
#include <dirent.h>
#include <string.h>
int main(int argc, char *argv[])
{
struct dirent *de;
DIR *dr = opendir("/bin"); /* directory to open */
short i;
// struct stat program;
if (dr == NULL) {
printf("directory could not be opened");
return 0;
}
while ((de = readdir(dr)) != NULL) {
printf(de->d_name);
printf("\n");
}
}
If I understand you correctly then you’re confusing the semantics of opendir/readdir with those of chdir:
opendir does not change the working directory. This means that, e.g. realpath("echo", resolved_path) won’t resolve echo as /bin/echo but as $PWD/echo (where $PWD is the environment variable holding your current working directory).
That said, as a general rule you should not chdir inside your process. Instead, you can construct the path explicitly … e.g. via sprintf:
const char *binpath = "/bin";
char *fullpath = malloc(strlen(binpath) + 1 + strlen(argv[1]) + 1);
sprintf(fullpath, "%s/%s", binpath, argv[1]);
… and then stat the result (no need to dynamically allocate your struct stat either):
struct stat st;
if (stat(fullpath, &st) != 0) {
fprintf(stderr, "error in stat(): %s", strerror(errno));
}
printf("%s has inode %ju\n", fullpath, (unsigned long) st.st_ino);

How to recursively go through folders and count total file size

I am trying to recursively go through my directories and print file size, then at the end print the total of all file size's. I cannot figure out what to pass my function recursively, and my variable total does not end up being correct,any help is greatly appreciated, thanks so much in advance.
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
void do_ls(char[]);
int total = 0;
int main(int ac, char *av[])
{
if (ac == 1)
do_ls(".");
else
{
while (--ac) {
printf("%s:\n", *++av);
do_ls(*av);
}
}
}
void do_ls(char dirname[])
{
DIR *dir_ptr;
struct dirent *direntp;
struct stat info;
if ((dir_ptr = opendir(dirname)) == NULL)
fprintf(stderr, "ls01: cannot opern %s\n", dirname);
else
{
while((direntp = readdir(dir_ptr)) != NULL) {
stat(direntp->d_name, &info);
if (S_ISDIR(info.st_mode))
printf("%s\n", direntp->d_name);
//I believe recursion goes here, I tried the following
//do_ls(direntp->d_name);
else
printf("%d %s\n", (int)info.st_size, direntp->d_name);
total += (int)info.st_size;
}
closedir(dir_ptr);
}
printf("Your total is: %d \n", total);
}
In the line:
while((direntp - readdir(dir_ptr)) != NULL)
you should be setting direntp, not subtracting (I assume).

Search_string in a loop of different files in C

I don't know how to solve this problem. I have this function that prints all the .txt files that I have, but I also need to search and print, after each file name, some specific strings (of each file) that contain some word.
This is the part that prints the name of the files.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <dirent.h>
int main() {
DIR* p;
struct dirent* pp;
p = opendir("./");
if (p != NULL) {
while ((pp = readdir(p)) != NULL) {
int length = strlen(pp->d_name);
if (strncmp(pp->d_name + length - 4, ".txt", 4) == 0)
puts(pp->d_name);
}
(void)closedir(p);
}
return 0;
}
I need to search some specific words (three different words) and print the line in where are contained, that would be three different lines.
Right now the program prints this:
0_email.txt
1_email.txt
Inside this files that are like emails, I need to print he date of when they were send (Date:), who (To:) and the subject (Subject:). This information is not always in the same line.
I have try this code, that search the word, but I am not able to make the program search in all the files (because this files can increase and have different names, no I can't not do it name by name) and to search several times
FILE *fp;
char filename[]="0_email.txt",line[200],search_string[]="To:";
fp=fopen(filename,"r");
if(!fp){
perror("could not find the file");
exit(0);
}
while ( fgets ( line, 200, fp ) != NULL ){
if(strstr(line,search_string))
fputs ( line, stdout );
}
fclose ( fp );
This second code is one that I found in Internet, I have just learn c programming and I'm not very familiar with it.
Thanks for your help!
You need to learn about functions.
You roughly need following:
It's untested code and there is still a lot of improvments that should be done. It does not exactly what you want and I'm not even sure if it compiles, but it should give you an idea what you need to do:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <dirent.h>
int CheckFile(const char *filename)
{
FILE *fp;
char line[200], search_string[] = "To:";
fp = fopen(filename, "r");
if (!fp) {
perror("could not find the file");
exit(0);
}
while (fgets(line, 200, fp) != NULL) {
if (strstr(line, search_string))
fputs(line, stdout);
}
fclose(fp);
}
int main() {
DIR* p;
struct dirent* pp;
p = opendir("./");
if (p != NULL) {
while ((pp = readdir(p)) != NULL) {
int length = strlen(pp->d_name);
if (strncmp(pp->d_name + length - 4, ".txt", 4) == 0)
CheckFile(pp->d_name);
}
(void)closedir(p);
}
return 0;
}

Get the full path of the files in C

Basically this piece of code gives me names of the files in the directory....But I need to get their paths instead.. I tried to use function realpath(). But I am using it wrong I guess(I showed in code where i wanted to use it). Any ideas how to fix it?. One more thing: It gives me only names of subdirectories, but basically I need to get paths of the their files too.Thanks.
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
int main (int c, char *v[]) {
int len, n;
struct dirent *pDirent;
DIR *pDir;
int ecode=0;
struct stat dbuf;
for (n=1; n<c; n++){
if (lstat(v[n], &dbuf) == -1){
perror (v[n]);
ecode ++;
}
else if(S_ISDIR(dbuf.st_mode)){
printf("%s is a directory/n ", v[n]);
}
else{
printf("%s is not a directory\n", v[n]);
}
}
if (c < 2) {
printf ("Usage: testprog <dirname>\n");
return 1;
}
pDir = opendir (v[1]);
if (pDir == NULL) {
printf ("Cannot open directory '%s'\n", v[1]);
return 1;
}
while ((pDirent = readdir(pDir)) != NULL) {
// here I tried to use realpath()
printf ("[%s]\n", realpath(pDirent->d_name));
}
closedir (pDir);
return 0;
}
All you need is to add the second argument to realpath, because it needs a buffer to write into. I recommend you take the line out of the printf statement and give it its own line. realpath() can return a char*, but it wasn't designed to.
#include <limits.h> //For PATH_MAX
char buf[PATH_MAX + 1];
while ((pDirent = readdir(pDir)) != NULL) {
realpath(pDirent->d_name, buf);
printf ("[%s]\n", buf);
}
This appears to display the full paths properly on my system.

Resources