a question about which

Posted on 2009-04-25
Last Modified: 2012-05-06
Hey, I'm trying to write a which function, I've gotten most of it down:

#include "sh.h"
#include <stdio.h>
#include <dirent.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

int which1(char *dir, char *sub_dir);

int main()
  DIR *pdir;
  struct pathelement *pathlist;
  int i = 0;
  int result;
  struct dirent *pfile;
  char *dir, *subdir, *full;
  pathlist = get_path();
  pdir = opendir("/usr/bin/");

  if(NULL == pdir)
      printf("Error Opening Directory\n");

    while(pfile = readdir(pdir)){
      result = which1(pfile->d_name, "ls");

        full = strcat(pathlist->element, "/ls");
        printf("%s\n", full);
    pathlist = pathlist->next;

  return 0;

int which1(char *dir, char *sub_dir){
 int result = 0;

  if(!(strcmp(sub_dir, dir)))
    result = 1;

  return result;

The actual looping is happening in the main function, the outer loop just checks if pathlist is null or not.

The inner loop is supposed to open the directory and keep sending the 2 parameters to which1, which compares the two strings and returns 1, and 0 if they aren't.

I'm just testing it on ls at the moment, I know I will need to change that string to a variable in my actual program... but what I can't figure out is, why it keeps appending the wrong directory to ls when its printing it.
Question by:errang
    LVL 4

    Accepted Solution

    Your outer loop just cycles through the path list, while the inner loop is doing the real work with readdir(). Note that the call to readdir passes pdir which was returned by opendir( "/usr/bin"). When you find the directory entry with "ls" you call strcat() with pathlist->element and "ls". However, the directory scan with readdir() is based on opendir( "/usr/bin") -- not pathlist.


    Author Closing Comment

    That fixed it, and I had to put pdir = opendir("/usr/bin/"); in the loop as well.
    LVL 4

    Expert Comment

    You do mean   pdir = opendir( pathlist->element)   yes ????

    Author Comment

    yeah, sorry I copy pasted... guess I wasn't looking.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now