Question

Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h>...

Combine two codes (code 1) to get names with(code 2) to get info:

Code 1:

#include<unistd.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<dirent.h>

#include<stdio.h>

#include<stdlib.h>

void do_ls(char []);

int main(int argc,char *argv[])

{

if(argc == 1)

do_ls(".");

else

while(--argc){

printf("%s:\n",*++argv);

do_ls(*argv);

}

}

void do_ls(char dirname[])

{

DIR *dir_ptr;

struct dirent *direntp;

if((dir_ptr = opendir(dirname)) == NULL)

fprintf(stderr,"ls1:cannot open %s\n",dirname);

else

{

while((direntp = readdir(dir_ptr)) != NULL)

printf("%s\n",direntp->d_name);

closedir(dir_ptr);

}

}

____________________________

code 2:

#include <stdio.h>

#include <sys/types.h>

#include <sys/stat.h>

void show_stat_info(char *, struct stat *);

int main(int argc, char *argv[])

{

struct stat info;

if (argc>1)

{

if( stat(argv[1], &info) != -1 ){

show_stat_info( argv[1], &info );

return 0;

}

else

perror(argv[1]);

}

return 1;

}

void show_stat_info(char *fname, struct stat *buf)

{

printf(" mode: %o\n", buf->st_mode);

printf(" links: %d\n", buf->st_nlink);

printf(" user: %d\n", buf->st_uid);

printf(" group: %d\n", buf->st_gid);

printf(" size: %d\n", (int)buf->st_size);

printf("modtime: %d\n", (int)buf->st_mtime);

printf(" name: %s\n", fname );

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the single c program by concatenating code 1 and 2.

Program:

#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<dirent.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>


void do_ls(char []);
void show_stat_info(char *fname, struct stat *buf);

int main(int argc,char *argv[])
{
   if(argc == 1)
   {
      do_ls(".");
   }
   else
      while(--argc){
         printf("%s:\n",*++argv);
         do_ls(*argv);
      }
}

void do_ls(char dirname[])
{
   DIR *dir_ptr;
   struct dirent *direntp;
   struct stat info;
   char filePath[1024]= {'\0'};
   size_t remaining = 1024, n = 0;

   if((dir_ptr = opendir(dirname)) == NULL)
   {
      fprintf(stderr,"ls1:cannot open %s\n",dirname);
   }
   else
   {
      while((direntp = readdir(dir_ptr)) != NULL)
      {
         /* Frame the complete path for directory to make stat works */
         strncat(filePath, dirname, remaining);
         strncat(filePath, "/", remaining);
         strncat(filePath, direntp->d_name, remaining);

         if( stat(filePath, &info) != -1 ){

            show_stat_info( filePath, &info );
         }
         memset(filePath, '\0', sizeof(filePath));
      }

      closedir(dir_ptr);
   }
}

void show_stat_info(char *fname, struct stat *buf)
{
   printf(" mode: %o\n", buf->st_mode);
   printf(" links: %d\n", buf->st_nlink);
   printf(" user: %d\n", buf->st_uid);
   printf(" group: %d\n", buf->st_gid);
   printf(" size: %d\n", (int)buf->st_size);
   printf(" modtime: %d\n", (int)buf->st_mtime);
   printf(" name: %s\n", fname );
}

Output:

USER>./a.out ../SHELL/
../SHELL/:
mode: 100644
links: 1
user: 22037710
group: 2000
size: 63
modtime: 1555665812
name: ../SHELL//msgs
mode: 40775
links: 7
user: 22037710
group: 2000
size: 4096
modtime: 1561390253
name: ../SHELL//..
mode: 100644
links: 1
user: 22037710
group: 2000
size: 1541
modtime: 1560316727
name: ../SHELL//shellScript_1.sh
mode: 100644
links: 1
user: 22037710
group: 2000
size: 3132
modtime: 1560241908
name: ../SHELL//logs

Screen Shot:

#include <unistd.h> include < sys/ types.h> include <sys/stat.h> #include< fcntl.h> include <dirent.h> include<stdio.h> #incl

closedir (dir_ptr); void show stat info (char *fname, stat *buf) struct printf ( mode: %o\n, buf->st_mode); >st_nlink); pri

Add a comment
Know the answer?
Add Answer to:
Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h>...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Run the code in Linux and provide the screenshot of the output and input #include <signal.h>...

    Run the code in Linux and provide the screenshot of the output and input #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> static void cleanup(); static void docleanup(int signum); static const char *SERVER_ADDR = "127.0.0.1"; static const int SERVER_PORT = 61234; static int cfd = -1; int main(int argc, char *argv[]) { struct sockaddr_in saddr; char buf[128]; int bufsize = 128, bytesread; struct sigaction sigact; printf("client starts running ...\n"); atexit(cleanup); sigact.sa_handler =...

  • I need help fixing this code in C. It keeps giving me an error saying error:...

    I need help fixing this code in C. It keeps giving me an error saying error: ‘O_RDWD’ undeclared (first use in this function) if ((fd = open(fifo, O_RDWD)) < 0) note: each undeclared identifier is reported only once for each function it appears in. Please fix so it compiles properly. //************************************************************************************************************************************************** #include <fcntl.h> #include <stdio.h> #include <errno.h> #include<stdlib.h> #include <string.h> #include<unistd.h> #include <sys/stat.h> #include <sys/types.h> #define MSGSIZE 63 char *fifo = "fifo"; int main(int argc, char **argv){    int fd;...

  • /* myloggerd.c * Source file for thread-lab * Creates a server to log messages sent from...

    /* myloggerd.c * Source file for thread-lab * Creates a server to log messages sent from various connections * in real time. * * Student: */ #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <sys/socket.h> #include <sys/un.h> #include <stdlib.h> #include <pthread.h> #include "message-lib.h" // forward declarations int usage( char name[] ); // a function to be executed by each thread void * recv_log_msgs( void * arg ); // globals int log_fd; // opened by main() but accessible...

  • C programming help! /* Your challenge is to format the following code in a readable manner, anwsering the questions in...

    C programming help! /* Your challenge is to format the following code in a readable manner, anwsering the questions in the comments. * */ #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <time.h> #include <sys/socket.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/uio.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <strings.h> #define BUFFERT 512 #define BACKLOG 1 int create_server_socket (int port); struct sockaddr_in sock_serv,sock_clt; int main(int argc, char** argv){ int sfd, fd; unsigned int length = sizeof(struct sockaddr_in); long int n, m, count...

  • devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include...

    devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include <ctype.h> #include <termios.h> #include <sys/types.h> #include <sys/mman.h>    #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \ __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0) #define MAP_SIZE 4096UL #define MAP_MASK (MAP_SIZE - 1) int main(int argc, char **argv) { int fd; void *map_base = NULL, *virt_addr = NULL; unsigned long read_result, writeval; off_t target; int access_type = 'w';    if(argc...

  • IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two...

    IN UNIX, MODIFY CODE, PROVIDE SCREENSHOTS FOR GOOD RATING: T1. Modify Client.c program to accept two arguments (IP add & port no. of the concurrent Server with thread - conServThread.c). Similarly, modify the Server (conServThread.c) program to accept an argument which is the port number of the server to bind and listen to. Try these two updated programs (server and client) with a port number (e.g., hhmm6) with current time where hh is hours in 24-hour format and mm is...

  • How to create different files to test whether it passes the serial number checking. (Hint: need...

    How to create different files to test whether it passes the serial number checking. (Hint: need a hex editor.) please provide two files, one pass, one cannot pass C code: ---------------------------------------------- #include <stdio.h> #include <string.h> int chkserial(char *s) { char buffer[16]; strcpy(buffer, s); return strcmp(buffer, "cs780880"); } void fullversion() { printf("Thanks for purchasing! Enjoy the full-version software!\n"); } void trialversion() { printf("This is a trial version. Please purchase the full version to enable all features!\n"); } int main(int argc, char...

  • For Unix in a C/C++ environment echoServer and echoClient are provided below In this lab, we will...

    For Unix in a C/C++ environment echoServer and echoClient are provided below In this lab, we will modifiy echoServer.c and echoClient.c programs (for the server's port# not fixed). (1) Modify echoServer.c program to take one argument (a port number) to be used for its listening port when it starts. (2) Modify echoClient.c program to take two arguments (server's IP address and Port number) to be used for its connection. (3) Find a port free for the server using netstat (see...

  • Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h>...

    Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h> #define SIZE (10) typedef struct _debugLab { int i; char c; } debugLab; // Prototypes void PrintUsage(char *); void DebugOption1(void); void DebugOption2(void); int main(int argc, char **argv) { int option = 0; if (argc == 1) { PrintUsage(argv[0]); exit(0); } option = atoi(argv[1]); if (option == 1) { DebugOption1(); } else if (option == 2) { DebugOption2(); } else { PrintUsage(argv[0]); exit(0); } }...

  • The operating system is Ubuntu 18.04 hello.c #include <stdio.h> int main(int argc, char *argv[]) { printf("Hello...

    The operating system is Ubuntu 18.04 hello.c #include <stdio.h> int main(int argc, char *argv[]) { printf("Hello world!\n"); return 0; } syscall-hello.c #include <unistd.h> #include <sys/syscall.h> char *buf = "Hello world!\n"; int main(int argc, char *argv) { size_t result; /* "man 2 write" to see arguments to write syscall */ result = syscall(SYS_write, 1, buf, 13); return (int) result; }Download and compile hello.ce and syscall-hello.com. Compile them statically and dynamically. How do the library and system calls produced by them compare...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT