C program to mimic wc command in unix


















Improve this question. Leon Leon 1, 3 3 gold badges 17 17 silver badges 38 38 bronze badges. Add a comment. Active Oldest Votes. Improve this answer. Sign up or log in Sign up using Google.

Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. Only if the code fails to execute wc will the else clause be executed, and then there is only the read end of the pipe left open.

Meanwhile, the original process simply exits. That closes the pipe, so the wc command gets no input, and reports 0 0 0 as a result. You need to rewrite the code. The parent process should wait until both its children execute. Especially while debugging, you should not ignore the exit status of children, and you should report it.

Here's some code that works. This is easier to understand, in general, than a bushy, multi-level set of conditions. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Executing the ls wc linux command in c program using 2 processes communicating trough a pipe Ask Question.

Asked today. Active today. Viewed 24 times. I'm currently having problems with the following exercise: I want to "mimic" the pipe command line ls wc in linux bash with the following program. What I do is: create a pipe create a reader child and writer child the writer child closes the pipe's reading side and redirects his stdout to the writing side of the pipe the reader child closes the pipe's writing side and makes the reading side of the pipe his stdin both children do an exec, the writer executes the ls program, passes the output through the pipe to the reader that executes the wc program on that output.

Remember to encrypt all data entering this file. Here we shall use a PATH variable to mimic the location that the system looks for the passwd and shadow files. The options could be various letters. However, to limit this program, use the -r and the -m options only. The -r option creates a system user. Note: useradd is 7 characters long. Following the second space, the character is a hyphen and the option character.

Once the command is entered, the password is set. Below are the steps to implement it using C program: 1. The main. It shall serve as a blueprint for the compiler. A quick look at the layout of the code to follow. Here, define a struct that shall hold all the data that must be updated. Any other method would introduce additional lines of code and make tracing the code difficult. Also, take it as great advice to always rename the struct you create. When using struct as parameters and return types, not renaming will cause substantial amounts of referencing and identifier errors during compilation.

Remember to insert a valid location or path of the files created for f1 and f2. It returns an object of type struct. Note: Use a capital S in Struct when declaring return type since typedef is being used to rename the struct in line Finally, void setpass prompts the user to associate a password with the new user that has been created.

This function is parameterized i. The passwd. The data is written on the passwd.



0コメント

  • 1000 / 1000