LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Blogs > rainbowsally
User Name
Password

Notices

Rate this Entry

A C/C++ SCRIPT to change symlinks to url links.

Posted 09-08-2013 at 02:05 PM by rainbowsally
Updated 09-08-2013 at 02:28 PM by rainbowsally

Today's feature:
  • A C/C++ utility (script if you have runc-rs) to change symlinks to url files.

[Requires runc-rs or at least librunc.a from runc-rs.
http://www.linuxquestions.org/questi...int-kde-35688/
Doesn't require mint to work, nor even a deb derivative.]

This experiment today is significantly less ambitious than the deb repository builder thingy in the previous blog entry and is again a nice intro to C/C++ and for folks moderately familiar with linux terminal commands who are sick to death of bash's ideosyncracies and/or limitations.

Now...

Ready?

file: symlink2url
purpose: utility (executable)
Code:
#!/usr/local/bin/runc

// Licence: (c) rainbowsally 2013, released under GPL 3.0+.
// http://fsf.org
// No warrantees, no promises, except that YOU have the control.  
// That's the one promise.  Your machine should do what you say, 
// not other way around.

/* Changes symlinks to KDE (and other??) url files to put the full path
 * to the destination in your browser so you can move around more easily.
 * UN-improves new KDE (and other?) (mal)functions.
 */


#include <stdio.h> // printf()
#include <string.h> // str... funcs
#include <malloc.h> // malloc()/free()
#include <stdlib.h> // exit()
#include <runc.h>

int usage(int errcode)
{
  printf("input is the name of the symlink\n");
  return errcode;
}

void error(const char* errstr);

int main(int argc, char** argv)
{
  if(argc < 2)
    return usage(1);
  if(argc > 2)
    error("Woops! Too many args.\n"
          " *** Did you forget to quote a name containing spaces?\n");
  
  /////////////////////////////////////////
  // setup tools and get data
  int err;
  char* sbuf;
  char* url;
  char* name;
  char** str = slist_new();
  char* p;
  
  // name MAY contain spaces, so quote it in the pipeRead()
  name = argv[1];
  err = slist_pipeRead(&str, tmpstr(256, "ls -l \"%s\"", name));
  
  /////////////////////////////////////////
  // copy and parse string
  if(! slist_count(&str))
    return usage(1);
  
  // string: <name> -> <fullpath>
  sbuf = strdup(str[0]);
  url = strstr(sbuf, " -> ");
  if(!url)
    error(tmpstr(256, "\"%s\" is not a symlink", name));
  
  url += 4; // step past " -> "
  if(*url != '/')
     error("The url is not an absolute path. WOOPS!\n");
  
  // printf("name = %s, url = %s\n", name, url); getc(stdin);
  
  // write the desktop file replacing the symlink
  char* filedata = tmpstr(4096, 
          "/usr/bin/xdg-open\n"
          "[Desktop Entry]\n"
          "Icon=folder-remote\n"
          "Type=Link\n"
          "URL[$e]=%s/\n", url);
  
  // use slist to write the file
  slist_clear(&str);
  slist_append(&str, filedata);
  err = system(tmpstr(256, "rm -f \"%s\"", name));
  if(err)
    error("Can't remove existing symlink for some reason???\n");
  
  slist_fileWrite(&str, name);
  
  slist_delete(&str);
  free(sbuf);
  return 0;
}

void error(const char* errstr)
{
  fprintf(stderr, "%s\n", errstr);
  exit(1);
}
Again, if this is your first time around with runc, just change the file to executable and execute it like you would a shell script.

If you like the concept of these utilities but you don't like the runc idea, these files can be renamed *.cpp and compiled with GCC.

Reimplementing the slist functions and temp strings is up to you. There are several ways to do this stuff, but the runc lib (librunc.a) does it and does it pretty well. For example, by piping line by line into a string and doing a short usleep() at each cycle we don't cause single CPU machines to appear to 'hang'. Even short usleep() calls allow linux to task-switch. Just a heads up for anyone ever encountering that problem. (Old KDE gui's also got their buttons stuck down if you didn't do a short pause of some kind.)

The Computer Mad Science Team

:-)
Posted in Uncategorized
Views 285 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 07:47 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration