C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 9th, 2003, 02:39 PM
linh linh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 245 linh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 27 sec
Reputation Power: 6
Converting bash code to C code

To run a bash comand within a C program, I would use

1) system ("ifconfig") or
2) f = popen ("ifconfig", "r") or
3) execlp ("bin/bash", "bash", "-c", linux_command, (char*)0);

The problem with the bash code below is they are too long to be put in a string and then execute it using one of the three method above.

How would I write the code below in C. Most critical is
1) What command to use to read (detect) from hda to hdz
2) I guess the bash command if [ -a /proc/ide/$i ] is short enough
to be used in one of the three method above.
3) As for the command hdparm -c1 -d1 -m16 /dev/$i > /dev/null
one of the three method above would do.
===========================================

#!/bin/bash

for i in hda hdb hdc hdd hde hdf hdg hdh hdi hdj hdk hdl hdm hdn hdo hdp hdq hdr hds hdt hdu hdv hdw hdx hdy hdz
do
if [ -a /proc/ide/$i ]
then
hdparm -c1 -d1 -m16 /dev/$i > /dev/null
fi
done

Reply With Quote
  #2  
Old June 9th, 2003, 07:45 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is online now
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,589 Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 28 m 57 sec
Reputation Power: 1001
Something like this maybe:
Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
  struct stat sb;
  char buf[4]="hda";
  char path[100];
  char c;
  char cmd[100];

  for (c='a'; c <='z'; c++) {
    buf[2] = c;
    sprintf(path, "/proc/ide/%s", buf);
    if (stat(path, &sb) == 0) {
      sprintf(cmd, "hdparm -c1 -d1 -m16 /dev/%s > /dev/null", buf);
      system(cmd);
    }
  }
  return 0;
}


Hope this helps

Reply With Quote
  #3  
Old June 10th, 2003, 09:04 AM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,840 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 36 m 16 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
Actually, the above bash code is part of an executable bash file. This file is what you would/could call instead of the bash code itself.
You don't call actual bash code from inside of a C program, you call executables/programs.
Bash scripts are made executable through thier permissions, but if you try and call bash code from inside a C program you will not usually get the response back that you expect.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Converting bash code to C code


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT