Fork me on GitHub

root/fs/script.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. script_load

   1 /*
   2  * fiwix/fs/script.c
   3  *
   4  * Copyright 2019, Jordi Sanfeliu. All rights reserved.
   5  * Distributed under the terms of the Fiwix License.
   6  */
   7 
   8 #include <fiwix/limits.h>
   9 #include <fiwix/process.h>
  10 #include <fiwix/errno.h>
  11 #include <fiwix/stdio.h>
  12 #include <fiwix/string.h>
  13 
  14 int script_load(char *interpreter, char *args, char *data)
  15 {
  16         char *p;
  17         int n, noargs;
  18 
  19         /* has shebang? */
  20         if(data[0] != '#' && data[1] != '!') {
  21                 return -ENOEXEC;
  22         }
  23 
  24         /* discard possible blanks before the interpreter name */
  25         for(n = 2; n < NAME_MAX; n++) {
  26                 if(data[n] != ' ' && data[n] != '\t') {
  27                         break;
  28                 }
  29         }
  30 
  31         /* get the interpreter name */
  32         p = interpreter;
  33         noargs = 0;
  34         while(n < NAME_MAX) {
  35                 if(data[n] == '\n' || data[n] == NULL) {
  36                         noargs = 1;
  37                         break;
  38                 }
  39                 if(data[n] == ' ' || data[n] == '\t') {
  40                         break;
  41                 }
  42                 *p = data[n];
  43                 n++;
  44                 p++;
  45         }
  46 
  47         if(!interpreter) {
  48                 return -ENOEXEC;
  49         }
  50 
  51 
  52         /* get the interpreter arguments */
  53         if(!noargs) {
  54                 p = args;
  55                 /* discard possible blanks before the arguments */
  56                 while(n < NAME_MAX) {
  57                         if(data[n] != ' ' && data[n] != '\t') {
  58                                 break;
  59                         }
  60                         n++;
  61                 }
  62                 while(n < NAME_MAX) {
  63                         if(data[n] == '\n' || data[n] == NULL) {
  64                                 break;
  65                         }
  66                         *p = data[n];
  67                         n++;
  68                         p++;
  69                 }
  70         }
  71 
  72         return 0;
  73 }

/* [previous][next][first][last][top][bottom][index][help] */