LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-10-2024, 07:13 PM   #1
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,150

Rep: Reputation: 393Reputation: 393Reputation: 393Reputation: 393
Looking for critique. My first actual program with C.


https://gitlab.com/jmgibson1981/apt-sort

I have been working on this. Refactored the whole thing in the last couple days. Curious how it looks.
 
Old 05-11-2024, 01:56 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,875
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
As a start: What does it do? What problem does it solve?
 
Old 05-11-2024, 08:06 AM   #3
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,150

Original Poster
Rep: Reputation: 393Reputation: 393Reputation: 393Reputation: 393
It started for an easy way to pull out -doc packages in apt. I could pipe a bunch of shell commands together but this does a pattern search and can regex to the beginning or end of the package name. Also returns just the package name without any of the other nonsense information when looking for something specific. One stop shop instead of piping a handful of commands with various flags to get only the relevant information.

Code:
$ aptsort --pattern ^test

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

testdisk
testdisk
testng
testrepository
testssl.sh
testu01-bin
testu01-bin
testu01-data
testu01-doc
Code:
$ aptsort --arch amd64 --pattern ^test

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

testdisk
testu01-bin
 
Old 05-11-2024, 10:05 AM   #4
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,000

Rep: Reputation: 472Reputation: 472Reputation: 472Reputation: 472Reputation: 472
My first impressions are:

* The code is nicely formatted
* The program did not fall into the trap of buffer overruns
* Function prototypes are better placed in individual .h files corresponding to the .c files
* Bash is better suited for a program that mainly calls another program

You may want to look into a command-line option parsing library.
Ed
 
Old 05-11-2024, 01:11 PM   #5
jmgibson1981
Senior Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 1,150

Original Poster
Rep: Reputation: 393Reputation: 393Reputation: 393Reputation: 393
Thank you for directing me to option parsing. Replaced my own with getopt(). Much simpler and less lines. Grateful.

Code:
   int option = 0;
   int optcounter = 1;
   while ((option = getopt(argc, args, "ap")) != -1) {
      if (t1.bailout) {
         break;
      }
      switch (option)
      {
         case 'a':
            if (args[optcounter + 1]) {
               t1.architecture = strdup(args[optcounter + 1]);
               optcounter += 2;
            } else {
               init_regchecks_struct(&t1,
                                     true);
            }
            break;
         case 'p':
            if (args[optcounter +1]) {
               test_formatting(args[optcounter + 1], &t1);
               optcounter += 2;
            } else {
               init_regchecks_struct(&t1,
                                     true);
            }
            break;
         default:
            init_regchecks_struct(&t1,
                                  true);
            break;
      }
I'll switch the headers around. The only reason I didn't is because there are so few of them.

Last edited by jmgibson1981; 05-11-2024 at 01:18 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for critique. Teaching myself C. jmgibson1981 Programming 25 01-11-2023 05:24 PM
My first ssh configuration, can you critique my steps? l33y Linux - Newbie 10 06-18-2014 11:40 PM
[SOLVED] Anyone willing to critique my first bash script? new2bash Programming 5 01-17-2014 02:38 PM
Building a new computer, looking for suggestions and build critique largedon Linux - Hardware 17 03-20-2013 01:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration