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
getusershell(3C)					   Standard C Library Functions 					  getusershell(3C)

NAME
getusershell, setusershell, endusershell - get legal user shells SYNOPSIS
#include <unistd.h> char *getusershell(void); void setusershell(void); void endusershell(void); DESCRIPTION
The getusershell() function returns a pointer to a legal user shell as defined by the system manager in the file /etc/shells. If /etc/shells does not exist, the following locations of the standard system shells are used in its place: /bin/bash /bin/csh /bin/jsh /bin/ksh /bin/pfcsh /bin/pfksh /bin/pfsh /bin/sh /bin/tcsh /bin/zsh /sbin/jsh /sbin/pfsh /sbin/sh /usr/bin/bash /usr/bin/csh /usr/bin/jsh /usr/bin/ksh /usr/bin/pfcsh /usr/bin/pfksh /usr/bin/pfsh /usr/bin/sh /usr/bin/tcsh /usr/bin/zsh /usr/xpg4/bin/sh The getusershell() function opens the file /etc/shells, if it exists, and returns the next entry in the list of shells. The setusershell() function rewinds the file or the list. The endusershell() function closes the file, frees any memory used by getusershell() and setusershell(), and rewinds the file /etc/shells. RETURN VALUES
The getusershell() function returns a null pointer on EOF. BUGS
All information is contained in memory that may be freed with a call to endusershell(), so it must be copied if it is to be saved. SunOS 5.10 30 Aug 2004 getusershell(3C)
All times are GMT -4. The time now is 05:16 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy