Sponsored Content
Top Forums Programming Find Argv[i] in /bin and /sbin Post 303036621 by realpath_issue on Thursday 4th of July 2019 11:48:39 PM
Old 07-05-2019
I can't find any source code for which command anywhere, maybe because it's so late I don't know. I was able to implement realpath the issue is I can't figure out how to do basically



Code:
if realpath == 0 (as in found)


return 0


else (as in not found)


return 1


So I got this


Code:
#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);
                        }
                }
        }
}

and based on the man page it should be working. I have tried



Code:
./a.out echo //echo is in /bin


// returns not found and 



./a.out ifjie4hfirhghrighih //not in /bin


// returns not found

I am very confused as to why this doesn't work as it seems it should be.
 

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
opendir, fdopendir, readdir, readdir_r, telldir, seekdir, rewinddir, closedir, dirfd -- directory operations LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <dirent.h> DIR * opendir(const char *filename); DIR * fdopendir(int fd); struct dirent * readdir(DIR *dirp); int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result); long telldir(DIR *dirp); void seekdir(DIR *dirp, long loc); void rewinddir(DIR *dirp); int closedir(DIR *dirp); int dirfd(DIR *dirp); DESCRIPTION
The opendir() function opens the directory named by filename, 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 filename cannot be accessed, or if it cannot malloc(3) enough memory to hold the whole thing, and sets the global variable errno to indicate the error. The fdopendir() function is equivalent to the opendir() function except that the directory is specified by a file descriptor fd rather than by a name. Upon successful return from fdopendir(), the file descriptor is under the control of the system, and if any attempt is made to close the file descriptor, or to modify the state of the associated description other than by means of closedir(), readdir(), readdir_r(), or rewinddir(), the behavior is undefined. Upon calling closedir() the file descriptor is closed. The FD_CLOEXEC flag is set on the file descriptor by a successful call to fdopendir(). The readdir() function returns a pointer to the next directory entry. It returns NULL upon reaching the end of the directory or on error. In the event of an error, errno may be set to any of the values documented for the getdirentries(2) system call. Note that the order of the directory entries vended by readdir() is not specified. Some filesystems may return entries in lexicographic sort order and others may not. The readdir_r() function 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. The readdir_r() function 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, 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). On failure, -1 is returned and the global variable errno is set to indicate the error. Sample code which searches a directory for entry ``name'' is: dirp = opendir("."); if (dirp == NULL) return (ERROR); len = strlen(name); while ((dp = readdir(dirp)) != NULL) { if (dp->d_namlen == len && strcmp(dp->d_name, name) == 0) { (void)closedir(dirp); return (FOUND); } } (void)closedir(dirp); return (NOT_FOUND); SEE ALSO
close(2), lseek(2), open(2), read(2), dir(5) HISTORY
The opendir(), readdir(), telldir(), seekdir(), rewinddir(), closedir(), and dirfd() functions appeared in 4.2BSD. The fdopendir() function appeared in FreeBSD 8.0. BSD
April 16, 2008 BSD
All times are GMT -4. The time now is 07:29 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy