Sponsored Content
Top Forums Programming Find Argv[i] in /bin and /sbin Post 303036617 by realpath_issue on Thursday 4th of July 2019 07:24:28 PM
Old 07-04-2019
I was unaware of these functions and will definitely take a look at those manpages, thank you so much


Edit:


Oh, I apologize but there seems to be some confusion. What this does is it checks /bin and /sbin to see if the program(s) given are found. So say I run


Code:
check echo

it will look in /bin and /sbin to see if echo is found. If found it will say "found" otherwise it will say "not found". Now my current code has an issue that I can't find a solution for


Code:
#include <stdio.h>
#include <sys/stat.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 (stat(argv[i], &program) == 0) {
                                printf("found\n");
                                closedir(dr);
                        }

                        else {
                                printf("not found\n");
                                closedir(dr);
                        }
                }
        }
}

stat is checking the current working directory and not /bin, how do I have it look at /bin?


I am also trying with strcmp() and I can't seem to get that working either:


Code:
#include <stdio.h>
#include <sys/stat.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);
                        }
                }
        }
}


Last edited by realpath_issue; 07-04-2019 at 09:37 PM.. Reason: added details
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

command find returned bash: /usr/bin/find: Argument list too long

Hello, I create a file touch 1201093003 fichcomp and inside a repertory (which hava a lot of files) I want to list all files created before this file : find *.* \! -maxdepth 1 - newer fichcomp but this command returned bash: /usr/bin/find: Argument list too long but i make a filter all... (1 Reply)
Discussion started by: yacsil
1 Replies

2. UNIX for Dummies Questions & Answers

Difference between /bin, /usr/bin, /sbin ?

Hi All, Can somebody tell me the difference between /bin, /usr/bin, /sbin ? Thanx in advance, Saneesh Joseph (3 Replies)
Discussion started by: saneeshjose
3 Replies

3. Solaris

different between /sbin/init and /usr/sbin/init

root@test09 # ls -al /sbin/init -r-xr-xr-x 1 root sys 550000 Jun 29 2002 /sbin/init root@test09 # ls -al /usr/sbin/init -r-xr-xr-x 1 root sys 37100 Jun 29 2002 /usr/sbin/init (2 Replies)
Discussion started by: userking
2 Replies

4. Red Hat

/usr/bin/find && -exec /bin/rm never work as expected

hi there, Would you able to advise that why the syntax or statement below couldn't work as expected ? /usr/bin/find /backup -name "*tar*" -mtime +2 -exec /bin/rm -f {} \; 1> /dev/null 2>&1 In fact, I was initially located it as in crontab job, but it doesn't work at all. So, I was... (9 Replies)
Discussion started by: rauphelhunter
9 Replies

5. Shell Programming and Scripting

#!/bin/bash cannot find file or directory

Hello, When i run a bash script on ubuntu i get this message.. #!/bin/bash cannot find file or directory... Can anibody help me with this, because the file actually exists.... Is there any extra configuration to be made? (5 Replies)
Discussion started by: oliveiraum
5 Replies

6. Programming

/usr/bin/ld: cannot find -lpam

I'm trying to compile sudo on RHEL 4.8 and during the make I get the this error. Does anyone know what package I'm missing? gcc -o sudo sudo_auth.o pam.o mkstemps.o ldap.o exec_pty.o get_pty.o iolog.o audit.o boottime.o check.o env.o exec.o getspwuid.o gettime.o goodpath.o fileops.o find_path.o... (2 Replies)
Discussion started by: woodson2
2 Replies

7. UNIX for Advanced & Expert Users

O argv, argv, wherefore art thou argv?

All of my machines (various open source derivatives on x86 and amd64) store argv above the stack (at a higher memory address). I am curious to learn if any systems store argv below the stack (at a lower memory address). I am particularly interested in proprietary Unices, such as Solaris, HP-UX,... (9 Replies)
Discussion started by: alister
9 Replies

8. Shell Programming and Scripting

If cmd in in "A/user/bin A/bin A/user/sbin" but not "B/user/bin B/bin B/user/sbin" directory print t

there are two directories A and B if cmd in in "A/user/bin A/bin A/user/sbin" but not "B/user/bin B/bin B/user/sbin" directory print them (1 Reply)
Discussion started by: yanglei_fage
1 Replies

9. UNIX and Linux Applications

/usr/bin/ld: cannot find -lz error

I am installing lxml module for python on redhat I have installed libxml2 already. When I run for libxslt: ./configure --prefix=libxslt_folder --with-libxml-prefix=libxml2_folder It is ok the I run : make I have error: /usr/bin/ld: cannot find -lz collect2: ld returned 1 exit status I... (4 Replies)
Discussion started by: AIX_30
4 Replies

10. UNIX for Dummies Questions & Answers

/usr/bin/ld: cannot find -lboost_regex-mt

...... (30 Replies)
Discussion started by: larry burns
30 Replies
DIRECTORY(3)						   BSD Library Functions Manual 					      DIRECTORY(3)

NAME
closedir, dirfd, opendir, readdir, readdir_r, rewinddir, seekdir, telldir -- directory operations LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <dirent.h> int closedir(DIR *dirp); int dirfd(DIR *dirp); DIR * opendir(const char *dirname); struct dirent * readdir(DIR *dirp); int readdir_r(DIR *restrict dirp, struct dirent *restrict entry, struct dirent **restrict result); void rewinddir(DIR *dirp); void seekdir(DIR *dirp, long loc); long telldir(DIR *dirp); DESCRIPTION
The opendir() function opens the directory named by dirname, associates a directory stream with it, and returns a pointer to be used to iden- tify the directory stream in subsequent operations. The pointer NULL is returned if dirname cannot be accessed or if it cannot malloc(3) enough memory to hold the whole thing. The readdir() function returns a pointer to the next directory entry. It returns NULL upon reaching the end of the directory or detecting an invalid seekdir() operation. readdir_r() provides the same functionality as readdir(), but the caller must provide a directory entry buffer to store the results in. If the read succeeds, result is pointed at the entry; upon reaching the end of the directory, result is set to NULL. readdir_r() returns 0 on success or an error number to indicate failure. The telldir() function returns the current location associated with the named directory stream. Values returned by telldir() are good only for the lifetime of the DIR pointer (e.g., dirp) from which they are derived. If the directory is closed and then reopened, prior values returned by telldir() will no longer be valid. The seekdir() function sets the position of the next readdir() operation on the directory stream. The new position reverts to the one asso- ciated with the directory stream when the telldir() operation was performed. The rewinddir() function resets the position of the named directory stream to the beginning of the directory. The closedir() function closes the named directory stream and frees the structure associated with the dirp pointer, returning 0 on success. On failure, -1 is returned and the global variable errno is set to indicate the error. The dirfd() function returns the integer file descriptor associated with the named directory stream, see open(2). Sample code which searches a directory for entry ``name'' is: len = strlen(name); dirp = opendir("."); while ((dp = readdir(dirp)) != NULL) if (dp->d_namlen == len && !strcmp(dp->d_name, name)) { (void)closedir(dirp); return FOUND; } (void)closedir(dirp); return NOT_FOUND; LEGACY SYNOPSIS
#include <sys/types.h> #include <dirent.h> <sys/types.h> is necessary for these functions. SEE ALSO
close(2), lseek(2), open(2), read(2), compat(5), dir(5) HISTORY
The closedir(), dirfd(), opendir(), readdir(), rewinddir(), seekdir(), and telldir() functions appeared in 4.2BSD. BSD
June 4, 1993 BSD
All times are GMT -4. The time now is 12:31 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy