All pastes #122110 Raw Edit

wizo

public c v1 · immutable
·published 2006-08-08 13:08 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#include <unistd.h>int main (){        char * buffer;        char * result = NULL;        char * com = NULL;        int code = 0;        buffer = getenv ("PATH");        char response[512];        fgets(response, 100, stdin);        response[strlen(response)-1] = NULL;        printf (">> Current path is: %s\n\n",buffer);        result = strtok(buffer,":");        if (buffer!=NULL)        {                while(result!=NULL)                {                        //printf("Searching in %s\n", result);                        com = strcat(result, "/");                        com = strcat(com, response);                        printf("Searching for %s\n", com);                        code = access(com, F_OK);                        if(code == 0)                        {                                printf("Found at %s!\n", result);                                break;                        }                        result = strtok(NULL, ":");                }        }        return 0;}output:wizo@alphak ~/Work/2nd Year/OS/lab2 $ ./pathtestls>> Current path is: /usr/local/bin:/usr/bin:/bin:/opt/bin:/usr/i386-pc-linux-gnu/gcc-bin/3.4.4Searching for /usr/local/bin/lsSearching for ls/lsSearching for /ls/lsSearching for /ls/lsSearching for /ls/ls<repeats till seg faults>-------------------------------------------------------------------------------------------int main (){        char * buffer;        char * result = NULL;        char * com = NULL;        int code = 0;        buffer = getenv ("PATH");        char response[512];        fgets(response, 100, stdin);        response[strlen(response)-1] = NULL;        printf (">> Current path is: %s\n\n",buffer);        result = strtok(buffer,":");        if (buffer!=NULL)        {                while(result!=NULL)                {                        printf("Searching in %s\n", result);                        /*com = strcat(result, "/");                        com = strcat(com, response);                        printf("Searching for %s\n", com);                        code = access(com, F_OK);                        if(code == 0)                        {                                printf("Found at %s!\n", result);                                break;                        }*/                        result = strtok(NULL, ":");                }        }        return 0;}output:wizo@alphak ~/Work/2nd Year/OS/lab2 $ !././pathtestls>> Current path is: /usr/local/bin:/usr/bin:/bin:/opt/bin:/usr/i386-pc-linux-gnu/gcc-bin/3.4.4Searching in /usr/local/binSearching in /usr/binSearching in /binSearching in /opt/binSearching in /usr/i386-pc-linux-gnu/gcc-bin/3.4.4