Are Linux commands really C object files? [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I was reading "The C Programming Language" by Kernighan & Ritchie and came across some programs which mimic some Unix commands (also implemented in Linux) such as the cat command. The program took command line arguments just like the original cat command.
I am just curious to know whether they are the same thing or not.
Correct me if I'm wrong, any help would be appreciated.

In a command-line environment (such as, of course, Unix/Linux), a principle unit of abstraction is the command. A command has a well-defined interface: the command-line arguments it expects, the input it reads (if any), and the output it generates. You can reimplement a command any time you like, either using a different internal algorithm, or a different language, or just because you want to write your own version. Yes, cat was originally written in C, but we could rewrite it in C++, or Perl, or Python, or sh, or other languages. As long as our reimplementation meets the same interface requirements, we can accurately say that it "is" cat.

Related

How to run VBS in C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
Does anyone know how I can run a VBS code in a C code?
I don't say run an external file, I say run a vbs CODE in the same code as C
In the same way Javascript and HTML come together, I want to do it with C and VBS
Responding to comments
There are two ways to add macro support to your applications. Pay money to Microsoft and license VBA or use VBScript/JScript for free.
C requires a lot of plumbing code. EG a . in C++/VB6 is lines of code in C as it has no knowledge of COM. But to write C one has to choose to use C++ or C# and restrict oneself from using the inbuilt features and then duplicate the same features with lots of lines of code. There is plenty of 20 year old source code on the internet showing how.
VBScript is an Active Scripting language. See https://en.wikipedia.org/wiki/Active_Scripting. This allows COM programming languages to add VBS/JS macros to their programs.
Active Scripting allows you to add a macro language to your program.
You can of course implement IActiveScripting and there is plenty of C code for that on the internet.
For 32 bit programs only there is a MSScript.ocx control that implements IActiveScript and related interfaces in an easy control.
The VBScript code is (I'm not a C programmer - but in C# and C++ [but not C] it is just as easy as VBScript) as they both do COM easily.
set ScriptControl1 = wscript.createObject("MSScriptControl.ScriptControl",SC)
With ScriptControl1
.Language = "VBScript"
.UseSafeSubset = False
.AllowUI = True
.AddCode Script
End With
The documentation was in System32 as msscript.chm but is no longer present in Win 10.
Here is some C# sample code. Note 32 Bit ONLY. https://learn.microsoft.com/en-us/troubleshoot/developer/visualstudio/cpp/libraries/call-script-control-run-method

C programming language,how to convert input string in code and run it [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Is there a way to transform what the user inputs in code?
like
if
char k[50];
printf("insert the code to insert");
scanf("%s", &k);
function =>
converts string and execute the string if the code is correct
for example
if the user insert "int a = 0;"
as input , the program run the input string?
What you're looking for is essentially something like the eval function that some languages has.
Nope, there is not a way to do that. At least not in a practical way. The reason is quite simple. In C, you need to compile the complete program before you run it. If there is even a single syntax error in you 10k+ lines of code, you will not get an executable binary from the compiler.
Python on the other hand can execute line by line. That's why you can execute Python code that has syntax errors. The program will not crash until you encounter the error.
Besides, you should always think twice before using something like this irregardless of the language. The risks for vulnerabilities are extremely high.
Contrary to some other languages, the C language doesn't have an eval function to do what you want. The only option is to write the source file into a file and then execute the compiler available on the host machine.

Getting started with coding unix commands [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have been learning c and data structures for quite some time now and I wanted to see whether I could apply what I have learnt. I searched a bit and found out that I could start with util linux but, before I could do so, I thought I'd check and perhaps dabble a bit with the code for basic unix commands like "cat". I was able to understand what a part of the code might have been trying to do, but I was not able to understand the entire code as a unit.
For example, in the "cat" code, a pointer to the output buffer and input buffer is declared and is appropriately used, which I could understand. What i could not understand, are parts of code like io_blksize (stat_buf) which has no description whatsoever, on what it does. Or how two pointers declared as pointers to the input and output buffers, actually correspond to the input and output buffers ?
So my question being, how do I approach these type of code, how can I understand something that has no description to what it does (in the example given above) and how can I make and implement changes in the code, so that I can see the changes when i run a command ?
(Would really appreciate references or topics I should start with, so that I can relate what I have learnt to how command code's can be modified. I also apologize if the question is to abstract.)
This is a bit of a subjective question so my answers will just be my opinion of course.
A good place to start when you run into something you don't recognise while reading source code is the manpages. Each function will generally have a manpage, e.g. man 2 read or man 3 printf. Beyond that, I feel perhaps you should get more of a foundation in Unix before attempting to read the straight source code, a good book is Advanced Programming in the Unix Environment. I've been working through it myself and am finding my Unix knowledge improving considerably.
Just my two cents.

How to run shell commands in a C program [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am really new to advanced programming (at least this is advanced for me)
I want to learn how to run shell commands through C program on windows
I did search for it and I know it has got something to do with system() and exec() but I didn't get a definite answer.
To begin with,i would like to execute cd command and also md command
So if someone can break this down to really basic level,it will be much appreciated. Thank you
P.S. I succeeded in doing so and I know now one shouldn't run system commands through C but this was just an assignment.Thank you
Here's a short program that runs dir from inside a C program.
#include <stdlib.h>
int main() {
system("dir");
return 0;
}
Basically, whatever command you pass as a string inside the parameter for system() is run using the shell on your system. In your case, since you are working on Windows, it is equivalent to running the string inside your command prompt. This is equivalent to the "DOS Commands" you talk about. However, these are actually shell commands.
Note: In general, you do NOT want to be running system() since there are almost always a better way of doing things. Also, if your code is just basically what is above, then you're better off writing a batch file (i.e. a .bat file).

Is it possible to port GNU grep as a library? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I would like to know if it is possible to port GNU grep as a libary, leaving aside the legal complications, if any, as this is purely for non-commercial, but academic use. I have seen many ports exist of GNU grep. For example : GNU grep for win 32 here.
I wonder why nobody has ever attempted to port grep as a library ? It would be a huge benefit to applications that exploit string searching/mining as they can use the power of GNU grep internally in the their applications. I would like to attempt this feat, but since I am new to string searching/mining, would love to know the obvious challenges that may arise and why it has not been done as yet.
EDIT - The advantage of a GNU grep library is that it will do string searching much faster, using its own modified version of boyer-moore. Where as when using a regular expression wrapper library such as PCRE or Boost reg exp or Qt Reg expressions etc, the application has to read the file line-by-line and parse each line against the regexp. This is the obvious advantage that I see.
Yes. Just link it as a library and call its main() with its intended arguments. Or better if you rename its main() to some better, f.e. to grep().

Resources